Created
February 6, 2019 17:54
-
-
Save igorvolnyi/2f889deeb524c5d98990b3d196aac7f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment