-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): adds resolvableWait and safeTrigger utils
- Loading branch information
Showing
8 changed files
with
107 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './resolvable-wait'; | ||
export * from './safe-promise'; | ||
export * from './safe-trigger'; |
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,24 @@ | ||
import { deferred } from 'promist'; | ||
|
||
export interface ResolvableWait { | ||
promise: Promise<void>; | ||
resolve: () => void; | ||
} | ||
|
||
export function resolvableWait(ms: number): ResolvableWait { | ||
const promise = deferred<void>(); | ||
let timer: null | NodeJS.Timer = setTimeout(resolve, ms); | ||
|
||
function resolve(): void { | ||
if (!timer) return; | ||
|
||
clearTimeout(timer); | ||
timer = null; | ||
promise.resolve(); | ||
} | ||
|
||
return { | ||
promise: promise.then((x) => x), | ||
resolve: resolve | ||
}; | ||
} |
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,17 @@ | ||
import { waitUntil } from 'promist'; | ||
|
||
/** | ||
* Will execute `test` forever until it doesn't throw or reject and returns true, and only then will it execute `fn`. | ||
*/ | ||
export function safeTrigger( | ||
test: () => boolean | Promise<boolean>, | ||
fn: () => void | ||
): void { | ||
waitUntil(async () => { | ||
try { | ||
return await test(); | ||
} catch (err) { | ||
return false; | ||
} | ||
}).then(() => fn()); | ||
} |
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