@oscarpalmer/timer 0.37.0 → 0.38.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -13
- package/dist/constants.js +11 -13
- package/dist/delay.js +3 -3
- package/dist/get.js +2 -2
- package/dist/global.js +4 -4
- package/dist/index.js +4 -4
- package/dist/repeat.js +2 -2
- package/dist/timer.full.js +434 -449
- package/dist/wait.js +2 -2
- package/dist/when.js +11 -8
- package/dist/work.js +5 -5
- package/package.json +17 -20
- package/src/constants.ts +20 -14
- package/src/delay.ts +6 -7
- package/src/get.ts +3 -5
- package/src/global.ts +4 -9
- package/src/is.ts +8 -0
- package/src/models.ts +35 -40
- package/src/repeat.ts +6 -6
- package/src/timer.ts +4 -17
- package/src/wait.ts +2 -2
- package/src/when.ts +35 -31
- package/src/work.ts +17 -32
- package/types/constants.d.ts +12 -12
- package/types/delay.d.ts +3 -1
- package/types/is.d.ts +8 -0
- package/types/repeat.d.ts +3 -0
- package/types/when.d.ts +15 -1
- package/dist/constants.cjs +0 -53
- package/dist/delay.cjs +0 -15
- package/dist/get.cjs +0 -17
- package/dist/global.cjs +0 -15
- package/dist/index.cjs +0 -14
- package/dist/is.cjs +0 -20
- package/dist/models.cjs +0 -7
- package/dist/repeat.cjs +0 -18
- package/dist/rolldown_runtime.cjs +0 -21
- package/dist/timer.cjs +0 -68
- package/dist/wait.cjs +0 -18
- package/dist/when.cjs +0 -96
- package/dist/work.cjs +0 -82
package/dist/wait.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MILLISECONDS, TYPE_WAIT } from "./constants.js";
|
|
2
2
|
import { getCallback, getValidNumber } from "./get.js";
|
|
3
3
|
import "./global.js";
|
|
4
4
|
import { TimerTrace } from "./models.js";
|
|
@@ -11,7 +11,7 @@ function wait(callback, time) {
|
|
|
11
11
|
onAfter: void 0,
|
|
12
12
|
onError: void 0,
|
|
13
13
|
count: -1,
|
|
14
|
-
interval: getValidNumber(time,
|
|
14
|
+
interval: getValidNumber(time, MILLISECONDS),
|
|
15
15
|
timeout: 0
|
|
16
16
|
}, true);
|
|
17
17
|
}
|
package/dist/when.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MESSAGE_DESTROYED, MESSAGE_STARTED, MILLISECONDS, TYPE_WHEN } from "./constants.js";
|
|
2
2
|
import { getValidNumber, getValidTimeout } from "./get.js";
|
|
3
3
|
import "./global.js";
|
|
4
4
|
import { TimerTrace } from "./models.js";
|
|
@@ -55,7 +55,7 @@ var When = class {
|
|
|
55
55
|
this.destroy();
|
|
56
56
|
},
|
|
57
57
|
count: getValidNumber(options?.count),
|
|
58
|
-
interval: getValidNumber(options?.interval,
|
|
58
|
+
interval: getValidNumber(options?.interval, MILLISECONDS),
|
|
59
59
|
timeout: getValidTimeout(options?.timeout)
|
|
60
60
|
}, false);
|
|
61
61
|
}
|
|
@@ -75,17 +75,20 @@ var When = class {
|
|
|
75
75
|
this.state.timer?.pause();
|
|
76
76
|
return this;
|
|
77
77
|
}
|
|
78
|
+
start(resolve, reject) {
|
|
79
|
+
const { state } = this;
|
|
80
|
+
if (state.timer == null) throw new Error(MESSAGE_DESTROYED);
|
|
81
|
+
if (state.started) throw new Error(MESSAGE_STARTED);
|
|
82
|
+
state.started = true;
|
|
83
|
+
state.timer.start();
|
|
84
|
+
return state.promise.then(resolve ?? noop, reject ?? noop);
|
|
85
|
+
}
|
|
78
86
|
stop() {
|
|
79
87
|
this.state.timer?.stop();
|
|
80
88
|
return this;
|
|
81
89
|
}
|
|
82
90
|
then(resolve, reject) {
|
|
83
|
-
|
|
84
|
-
if (state.timer == null) throw new Error(destroyedMessage);
|
|
85
|
-
if (state.started) throw new Error(startedMessage);
|
|
86
|
-
state.started = true;
|
|
87
|
-
state.timer.start();
|
|
88
|
-
return state.promise.then(resolve ?? noop, reject ?? noop);
|
|
91
|
+
return this.start(resolve, reject);
|
|
89
92
|
}
|
|
90
93
|
};
|
|
91
94
|
function when(condition, options) {
|
package/dist/work.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { TYPE_WAIT, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP
|
|
1
|
+
import { BUFFER_INTERVAL, MILLISECONDS, TIMERS_ACTIVE, TYPE_WAIT, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP } from "./constants.js";
|
|
2
2
|
function finish(timer, state, options, success) {
|
|
3
3
|
cancelAnimationFrame(state.frame);
|
|
4
|
-
|
|
4
|
+
TIMERS_ACTIVE.delete(timer.instance);
|
|
5
5
|
state.active = false;
|
|
6
6
|
state.elapsed = 0;
|
|
7
7
|
state.frame = void 0;
|
|
@@ -34,7 +34,7 @@ function run(timer, state, options) {
|
|
|
34
34
|
finish(timer, state, options, false);
|
|
35
35
|
return;
|
|
36
36
|
}
|
|
37
|
-
if (options.interval ===
|
|
37
|
+
if (options.interval === MILLISECONDS || state.elapsed >= options.interval - 5) {
|
|
38
38
|
if (options.count > -1) state.callback(state.index);
|
|
39
39
|
start = now;
|
|
40
40
|
state.elapsed = 0;
|
|
@@ -55,7 +55,7 @@ function setState(type, state) {
|
|
|
55
55
|
}
|
|
56
56
|
function stop(timer, state, options) {
|
|
57
57
|
cancelAnimationFrame(state.frame);
|
|
58
|
-
|
|
58
|
+
TIMERS_ACTIVE.delete(timer.instance);
|
|
59
59
|
options.onAfter?.(false);
|
|
60
60
|
state.active = !stop;
|
|
61
61
|
state.frame = void 0;
|
|
@@ -73,7 +73,7 @@ function work(type, timer, state, options) {
|
|
|
73
73
|
state.active = true;
|
|
74
74
|
state.paused = type === WORK_PAUSE;
|
|
75
75
|
if (state.paused) return pause(timer, state);
|
|
76
|
-
|
|
76
|
+
TIMERS_ACTIVE.add(timer.instance);
|
|
77
77
|
const runner = run(timer, state, options);
|
|
78
78
|
state.frame = requestAnimationFrame(runner);
|
|
79
79
|
return timer.instance;
|
package/package.json
CHANGED
|
@@ -4,21 +4,20 @@
|
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@oscarpalmer/atoms": "^0.
|
|
7
|
+
"@oscarpalmer/atoms": "^0.115"
|
|
8
8
|
},
|
|
9
9
|
"description": "A better solution for timeout- and interval-based timers.",
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@
|
|
12
|
-
"@
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"rollup": "^4.50",
|
|
11
|
+
"@types/node": "^25",
|
|
12
|
+
"@vitest/coverage-istanbul": "^4",
|
|
13
|
+
"jsdom": "^27.3",
|
|
14
|
+
"oxfmt": "^0.18",
|
|
15
|
+
"oxlint": "^1.33",
|
|
16
|
+
"rolldown": "1.0.0-beta.55",
|
|
18
17
|
"tslib": "^2.8",
|
|
19
18
|
"typescript": "^5.9",
|
|
20
|
-
"vite": "
|
|
21
|
-
"vitest": "^
|
|
19
|
+
"vite": "8.0.0-beta.2",
|
|
20
|
+
"vitest": "^4"
|
|
22
21
|
},
|
|
23
22
|
"exports": {
|
|
24
23
|
"./package.json": "./package.json",
|
|
@@ -49,10 +48,10 @@
|
|
|
49
48
|
"types"
|
|
50
49
|
],
|
|
51
50
|
"keywords": [
|
|
52
|
-
"
|
|
53
|
-
"setTimeout",
|
|
51
|
+
"requestAnimationFrame",
|
|
54
52
|
"setInterval",
|
|
55
|
-
"
|
|
53
|
+
"setTimeout",
|
|
54
|
+
"timer"
|
|
56
55
|
],
|
|
57
56
|
"license": "MIT",
|
|
58
57
|
"module": "dist/index.js",
|
|
@@ -62,16 +61,14 @@
|
|
|
62
61
|
"url": "git+https://github.com/oscarpalmer/timer.git"
|
|
63
62
|
},
|
|
64
63
|
"scripts": {
|
|
65
|
-
"build": "npm run clean &&
|
|
66
|
-
"build:js": "npx vite build",
|
|
64
|
+
"build": "npm run clean && npx vite build && npm run rolldown:build && npx tsc",
|
|
67
65
|
"clean": "rm -rf ./dist && rm -rf ./types && rm -f ./tsconfig.tsbuildinfo",
|
|
68
|
-
"
|
|
69
|
-
"
|
|
66
|
+
"rolldown:build": "npx rolldown -c",
|
|
67
|
+
"rolldown:watch": "npx rolldown -c --watch",
|
|
70
68
|
"test": "npx vitest --coverage",
|
|
71
|
-
"types": "npx tsc && npx dts-bundle-generator --config ./dts.config.cts --silent",
|
|
72
69
|
"watch": "npx vite build --watch"
|
|
73
70
|
},
|
|
74
71
|
"type": "module",
|
|
75
72
|
"types": "types/index.d.ts",
|
|
76
|
-
"version": "0.
|
|
77
|
-
}
|
|
73
|
+
"version": "0.38.1"
|
|
74
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -34,41 +34,47 @@ function calculate(): Promise<number> {
|
|
|
34
34
|
|
|
35
35
|
//
|
|
36
36
|
|
|
37
|
-
export const defaultTimeout = 30_000;
|
|
38
|
-
|
|
39
37
|
/**
|
|
40
|
-
*
|
|
38
|
+
* Buffer value to use when evaluating if a specific time is within a certain range
|
|
41
39
|
*/
|
|
42
|
-
export const
|
|
40
|
+
export const BUFFER_INTERVAL = 5;
|
|
41
|
+
|
|
42
|
+
export const DEFAULT_TIMEOUT = 30_000;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
45
|
+
* Message to show when a when-timer is destroyed
|
|
46
46
|
*/
|
|
47
|
-
export const
|
|
47
|
+
export const MESSAGE_DESTROYED = 'Timer has already been destroyed';
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
* Message to show when a when-timer is
|
|
50
|
+
* Message to show when a when-timer is started
|
|
51
51
|
*/
|
|
52
|
-
export const
|
|
52
|
+
export const MESSAGE_STARTED = 'Timer has already been started';
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
|
-
* A set of
|
|
55
|
+
* A set of all active timers
|
|
56
56
|
*/
|
|
57
|
-
export const
|
|
57
|
+
export const TIMERS_ACTIVE = new Set<Timer>();
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
|
-
*
|
|
60
|
+
* A set of timers that were paused due to the document being hidden
|
|
61
61
|
*/
|
|
62
|
-
export const
|
|
62
|
+
export const TIMERS_HIDDEN = new Set<Timer>();
|
|
63
63
|
|
|
64
64
|
export const TYPE_REPEAT: TimerType = 'repeat';
|
|
65
|
+
|
|
65
66
|
export const TYPE_WAIT: TimerType = 'wait';
|
|
67
|
+
|
|
66
68
|
export const TYPE_WHEN: TimerType = 'when';
|
|
67
69
|
|
|
68
70
|
export const WORK_CONTINUE: WorkHandlerType = 'continue';
|
|
71
|
+
|
|
69
72
|
export const WORK_PAUSE: WorkHandlerType = 'pause';
|
|
73
|
+
|
|
70
74
|
export const WORK_RESTART: WorkHandlerType = 'restart';
|
|
75
|
+
|
|
71
76
|
export const WORK_START: WorkHandlerType = 'start';
|
|
77
|
+
|
|
72
78
|
export const WORK_STOP: WorkHandlerType = 'stop';
|
|
73
79
|
|
|
74
80
|
//
|
|
@@ -76,8 +82,8 @@ export const WORK_STOP: WorkHandlerType = 'stop';
|
|
|
76
82
|
/**
|
|
77
83
|
* A calculated average of the refresh rate of the display _(in milliseconds)_
|
|
78
84
|
*/
|
|
79
|
-
export let
|
|
85
|
+
export let MILLISECONDS = 1000 / 60;
|
|
80
86
|
|
|
81
87
|
calculate().then(value => {
|
|
82
|
-
|
|
88
|
+
MILLISECONDS = value;
|
|
83
89
|
});
|
package/src/delay.ts
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {BUFFER_INTERVAL, MILLISECONDS} from './constants';
|
|
2
2
|
import {getValidNumber} from './get';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Create a delayed promise that resolves after a certain amount of time
|
|
5
|
+
* Create a delayed promise that resolves after a certain amount of time
|
|
6
|
+
* @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
|
|
7
|
+
* @returns A promise that resolves after the delay
|
|
6
8
|
*/
|
|
7
9
|
export function delay(time?: number): Promise<void> {
|
|
8
10
|
return new Promise(resolve => {
|
|
9
|
-
const interval = getValidNumber(time,
|
|
11
|
+
const interval = getValidNumber(time, MILLISECONDS);
|
|
10
12
|
|
|
11
13
|
let start: DOMHighResTimeStamp;
|
|
12
14
|
|
|
13
15
|
function step(now: DOMHighResTimeStamp) {
|
|
14
16
|
start ??= now;
|
|
15
17
|
|
|
16
|
-
if (
|
|
17
|
-
interval === milliseconds ||
|
|
18
|
-
now - start >= interval - intervalBuffer
|
|
19
|
-
) {
|
|
18
|
+
if (interval === MILLISECONDS || now - start >= interval - BUFFER_INTERVAL) {
|
|
20
19
|
resolve();
|
|
21
20
|
} else {
|
|
22
21
|
requestAnimationFrame(step);
|
package/src/get.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import type {GenericCallback} from '@oscarpalmer/atoms';
|
|
2
2
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
3
|
-
import {
|
|
3
|
+
import {DEFAULT_TIMEOUT} from './constants';
|
|
4
4
|
|
|
5
5
|
export function getCallback(value: unknown): GenericCallback {
|
|
6
6
|
return typeof value === 'function' ? (value as GenericCallback) : noop;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export function getValidTimeout(value: unknown): number {
|
|
10
|
-
return typeof value === 'number' && value > 0 ? value :
|
|
10
|
+
return typeof value === 'number' && value > 0 ? value : DEFAULT_TIMEOUT;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export function getValidNumber(value: unknown, defaultValue?: number): number {
|
|
14
14
|
const actualDefault = defaultValue ?? 0;
|
|
15
15
|
|
|
16
|
-
return typeof value === 'number' && value > actualDefault
|
|
17
|
-
? value
|
|
18
|
-
: actualDefault;
|
|
16
|
+
return typeof value === 'number' && value > actualDefault ? value : actualDefault;
|
|
19
17
|
}
|
package/src/global.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
activeTimers,
|
|
3
|
-
hiddenTimers,
|
|
4
|
-
WORK_CONTINUE,
|
|
5
|
-
WORK_PAUSE,
|
|
6
|
-
} from './constants';
|
|
1
|
+
import {TIMERS_ACTIVE, TIMERS_HIDDEN, WORK_CONTINUE, WORK_PAUSE} from './constants';
|
|
7
2
|
import type {Timer} from './timer';
|
|
8
3
|
|
|
9
4
|
declare global {
|
|
@@ -14,16 +9,16 @@ declare global {
|
|
|
14
9
|
if (globalThis._oscarpalmer_timers == null) {
|
|
15
10
|
Object.defineProperty(globalThis, '_oscarpalmer_timers', {
|
|
16
11
|
get() {
|
|
17
|
-
return globalThis._oscarpalmer_timer_debug ? [...
|
|
12
|
+
return globalThis._oscarpalmer_timer_debug ? [...TIMERS_ACTIVE] : [];
|
|
18
13
|
},
|
|
19
14
|
});
|
|
20
15
|
}
|
|
21
16
|
|
|
22
17
|
/* istanbul ignore next */
|
|
23
18
|
document.addEventListener('visibilitychange', () => {
|
|
24
|
-
const from = document.hidden ?
|
|
19
|
+
const from = document.hidden ? TIMERS_ACTIVE : TIMERS_HIDDEN;
|
|
25
20
|
const method = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
|
|
26
|
-
const to = document.hidden ?
|
|
21
|
+
const to = document.hidden ? TIMERS_HIDDEN : TIMERS_ACTIVE;
|
|
27
22
|
|
|
28
23
|
for (const timer of from) {
|
|
29
24
|
timer[method]();
|
package/src/is.ts
CHANGED
|
@@ -9,6 +9,8 @@ function is(names: string[], value: unknown) {
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Is the value a repeating timer?
|
|
12
|
+
* @param value Value to check
|
|
13
|
+
* @returns `true` if the value is a repeating timer
|
|
12
14
|
*/
|
|
13
15
|
export function isRepeated(value: unknown): value is Timer {
|
|
14
16
|
return is([TYPE_REPEAT], value);
|
|
@@ -16,6 +18,8 @@ export function isRepeated(value: unknown): value is Timer {
|
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* Is the value a timer?
|
|
21
|
+
* @param value Value to check
|
|
22
|
+
* @returns `true` if the value is a timer
|
|
19
23
|
*/
|
|
20
24
|
export function isTimer(value: unknown): value is Timer {
|
|
21
25
|
return is([TYPE_REPEAT, TYPE_WAIT], value);
|
|
@@ -23,6 +27,8 @@ export function isTimer(value: unknown): value is Timer {
|
|
|
23
27
|
|
|
24
28
|
/**
|
|
25
29
|
* Is the value a waiting timer?
|
|
30
|
+
* @param value Value to check
|
|
31
|
+
* @returns `true` if the value is a waiting timer
|
|
26
32
|
*/
|
|
27
33
|
export function isWaited(value: unknown): value is Timer {
|
|
28
34
|
return is([TYPE_WAIT], value);
|
|
@@ -30,6 +36,8 @@ export function isWaited(value: unknown): value is Timer {
|
|
|
30
36
|
|
|
31
37
|
/**
|
|
32
38
|
* Is the value a conditional timer?
|
|
39
|
+
* @param value Value to check
|
|
40
|
+
* @returns `true` if the value is a conditional timer
|
|
33
41
|
*/
|
|
34
42
|
export function isWhen(value: unknown): value is When {
|
|
35
43
|
return is([TYPE_WHEN], value) && typeof (value as When).then === 'function';
|
package/src/models.ts
CHANGED
|
@@ -4,27 +4,27 @@ import type {Timer} from './timer';
|
|
|
4
4
|
* Options for a repeating timer
|
|
5
5
|
*/
|
|
6
6
|
export type RepeatOptions = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Callback to be called when the timer has stopped, either manually or by completing its work
|
|
9
|
+
*/
|
|
10
|
+
onAfter: (finished: boolean) => void;
|
|
11
|
+
/**
|
|
12
|
+
* Callback to be called after the timer has timed out
|
|
13
|
+
*/
|
|
14
|
+
onTimeout: () => void;
|
|
15
|
+
/**
|
|
16
|
+
* How many times the timer should repeat
|
|
17
|
+
*/
|
|
18
|
+
count: number;
|
|
19
|
+
/**
|
|
20
|
+
* The interval between each repeat
|
|
21
|
+
*/
|
|
22
|
+
interval: number;
|
|
23
|
+
/**
|
|
24
|
+
* The timeout for the timer _(any value above `0` will enable the timeout)_
|
|
25
|
+
*/
|
|
26
|
+
timeout: number;
|
|
27
|
+
};
|
|
28
28
|
|
|
29
29
|
export type TimerOptions = {
|
|
30
30
|
onAfter: ((finished: boolean) => void) | undefined;
|
|
@@ -60,19 +60,19 @@ export type TimerType = 'repeat' | 'wait' | 'when';
|
|
|
60
60
|
* Options for a conditional timer
|
|
61
61
|
*/
|
|
62
62
|
export type WhenOptions = {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
63
|
+
/**
|
|
64
|
+
* How many times the timer should check the condition
|
|
65
|
+
*/
|
|
66
|
+
count: number;
|
|
67
|
+
/**
|
|
68
|
+
* Then interval between each condtional check
|
|
69
|
+
*/
|
|
70
|
+
interval: number;
|
|
71
|
+
/**
|
|
72
|
+
* The timeout for the timer _(any value above `0` will enable the timeout)_
|
|
73
|
+
*/
|
|
74
|
+
timeout: number;
|
|
75
|
+
};
|
|
76
76
|
|
|
77
77
|
export type WhenState = {
|
|
78
78
|
promise: Promise<void>;
|
|
@@ -94,9 +94,4 @@ export type WorkHandlerTimer = {
|
|
|
94
94
|
type: TimerType;
|
|
95
95
|
};
|
|
96
96
|
|
|
97
|
-
export type WorkHandlerType =
|
|
98
|
-
| 'continue'
|
|
99
|
-
| 'pause'
|
|
100
|
-
| 'restart'
|
|
101
|
-
| 'start'
|
|
102
|
-
| 'stop';
|
|
97
|
+
export type WorkHandlerType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
|
package/src/repeat.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {MILLISECONDS, TYPE_REPEAT} from './constants';
|
|
2
2
|
import {getCallback, getValidNumber} from './get';
|
|
3
3
|
import './global';
|
|
4
4
|
import {type RepeatOptions, TimerTrace} from './models';
|
|
@@ -6,11 +6,11 @@ import {Timer} from './timer';
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Create a repeating timer
|
|
9
|
+
* @param callback Callback to run on each interval
|
|
10
|
+
* @param options Timer options
|
|
11
|
+
* @returns Timer instance
|
|
9
12
|
*/
|
|
10
|
-
export function repeat(
|
|
11
|
-
callback: (index: number) => void,
|
|
12
|
-
options?: Partial<RepeatOptions>,
|
|
13
|
-
): Timer {
|
|
13
|
+
export function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer {
|
|
14
14
|
return new Timer(
|
|
15
15
|
TYPE_REPEAT,
|
|
16
16
|
{
|
|
@@ -21,7 +21,7 @@ export function repeat(
|
|
|
21
21
|
onAfter: getCallback(options?.onAfter),
|
|
22
22
|
onError: getCallback(options?.onTimeout),
|
|
23
23
|
count: getValidNumber(options?.count),
|
|
24
|
-
interval: getValidNumber(options?.interval,
|
|
24
|
+
interval: getValidNumber(options?.interval, MILLISECONDS),
|
|
25
25
|
timeout: getValidNumber(options?.timeout),
|
|
26
26
|
},
|
|
27
27
|
true,
|
package/src/timer.ts
CHANGED
|
@@ -1,21 +1,10 @@
|
|
|
1
1
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
WORK_PAUSE,
|
|
5
|
-
WORK_RESTART,
|
|
6
|
-
WORK_START,
|
|
7
|
-
WORK_STOP,
|
|
8
|
-
} from './constants';
|
|
9
|
-
import type {
|
|
10
|
-
TimerOptions,
|
|
11
|
-
TimerState,
|
|
12
|
-
TimerType,
|
|
13
|
-
WorkHandlerType,
|
|
14
|
-
} from './models';
|
|
2
|
+
import {WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP} from './constants';
|
|
3
|
+
import type {TimerOptions, TimerState, TimerType, WorkHandlerType} from './models';
|
|
15
4
|
import {stop, work} from './work';
|
|
16
5
|
|
|
17
6
|
export class Timer {
|
|
18
|
-
private
|
|
7
|
+
declare private readonly $timer: TimerType;
|
|
19
8
|
|
|
20
9
|
protected readonly state: TimerState;
|
|
21
10
|
|
|
@@ -44,9 +33,7 @@ export class Timer {
|
|
|
44
33
|
* Get the timer's origin _(if debugging is enabled)_
|
|
45
34
|
*/
|
|
46
35
|
get trace(): string | undefined {
|
|
47
|
-
return (globalThis._oscarpalmer_timer_debug ?? false)
|
|
48
|
-
? this.state.trace
|
|
49
|
-
: undefined;
|
|
36
|
+
return (globalThis._oscarpalmer_timer_debug ?? false) ? this.state.trace : undefined;
|
|
50
37
|
}
|
|
51
38
|
|
|
52
39
|
constructor(
|
package/src/wait.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {MILLISECONDS, TYPE_WAIT} from './constants';
|
|
2
2
|
import {getCallback, getValidNumber} from './get';
|
|
3
3
|
import './global';
|
|
4
4
|
import {TimerTrace} from './models';
|
|
@@ -20,7 +20,7 @@ export function wait(callback: () => void, time?: number): Timer {
|
|
|
20
20
|
onAfter: undefined,
|
|
21
21
|
onError: undefined,
|
|
22
22
|
count: -1,
|
|
23
|
-
interval: getValidNumber(time,
|
|
23
|
+
interval: getValidNumber(time, MILLISECONDS),
|
|
24
24
|
timeout: 0,
|
|
25
25
|
},
|
|
26
26
|
true,
|
package/src/when.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
|
-
import {
|
|
3
|
-
destroyedMessage,
|
|
4
|
-
milliseconds,
|
|
5
|
-
startedMessage,
|
|
6
|
-
TYPE_WHEN,
|
|
7
|
-
} from './constants';
|
|
2
|
+
import {MESSAGE_DESTROYED, MILLISECONDS, MESSAGE_STARTED, TYPE_WHEN} from './constants';
|
|
8
3
|
import {getValidNumber, getValidTimeout} from './get';
|
|
9
4
|
import './global';
|
|
10
5
|
import {TimerTrace, type WhenOptions, type WhenState} from './models';
|
|
@@ -46,9 +41,7 @@ class When {
|
|
|
46
41
|
* Get the timer's origin _(if debugging is enabled)_
|
|
47
42
|
*/
|
|
48
43
|
get trace(): string | undefined {
|
|
49
|
-
return (globalThis._oscarpalmer_timer_debug ?? false)
|
|
50
|
-
? this.state.timer?.trace
|
|
51
|
-
: undefined;
|
|
44
|
+
return (globalThis._oscarpalmer_timer_debug ?? false) ? this.state.timer?.trace : undefined;
|
|
52
45
|
}
|
|
53
46
|
|
|
54
47
|
constructor(condition: () => boolean, options?: Partial<WhenOptions>) {
|
|
@@ -93,7 +86,7 @@ class When {
|
|
|
93
86
|
this.destroy();
|
|
94
87
|
},
|
|
95
88
|
count: getValidNumber(options?.count),
|
|
96
|
-
interval: getValidNumber(options?.interval,
|
|
89
|
+
interval: getValidNumber(options?.interval, MILLISECONDS),
|
|
97
90
|
timeout: getValidTimeout(options?.timeout),
|
|
98
91
|
},
|
|
99
92
|
false,
|
|
@@ -133,30 +126,20 @@ class When {
|
|
|
133
126
|
}
|
|
134
127
|
|
|
135
128
|
/**
|
|
136
|
-
*
|
|
129
|
+
* Start the timer
|
|
130
|
+
* @param resolve Optional resolve callback
|
|
131
|
+
* @param reject Optional reject callback
|
|
132
|
+
* @returns Promise that resolves when the condition is met
|
|
137
133
|
*/
|
|
138
|
-
|
|
139
|
-
this.state.timer?.stop();
|
|
140
|
-
|
|
141
|
-
return this;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Starts the timer and returns a promise that resolves when the condition is met
|
|
146
|
-
*/
|
|
147
|
-
// biome-ignore lint/suspicious/noThenProperty: returning a promise-like object, so it's ok ;)
|
|
148
|
-
then(
|
|
149
|
-
resolve?: (() => void) | null,
|
|
150
|
-
reject?: (() => void) | null,
|
|
151
|
-
): Promise<void> {
|
|
134
|
+
start(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void> {
|
|
152
135
|
const {state} = this;
|
|
153
136
|
|
|
154
137
|
if (state.timer == null) {
|
|
155
|
-
throw new Error(
|
|
138
|
+
throw new Error(MESSAGE_DESTROYED);
|
|
156
139
|
}
|
|
157
140
|
|
|
158
141
|
if (state.started) {
|
|
159
|
-
throw new Error(
|
|
142
|
+
throw new Error(MESSAGE_STARTED);
|
|
160
143
|
}
|
|
161
144
|
|
|
162
145
|
state.started = true;
|
|
@@ -165,15 +148,36 @@ class When {
|
|
|
165
148
|
|
|
166
149
|
return state.promise.then(resolve ?? noop, reject ?? noop);
|
|
167
150
|
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Stops the timer _(if it was running)_
|
|
154
|
+
*/
|
|
155
|
+
stop(): When {
|
|
156
|
+
this.state.timer?.stop();
|
|
157
|
+
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Start the timer
|
|
163
|
+
* @deprecated Use `start()` instead
|
|
164
|
+
* @param resolve Optional resolve callback
|
|
165
|
+
* @param reject Optional reject callback
|
|
166
|
+
* @returns Promise that resolves when the condition is met
|
|
167
|
+
*/
|
|
168
|
+
// oxlint-disable-next-line no-thenable: Returning a promise-like object, so it's ok ;)
|
|
169
|
+
then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void> {
|
|
170
|
+
return this.start(resolve, reject);
|
|
171
|
+
}
|
|
168
172
|
}
|
|
169
173
|
|
|
170
174
|
/**
|
|
171
175
|
* Create a conditional timer
|
|
176
|
+
* @param condition Condition to check
|
|
177
|
+
* @param options Timer options
|
|
178
|
+
* @returns Timer instance
|
|
172
179
|
*/
|
|
173
|
-
export function when(
|
|
174
|
-
condition: () => boolean,
|
|
175
|
-
options?: Partial<WhenOptions>,
|
|
176
|
-
): When {
|
|
180
|
+
export function when(condition: () => boolean, options?: Partial<WhenOptions>): When {
|
|
177
181
|
return new When(condition, options);
|
|
178
182
|
}
|
|
179
183
|
|