-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
69 lines (56 loc) · 1.71 KB
/
controller.js
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
class PieClientSideController {
constructor(config, controllerMap) {
if (!Array.isArray(config.models)) {
throw new Error('config.models must be defined');
}
if (!controllerMap) {
throw new Error('controllerMap must be defined');
}
this._config = config;
this._controllerMap = controllerMap;
this._processor = new Factory().getProcessor(this._config);
}
model(session, env) {
return this._callComponentController('model', session, env)
}
outcome(session, env) {
return this._callComponentController('outcome', session, env).then(
(outcomes) => {
return this._processor.score(session, outcomes);
});
}
getLanguages() {
if (Array.isArray(this._config.langs)) {
return Promise.resolve(this._config.langs);
} else {
return Promise.resolve([]);
}
}
_callComponentController(fnName, session, env) {
let toData = (model) => {
if (!model.element || !model.id) {
throw new Error(`This model is missing either an 'element' or 'id' property: ${JSON.stringify(model)}`);
}
return {
id: model.id,
element: model.element,
model: model,
session: _.find(session, { id: model.id })
}
};
let toPromise = (data) => {
let failed = () => Promise.reject(new Error(`Can't find function for ${data.element}`));
let modelFn = this._controllerMap[data.element][fnName] || failed;
return modelFn(data.model, data.session, env)
.then((result) => {
result.id = data.id;
return result;
});
};
let promises = _(this._pies)
.map(toData)
.map(toPromise)
.value();
return Promise.all(promises);
}
}