diff options
author | Leonid <logvinov.leon@gmail.com> | 2018-01-13 07:51:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-13 07:51:23 +0800 |
commit | a95049450346459c5e6a211464e94fc4f702cebb (patch) | |
tree | 2cff4f477fd145f4eac897bc19d7dc0a3aecf723 /packages/utils | |
parent | 80a46d14beec7544849247f9362db3de3bae04a8 (diff) | |
parent | 6a56f209289105d7641c547a702469cbc9259029 (diff) | |
download | dexon-sol-tools-a95049450346459c5e6a211464e94fc4f702cebb.tar dexon-sol-tools-a95049450346459c5e6a211464e94fc4f702cebb.tar.gz dexon-sol-tools-a95049450346459c5e6a211464e94fc4f702cebb.tar.bz2 dexon-sol-tools-a95049450346459c5e6a211464e94fc4f702cebb.tar.lz dexon-sol-tools-a95049450346459c5e6a211464e94fc4f702cebb.tar.xz dexon-sol-tools-a95049450346459c5e6a211464e94fc4f702cebb.tar.zst dexon-sol-tools-a95049450346459c5e6a211464e94fc4f702cebb.zip |
Merge pull request #312 from 0xProject/feature/error-reporting-intervals
Better error handling on async/sync intervals
Diffstat (limited to 'packages/utils')
-rw-r--r-- | packages/utils/CHANGELOG.md | 3 | ||||
-rw-r--r-- | packages/utils/src/interval_utils.ts | 21 |
2 files changed, 22 insertions, 2 deletions
diff --git a/packages/utils/CHANGELOG.md b/packages/utils/CHANGELOG.md index 899482c4c..e77a7fa03 100644 --- a/packages/utils/CHANGELOG.md +++ b/packages/utils/CHANGELOG.md @@ -1,3 +1,6 @@ # CHANGELOG ## vx.x.x + +* Add `onError` parameter to `intervalUtils.setAsyncExcludingInterval` (#312) +* Add `intervalUtils.setInterval` (#312) diff --git a/packages/utils/src/interval_utils.ts b/packages/utils/src/interval_utils.ts index 62b79f2f5..ebecc7015 100644 --- a/packages/utils/src/interval_utils.ts +++ b/packages/utils/src/interval_utils.ts @@ -1,14 +1,18 @@ import * as _ from 'lodash'; export const intervalUtils = { - setAsyncExcludingInterval(fn: () => Promise<void>, intervalMs: number) { + setAsyncExcludingInterval(fn: () => Promise<void>, intervalMs: number, onError: (err: Error) => void) { let locked = false; const intervalId = setInterval(async () => { if (locked) { return; } else { locked = true; - await fn(); + try { + await fn(); + } catch (err) { + onError(err); + } locked = false; } }, intervalMs); @@ -17,4 +21,17 @@ export const intervalUtils = { clearAsyncExcludingInterval(intervalId: NodeJS.Timer): void { clearInterval(intervalId); }, + setInterval(fn: () => void, intervalMs: number, onError: (err: Error) => void) { + const intervalId = setInterval(() => { + try { + fn(); + } catch (err) { + onError(err); + } + }, intervalMs); + return intervalId; + }, + clearInterval(intervalId: NodeJS.Timer): void { + clearInterval(intervalId); + }, }; |