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
| let saveWaiting = false; | |
| function delaySaveIfRunning(args, promise) { | |
| if ((promise instanceof Promise) ) { | |
| if (promise.state === 'pending') { | |
| if (!saveWaiting) { | |
| saveWaiting = true; | |
| promise.then(() => { | |
| saveWaiting = false; | |
| object.save.apply(object, args); | |
| }); |
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
| function dontCallInParallel(args, promise) { | |
| return (promise instanceof Promise) && (promise.state !== 'pending'); | |
| } |
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
| function dontCacheRejections(args, promise) { | |
| return (promise instanceof Promise) && (promise.state === 'rejected'); | |
| } |
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
| function memoize(func, resolver, rejector) { | |
| const memoized = function(...args) { | |
| const key = resolver ? resolver.apply(this, args) : args[0] | |
| const cache = memoized.cache | |
| if (cache.has(key) && (!rejector || !rejector(args, cache.get(key)))) { | |
| return cache.get(key); | |
| } | |
| const result = func.apply(this, args); | |
| appendState(result); // Append state from above | |
| memoized.cache = cache.set(key, result) || cache; |
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
| function appendState(promise) { | |
| if (promise instanceof Promise) { | |
| promise.state = 'pending'; | |
| promise.then(x => { | |
| promise.state = 'resolved'; | |
| }); | |
| promise.catch(e => { | |
| promise.state = 'rejected'; | |
| }); | |
| } |
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
| function memoize(func, resolver) { | |
| const memoized = function(...args) { | |
| const key = resolver ? resolver.apply(this, args) : args[0]; | |
| const cache = memoized.cache; | |
| if (cache.has(key)) { | |
| return cache.get(key); | |
| } | |
| const result = func.apply(this, args); | |
| memoized.cache = cache.set(key, result) || cache; | |
| return result; |
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
| function memoize(func, resolver) { | |
| const memoized = function(...args) { | |
| const key = resolver ? resolver.apply(this, args) : args[0] | |
| const cache = memoized.cache | |
| if (cache.has(key)) { | |
| return cache.get(key) | |
| } | |
| const result = func.apply(this, args) | |
| memoized.cache = cache.set(key, result) || cache | |
| return result |
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
| let data = null; | |
| function get(cb) { | |
| if (data) { | |
| cb(data); | |
| } else { | |
| callbacks.push(cb); | |
| } | |
| if (callbacks.length === 1) { | |
| ... | |
| data = response; |
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
| // Convert to decorators | |
| function Time(key) { | |
| return function (target, functionName, descriptor) { | |
| const x = target[functionName]; | |
| descriptor.value = async (...args) => | |
| await time(key, async () => | |
| await x.apply(target, args) | |
| ); | |
| }; | |
| } |
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
| class CheckpointBuilder { | |
| ... | |
| } | |
| async function time(name, method) { | |
| const builder = new CheckpointBuilder(name); | |
| await method(builder).finally(() => { | |
| builder.post(); | |
| }) | |
| } |
NewerOlder