-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflow.js
92 lines (70 loc) · 2.09 KB
/
flow.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
// handle arguments
function listenHandler (emit) {
var self = this;
var args = [emit.n];
// add static arguments
if (emit.a) {
args.concat(emit.a);
}
return function () {
// convert arguments to array
var arguments_array = Array.prototype.slice.call(arguments, 0);
if (emit.c && M.custom(emit.c)) {
arguments_array = M.custom(emit.c).call(self, arguments_array);
}
// emit the chained event and pass params
self.emit.apply(self, args.concat(arguments_array));
};
}
function chain (event, obs, emit, i) {
var self = this;
for (i = 0; i < emit.length; ++i) {
if (typeof emit[i] === 'string') {
emit[i] = {n: emit[i]};
}
if (!emit[i][1]) {
self.on(event, obs, listenHandler.call(self, emit[i]));
} else {
self.once(event, obs, listenHandler.call(self, emit[i]));
}
}
}
// setup internal event flow
function setupInternalEventFlow (eventFlow, obs) {
var self = this;
obs = obs || self.miid;
for (var event in eventFlow) {
if (eventFlow.hasOwnProperty(event)) {
chain.call(self, event, obs, eventFlow[event]);
}
}
}
// setup external event flow
function setupExternalEventFlow (eventFlow) {
var self = this;
for (var obs in eventFlow) {
if (eventFlow.hasOwnProperty(obs)) {
setupInternalEventFlow.call(self, eventFlow[obs], obs);
}
}
}
// listen to methods
function methodToEvent (methods) {
var self = this;
for (var event in methods) {
if (typeof methods[event] === 'function') {
self.on(event, methods[event]);
}
}
}
// constructor
module.exports = function (module, methods, eventFlow) {
// listen to methods
if (methods) {
methodToEvent.call(module, methods);
}
// setup event flow
if (eventFlow) {
setupExternalEventFlow.call(module, eventFlow);
}
};