diff options
Diffstat (limited to 'packages/utils/src/interval_utils.ts')
-rw-r--r-- | packages/utils/src/interval_utils.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/packages/utils/src/interval_utils.ts b/packages/utils/src/interval_utils.ts new file mode 100644 index 000000000..ebecc7015 --- /dev/null +++ b/packages/utils/src/interval_utils.ts @@ -0,0 +1,37 @@ +import * as _ from 'lodash'; + +export const intervalUtils = { + setAsyncExcludingInterval(fn: () => Promise<void>, intervalMs: number, onError: (err: Error) => void) { + let locked = false; + const intervalId = setInterval(async () => { + if (locked) { + return; + } else { + locked = true; + try { + await fn(); + } catch (err) { + onError(err); + } + locked = false; + } + }, intervalMs); + return intervalId; + }, + 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); + }, +}; |