@matrixai/timer
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -115,3 +115,3 @@ import type { PromiseCancellableController } from '@matrixai/async-cancellable'; | ||
* Refreshes the timer to the original delay and updates the scheduled time. | ||
* If the timer has already ended this does nothing. | ||
* @throws {ErrorTimerEnded} If the timer is `settling` or `settled` | ||
*/ | ||
@@ -121,2 +121,3 @@ refresh(): void; | ||
* Resets the timer with a new delay and updates the scheduled time and delay. | ||
* @throws {ErrorTimerEnded} If the timer is `settling` or `settled` | ||
*/ | ||
@@ -123,0 +124,0 @@ reset(delay: number): void; |
@@ -187,15 +187,15 @@ import { performance } from 'node:perf_hooks'; | ||
* Refreshes the timer to the original delay and updates the scheduled time. | ||
* If the timer has already ended this does nothing. | ||
* @throws {ErrorTimerEnded} If the timer is `settling` or `settled` | ||
*/ | ||
refresh() { | ||
if (this.timeoutRef == null) | ||
if (this._status != null) | ||
throw new ErrorTimerEnded(); | ||
this.timeoutRef.refresh(); | ||
this._scheduled = new Date(performance.timeOrigin + performance.now() + this._delay); | ||
return this.reset(this._delay); | ||
} | ||
/** | ||
* Resets the timer with a new delay and updates the scheduled time and delay. | ||
* @throws {ErrorTimerEnded} If the timer is `settling` or `settled` | ||
*/ | ||
reset(delay) { | ||
if (this.timeoutRef == null) | ||
if (this._status != null) | ||
throw new ErrorTimerEnded(); | ||
@@ -215,3 +215,3 @@ // This needs to re-create the timeout with the constructor logic. | ||
this.timeoutRef = setInterval(() => { }, 2 ** 31 - 1); | ||
this._scheduled = undefined; | ||
delete this._scheduled; | ||
} | ||
@@ -235,6 +235,7 @@ } | ||
} | ||
delete this.handler; | ||
this._status = 'settled'; | ||
} | ||
async reject(reason) { | ||
if ((this.lazy && (this._status == null || this._status === 'settling')) || | ||
if ((this.lazy && this._status === 'settling') || | ||
this._status === 'settled') { | ||
@@ -247,2 +248,3 @@ return; | ||
this.rejectP(reason); | ||
delete this.handler; | ||
this._status = 'settled'; | ||
@@ -249,0 +251,0 @@ } |
{ | ||
"name": "@matrixai/timer", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"author": "Roger Qiu", | ||
@@ -5,0 +5,0 @@ "description": "Stopwatch timer object", |
@@ -16,2 +16,11 @@ # js-timer | ||
## Usage | ||
```ts | ||
import { Timer } from '@matrixai/timer'; | ||
const t1 = new Timer(() => 2, 10); | ||
const result = await t1; | ||
``` | ||
## Development | ||
@@ -18,0 +27,0 @@ |
Sorry, the diff of this file is not supported yet
37774
475
82