-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
92 lines (81 loc) · 2.62 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const childProcess = require('child_process');
const path = require('path');
const AOTStore = {};
class ActionsOverTime {
static createActionOverTimeEmitter(options) {
if (!options.key) {
throw new Error('ActionsOverTime: can\'t create an emitter with no key.');
}
if (AOTStore[options.key]) {
return AOTStore[options.key].actions;
} else {
const self = new ActionsOverTime(options);
AOTStore[options.key] = { actions: {
addAction: self.createAction.bind(self),
removeAction: self.removeAction.bind(self),
updateAction: self.updateAction.bind(self),
addSubscriber: self.addSubscriber.bind(self)
}};
return AOTStore[options.key].actions;
}
}
constructor(options) {
this.options = options;
this.createLoopFork();
}
completeActionCallback(callbackCount, actionEvent) {
this.aotApp.send({ message: 'COMPLETE_ACTION' });
}
rejectActionCallback(reason) {
if (reason.constructor.name === 'Error') {
throw reason;
} else {
throw new Error(reason);
}
}
handleResponse(actionEvent) {
// Todo - support multiple callbacks
AOTStore[this.options.key]['subscribers'][actionEvent.action][0](
this.completeActionCallback.bind(this, actionEvent),
this.rejectActionCallback.bind(this),
actionEvent.actionState
);
}
createLoopFork() {
this.aotApp = childProcess.fork(path.resolve(`${__dirname}/aot`), [], { env: this.options });
this.aotApp.on('message', this.handleResponse.bind(this));
}
addSubscriber(actionName, callback) {
if (!AOTStore[this.options.key]['subscribers']) {
AOTStore[this.options.key]['subscribers'] = {};
}
if (AOTStore[this.options.key]['subscribers'][actionName]) {
console.warn('eventually currently only support single subscribers');
AOTStore[this.options.key]['subscribers'][actionName].push(callback);
} else {
AOTStore[this.options.key]['subscribers'][actionName] = [callback];
}
this.aotApp.send({ message: 'ADDED_SUBSCRIBER', action: actionName });
}
removeAction(appId, action) {
this.aotApp.send({
message: 'REMOVE_ACTION',
actionData: { appId: appId, action: action }
});
}
updateAction(appId, action, data) {
this.aotApp.send({
message: 'UPDATE_ACTION',
actionData: { appId: appId, action: action, data: data }
});
}
createAction(appId, actionName, date, state) {
this.aotApp.send({ message: 'ADD_ACTION', actionData: {
appId: appId,
action: actionName,
date: date,
actionState: state
}});
}
}
exports.ActionsOverTime = ActionsOverTime;