From 48440fccb0bba93b0159c4388bf019c224b503fb Mon Sep 17 00:00:00 2001 From: Josh Guyette Date: Mon, 17 Jul 2023 20:37:42 -0500 Subject: [PATCH] version 1.1.1 --- package.json | 2 +- src/Scheduler.test.ts | 13 +++++++++ src/Scheduler.ts | 62 +++++++++++++++++++------------------------ 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index b48a71c..80b7797 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "schedule-later", - "version": "1.1.0", + "version": "1.1.1", "types": "dist/Scheduler.d.ts", "main": "dist/Scheduler.js", "author": "Nightness", diff --git a/src/Scheduler.test.ts b/src/Scheduler.test.ts index 7e90078..c7551c9 100644 --- a/src/Scheduler.test.ts +++ b/src/Scheduler.test.ts @@ -35,6 +35,19 @@ describe('setTimeout Tests', () => { jest.advanceTimersByTime(TimeInMS.SECOND) expect(fn).not.toBeCalled() }) + + test('startTimeout, should still call the function if stop function is called and canceled', () => { + const fn = jest.fn() + const stop = startTimeout(fn, { ms: TimeInMS.SECOND }) + + expect(fn).not.toBeCalled() + const stopCancel = stop({ + ms: TimeInMS.SECOND * 5, + })! + jest.advanceTimersByTime(TimeInMS.SECOND) + stopCancel() + expect(fn).toBeCalled() + }) }) describe('setInterval Tests', () => { diff --git a/src/Scheduler.ts b/src/Scheduler.ts index a1d0322..54a51d5 100644 --- a/src/Scheduler.ts +++ b/src/Scheduler.ts @@ -22,7 +22,7 @@ export type TimeUntil = { ms?: number } -export type StopCancelFunction = (stopRunning: boolean) => void +export type StopCancelFunction = (stopRunning?: boolean) => void export type StopFunction = (stopTime?: TimeUntil) => StopCancelFunction | null @@ -84,29 +84,25 @@ export function startTimeout( if (timeout) clearTimeout(timeout) } - // stop() function to stop the interval and timeout - // stop(stopInMS) will stop the interval and timeout in stopInMS milliseconds - // stop(stopHour, stopMinute) will stop the interval and timeout at the next stopHour:stopMinute + /** + * Stops a timeout + * @param {TimeUntil} stopTime - optional + * @return {StopCancelFunction} + */ const stop = (stopTime?: TimeUntil): StopCancelFunction | null => { - if (stopTime === undefined) { + if (!stopTime) { stopNow() return null } - if ((stopTime as any) instanceof Date) { - // @ts-ignore - TS doesn't know this is allowed - const timeFromNow = stopTime - new Date() - const stopTimeout = setTimeout(stopNow, timeFromNow) - // stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true) - return (stopRunning: boolean = false) => { - clearTimeout(stopTimeout) - if (stopRunning) stopNow() - } - } - const stopTimeout = setTimeout(stopNow, timeUntil(stopTime)) - // stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true) - return (stopRunning: boolean = false) => { + + /** + * Cancels a delayed stop + * @param {boolean} stopRunning - optional + * @return void + */ + return (stopRunning?: boolean) => { clearTimeout(stopTimeout) if (stopRunning) stopNow() } @@ -120,7 +116,7 @@ export function startTimeout( * Start an interval * @param {Function} intervalFunc * @param {number} intervalMS - * @param {TimeUntil} start + * @param {TimeUntil} start - optional * @return {StopFunction} */ export function startInterval( @@ -146,29 +142,25 @@ export function startInterval( if (interval) clearInterval(interval) } - // stop() function to stop the interval and timeout - // stop(stopInMS) will stop the interval and timeout in stopInMS milliseconds - // stop(stopHour, stopMinute) will stop the interval and timeout at the next stopHour:stopMinute + /** + * Stops an interval + * @param {TimeUntil} stopTime - optional + * @return {StopCancelFunction} + */ const stop = (stopTime?: TimeUntil): StopCancelFunction | null => { if (stopTime === undefined) { stopNow() return null } - if ((stopTime as any) instanceof Date) { - // @ts-ignore - TS doesn't know this is allowed - const timeFromNow = stopTime - new Date() - const stopTimeout = setTimeout(stopNow, timeFromNow) - // stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true) - return (stopRunning: boolean = false) => { - clearTimeout(stopTimeout) - if (stopRunning) stopNow() - } - } - const stopTimeout = setTimeout(stopNow, timeUntil(stopTime)) - // stopRunning is a boolean that will either cancel the stop (false), or stop the interval now (true) - return (stopRunning: boolean = false) => { + + /** + * Cancels a delayed stop + * @param {boolean} stopRunning - optional + * @return void + */ + return (stopRunning?: boolean) => { clearTimeout(stopTimeout) if (stopRunning) stopNow() }