Created
November 13, 2019 02:16
-
-
Save tobiasl-sportsbet/7bcb679f60e551acb4c67dab6ce828ef to your computer and use it in GitHub Desktop.
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
| const { log } = console; | |
| function* initTrampolineManager() { | |
| let aPrime = undefined; | |
| let count = 0; | |
| const trackChain = () => { | |
| count++; | |
| return () => { | |
| count--; | |
| if (count <= 0) { | |
| count = 0; | |
| aPrime = undefined; | |
| } | |
| }; | |
| }; | |
| do { | |
| let fluents = yield aPrime; | |
| if ( | |
| Object.is(Boolean(aPrime), false) && | |
| typeof fluents.prime === "function" | |
| ) { | |
| aPrime = fluents.prime(); | |
| } | |
| if (aPrime instanceof Promise && typeof fluents.chain === "function") { | |
| aPrime.then(fluents.chain).then(trackChain()); | |
| } | |
| } while (true); | |
| } | |
| const trampolinePromiseOffPrime = { | |
| _manager: undefined, | |
| get manager() { | |
| if (this._manager) { | |
| return this._manager; | |
| } else { | |
| this._manager = initTrampolineManager(); | |
| this._manager.next(); | |
| return this._manager; | |
| } | |
| }, | |
| prime: function(init) { | |
| const chain = chainAble => { | |
| return { | |
| collect: this.manager.next({ prime: init, chain: chainAble }).value, | |
| chain | |
| }; | |
| }; | |
| return { | |
| chain | |
| }; | |
| } | |
| }; | |
| function getTimeoutPromise(label, timeout = 1000) { | |
| return () => | |
| new Promise(resolve => { | |
| setTimeout(() => { | |
| resolve(`"${label}" - "${timeout}"`); | |
| }, timeout); | |
| }); | |
| } | |
| function getMockChainer(label) { | |
| return promiseReponse => { | |
| log("\x1b[33m%s\x1b[0m", "Log") | |
| log("\x1b[34m%s\x1b[0m", `\tChain:`, `\x1b[32m${label}\x1b[0m`) | |
| log("\x1b[34m%s\x1b[0m", `\tPrime Resolve:`, `\x1b[32m${promiseReponse}\x1b[0m`) | |
| log(`\x1b[31m${"Done"}\x1b[0m\n`) | |
| }; | |
| } | |
| log("Start"); | |
| log("Case 1"); | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("Promise1", 1)) | |
| .chain(getMockChainer("test1")) | |
| .chain(getMockChainer("test1")) | |
| .chain(getMockChainer("test1")) | |
| .chain(getMockChainer("test1")).collect; | |
| log("Case 2"); | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("promise2", 1000)) | |
| .chain(getMockChainer("test2")).collect; | |
| log("Case 3"); | |
| /** | |
| * Note: not calling `collect` on this chain. | |
| * `collect` just exposes the primed promise for you if you | |
| * wanted to do something with it. | |
| */ | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("Promise3", 1000)) | |
| .chain(getMockChainer("test3")) | |
| .chain(getMockChainer("test3")) | |
| .chain(getMockChainer("test3")); | |
| log("Case 4"); | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("Promise4", 1000)) | |
| .chain(getMockChainer("test4")).collect; | |
| log("SetTimeout: 1\n"); | |
| setTimeout(() => { | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("Promise5", 500)) | |
| .chain(getMockChainer("test5")).collect; | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("Promise6", 1)) | |
| .chain(getMockChainer("test6")) | |
| .chain(getMockChainer("test6")); | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("Promise7", 10000)) | |
| .chain(getMockChainer("test7")).collect; | |
| log("SetTimeout: 2\n"); | |
| setTimeout(() => { | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("Promise8", 5000)) | |
| .chain(getMockChainer("test8")).collect; | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("Promise9", 5000)) | |
| .chain(getMockChainer("test9")).collect; | |
| trampolinePromiseOffPrime | |
| .prime(getTimeoutPromise("Promise10", 5000)) | |
| .chain(getMockChainer("test10")).collect; | |
| }, 20000); | |
| }, 5000); | |
| log("End"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment