Last active
January 14, 2017 03:53
-
-
Save ORESoftware/7ba04b0f1725000609aa7335c3eb9984 to your computer and use it in GitHub Desktop.
An asynchronous constructor pattern
This file contains hidden or 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
// asynchronous constructor example, | |
// like always do not explictly return anything from the constructor | |
function Queue(){ | |
this.ready = false; | |
let callable = true; | |
let ee = new EE(); // new event emitter | |
// the init method represents our real constructor | |
this.init = function (){ | |
if(this.ready){ | |
return simpleSyncObservable(); | |
} | |
if(!callable){ | |
// we already called the init method, but aren't yet ready | |
return simpleEventObservable(ee); | |
} | |
callable = false; | |
// we only want to invoke the below chain once | |
// whichever method calls it first | |
return someObservable() | |
.do(() => { | |
this.ready = true; | |
ee.emit('ready'); | |
}); | |
} | |
} | |
// every method will probably need to call init() first before anything | |
Queue.prototype.obs7 = function(){ | |
// most of our methods start with init() | |
return this.init() | |
.mapTo(() => 7); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment