diff --git a/lib/src/timer/handler.ts b/lib/src/timer/handler.ts index c45da78e..1d376974 100644 --- a/lib/src/timer/handler.ts +++ b/lib/src/timer/handler.ts @@ -6,6 +6,13 @@ * Licensed under the MIT license. */ +import { objDefineProp } from "../object/define"; + +const REF = "ref"; +const UNREF = "un" + REF as "unref"; +const HAS_REF = "hasRef"; +const ENABLED = "enabled"; + /** * A Timer handler which is returned from {@link scheduleTimeout} which contains functions to * cancel or restart (refresh) the timeout function. @@ -92,6 +99,55 @@ export interface ITimerHandler { * ``` */ hasRef(): boolean; + + /** + * Gets or Sets a flag indicating if the underlying timer is currently enabled and running. + * Setting the enabled flag to the same as it's current value has no effect, setting to `true` + * when already `true` will not {@link ITimerHandler.refresh | refresh}() the timer. + * And setting to 'false` will {@link ITimerHandler.cancel | cancel}() the timer. + * @since 0.8.1 + * @example + * ```ts + * let theTimer = createTimeout(...); + * + * // Check if enabled + * theTimer.enabled; // false + * + * // Start the timer + * theTimer.enabled = true; // Same as calling refresh() + * theTimer.enabled; //true + * + * // Has no effect as it's already running + * theTimer.enabled = true; + * + * // Will refresh / restart the time + * theTimer.refresh() + * + * let theTimer = scheduleTimeout(...); + * + * // Check if enabled + * theTimer.enabled; // true + * ``` + */ + enabled: boolean; +} + +/** + * @ignore + * @internal + */ +export interface _TimerHandler { + /** + * The public handler to return to the caller + */ + h: ITimerHandler, + + /** + * The callback function that should be called when the timer operation + * has completed and will not automatically rescheduled + * @returns + */ + dn: () => void } /** @@ -101,49 +157,70 @@ export interface ITimerHandler { * it directly used / returned by the pulic functions used to create timers (idle, interval and timeout) * @param startTimer - Should the timer be started as part of creating the handler * @param refreshFn - The function used to create/start or refresh the timer - * @param cancelFn - The function used to cancel the timer + * @param cancelFn - The function used to cancel the timer. * @returns The new ITimerHandler instance */ -export function _createTimerHandler(startTimer: boolean, refreshFn: (timerId: T) => T, cancelFn: (timerId: T) => void): ITimerHandler { +export function _createTimerHandler(startTimer: boolean, refreshFn: (timerId: T) => T, cancelFn: (timerId: T) => void): _TimerHandler { let ref = true; let timerId: T = startTimer ? refreshFn(null) : null; + let theTimerHandler: ITimerHandler; - function _unref() { + const _unref = () => { ref = false; - timerId && timerId["unref"] && timerId["unref"](); - return timer; - } + timerId && timerId[UNREF] && timerId[UNREF](); + return theTimerHandler; + }; - function _ref() { + const _ref = () => { ref = true; - timerId && timerId["ref"] && timerId["ref"](); - return timer; - } + timerId && timerId[REF] && timerId[REF](); + return theTimerHandler; + }; - function _hasRef() { - if (timerId && timerId["hasRef"]) { - return timerId["hasRef"](); + const _hasRef = () => { + if (timerId && timerId[HAS_REF]) { + return timerId[HAS_REF](); } return ref; + }; + + const _refresh = () => { + timerId = refreshFn(timerId); + if (!ref) { + _unref(); + } + + return theTimerHandler; + }; + + const _cancel = () => { + timerId && cancelFn(timerId); + timerId = null; + }; + + const _setEnabled = (value: boolean) => { + !value && timerId && _cancel(); + value && !timerId && _refresh(); } - let timer: ITimerHandler = { - cancel: function() { - timerId && cancelFn(timerId); - timerId = null; - }, - refresh: function() { - timerId = refreshFn(timerId); - if (!ref) { - _unref(); - } - - return timer; - }, - hasRef: _hasRef, - ref: _ref, - unref: _unref + theTimerHandler = { + cancel: _cancel, + refresh: _refresh, + [HAS_REF]: _hasRef, + [REF]: _ref, + [UNREF]: _unref, + [ENABLED]: false }; - return timer; + objDefineProp(theTimerHandler, ENABLED, { + get: () => !!timerId, + set: _setEnabled + }); + + return { + h: theTimerHandler, + dn: () => { + timerId = null; + } + }; } diff --git a/lib/src/timer/idle.ts b/lib/src/timer/idle.ts index f5dda524..952b64a2 100644 --- a/lib/src/timer/idle.ts +++ b/lib/src/timer/idle.ts @@ -113,7 +113,11 @@ export function setDefaultMaxExecutionTime(maxTime: number) { * // Instead of calling cancelIdleCallback() with the returned value from requestIdleCallback() the returned * // handler instance can be used instead to cancel the idle timer * theIdleTimer.cancel(); - * + * theIdleTimer.enabled; // false + * + * // You can start the timer via enabled + * theIdleTimer.enabled = true; + * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theIdleTimer.refresh(); * ``` @@ -130,14 +134,17 @@ export function scheduleIdleCallback(callback: IdleRequestCallback, options?: Id } if (hasIdleCallback()) { - return _createTimerHandler(true, function (idleId: number) { + let handler = _createTimerHandler(true, (idleId: number) => { idleId && cancelIdleCallback(idleId); return requestIdleCallback((deadline: IdleDeadline) => { + handler.dn(); callback(deadline || _createDeadline(false)); }, options); - }, function(idleId: number) { + }, (idleId: number) => { cancelIdleCallback(idleId); - }) + }); + + return handler.h; } let timeout = (options || {}).timeout; diff --git a/lib/src/timer/interval.ts b/lib/src/timer/interval.ts index 3cccfaec..aebbde6e 100644 --- a/lib/src/timer/interval.ts +++ b/lib/src/timer/interval.ts @@ -32,8 +32,12 @@ import { ITimerHandler, _createTimerHandler } from "./handler"; * // Instead of calling clearInterval() with the returned value from setInterval() the returned * // handler instance can be used instead to cancel the timer * theIntervalTimer.cancel(); + * theIntervalTimer.enabled; // false + * + * // You can start the timer via enabled + * theIntervalTimer.enabled = true; * - * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` + * // Or you can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theIntervalTimer.refresh(); * ``` */ @@ -42,10 +46,12 @@ export function scheduleInterval(callback: (...args: A) => void let self = this; let theArguments = _extractArgs(arguments, 0); - return _createTimerHandler(true, function (intervalId: any) { + let handler = _createTimerHandler(true, (intervalId: any) => { intervalId && clearInterval(intervalId); return setInterval.apply(self, theArguments); - }, function (intervalId: any) { + }, (intervalId: any) => { clearInterval(intervalId); }); + + return handler.h; } diff --git a/lib/src/timer/timeout.ts b/lib/src/timer/timeout.ts index 5dae0868..4141f7fa 100644 --- a/lib/src/timer/timeout.ts +++ b/lib/src/timer/timeout.ts @@ -17,7 +17,13 @@ function _createTimeoutWith(self: any, startTimer: boolean, overrideFn: TimeoutO let setFn: TimeoutOverrideFn = (len > 0 ? overrideFn[0] : (!isArr ? overrideFn : UNDEF_VALUE)) || setTimeout; let clearFn: ClearTimeoutOverrideFn = (len > 1 ? overrideFn[1] : UNDEF_VALUE) || clearTimeout; - return _createTimerHandler(startTimer, function (timerId?: any) { + let timerFn = theArgs[0]; + theArgs[0] = function () { + handler.dn(); + timerFn.apply(self, arguments); + }; + + let handler = _createTimerHandler(startTimer, (timerId?: any) => { if (timerId) { if (timerId.refresh) { timerId.refresh(); @@ -31,6 +37,8 @@ function _createTimeoutWith(self: any, startTimer: boolean, overrideFn: TimeoutO }, function (timerId: any) { clearFn.call(self, timerId); }); + + return handler.h; } /** @@ -92,7 +100,11 @@ export type TimeoutOverrideFuncs = [ TimeoutOverrideFn | null, ClearTimeoutOverr * // Instead of calling clearTimeout() with the returned value from setTimeout() the returned * // handler instance can be used instead to cancel the timer * theTimeout.cancel(); - * + * theTimeout.enabled; // false + * + * // You can start the timer via enabled + * theTimeout.enabled = true; + * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theTimeout.refresh(); * ``` @@ -141,6 +153,10 @@ export function scheduleTimeout(callback: (...args: A) => void, * // Instead of calling clearTimeout() with the returned value from setTimeout() the returned * // handler instance can be used instead to cancel the timer * theTimeout.cancel(); + * theTimeout.enabled; // false + * + * // You can start the timer via enabled + * theTimeout.enabled = true; * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theTimeout.refresh(); @@ -170,6 +186,10 @@ export function scheduleTimeout(callback: (...args: A) => void, * // Instead of calling clearTimeout() with the returned value from setTimeout() the returned * // handler instance can be used instead to cancel the timer, internally this will call the newClearTimeoutFn * theTimeout.cancel(); + * theTimeout.enabled; // false + * + * // You can start the timer via enabled + * theTimeout.enabled = true; * * // You can also "restart" the timer, whether it has previously triggered not not via the `refresh()` * theTimeout.refresh(); @@ -207,6 +227,9 @@ export function scheduleTimeoutWith(overrideFn: TimeoutOverride * * // As the timer is not started you will need to call "refresh" to start the timer * theTimeout.refresh(); + * + * // or set enabled to true + * theTimeout.enabled = true; * ``` */ export function createTimeout(callback: (...args: A) => void, timeout: number, ...args: A): ITimerHandler; @@ -252,6 +275,9 @@ export function createTimeout(callback: (...args: A) => void, t * * // As the timer is not started you will need to call "refresh" to start the timer * theTimeout.refresh(); + * + * // or set enabled to true + * theTimeout.enabled = true; * ``` * @example * ```ts @@ -277,6 +303,9 @@ export function createTimeout(callback: (...args: A) => void, t * * // As the timer is not started you will need to call "refresh" to start the timer * theTimeout.refresh(); + * + * // or set enabled to true + * theTimeout.enabled = true; * ``` */ export function createTimeoutWith(overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncs, callback: (...args: A) => void, timeout: number, ...args: A): ITimerHandler; diff --git a/lib/test/src/common/timer/idle.test.ts b/lib/test/src/common/timer/idle.test.ts index 925534c2..0ab5bb3c 100644 --- a/lib/test/src/common/timer/idle.test.ts +++ b/lib/test/src/common/timer/idle.test.ts @@ -49,30 +49,33 @@ describe("idle tests", () => { let waitTimeout: any; assert.ok(hasIdleCallback(), "Expected the requestIdleCallback to exist"); - scheduleIdleCallback((deadline) => { + let handler = scheduleIdleCallback((deadline) => { idleCalled++; - assert.equal(false, deadline.didTimeout, "Expected the deadline to not be timedout - " + dumpObj(deadline)); + assert.equal(deadline.didTimeout, false, "Expected the deadline to not be timedout - " + dumpObj(deadline)); isDone = true; done(); if (waitTimeout) { orgClearTimeout(waitTimeout); } }); - - assert.equal(0, idleCalled, "Idle should not have been called yet"); + + assert.equal(handler.enabled, true, "Check that the handler is running"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); } clock.tick(1); - assert.equal(0, idleCalled, "Idle should not yet have been called"); + assert.equal(idleCalled, 0, "Idle should not yet have been called"); waitTimeout = orgTimeout(() => { if (!isDone) { - assert.equal(1, idleCalled, "Idle should have been called only once"); + assert.equal(idleCalled, 1, "Idle should have been called only once"); done(); } }, 1000); + + assert.equal(handler.enabled, false, "Check that the handler is stopped"); }); it("cancel idle with real requestIdleCallback", (done) => { @@ -83,18 +86,21 @@ describe("idle tests", () => { idleCalled++; assert.ok(false, "should not have been called"); }); + + assert.equal(theIdle.enabled, true, "Check that the handler is running"); theIdle.cancel(); - - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(theIdle.enabled, false, "Check that the handler is stopped"); + + assert.equal(idleCalled, 0, "Idle should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); } clock.tick(1); - assert.equal(0, idleCalled, "Idle should not have been called"); + assert.equal(idleCalled, 0, "Idle should not have been called"); orgTimeout(() => { - assert.equal(0, idleCalled, "Idle should still not have been called"); + assert.equal(idleCalled, 0, "Idle should still not have been called"); done(); }, 1000); }); @@ -107,28 +113,33 @@ describe("idle tests", () => { assert.ok(hasIdleCallback(), "Expected the requestIdleCallback to exist"); let theIdle = scheduleIdleCallback((deadline) => { idleCalled++; - assert.equal(false, deadline.didTimeout, "Expected the deadline to not be timedout - " + dumpObj(deadline)); + assert.equal(deadline.didTimeout, false, "Expected the deadline to not be timedout - " + dumpObj(deadline)); isDone = true; done(); if (waitTimeout) { orgClearTimeout(waitTimeout); } }); + + assert.equal(theIdle.enabled, true, "Check that the handler is running"); theIdle.cancel(); + assert.equal(theIdle.enabled, false, "Check that the handler is stopped"); - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); } clock.tick(1); - assert.equal(0, idleCalled, "Idle should still not have been called"); + assert.equal(idleCalled, 0, "Idle should still not have been called"); + assert.equal(theIdle.enabled, false, "Check that the handler is stopped"); theIdle.refresh(); + assert.equal(theIdle.enabled, true, "Check that the handler is running"); clock.tick(99); waitTimeout = orgTimeout(() => { if (!isDone) { - assert.equal(1, idleCalled, "Idle should have been called only once"); + assert.equal(idleCalled, 0, "Idle should have been called only once"); done(); } }, 1000); @@ -167,9 +178,9 @@ describe("idle tests", () => { let idleIterations = 0; assert.ok(!hasIdleCallback(), "Expected the requestIdleCallback to not exist"); - scheduleIdleCallback((deadline) => { + let handler = scheduleIdleCallback((deadline) => { idleCalled++; - assert.equal(true, deadline.didTimeout, "Expected the timeout to have timed out"); + assert.equal(deadline.didTimeout, true, "Expected the timeout to have timed out"); while (deadline.timeRemaining() > 0) { idleIterations++; clock.tick(1); @@ -177,17 +188,19 @@ describe("idle tests", () => { }, { timeout: 100 }); - - assert.equal(0, idleCalled, "Idle should not have been called yet"); + + assert.equal(handler.enabled, true, "Check that the handler is running"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); } - assert.equal(99, elapsedTime(startTime), "Calculated passed time"); + assert.equal(elapsedTime(startTime), 99, "Calculated passed time"); clock.tick(1); - assert.equal(1, idleCalled, "Idle should have been called only once"); - assert.equal(50, idleIterations, "Idle should have been called only once"); - assert.equal(150, elapsedTime(startTime), "Calculated passed time including the deadline and max execution"); + assert.equal(idleCalled, 1, "Idle should have been called only once"); + assert.equal(idleIterations, 50, "Idle should have been called only once"); + assert.equal(elapsedTime(startTime), 150, "Calculated passed time including the deadline and max execution"); + assert.equal(handler.enabled, false, "Check that the handler is stopped"); }); it("set default timeout and execution time", () => { @@ -198,25 +211,27 @@ describe("idle tests", () => { assert.ok(!hasIdleCallback(), "Expected the requestIdleCallback to not exist"); setDefaultIdleTimeout(150); setDefaultMaxExecutionTime(42); - scheduleIdleCallback((deadline) => { + let handler = scheduleIdleCallback((deadline) => { idleCalled++; - assert.equal(true, deadline.didTimeout, "Expected the timeout to have timed out"); + assert.equal(deadline.didTimeout, true, "Expected the timeout to have timed out"); while (deadline.timeRemaining() > 0) { idleIterations++; clock.tick(1); } }); - - assert.equal(0, idleCalled, "Idle should not have been called yet"); + + assert.equal(handler.enabled, true, "Check that the handler is running"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); for (let lp = 0; lp < 149; lp++) { clock.tick(1); - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); } - assert.equal(149, elapsedTime(startTime), "Calculated passed time"); + assert.equal(elapsedTime(startTime), 149, "Calculated passed time"); clock.tick(1); - assert.equal(1, idleCalled, "Idle should have been called only once"); - assert.equal(42, idleIterations, "Idle should have been called only once"); - assert.equal(192, elapsedTime(startTime), "Calculated passed time including the deadline and max execution"); + assert.equal(idleCalled, 1, "Idle should have been called only once"); + assert.equal(idleIterations, 42, "Idle should have been called only once"); + assert.equal(elapsedTime(startTime), 192, "Calculated passed time including the deadline and max execution"); + assert.equal(handler.enabled, false, "Check that the handler is stopped"); }); }); @@ -228,14 +243,17 @@ describe("idle tests", () => { timeout: 100 }); - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(theIdle.enabled, true, "Check that the handler is running"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); } theIdle.cancel(); + assert.equal(theIdle.enabled, false, "Check that the handler is stopped"); + clock.tick(1); - assert.equal(0, idleCalled, "Idle should not have been called yet"); + assert.equal(idleCalled, 0, "Idle should not have been called yet"); clock.tick(1000); }); }); diff --git a/lib/test/src/common/timer/interval.test.ts b/lib/test/src/common/timer/interval.test.ts index caae6f68..eb930167 100644 --- a/lib/test/src/common/timer/interval.test.ts +++ b/lib/test/src/common/timer/interval.test.ts @@ -23,24 +23,27 @@ describe("interval tests", () => { it("basic interval", () => { let intervalCalled = 0; - scheduleInterval(() => { + let handler = scheduleInterval(() => { intervalCalled++; }, 100); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(handler.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); } clock.tick(1); - assert.equal(1, intervalCalled, "Interval should have been called only once"); + assert.equal(intervalCalled, 1, "Interval should have been called only once"); + assert.equal(handler.enabled, true, "Check that the handler is running"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(1, intervalCalled, "Interval should have been called only once"); + assert.equal(intervalCalled, 1, "Interval should have been called only once"); } clock.tick(1); - assert.equal(2, intervalCalled, "Interval should have been called twice"); + assert.equal(intervalCalled, 2, "Interval should have been called twice"); + assert.equal(handler.enabled, true, "Check that the handler is running"); }); it("cancel interval", () => { @@ -49,14 +52,17 @@ describe("interval tests", () => { intervalCalled ++; }, 100); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); } theInterval.cancel(); + assert.equal(theInterval.enabled, false, "Check that the handler is stopped"); + clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); clock.tick(1000); }); @@ -66,53 +72,60 @@ describe("interval tests", () => { intervalCalled ++; }, 100); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); } theInterval.refresh(); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); for (let lp = 0; lp < 98; lp++) { clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); } clock.tick(1); - assert.equal(1, intervalCalled, "Interval should have been called yet"); + assert.equal(intervalCalled, 1, "Interval should have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); // reset theInterval.refresh(); - assert.equal(1, intervalCalled, "Interval should not have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 1, "Interval should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(1, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 1, "Interval should not have been called yet"); } clock.tick(1); - assert.equal(2, intervalCalled, "Interval should have been called yet"); - + assert.equal(intervalCalled, 2, "Interval should have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); }); describe("pass extra arguments", () => { it("basic Interval", () => { let intervalCalled = 0; let theArgs: any = null; - scheduleInterval(function (a, b) { + let theInterval = scheduleInterval(function (a, b) { intervalCalled ++; theArgs = arguments; }, 100, "Hello", "Darkness"); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); assert.equal(null, theArgs, "No arguments"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); assert.equal(null, theArgs, "No arguments"); } clock.tick(1); - assert.equal(1, intervalCalled, "Interval should have been called yet"); - assert.equal(2, theArgs.length); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 1, "Interval should have been called yet"); + assert.equal(theArgs.length, 2); assert.equal("Hello", theArgs[0], "First Arg"); assert.equal("Darkness", theArgs[1], "Second Arg"); }); @@ -125,23 +138,27 @@ describe("interval tests", () => { theArgs = arguments; }, 100, "Hello", "Darkness"); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); assert.equal(null, theArgs, "No arguments"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); assert.equal(null, theArgs, "No arguments"); } theInterval.refresh(); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); for (let lp = 0; lp < 98; lp++) { clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); } clock.tick(1); - assert.equal(1, intervalCalled, "Interval should have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 1, "Interval should have been called yet"); assert.equal(2, theArgs.length); assert.equal("Hello", theArgs[0], "First Arg"); assert.equal("Darkness", theArgs[1], "Second Arg"); @@ -153,24 +170,28 @@ describe("interval tests", () => { intervalCalled ++; }, 100); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); } theInterval.cancel(); + assert.equal(theInterval.enabled, false, "Check that the handler is stopped"); + clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); theInterval.refresh(); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(0, intervalCalled, "Interval should not have been called yet"); + assert.equal(intervalCalled, 0, "Interval should not have been called yet"); } clock.tick(1); - assert.equal(1, intervalCalled, "Interval should have been called yet"); + assert.equal(intervalCalled, 1, "Interval should have been called yet"); + assert.equal(theInterval.enabled, true, "Check that the handler is running"); }); }); - }); diff --git a/lib/test/src/common/timer/timeout.test.ts b/lib/test/src/common/timer/timeout.test.ts index 41fd9d1a..ebdee125 100644 --- a/lib/test/src/common/timer/timeout.test.ts +++ b/lib/test/src/common/timer/timeout.test.ts @@ -27,13 +27,13 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); }); it("cancel timeout", () => { @@ -42,14 +42,36 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } theTimeout.cancel(); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + + clock.tick(1); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + }); + + it("cancel timeout via enabled", () => { + let timeoutCalled = false; + let theTimeout = scheduleTimeout(() => { + timeoutCalled = true; + }, 100); + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + for (let lp = 0; lp < 99; lp++) { + clock.tick(1); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + } + theTimeout.enabled = false; + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); }); it("refresh timeout", () => { @@ -58,70 +80,165 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); theTimeout.refresh(); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 98; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); // reset theTimeout.refresh(); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + timeoutCalled = false; - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); // reset theTimeout.refresh(); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + timeoutCalled = false; - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); theTimeout.refresh(); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 98; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + }); + + it("refresh timeout via enabled", () => { + let timeoutCalled = false; + let theTimeout = scheduleTimeout(() => { + timeoutCalled = true; + }, 100); + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + for (let lp = 0; lp < 99; lp++) { + clock.tick(1); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + + // Doesn't reset the timer + theTimeout.enabled = true; + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + + clock.tick(1); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + + // restart the timer + theTimeout.enabled = true; + timeoutCalled = false; + for (let lp = 0; lp < 99; lp++) { + clock.tick(1); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + } + clock.tick(1); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + + // reset + theTimeout.enabled = true; + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + + timeoutCalled = false; + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + for (let lp = 0; lp < 99; lp++) { + clock.tick(1); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + clock.tick(1); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + + // reset the timer + theTimeout.enabled = true; + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + + timeoutCalled = false; + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + for (let lp = 0; lp < 99; lp++) { + clock.tick(1); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + + // Should not refresh the timer + theTimeout.enabled = true; + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + + clock.tick(1); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); }); describe("pass extra arguments", () => { it("basic timeout", () => { let timeoutCalled = false; let theArgs: any = null; - scheduleTimeout(function (a, b) { + let theTimeout = scheduleTimeout(function (a, b) { timeoutCalled = true; theArgs = arguments; }, 100, "Hello", "Darkness"); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(null, theArgs, "No arguments"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(null, theArgs, "No arguments"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); assert.equal(2, theArgs.length); assert.equal("Hello", theArgs[0], "First Arg"); assert.equal("Darkness", theArgs[1], "Second Arg"); @@ -135,23 +252,31 @@ describe("timeout tests", () => { theArgs = arguments; }, 100, "Hello", "Darkness"); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(null, theArgs, "No arguments"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(null, theArgs, "No arguments"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); theTimeout.refresh(); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 98; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); assert.equal(2, theArgs.length); assert.equal("Hello", theArgs[0], "First Arg"); assert.equal("Darkness", theArgs[1], "Second Arg"); @@ -163,23 +288,30 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); theTimeout.cancel(); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); theTimeout.refresh(); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); }); }); @@ -193,20 +325,24 @@ describe("timeout tests", () => { it("basic timeout", () => { let timeoutCalled = false; - scheduleTimeoutWith(newSetTimeoutFn, () => { + let theTimeout = scheduleTimeoutWith(newSetTimeoutFn, () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); - assert.equal(1, overrideCalled, "The override should have been called"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(overrideCalled, 1, "The override should have been called"); }); }); @@ -226,22 +362,26 @@ describe("timeout tests", () => { it("basic timeout no clear", () => { let timeoutCalled = false; - scheduleTimeoutWith([newSetTimeoutFn, clearSetTimeoutFn], () => { + let theTimeout = scheduleTimeoutWith([newSetTimeoutFn, clearSetTimeoutFn], () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); - assert.equal(0, clearCalled, "The clear override should not have been called"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); + assert.equal(clearCalled, 0, "The clear override should not have been called"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); - assert.equal(1, overrideCalled, "The override should have been called"); - assert.equal(0, clearCalled, "The clear override should not have been called"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(overrideCalled, 1, "The override should have been called"); + assert.equal(clearCalled, 0, "The clear override should not have been called"); }); }); @@ -261,40 +401,99 @@ describe("timeout tests", () => { it("basic timeout", () => { let timeoutCalled = false; + overrideCalled = 0; + clearCalled = 0; + let theTimeout = scheduleTimeoutWith([newSetTimeoutFn, clearSetTimeoutFn], () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); - assert.equal(0, clearCalled, "The clear override should not have been called"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); + assert.equal(clearCalled, 0, "The clear override should not have been called"); } + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); theTimeout.cancel(); - assert.equal(1, clearCalled, "The clear override should have been called"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + + assert.equal(clearCalled, 1, "The clear override should have been called"); clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); - assert.equal(1, clearCalled, "The clear override should have been called"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); + assert.equal(clearCalled, 1, "The clear override should have been called"); theTimeout.refresh(); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(2, overrideCalled, "The override should not have been called yet"); - assert.equal(1, clearCalled, "The clear override should have been called once"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 2, "The override should not have been called yet"); + assert.equal(clearCalled, 1, "The clear override should have been called once"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(2, overrideCalled, "The override should not have been called yet"); - assert.equal(1, clearCalled, "The clear override should have been called once"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 2, "The override should not have been called yet"); + assert.equal(clearCalled, 1, "The clear override should have been called once"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); - assert.equal(2, overrideCalled, "The override should have been called"); - assert.equal(1, clearCalled, "The clear override should have been called once"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(overrideCalled, 2, "The override should have been called"); + assert.equal(clearCalled, 1, "The clear override should have been called once"); + }); + + it("basic timeout using enabled", () => { + let timeoutCalled = false; + overrideCalled = 0; + clearCalled = 0; + + let theTimeout = scheduleTimeoutWith([newSetTimeoutFn, clearSetTimeoutFn], () => { + timeoutCalled = true; + }, 100); + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); + for (let lp = 0; lp < 99; lp++) { + clock.tick(1); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); + assert.equal(clearCalled, 0, "The clear override should not have been called"); + } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + + // Stop the timer + theTimeout.enabled = false; + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + + assert.equal(clearCalled, 1, "The clear override should have been called"); + clock.tick(1); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); + assert.equal(clearCalled, 1, "The clear override should have been called"); + + // Start the timer + theTimeout.enabled = true; + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 2, "The override should not have been called yet"); + assert.equal(clearCalled, 1, "The clear override should have been called once"); + for (let lp = 0; lp < 99; lp++) { + clock.tick(1); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 2, "The override should not have been called yet"); + assert.equal(clearCalled, 1, "The clear override should have been called once"); + } + clock.tick(1); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(overrideCalled, 2, "The override should have been called"); + assert.equal(clearCalled, 1, "The clear override should have been called once"); }); }); @@ -312,79 +511,96 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); } + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); theTimeout.cancel(); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(1, overrideCalled, "The override should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 1, "The override should not have been called yet"); theTimeout.refresh(); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(2, overrideCalled, "The override should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 2, "The override should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); - assert.equal(2, overrideCalled, "The override should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); + assert.equal(overrideCalled, 2, "The override should not have been called yet"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); - assert.equal(2, overrideCalled, "The override should have been called"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(overrideCalled, 2, "The override should have been called"); }); }); describe("override timeout with no override", () => { it("basic timeout", () => { let timeoutCalled = false; - scheduleTimeoutWith(null as any, () => { + let theTimeout = scheduleTimeoutWith(null as any, () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); }); }); describe("override timeout with no overrides", () => { it("basic timeout", () => { let timeoutCalled = false; - scheduleTimeoutWith([null, null], () => { + let theTimeout = scheduleTimeoutWith([null, null], () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); }); it("basic timeout with empty array", () => { let timeoutCalled = false; - scheduleTimeoutWith([] as unknown as TimeoutOverrideFuncs, () => { + let theTimeout = scheduleTimeoutWith([] as unknown as TimeoutOverrideFuncs, () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } + + assert.equal(theTimeout.enabled, true, "Check that the handler is running"); clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(theTimeout.enabled, false, "Check that the handler is stopped"); }); it("basic create timeout", () => { @@ -393,19 +609,22 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timer.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); clock.tick(500); - assert.equal(false, timeoutCalled, "Timeout should still not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should still not have been called yet"); timer.refresh(); + assert.equal(timer.enabled, true, "Check that the handler is running"); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(timer.enabled, false, "Check that the handler is stopped"); }); it("basic timeout with empty array", () => { @@ -414,19 +633,22 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timer.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); clock.tick(500); - assert.equal(false, timeoutCalled, "Timeout should still not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should still not have been called yet"); timer.refresh(); + assert.equal(timer.enabled, true, "Check that the handler is running"); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(timer.enabled, false, "Check that the handler is stopped"); }); }); @@ -437,7 +659,8 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timer.enabled, true, "Check that the handler is running"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(timer.hasRef(), true, "Check that the default for creating and scheduling a new timer is referenced"); timer.unref(); assert.equal(timer.hasRef(), false, "Check that the timer can be unref'd"); @@ -455,10 +678,11 @@ describe("timeout tests", () => { for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timer.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); assert.equal(timer.hasRef(), true, "Check that the timer is still considered referenced even after the timeout function has been called"); timer.unref(); @@ -482,7 +706,8 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timer.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(timer.hasRef(), true, "Check that the default for creating and scheduling a new timer is referenced"); timer.unref(); assert.equal(timer.hasRef(), false, "Check that the timer can be unref'd"); @@ -500,8 +725,9 @@ describe("timeout tests", () => { clock.tick(500); timer.refresh(); + assert.equal(timer.enabled, true, "Check that the handler is running"); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(timer.hasRef(), true, "Check that the default for creating and scheduling a new timer is referenced"); timer.unref(); assert.equal(timer.hasRef(), false, "Check that the timer can be unref'd"); @@ -518,11 +744,11 @@ describe("timeout tests", () => { assert.equal(timer.hasRef(), true, "Check that calling unref multiple times has no impact"); clock.tick(99); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); timer.cancel(); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(timer.hasRef(), true, "Check that the default for creating and scheduling a new timer is referenced"); timer.unref(); assert.equal(timer.hasRef(), false, "Check that the timer can be unref'd"); @@ -539,11 +765,14 @@ describe("timeout tests", () => { assert.equal(timer.hasRef(), true, "Check that calling unref multiple times has no impact"); timer.refresh(); + assert.equal(timer.enabled, true, "Check that the handler is running"); + clock.tick(99); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); timer.refresh(); + assert.equal(timer.enabled, true, "Check that the handler is running"); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(timer.hasRef(), true, "Check that the default for creating and scheduling a new timer is referenced"); timer.unref(); assert.equal(timer.hasRef(), false, "Check that the timer can be unref'd"); @@ -561,10 +790,11 @@ describe("timeout tests", () => { for (let lp = 0; lp < 99; lp++) { clock.tick(1); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); } clock.tick(1); - assert.equal(true, timeoutCalled, "Timeout should have been called yet"); + assert.equal(timeoutCalled, true, "Timeout should have been called yet"); + assert.equal(timer.enabled, false, "Check that the handler is stopped"); assert.equal(timer.hasRef(), true, "Check that the timer is still considered referenced even after the timeout function has been called"); timer.unref(); @@ -588,7 +818,8 @@ describe("timeout tests", () => { timeoutCalled = true; }, 100); - assert.equal(false, timeoutCalled, "Timeout should not have been called yet"); + assert.equal(timer.enabled, false, "Check that the handler is stopped"); + assert.equal(timeoutCalled, false, "Timeout should not have been called yet"); assert.equal(timer.hasRef(), true, "Check that the default for creating and scheduling a new timer is referenced"); timer.unref(); assert.equal(timer.hasRef(), false, "Check that the timer can be unref'd"); @@ -598,12 +829,15 @@ describe("timeout tests", () => { assert.equal(timer.hasRef(), false, "Check that the timer can be unref'd"); timer.refresh(); + assert.equal(timer.enabled, true, "Check that the handler is running"); assert.equal(timer.hasRef(), false, "Check that the timer is unref'd after refresh"); timer.cancel(); + assert.equal(timer.enabled, false, "Check that the handler is stopped"); assert.equal(timer.hasRef(), false, "Check that the timer is unref'd after refresh"); timer.refresh(); + assert.equal(timer.enabled, true, "Check that the handler is running"); assert.equal(timer.hasRef(), false, "Check that the timer is unref'd after refresh"); }); });