@matrixai/timer
Advanced tools
Comparing version 1.0.0 to 1.1.0
export { default as Timer } from './Timer'; | ||
export * as errors from './errors'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -6,5 +29,6 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Timer = void 0; | ||
exports.errors = exports.Timer = void 0; | ||
var Timer_1 = require("./Timer"); | ||
Object.defineProperty(exports, "Timer", { enumerable: true, get: function () { return __importDefault(Timer_1).default; } }); | ||
exports.errors = __importStar(require("./errors")); | ||
//# sourceMappingURL=index.js.map |
@@ -12,3 +12,3 @@ import type { PromiseCancellableController } from '@matrixai/async-cancellable'; | ||
*/ | ||
readonly delay: number; | ||
protected _delay: number; | ||
/** | ||
@@ -30,3 +30,3 @@ * If it is lazy, the timer will not eagerly reject | ||
*/ | ||
readonly scheduled?: Date; | ||
protected _scheduled?: Date; | ||
/** | ||
@@ -78,2 +78,13 @@ * Handler to be executed | ||
/** | ||
* Timestamp when this is scheduled to finish and execute the handler | ||
* Guaranteed to be weakly monotonic within the process lifetime | ||
* Compare this with `performance.now()` not `Date.now()` | ||
*/ | ||
get scheduled(): Date | undefined; | ||
/** | ||
* Delay in milliseconds | ||
* This may be `Infinity` | ||
*/ | ||
get delay(): number; | ||
/** | ||
* Gets the remaining time in milliseconds | ||
@@ -105,2 +116,11 @@ * This will return `Infinity` if `delay` is `Infinity` | ||
finally(onFinally?: ((signal: AbortSignal) => void) | undefined | null, controller?: PromiseCancellableController): PromiseCancellable<T>; | ||
/** | ||
* Refreshes the timer to the original delay and updates the scheduled time. | ||
* If the timer has already ended this does nothing. | ||
*/ | ||
refresh(): void; | ||
/** | ||
* Resets the timer with a new delay and updates the scheduled time and delay. | ||
*/ | ||
reset(delay: number): void; | ||
protected fulfill(): Promise<void>; | ||
@@ -107,0 +127,0 @@ protected reject(reason?: any): Promise<void>; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const async_cancellable_1 = require("@matrixai/async-cancellable"); | ||
const errors_1 = require("@/errors"); | ||
/** | ||
@@ -40,3 +41,3 @@ * Just like `setTimeout` or `setInterval`, | ||
this.handler = handler; | ||
this.delay = delay; | ||
this._delay = delay; | ||
this.lazy = lazy; | ||
@@ -65,3 +66,3 @@ let abortController; | ||
this.timestamp = new Date(perf_hooks_1.performance.timeOrigin + perf_hooks_1.performance.now()); | ||
this.scheduled = new Date(this.timestamp.getTime() + delay); | ||
this._scheduled = new Date(this.timestamp.getTime() + delay); | ||
} | ||
@@ -82,2 +83,17 @@ else { | ||
/** | ||
* Timestamp when this is scheduled to finish and execute the handler | ||
* Guaranteed to be weakly monotonic within the process lifetime | ||
* Compare this with `performance.now()` not `Date.now()` | ||
*/ | ||
get scheduled() { | ||
return this._scheduled; | ||
} | ||
/** | ||
* Delay in milliseconds | ||
* This may be `Infinity` | ||
*/ | ||
get delay() { | ||
return this._delay; | ||
} | ||
/** | ||
* Gets the remaining time in milliseconds | ||
@@ -90,5 +106,6 @@ * This will return `Infinity` if `delay` is `Infinity` | ||
return 0; | ||
if (this.scheduled == null) | ||
if (this._scheduled == null) | ||
return Infinity; | ||
return Math.max(Math.trunc(this.scheduled.getTime() - (perf_hooks_1.performance.timeOrigin + perf_hooks_1.performance.now())), 0); | ||
return Math.max(Math.trunc(this._scheduled.getTime() - | ||
(perf_hooks_1.performance.timeOrigin + perf_hooks_1.performance.now())), 0); | ||
} | ||
@@ -129,2 +146,34 @@ /** | ||
} | ||
/** | ||
* Refreshes the timer to the original delay and updates the scheduled time. | ||
* If the timer has already ended this does nothing. | ||
*/ | ||
refresh() { | ||
if (this.timeoutRef == null) | ||
throw new errors_1.ErrorTimerEnded(); | ||
this.timeoutRef.refresh(); | ||
this._scheduled = new Date(perf_hooks_1.performance.timeOrigin + perf_hooks_1.performance.now() + this._delay); | ||
} | ||
/** | ||
* Resets the timer with a new delay and updates the scheduled time and delay. | ||
*/ | ||
reset(delay) { | ||
if (this.timeoutRef == null) | ||
throw new errors_1.ErrorTimerEnded(); | ||
// This needs to re-create the timeout with the constructor logic. | ||
clearTimeout(this.timeoutRef); | ||
// If the delay is Infinity, this promise will never resolve | ||
// it may still reject however | ||
this._delay = delay; | ||
if (isFinite(delay)) { | ||
this.timeoutRef = setTimeout(() => void this.fulfill(), delay); | ||
this._scheduled = new Date(perf_hooks_1.performance.timeOrigin + perf_hooks_1.performance.now() + delay); | ||
} | ||
else { | ||
// Infinite interval, make sure you are cancelling the `Timer` | ||
// otherwise you will keep the process alive | ||
this.timeoutRef = setInterval(() => { }, 2 ** 31 - 1); | ||
this._scheduled = undefined; | ||
} | ||
} | ||
async fulfill() { | ||
@@ -131,0 +180,0 @@ this._status = 'settling'; |
{ | ||
"name": "@matrixai/timer", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "Roger Qiu", | ||
@@ -30,3 +30,3 @@ "description": "Stopwatch timer object", | ||
"dependencies": { | ||
"@matrixai/async-cancellable": "^1.0.2" | ||
"@matrixai/async-cancellable": "^1.0.4" | ||
}, | ||
@@ -39,2 +39,3 @@ "devDependencies": { | ||
"@typescript-eslint/parser": "^5.23.0", | ||
"@matrixai/errors": "^1.1.7", | ||
"eslint": "^8.15.0", | ||
@@ -41,0 +42,0 @@ "eslint-config-prettier": "^8.5.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35028
12
383
20