From 065570ebf57eb37b14ffd0b2fe131c3dcec4064a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 10 Jan 2018 13:50:26 +0100 Subject: Add an error handler parameter to intervals --- packages/utils/src/interval_utils.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'packages/utils') 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, intervalMs: number) { + setAsyncExcludingInterval(fn: () => Promise, 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); + }, }; -- cgit v1.2.3