diff options
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); + }, }; |