-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (99 loc) · 3.24 KB
/
index.js
File metadata and controls
120 lines (99 loc) · 3.24 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
/* jshint maxparams: 6 */
'use strict';
var organic = require('organic');
var dict = require('dict');
var Iterator = require('./roiterator');
var arrayRemove = require('./arrayRemove');
var MyMap = require('./map');
var util = require('util');
//to be used with Plasma.use and Plasma.decorate
module.exports = function (state) { //this is bound to new plasma object
state._dissolved = new MyMap();
var plasma = this;
plasma.dissolve = function (chemical) {
state._dissolved.set(chemical, []);
this.emit(chemical);
};
plasma.precipitate = function (chemical) {
var ls = state._dissolved.remove(chemical);
if (ls) {
for (var i = 0; i < ls.length; i ++) {
ls[i](chemical);
}
}
};
plasma.precipitateAll = function (chemicalPattern) {
var self = this;
matchDissolvedToPattern(this, state, chemicalPattern, function (key, value) {
self.precipitate(key);
});
};
plasma.onDissolved = function (chemicalPattern, handler, context) {
this.on(chemicalPattern, handler, context);
matchDissolvedToPattern(this, state, chemicalPattern, function (key, value) {
handler.call(context, key);
});
};
plasma.onPrecipitate = function (chemical, handler, context) {
var lsnr = state._dissolved.get(chemical);
if(lsnr) lsnr.push(handler.bind(context));
};
plasma.onAll = function (patterns, handler, context) {
//do not accept empty pattern array
if (! (util.isArray(patterns) && patterns.length > 0)) {
throw 'Illegal Arguments - patterns must be not empty array';
}
var it = new Iterator.atEnd(patterns);
return new PartialMatchListener(this, null, it, handler.bind(context));
};
};
function matchDissolvedToPattern (plasma, state, chemicalPattern, callback) {
var strPattern = plasma.chemicalPatternToString(chemicalPattern);
state._dissolved.forEach(function (key,value) {
if (plasma.getChemicalType(key) === strPattern) {
callback(key, value);
}
});
}
var FakePartialMatchListener = { stop: function () {} };
function PartialMatchListener (plasma, pmo, patternIt, handler) {
//all conditions met
if (patternIt.index() === -1) {
invokeHandler(plasma, pmo, handler);
return FakePartialMatchListener;
}
var pattern = patternIt.val();
var createdPM = [];
var activationHandler = function (chemical) {
var cpmo = new PartialMatchObject(plasma, chemical, pmo, patternIt.prev(), handler);
createdPM.push(cpmo);
plasma.onPrecipitate(chemical, function () {
arrayRemove(createdPM, cpmo);
cpmo.drop();
});
};
this.stop = function () {
plasma.off(pattern, activationHandler);
for (var i = 0; i < createdPM.length; i++) {
createdPM[i].drop();
}
createdPM = [];
};
plasma.onDissolved(pattern, activationHandler, this);
}
function PartialMatchObject(plasma, chemical, parent, remainingPatterns, handler) {
this.value = chemical;
this.parent = parent;
this.pml = new PartialMatchListener(plasma, this, remainingPatterns, handler);
this.drop = function () {
this.pml.stop();
};
}
function invokeHandler(plasma, pmo, handler) {
var args = [pmo.value];
while (pmo.parent !== null) {
pmo = pmo.parent;
args.push(pmo.value);
}
handler.apply(null, args);
}