Repository navigation
Expand file tree
/
Copy pathexample.js
More file actions
211 lines (180 loc) · 5.67 KB
/
Copy pathexample.js
File metadata and controls
211 lines (180 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"use strict";
const {
Container, bean, value, promise, constructor, factory,
bound, promiser, seeker
} = require(".");
/*
* Components, which would ordinarily be exported from other modules.
*/
const localStore = {
flour: "self-raising flour"
};
const castorSugar = "castor sugar";
class Mixer {
constructor(butter, sugar, egg, milk, flour) {
Object.assign(this, { butter, sugar, egg, milk, flour });
}
async getMixture() {
return `mixture of ${this.butter}, ${this.sugar}, ${this.egg}, ${this.milk}, and ${this.flour}`;
}
}
function createFlour(store) {
return sift(store.flour);
}
function sift(ingredient) {
return `sifted ${ingredient}`;
}
class CreamTopMilk {
constructor() {
this.state = "cream-top milk";
this.cream = "";
this.milkWithoutCream = "";
}
async pasteurize() {
this.state = `pasteurized ${this.state}`;
return this;
}
async separate() {
this.cream = `cream separated from ${this.state}`;
this.milkWithoutCream = `milk separated from ${this.state}`;
return this;
}
async getCream() {
await this.separate();
return this.cream;
}
async getMilk() {
await this.separate();
return this.milkWithoutCream;
}
}
function createButter(creamTopMilk) {
return creamTopMilk.getCream()
.then(cream => `butter churned from ${cream}`);
}
class JamFactory {
constructor() {
this.jam = "jam";
}
async getJam() {
return this.jam;
}
}
class Oven {
constructor(type) {
this.type = type;
}
async preheat() {
return `preheated ${this.type} oven`;
}
}
class Pudding {
constructor(oven, promisedMixture, getMeringue, getJam) {
this.product = Promise.all([promisedMixture, oven.preheat()]).then(([mixture, oven]) => {
return `${mixture}, baked in ${oven}`;
});
this.getMeringue = getMeringue;
this.getJam = getJam;
this.eater = null;
}
addToppings() {
const baseProduct = this.product;
this.product = Promise.all([this.getMeringue(), this.getJam()])
.then(([meringue, jam]) => {
return baseProduct.then(product => `${product}, topped with ${meringue}, and ${jam}`);
});
return this;
}
async serveTo(person) {
if (this.eater) throw new Error(`already eaten by ${this.eater}`);
this.eater = person;
return (await this.product) + `, eaten by ${person}`;
}
}
class Chicken {
constructor(maybeGetCreateEgg) {
this.origin = maybeGetCreateEgg() ? "an egg" : "nothing";
}
async lay() {
return `egg laid by chicken created from ${this.origin}`;
}
}
function createCreateEgg(chicken) {
return async function createEgg() {
return chicken.lay();
}
}
class MeringueFactory {
constructor(createEgg, sugar) {
this.createEgg = createEgg;
this.sugar = sugar;
}
async create() {
return `meringue made from whipped white of ${await this.createEgg()}, and ${this.sugar}`;
}
}
/*
* The container.
*/
const container = new Container();
container.register("store", value(localStore));
container.register("store[sugar]", value(castorSugar));
container.register("chicken", constructor(Chicken), seeker("createEgg"));
container.register("createEgg", factory(createCreateEgg), "chicken");
container.register("meringueFactory", constructor(MeringueFactory), "createEgg", "store[sugar]");
container.register("jamFactory", constructor(JamFactory));
container.register("createCookingScope", factory(createCreateCookingScope), value(container));
function createCreateCookingScope(parent) {
return async function createCookingScope() {
const child = new Container();
child.register("parent", value(parent));
child.register("store", "parent.store");
child.register("meringueFactory", bean("parent.meringueFactory"));
child.register("jamFactory", bean("parent.jamFactory"));
child.register("parentCreateEgg", value(await parent.get("createEgg")));
child.register("mixer", constructor(Mixer), "butter", "sugar", "eggForMixture", "milk", "flour");
child.register("flour", factory(createFlour), "store");
child.register("creamTopMilk", promise((new CreamTopMilk()).pasteurize()));
child.register("butter", factory(createButter), "creamTopMilk");
child.register("milk", factory("creamTopMilk.getMilk"));
child.register("mixture", factory(bean("mixer.getMixture")));
child.register("sugar", factory(sift), "store[sugar]");
child.register("oven", constructor(Oven), value("moderate"));
child.register("pudding", constructor(Pudding),
bean("oven"), promise("mixture"), promiser("meringue"), bound("jamFactory.getJam"));
child.register("chicken", constructor(Chicken), seeker("parentCreateEgg"));
child.register("hen", bean("chicken"));
child.register("createEgg", factory(createCreateEgg), "hen");
child.register("eggForMixture", factory("createEgg"));
child.register("meringue", factory("meringueFactory.create"));
return child;
};
}
/*
* Fake meringue.
*/
function useFakeMeringue({ register, replacement, factory }) {
register(replacement("meringueFactory", "realMeringueFactory"), factory((realMeringueFactory) => ({
async create() {
return `fake meringue instead of ${await realMeringueFactory.create()}`;
}
})), "realMeringueFactory");
}
useFakeMeringue(container);
/*
* Use it.
*/
// Try to serve the same pudding twice (from the same scope)
const promisedScope = container.get("createCookingScope").then(create => create());
promisedScope.then(scope => scope.get("pudding"))
.then(pudding => pudding.addToppings().serveTo("Trillian"))
.then(console.log, console.error);
promisedScope.then(scope => scope.get("pudding"))
.then(pudding => pudding.serveTo("Zaphod"))
.then(console.log, console.error);
// Create a different scope to bake another pudding
container.get("createCookingScope")
.then(create => create())
.then(scope => scope.get("pudding"))
.then(pudding => pudding.serveTo("Ben"))
.then(console.log, console.error);