-
Notifications
You must be signed in to change notification settings - Fork 20
/
PromisesBus.js
34 lines (33 loc) · 970 Bytes
/
PromisesBus.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
const PromisesBus = function() {
this.promises = {};
this.resolves = {};
this.rejects = {};
}
PromisesBus.prototype.when = function(what) {
return this.register(what);
},
PromisesBus.prototype.register = function(what) {
if (this.promises.hasOwnProperty(what)) {
return this.promises[what];
}
this.promises[what] = new Promise((resolve,reject) => {
this.resolves[what] = resolve;
this.rejects[what] = reject;
});
return this.promises[what];
}
PromisesBus.prototype.unregister = function(what) {
this.register(what);
delete this.promises[what];
delete this.resolves[what];
delete this.rejects[what];
}
PromisesBus.prototype.resolve = function(what) {
this.register(what);
this.resolves[what].apply(this.promises[what],Array.prototype.slice.call(arguments,1));
}
PromisesBus.prototype.reject = function(what) {
this.register(what);
this.rejects[what].apply(this.promises[what],Array.prototype.slice.call(arguments,1));
}
export default PromisesBus;