From e74b085033f2befec2bdf5ffbff736031b2e47b6 Mon Sep 17 00:00:00 2001 From: Andrija Milojevic Date: Fri, 7 Mar 2025 18:59:20 +0100 Subject: [PATCH 1/3] Including callback reference in the TimeoutController --- index.js | 197 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 111 insertions(+), 86 deletions(-) diff --git a/index.js b/index.js index 300020e..d0ff326 100644 --- a/index.js +++ b/index.js @@ -7,15 +7,16 @@ const now = require("performance-now"); */ class TimeoutController { - /** - * @param {TimeoutConfiguration} config - */ - constructor(config) { - this.config = config; - } - cancel() { - this.config.cancelled = true; - } + /** + * @param {TimeoutConfiguration} config + */ + constructor(config) { + this.config = config; + this.callback = config.callback; + } + cancel() { + this.config.cancelled = true; + } } /** @@ -25,10 +26,16 @@ class TimeoutController { * @param {IntervalConfiguration | TimeoutConfiguration} config Don't set this parameter manually * @return {TimeoutController} */ -function prcTimeout(milliseconds, callback, config = { cancelled: false }) { - const executeAfter = now() + milliseconds; - requestAnimationFrame((ts) => tick(ts, executeAfter, callback, false, config)); - return new TimeoutController(config); +function prcTimeout( + milliseconds, + callback, + config = { cancelled: false, callback } +) { + const executeAfter = now() + milliseconds; + requestAnimationFrame((ts) => + tick(ts, executeAfter, callback, false, config) + ); + return new TimeoutController(config); } /** @@ -38,10 +45,14 @@ function prcTimeout(milliseconds, callback, config = { cancelled: false }) { * @param {IntervalConfiguration} config Don't set this parameter manually * @return {TimeoutController} */ -function prcTimeoutWithDelta(milliseconds, callback, config = { cancelled: false, lastCallTimestamp: 0 }) { - const executeAfter = now() + milliseconds; - requestAnimationFrame(ts => tick(ts, executeAfter, callback, true, config)); - return new TimeoutController(config); +function prcTimeoutWithDelta( + milliseconds, + callback, + config = { cancelled: false, lastCallTimestamp: 0, callback } +) { + const executeAfter = now() + milliseconds; + requestAnimationFrame((ts) => tick(ts, executeAfter, callback, true, config)); + return new TimeoutController(config); } /** @@ -53,90 +64,95 @@ function prcTimeoutWithDelta(milliseconds, callback, config = { cancelled: false */ class IntervalController { - /** - * @param {IntervalConfiguration} config - * @param {boolean} hasDelta - */ - constructor(config, hasDelta) { - this.config = config; - this.callback = config.callback; - this.hasDelta = hasDelta; - } - - restart() { - this.config.cancelled = true; - if (this.hasDelta) { - const newInterval = prcIntervalWithDelta(this.config.interval, this.config.callback); - this.config = newInterval.config; - } else { - const newInterval = prcInterval(this.config.interval, this.config.callback); - this.config = newInterval.config; - } + /** + * @param {IntervalConfiguration} config + * @param {boolean} hasDelta + */ + constructor(config, hasDelta) { + this.config = config; + this.callback = config.callback; + this.hasDelta = hasDelta; + } + + restart() { + this.config.cancelled = true; + if (this.hasDelta) { + const newInterval = prcIntervalWithDelta( + this.config.interval, + this.config.callback + ); + this.config = newInterval.config; + } else { + const newInterval = prcInterval( + this.config.interval, + this.config.callback + ); + this.config = newInterval.config; } + } - cancel() { - this.config.cancelled = true; - } + cancel() { + this.config.cancelled = true; + } - pauseResume() { - if (this.config.cancelled) { - this.restart(); - } else { - this.config.cancelled = true; - } + pauseResume() { + if (this.config.cancelled) { + this.restart(); + } else { + this.config.cancelled = true; } + } - setPeriod(milliseconds) { - this.config.interval = milliseconds; - this.restart(); - } - - getPeriod() { - return this.config.interval; - } + setPeriod(milliseconds) { + this.config.interval = milliseconds; + this.restart(); + } + getPeriod() { + return this.config.interval; + } } /** - * + * * @param {Number} milliseconds Delay time in milliseconds * @param {Function} callback Function which will be executed after delay time * @returns {IntervalController} */ function prcInterval(milliseconds, callback) { - /** @type {IntervalConfiguration} */ let config = { - cancelled: false, - interval: milliseconds, - callback: callback, - }; - const configuredCallback = () => { - callback(); - prcTimeout(milliseconds, configuredCallback, config); - } + /** @type {IntervalConfiguration} */ let config = { + cancelled: false, + interval: milliseconds, + callback: callback, + }; + const configuredCallback = () => { + callback(); prcTimeout(milliseconds, configuredCallback, config); - return new IntervalController(config, false); + }; + prcTimeout(milliseconds, configuredCallback, config); + return new IntervalController(config, false); } /** - * + * * @param {Number} milliseconds Delay time in milliseconds * @param {Function} callback Function which will be executed after delay time * @returns {IntervalConfiguration} */ function prcIntervalWithDelta(milliseconds, callback) { - /** @type {IntervalConfiguration} */ let config = { - cancelled: false, - interval: milliseconds, - callback: callback, - lastCallTimestamp: 0 - }; - - const configuredCallback = (deltaT) => { - callback(deltaT); - prcTimeoutWithDelta(milliseconds, configuredCallback, config); - } + /** @type {IntervalConfiguration} */ let config = { + cancelled: false, + interval: milliseconds, + callback: callback, + lastCallTimestamp: 0, + }; + + const configuredCallback = (deltaT) => { + callback(deltaT); prcTimeoutWithDelta(milliseconds, configuredCallback, config); - return new IntervalController(config, true); + }; + prcTimeoutWithDelta(milliseconds, configuredCallback, config); + return new IntervalController(config, true); } /** @@ -147,14 +163,23 @@ function prcIntervalWithDelta(milliseconds, callback) { * @param {TimeoutConfiguration | IntervalConfiguration} config Interval or Timeout config */ function tick(timestamp, executeAfter, callback, bindDeltaT, config) { - if (timestamp < executeAfter && !config.cancelled) { - requestAnimationFrame((ts) => tick(ts, executeAfter, callback, bindDeltaT, config)); - } else if (!config.cancelled && bindDeltaT) { - callback(timestamp - config.lastCallTimestamp); - config.lastCallTimestamp = timestamp; - } else if (!config.cancelled) { - callback(); - } + if (timestamp < executeAfter && !config.cancelled) { + requestAnimationFrame((ts) => + tick(ts, executeAfter, callback, bindDeltaT, config) + ); + } else if (!config.cancelled && bindDeltaT) { + callback(timestamp - config.lastCallTimestamp); + config.lastCallTimestamp = timestamp; + } else if (!config.cancelled) { + callback(); + } } -module.exports = { prcTimeout, prcTimeoutWithDelta, prcInterval, prcIntervalWithDelta, IntervalController, TimeoutController }; \ No newline at end of file +module.exports = { + prcTimeout, + prcTimeoutWithDelta, + prcInterval, + prcIntervalWithDelta, + IntervalController, + TimeoutController, +}; From 436a52226353b3b746104d8b4e4f6adc0e57b1b6 Mon Sep 17 00:00:00 2001 From: Andrija Milojevic Date: Fri, 7 Mar 2025 18:59:52 +0100 Subject: [PATCH 2/3] Bumping version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2610070..51b3445 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "precision-timeout-interval", - "version": "4.3.0", + "version": "4.3.1", "description": "High precision, hardware accelerated timeout and interval methods for javascript", "main": "index.js", "scripts": { From 1838929d9a430575e677b6383dca492ee98b0549 Mon Sep 17 00:00:00 2001 From: Andrija Milojevic Date: Fri, 7 Mar 2025 19:02:10 +0100 Subject: [PATCH 3/3] Fixing types --- index.d.ts | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/index.d.ts b/index.d.ts index c42029d..1131e95 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,19 +1,39 @@ type TimeoutController = { - cancel(): void -} + cancel(): void; + callback: Function; +}; -declare function prcTimeout(milliseconds:number, callback: Function) : TimeoutController; -declare function prcTimeoutWithDelta(milliseconds:number, callback: Function) : TimeoutController; +declare function prcTimeout( + milliseconds: number, + callback: Function +): TimeoutController; +declare function prcTimeoutWithDelta( + milliseconds: number, + callback: Function +): TimeoutController; type IntervalController = { - cancel(): void, - restart(): void, - setPeriod(milliseconds:number): void, - getPeriod(): number, - pauseResume(): void, - callback: Function -} -declare function prcInterval(milliseconds:number, callback:Function) : IntervalController; -declare function prcIntervalWithDelta(milliseconds:number, callback:Function) : IntervalController; + cancel(): void; + restart(): void; + setPeriod(milliseconds: number): void; + getPeriod(): number; + pauseResume(): void; + callback: Function; +}; +declare function prcInterval( + milliseconds: number, + callback: Function +): IntervalController; +declare function prcIntervalWithDelta( + milliseconds: number, + callback: Function +): IntervalController; -export {prcTimeout, prcTimeoutWithDelta, prcInterval, prcIntervalWithDelta, TimeoutController, IntervalController} \ No newline at end of file +export { + prcTimeout, + prcTimeoutWithDelta, + prcInterval, + prcIntervalWithDelta, + TimeoutController, + IntervalController, +};