forked from vesvault/libVES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrigger.js
More file actions
147 lines (144 loc) · 3.36 KB
/
Trigger.js
File metadata and controls
147 lines (144 loc) · 3.36 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
/**
* @title Trigger
*
* Dependency Driven Interface, compatible with Promise
*
* @version 0.5 alpha
* @author Jim Zubov <jz@vesvault.com> (VESvault)
* GPL license, http://www.gnu.org/licenses/
*/
Trigger = function(evalfn) {
this.id = Trigger.nextId++;
// console.log('id = '+ this.id);
Trigger._all[this.id] = this;
this._deps = {};
this._follow = [];
if (typeof(evalfn) == 'function') window.setTimeout((function() {
evalfn(this.resolve.bind(this),this.reject.bind(this));
}).bind(this),0);
this.nextIdx = 0;
};
Trigger._all = [];
Trigger.all = function(list) {
var rs = Trigger.resolve([]);
for (var i = 0; i < list.length; i++) rs = (function(val) {
return rs.then(function(r) {
return Trigger.resolve(val).then(function(v) {
r.push(v);
return r;
});
});
})(list[i]);
return rs;
return new Trigger(function(resolve,reject) {
var rslvd = [];
var rslvc = 0;
var vals = [];
var rslv = function(v,i) {
if (!rslvd[i]) {
rslvc++;
rslvd[i] = true;
vals[i] = v;
if (rslvc >= list.length) resolve(vals);
}
};
var rjct = function(v,i) {
if (rslvd[i]) {
rslvc--;
rslvd[i] = false;
}
reject(v);
};
list.map(function(e,i) {
if (e instanceof Trigger) e.then(function(v) {
rslv(v,i);
}).catch(function(v) {
rjct(v,i);
});
else rslv(e,i);
});
});
};
Trigger.resolve = function(value) {
return new Trigger(function(resolve,reject) {
resolve(value);
});
};
Trigger.reject = function(value) {
return new Trigger(function(resolve,reject) {
reject(value);
});
};
Trigger.nextId = 1;
Trigger.prototype = {
value: undefined,
status: 'pending',
_set: function(value,status,depth) {
if (depth != null) {
for (var i = depth; i < this._follow.length; i++) this._follow[i].then(null,'#' + this.id);
this._follow.length = depth;
if (value instanceof Trigger) {
var self = this;
this._follow.push(value);
return value.then(function(val) {
self._set(val,'resolved',depth + 1);
return val;
},'#' + self.id).catch(function(err) {
self.reject(err);
throw err;
},'#' + self.id);
}
}
if (this.status != status || this.value !== value) {
this.status = status;
this.value = value;
window.clearTimeout(this._tmout);
this._tmout = window.setTimeout((function() {
for (var i in this._deps) this._deps[i](value,status == 'rejected');
}).bind(this),0);
}
return this;
},
resolve: function(value) {
return this._set(value,'resolved',0);
},
reject: function(value) {
return this._set(value,'rejected');
},
_register: function(callbk,label,rj) {
if (typeof(callbk) != 'function') {
if (label != null) delete(this._deps[label]);
return this;
}
if (label == null) label = '__' + this.nextIdx++;
var t = new Trigger();
var fn = this._deps[label] = function(val,error) {
try {
if (rj) {
if (error) val = callbk(val);
} else {
if (error) throw val;
val = callbk(val);
}
t.resolve(val);
} catch (e) {
t.reject(e);
}
};
switch (this.status) {
case 'resolved':
fn(this.value);
break;
case 'rejected':
fn(this.value,true);
break;
}
return t;
},
then: function(callbk,label) {
return this._register(callbk,label);
},
catch: function(callbk,label) {
return this._register(callbk,label,true);
}
};