-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createSingletonPromise, sleep } from './promise' | ||
|
||
it('promise', async() => { | ||
let dummy = 0 | ||
|
||
const promise = createSingletonPromise(async() => { | ||
sleep(10) | ||
dummy += 1 | ||
return dummy | ||
}) | ||
|
||
expect(dummy).toBe(0) | ||
|
||
await promise() | ||
|
||
expect(dummy).toBe(1) | ||
|
||
await promise() | ||
expect(await promise()).toBe(1) | ||
|
||
expect(dummy).toBe(1) | ||
|
||
promise.reset() | ||
|
||
await promise() | ||
|
||
expect(dummy).toBe(2) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Fn } from './types' | ||
|
||
export interface SingletonPromiseReturn<T> { | ||
(): Promise<T> | ||
/** | ||
* Reset current staled promise. | ||
* await it to have proper shutdown. | ||
*/ | ||
reset: () => Promise<void> | ||
} | ||
|
||
/** | ||
* Create singleton promise function | ||
* | ||
* @example | ||
* ``` | ||
* const promise = createSingletonPromise(async () => { ... }) | ||
* | ||
* await promise() | ||
* await promise() // all of them will be bind to a single promise instance | ||
* await promise() // and be resolved together | ||
* ``` | ||
*/ | ||
export function createSingletonPromise<T>(fn: () => Promise<T>): SingletonPromiseReturn<T> { | ||
let _promise: Promise<T> | undefined | ||
|
||
function wrapper() { | ||
if (!_promise) | ||
_promise = fn() | ||
return _promise | ||
} | ||
wrapper.reset = async() => { | ||
const _prev = _promise | ||
_promise = undefined | ||
if (_prev) | ||
await _prev | ||
} | ||
|
||
return wrapper | ||
} | ||
|
||
/** | ||
* Promised `setTimeout` | ||
*/ | ||
export function sleep(ms: number, callback?: Fn<any>) { | ||
return new Promise<void>(resolve => | ||
setTimeout(async() => { | ||
await callback?.() | ||
resolve() | ||
}, ms), | ||
) | ||
} |