context.js
10.3 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import StateMachine from './statemachine.js';
import isInNode from './arcs.js';
/**
* @class ARCS.Context
* @classdesc Class representing a context containing libraries and components
* used by different parts of the framework.
* @param ctx {object} an object representing data for the context.
*/
let Context = function( ctx ) {
let components = {};
let constants = {};
let factories = {};
let libraries = [];
//var depLibPromises=[];
let self = this;
var loadLibraries;
var loadDataFile;
var promiseLibrary;
var instanciateComponents;
let preInit = false;
factories.StateMachine = StateMachine;
if (ctx !== undefined) {
libraries = ctx.libraries;
var p;
for (p in ctx.components) {
if (ctx.components.hasOwnProperty(p)) {
components[p] = ctx.components[p];
}
}
if (ctx.constants !== undefined) {
for (p in ctx.constants) {
if (ctx.constants.hasOwnProperty(p)) {
constants[p] = ctx.constants[p];
}
}
}
}
var loadDataFile = async function(fileName) {
var dataPromise ;
if (isInNode()) {
return new Promise(function (resolve, reject) {
var dep = require(/* webpackIgnore: true */fileName);
if (dep !== undefined) {
resolve(dep);
} else {
reject("[ARCS] File not found");
}
});
} else {
var client = await fetch(fileName);
return client.json();
}
};
var loadLibraries = function () {
var i;
// we will use different instances of require either the one of node
// or the one from require.js
Context.currentContext = self;
var res=[];
for(i=0; i < libraries.length; i++) {
res.push(self.loadLibrary(libraries[i]));
}
return Promise.all(res);
};
var instanciateComponents = async function() {
var p, promises=[];
for (p in components) {
if (components.hasOwnProperty(p)) {
if (factories[components[p].type] === undefined) {
console.error("[ARCS] Factory " + components[p].type + " not found.");
console.error("[ARCS] Context dump follows: ", libraries, components, constants);
return ;
}
var factory = factories[components[p].type];
//console.log("instanciating ", p);
try {
if (components[p].value !== undefined || components[p].url !== undefined || components[p].ref !== undefined) {
if (components[p].value !== undefined) {
components[p].instance = new factory(components[p].value);
}
if (components[p].url !== undefined) {
// we need currying here !
var delayInstanciation = function(p,factory) {
return function(obj) {
console.log("instanciating from data file");
components[p].instance = new factory(obj);
return Promise.resolve();
}
};
console.log("loading data file", components[p].url);
promises.push(
loadDataFile(components[p].url).then(delayInstanciation(p,factory))
);
}
if (components[p].ref !== undefined) {
if (constants[components[p].ref] !== undefined) {
components[p].instance = new factory(constants[components[p].ref]);
}
}
} else {
components[p].instance = new factory();
}
} catch(e) { console.error("[ARCS] Component of type ", p, " not instanciated.", e);}
}
}
return Promise.all(promises);
};
/**
* loads a given library and, if necessary, launches a call back function
* when the library is loaded.
* @param libName {string} name of the library to load
* @param cbFunction {function} callback function to call when library is loaded
*/
this.loadLibrary = function (libName, cbFunction) {
var libUrl = libName, libActualName = libName;
Context.currentContext = self;
if (typeof libName !== "string") {
libActualName = libName.name;
libUrl = libName.url;
}
if (libraries.indexOf(libActualName) < 0) {
libraries.push(libActualName);
}
// TODO promisify call to cbFunction
return import(/* webpackIgnore: true */libUrl).then( function(module) {
// TODO insert here component factories
for (p in module.default) {
if (module.default.hasOwnProperty(p)) {
Context.currentContext.setFactory(p,module.default[p]);
}
}
if (cbFunction !== undefined) {
cbFunction();
}
}).catch( function(msg) { console.error("[ARCS] Trouble loading '",libUrl,"' with reason -", {msg}) });
};
/**
* @return the component list stored inside context
*/
this.getComponentList = function () {
var list = Object.keys(components);
var i;
for (i = 0; i < list.length; i++) {
if ( ! components.hasOwnProperty(list[i])) {
list.splice(i--,1);
}
}
return list;
};
this.getConstant = function(cName) {
/*if (!constants.hasOwnProperty(cName)) {
return undefined;
}*/
return constants[cName];
};
// to determine if really needed
this.getComponentType = function(cName) {
/*if (!components.hasOwnProperty(cName))
return undefined;*/
if (components[cName] === undefined) return undefined;
return components[cName].type;
};
// to determine if really needed
this.getComponentValue = function(cName) {
/*if (!components.hasOwnProperty(cName))
return undefined;*/
if (components[cName] === undefined) return undefined;
return components[cName].value;
};
// to determine if really needed
this.getComponent = function (cName) {
/*if (!components.hasOwnProperty(cName))
return undefined;*/
if (components[cName] === undefined) return undefined;
return components[cName].instance;
};
// to determine if really needed
this.getComponentName = function (cmp) {
var i, keys;
keys = components.getComponentList();
for(i = 0; i < keys.length; i++) {
if (components[keys[i]].instance === cmp) {
return keys[i];
}
}
return undefined;
};
this.addFactories = function(obj) {
preInit = true;
for(p in obj) {
if( obj.hasOwnProperty(p))
factories[p] = obj[p];
}
};
this.setFactory = function(key, factory ) {
factories[key] = factory;
};
this.toJSON = function () {
var res = {}, p;
for (p in components) {
if (components.hasOwnProperty(p)) {
res[p] = { type: components[p].type, value: components[p].value };
}
}
return res;
};
// functions used with editor
this.setComponentValue = function (cName, cValue) {
components[cName].value = cValue; // to modifiy
};
this.addComponent = function (cName, cType, cValue) {
var component;
components[cName] = {};
components[cName].type = cType;
components[cName].value = cValue;
var factory = factories[cType];
if (factory !== undefined) {
component = new factory(cValue);
}
components[cName].instance = component;
};
this.removeComponent = function (cName) {
delete components[cName];
};
// see if it is needed
this.getFactory = function (fName) {
return factories[fName];
};
// see if it is needed
this.getFactoryList = function() {
return Object.keys(factories);
};
// this should return a promise !
this.instanciate = async function () {
//! TODO
try {
if (!preInit)
await loadLibraries();
await instanciateComponents();
} catch(e) {
console.error("[ARCS] Trouble instanciating context", e);
};
};
var chainPrototype = function (obj, proto) {
// this stunt seems better than using
// Object.setPrototypeOf or using [object].__proto__
// due to javascript engine optimizations
var newObj = Object.create(proto);
var p ;
for (p in obj) {
if (obj.hasOwnProperty(p)) {
newObj[p] = obj[p];
}
}
return newObj;
};
this.chain = function (cmp,cst,fct) {
// cmp and cst are the children context elements
// we need to chain contexts properly.
return [ chainPrototype(cmp, components),
chainPrototype(cst, constants),
chainPrototype(fct, factories)
];
};
this.setParent = function (ctx) {
// chaining factories is also important if contexts are repeating
// the same things
if (ctx === undefined) return;
var v = ctx.chain(components, constants, factories);
components = v[0];
constants = v[1];
factories = v[2];
};
};
/** pseudo-singleton to current context being used */
Context.currentContext = null;
export default Context;