Last active
March 27, 2019 11:53
-
-
Save JasonKleban/0c0b4fefe7553c4da41a6770bda3864b to your computer and use it in GitHub Desktop.
Convert events into Promises to be awaited
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
export interface LockStep<T = void> { | |
edge : Promise<T>; | |
} | |
export class LockStepper<T = void> implements LockStep<T> { | |
private _edge! : Promise<T>; | |
private _resolve! : (value?: T | PromiseLike<T> | undefined) => void; | |
constructor () { | |
this.rearm(); | |
} | |
private rearm() { | |
this._edge = new Promise<T>((resolve) => { | |
this._resolve = resolve; | |
}); | |
} | |
get edge() { | |
return this._edge; | |
} | |
expose() { | |
return this as LockStep<T>; | |
} | |
step(value?: T | PromiseLike<T> | undefined) { | |
const _resolve = this._resolve; | |
this.rearm(); | |
_resolve(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment