@oscarpalmer/timer 0.42.0 → 0.44.0
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 +1 -1
- package/dist/constants.d.mts +4 -6
- package/dist/delay.d.mts +1 -2
- package/dist/get.d.mts +0 -1
- package/dist/global.d.mts +1 -2
- package/dist/index.d.mts +74 -97
- package/dist/index.mjs +172 -230
- package/dist/is.d.mts +1 -3
- package/dist/is.mjs +1 -1
- package/dist/models.d.mts +87 -5
- package/dist/repeat.d.mts +4 -3
- package/dist/repeat.mjs +3 -2
- package/dist/timer.d.mts +3 -50
- package/dist/timer.mjs +60 -91
- package/dist/wait.d.mts +4 -2
- package/dist/wait.mjs +3 -2
- package/dist/when.d.mts +3 -54
- package/dist/when.mjs +89 -126
- package/dist/work.d.mts +1 -3
- package/dist/work.mjs +1 -1
- package/package.json +11 -6
- package/src/constants.ts +4 -5
- package/src/delay.ts +0 -1
- package/src/get.ts +1 -1
- package/src/global.ts +1 -1
- package/src/index.ts +5 -5
- package/src/is.ts +2 -3
- package/src/models.ts +105 -4
- package/src/repeat.ts +4 -5
- package/src/timer.ts +75 -119
- package/src/wait.ts +4 -5
- package/src/when.ts +112 -158
- package/src/work.ts +3 -4
package/dist/timer.d.mts
CHANGED
|
@@ -1,52 +1,5 @@
|
|
|
1
|
-
import { TimerOptions, TimerState
|
|
2
|
-
|
|
1
|
+
import { Timer, TimerName, TimerOptions, TimerState } from "./models.mjs";
|
|
3
2
|
//#region src/timer.d.ts
|
|
4
|
-
declare
|
|
5
|
-
#private;
|
|
6
|
-
protected readonly options: TimerOptions;
|
|
7
|
-
private readonly $timer;
|
|
8
|
-
protected readonly state: TimerState;
|
|
9
|
-
/**
|
|
10
|
-
* Is the timer active?
|
|
11
|
-
*/
|
|
12
|
-
get active(): boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Is the timer destroyed?
|
|
15
|
-
*/
|
|
16
|
-
get destroyed(): boolean;
|
|
17
|
-
/**
|
|
18
|
-
* Is the timer paused?
|
|
19
|
-
*/
|
|
20
|
-
get paused(): boolean;
|
|
21
|
-
/**
|
|
22
|
-
* Get the timer's origin _(if debugging is enabled)_
|
|
23
|
-
*/
|
|
24
|
-
get trace(): string | undefined;
|
|
25
|
-
constructor(type: TimerType, state: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean);
|
|
26
|
-
/**
|
|
27
|
-
* Continue running the timer _(if it's paused)_
|
|
28
|
-
*/
|
|
29
|
-
continue(): Timer;
|
|
30
|
-
/**
|
|
31
|
-
* Destroy the timer
|
|
32
|
-
*/
|
|
33
|
-
destroy(): void;
|
|
34
|
-
/**
|
|
35
|
-
* Pause the timer _(if it's running)_
|
|
36
|
-
*/
|
|
37
|
-
pause(): Timer;
|
|
38
|
-
/**
|
|
39
|
-
* Restart the timer _(or start it, if it's not running)_
|
|
40
|
-
*/
|
|
41
|
-
restart(): Timer;
|
|
42
|
-
/**
|
|
43
|
-
* Start the timer _(if it's not running)_
|
|
44
|
-
*/
|
|
45
|
-
start(): Timer;
|
|
46
|
-
/**
|
|
47
|
-
* Stop the timer _(if it's running)_
|
|
48
|
-
*/
|
|
49
|
-
stop(): Timer;
|
|
50
|
-
}
|
|
3
|
+
declare function createTimer(name: TimerName, pick: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean): Timer;
|
|
51
4
|
//#endregion
|
|
52
|
-
export {
|
|
5
|
+
export { createTimer };
|
package/dist/timer.mjs
CHANGED
|
@@ -2,97 +2,66 @@ import { WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP } from "
|
|
|
2
2
|
import { stop, work } from "./work.mjs";
|
|
3
3
|
import { noop } from "@oscarpalmer/atoms/function";
|
|
4
4
|
//#region src/timer.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Is the timer active?
|
|
9
|
-
*/
|
|
10
|
-
get active() {
|
|
11
|
-
return this.state.active;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Is the timer destroyed?
|
|
15
|
-
*/
|
|
16
|
-
get destroyed() {
|
|
17
|
-
return this.state.destroyed;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Is the timer paused?
|
|
21
|
-
*/
|
|
22
|
-
get paused() {
|
|
23
|
-
return this.state.paused;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Get the timer's origin _(if debugging is enabled)_
|
|
27
|
-
*/
|
|
28
|
-
get trace() {
|
|
29
|
-
return globalThis._oscarpalmer_timer_debug ?? false ? this.state.trace : void 0;
|
|
30
|
-
}
|
|
31
|
-
constructor(type, state, options, start) {
|
|
32
|
-
this.options = options;
|
|
33
|
-
Object.defineProperty(this, "$timer", { value: type });
|
|
34
|
-
this.state = {
|
|
35
|
-
...state,
|
|
36
|
-
active: false,
|
|
37
|
-
destroyed: false,
|
|
38
|
-
elapsed: 0,
|
|
39
|
-
frame: void 0,
|
|
40
|
-
index: 0,
|
|
41
|
-
paused: false,
|
|
42
|
-
total: 0
|
|
43
|
-
};
|
|
44
|
-
if (start) this.start();
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Continue running the timer _(if it's paused)_
|
|
48
|
-
*/
|
|
49
|
-
continue() {
|
|
50
|
-
return this.#work(WORK_CONTINUE);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Destroy the timer
|
|
54
|
-
*/
|
|
55
|
-
destroy() {
|
|
56
|
-
this.state.destroyed = true;
|
|
57
|
-
this.options.onAfter = noop;
|
|
58
|
-
this.options.onError = noop;
|
|
59
|
-
this.state.callback = noop;
|
|
60
|
-
if (!globalThis._oscarpalmer_timer_debug) this.state.trace = void 0;
|
|
61
|
-
stop({
|
|
62
|
-
instance: this,
|
|
63
|
-
type: this.$timer
|
|
64
|
-
}, this.state, this.options);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Pause the timer _(if it's running)_
|
|
68
|
-
*/
|
|
69
|
-
pause() {
|
|
70
|
-
return this.#work(WORK_PAUSE);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Restart the timer _(or start it, if it's not running)_
|
|
74
|
-
*/
|
|
75
|
-
restart() {
|
|
76
|
-
return this.#work(WORK_RESTART);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Start the timer _(if it's not running)_
|
|
80
|
-
*/
|
|
81
|
-
start() {
|
|
82
|
-
return this.#work(WORK_START);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Stop the timer _(if it's running)_
|
|
86
|
-
*/
|
|
87
|
-
stop() {
|
|
88
|
-
return this.#work(WORK_STOP);
|
|
89
|
-
}
|
|
90
|
-
#work(type) {
|
|
5
|
+
function createTimer(name, pick, options, start) {
|
|
6
|
+
function worker(type) {
|
|
91
7
|
return work(type, {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
},
|
|
8
|
+
name,
|
|
9
|
+
instance
|
|
10
|
+
}, state, options);
|
|
95
11
|
}
|
|
96
|
-
|
|
12
|
+
const state = {
|
|
13
|
+
...pick,
|
|
14
|
+
active: false,
|
|
15
|
+
destroyed: false,
|
|
16
|
+
elapsed: 0,
|
|
17
|
+
frame: void 0,
|
|
18
|
+
index: 0,
|
|
19
|
+
paused: false,
|
|
20
|
+
total: 0
|
|
21
|
+
};
|
|
22
|
+
const instance = {
|
|
23
|
+
continue: () => worker(WORK_CONTINUE),
|
|
24
|
+
destroy: () => destroyTimer(name, instance, state, options),
|
|
25
|
+
pause: () => worker(WORK_PAUSE),
|
|
26
|
+
restart: () => worker(WORK_RESTART),
|
|
27
|
+
start: () => worker(WORK_START),
|
|
28
|
+
stop: () => worker(WORK_STOP)
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperties(instance, {
|
|
31
|
+
$timer: {
|
|
32
|
+
enumerable: false,
|
|
33
|
+
value: name
|
|
34
|
+
},
|
|
35
|
+
active: {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: () => state.active
|
|
38
|
+
},
|
|
39
|
+
destroyed: {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: () => state.destroyed
|
|
42
|
+
},
|
|
43
|
+
paused: {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: () => state.paused
|
|
46
|
+
},
|
|
47
|
+
trace: {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: () => globalThis._oscarpalmer_timer_debug ?? false ? state.trace : void 0
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
if (start) instance.start();
|
|
53
|
+
return Object.freeze(instance);
|
|
54
|
+
}
|
|
55
|
+
function destroyTimer(name, instance, state, options) {
|
|
56
|
+
state.destroyed = true;
|
|
57
|
+
options.onAfter = noop;
|
|
58
|
+
options.onError = noop;
|
|
59
|
+
state.callback = noop;
|
|
60
|
+
if (!globalThis._oscarpalmer_timer_debug) state.trace = void 0;
|
|
61
|
+
stop({
|
|
62
|
+
instance,
|
|
63
|
+
name
|
|
64
|
+
}, state, options);
|
|
65
|
+
}
|
|
97
66
|
//#endregion
|
|
98
|
-
export {
|
|
67
|
+
export { createTimer };
|
package/dist/wait.d.mts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { Timer } from "./
|
|
1
|
+
import { Timer } from "./models.mjs";
|
|
2
|
+
import "./global.mjs";
|
|
2
3
|
//#region src/wait.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* Create a waiting timer
|
|
6
|
+
*
|
|
5
7
|
* @param callback Callback to run when the timer has finished
|
|
6
8
|
* @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
|
|
7
9
|
*/
|
|
8
10
|
declare function wait(callback: () => void, time?: number): Timer;
|
|
9
11
|
//#endregion
|
|
10
|
-
export {
|
|
12
|
+
export { wait };
|
package/dist/wait.mjs
CHANGED
|
@@ -2,15 +2,16 @@ import { TYPE_WAIT } from "./constants.mjs";
|
|
|
2
2
|
import { getCallback, getValidNumber } from "./get.mjs";
|
|
3
3
|
import "./global.mjs";
|
|
4
4
|
import { TimerTrace } from "./models.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { createTimer } from "./timer.mjs";
|
|
6
6
|
//#region src/wait.ts
|
|
7
7
|
/**
|
|
8
8
|
* Create a waiting timer
|
|
9
|
+
*
|
|
9
10
|
* @param callback Callback to run when the timer has finished
|
|
10
11
|
* @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
|
|
11
12
|
*/
|
|
12
13
|
function wait(callback, time) {
|
|
13
|
-
return
|
|
14
|
+
return createTimer(TYPE_WAIT, {
|
|
14
15
|
callback: getCallback(callback),
|
|
15
16
|
trace: new TimerTrace().stack
|
|
16
17
|
}, {
|
package/dist/when.d.mts
CHANGED
|
@@ -1,57 +1,6 @@
|
|
|
1
|
-
import { WhenOptions } from "./models.mjs";
|
|
1
|
+
import { When, WhenOptions } from "./models.mjs";
|
|
2
|
+
import "./global.mjs";
|
|
2
3
|
//#region src/when.d.ts
|
|
3
|
-
declare class When {
|
|
4
|
-
private readonly $timer;
|
|
5
|
-
private readonly state;
|
|
6
|
-
/**
|
|
7
|
-
* Is the timer active?
|
|
8
|
-
*/
|
|
9
|
-
get active(): boolean;
|
|
10
|
-
/**
|
|
11
|
-
* Is the timer destroyed?
|
|
12
|
-
*/
|
|
13
|
-
get destroyed(): boolean;
|
|
14
|
-
/**
|
|
15
|
-
* Is the timer paused?
|
|
16
|
-
*/
|
|
17
|
-
get paused(): boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Get the timer's origin _(if debugging is enabled)_
|
|
20
|
-
*/
|
|
21
|
-
get trace(): string | undefined;
|
|
22
|
-
constructor(condition: () => boolean, options?: Partial<WhenOptions>);
|
|
23
|
-
/**
|
|
24
|
-
* Continues the timer _(if it was paused)_
|
|
25
|
-
*/
|
|
26
|
-
continue(): When;
|
|
27
|
-
/**
|
|
28
|
-
* Destroys the timer _(and stops it,if it was running)_
|
|
29
|
-
*/
|
|
30
|
-
destroy(): void;
|
|
31
|
-
/**
|
|
32
|
-
* Pauses the timer _(if it was running)_
|
|
33
|
-
*/
|
|
34
|
-
pause(): When;
|
|
35
|
-
/**
|
|
36
|
-
* Start the timer
|
|
37
|
-
* @param resolve Optional resolve callback
|
|
38
|
-
* @param reject Optional reject callback
|
|
39
|
-
* @returns Promise that resolves when the condition is met
|
|
40
|
-
*/
|
|
41
|
-
start(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
|
|
42
|
-
/**
|
|
43
|
-
* Stops the timer _(if it was running)_
|
|
44
|
-
*/
|
|
45
|
-
stop(): When;
|
|
46
|
-
/**
|
|
47
|
-
* Start the timer
|
|
48
|
-
* @deprecated Use `start()` instead
|
|
49
|
-
* @param resolve Optional resolve callback
|
|
50
|
-
* @param reject Optional reject callback
|
|
51
|
-
* @returns Promise that resolves when the condition is met
|
|
52
|
-
*/
|
|
53
|
-
then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
|
|
54
|
-
}
|
|
55
4
|
/**
|
|
56
5
|
* Create a conditional timer
|
|
57
6
|
* @param condition Condition to check
|
|
@@ -60,4 +9,4 @@ declare class When {
|
|
|
60
9
|
*/
|
|
61
10
|
declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
|
|
62
11
|
//#endregion
|
|
63
|
-
export {
|
|
12
|
+
export { when };
|
package/dist/when.mjs
CHANGED
|
@@ -1,134 +1,47 @@
|
|
|
1
|
-
import { MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN } from "./constants.mjs";
|
|
1
|
+
import { MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN, WORK_CONTINUE } from "./constants.mjs";
|
|
2
2
|
import { getValidNumber, getValidTimeout } from "./get.mjs";
|
|
3
3
|
import "./global.mjs";
|
|
4
4
|
import { TimerTrace } from "./models.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { createTimer } from "./timer.mjs";
|
|
6
6
|
import { noop } from "@oscarpalmer/atoms/function";
|
|
7
7
|
//#region src/when.ts
|
|
8
|
-
|
|
9
|
-
state
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Is the timer paused?
|
|
30
|
-
*/
|
|
31
|
-
get paused() {
|
|
32
|
-
return this.state.timer?.paused ?? false;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Get the timer's origin _(if debugging is enabled)_
|
|
36
|
-
*/
|
|
37
|
-
get trace() {
|
|
38
|
-
return globalThis._oscarpalmer_timer_debug ?? false ? this.state.timer?.trace : void 0;
|
|
39
|
-
}
|
|
40
|
-
constructor(condition, options) {
|
|
41
|
-
Object.defineProperty(this, "$timer", { value: TYPE_WHEN });
|
|
42
|
-
const { state } = this;
|
|
43
|
-
state.promise = new Promise((resolve, reject) => {
|
|
44
|
-
state.resolver = resolve;
|
|
45
|
-
state.rejecter = reject;
|
|
46
|
-
});
|
|
47
|
-
let result = false;
|
|
48
|
-
this.state.timer = new Timer(TYPE_WHEN, {
|
|
49
|
-
callback() {
|
|
50
|
-
try {
|
|
51
|
-
if (condition()) {
|
|
52
|
-
result = true;
|
|
53
|
-
state.timer.stop();
|
|
54
|
-
}
|
|
55
|
-
} catch {
|
|
56
|
-
state.timer.stop();
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
trace: new TimerTrace().stack
|
|
60
|
-
}, {
|
|
61
|
-
onAfter: () => {
|
|
62
|
-
if (result) state.resolver?.();
|
|
63
|
-
else state.rejecter?.();
|
|
64
|
-
this.destroy();
|
|
65
|
-
},
|
|
66
|
-
onError: () => {
|
|
67
|
-
state.rejecter?.();
|
|
68
|
-
this.destroy();
|
|
69
|
-
},
|
|
70
|
-
count: getValidNumber(options?.count),
|
|
71
|
-
interval: getValidNumber(options?.interval),
|
|
72
|
-
timeout: getValidTimeout(options?.timeout)
|
|
73
|
-
}, false);
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Continues the timer _(if it was paused)_
|
|
77
|
-
*/
|
|
78
|
-
continue() {
|
|
79
|
-
this.state.timer?.continue();
|
|
80
|
-
return this;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Destroys the timer _(and stops it,if it was running)_
|
|
84
|
-
*/
|
|
85
|
-
destroy() {
|
|
86
|
-
const { state } = this;
|
|
87
|
-
state.timer?.destroy();
|
|
88
|
-
state.promise = void 0;
|
|
89
|
-
state.resolver = noop;
|
|
90
|
-
state.rejecter = noop;
|
|
91
|
-
state.timer = void 0;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Pauses the timer _(if it was running)_
|
|
95
|
-
*/
|
|
96
|
-
pause() {
|
|
97
|
-
this.state.timer?.pause();
|
|
98
|
-
return this;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Start the timer
|
|
102
|
-
* @param resolve Optional resolve callback
|
|
103
|
-
* @param reject Optional reject callback
|
|
104
|
-
* @returns Promise that resolves when the condition is met
|
|
105
|
-
*/
|
|
106
|
-
start(resolve, reject) {
|
|
107
|
-
const { state } = this;
|
|
108
|
-
if (state.timer == null) throw new Error(MESSAGE_DESTROYED);
|
|
109
|
-
if (state.started) throw new Error(MESSAGE_STARTED);
|
|
110
|
-
state.started = true;
|
|
111
|
-
state.timer.start();
|
|
112
|
-
return state.promise.then(resolve ?? noop, reject ?? noop);
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Stops the timer _(if it was running)_
|
|
116
|
-
*/
|
|
117
|
-
stop() {
|
|
118
|
-
this.state.timer?.stop();
|
|
119
|
-
return this;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Start the timer
|
|
123
|
-
* @deprecated Use `start()` instead
|
|
124
|
-
* @param resolve Optional resolve callback
|
|
125
|
-
* @param reject Optional reject callback
|
|
126
|
-
* @returns Promise that resolves when the condition is met
|
|
127
|
-
*/
|
|
128
|
-
then(resolve, reject) {
|
|
129
|
-
return this.start(resolve, reject);
|
|
8
|
+
function destroyWhen(state) {
|
|
9
|
+
state.timer?.destroy();
|
|
10
|
+
state.promise = void 0;
|
|
11
|
+
state.resolver = noop;
|
|
12
|
+
state.rejecter = noop;
|
|
13
|
+
state.timer = void 0;
|
|
14
|
+
}
|
|
15
|
+
function onAfter(instance, state) {
|
|
16
|
+
if (state.result) state.resolver?.();
|
|
17
|
+
else state.rejecter?.();
|
|
18
|
+
instance.destroy();
|
|
19
|
+
}
|
|
20
|
+
function onCallback(condition, state) {
|
|
21
|
+
try {
|
|
22
|
+
if (condition()) {
|
|
23
|
+
state.result = true;
|
|
24
|
+
state.timer.stop();
|
|
25
|
+
}
|
|
26
|
+
} catch {
|
|
27
|
+
state.timer.stop();
|
|
130
28
|
}
|
|
131
|
-
}
|
|
29
|
+
}
|
|
30
|
+
function onError(instance, state) {
|
|
31
|
+
state.rejecter?.();
|
|
32
|
+
instance.destroy();
|
|
33
|
+
}
|
|
34
|
+
function onWhen(type, instance, state) {
|
|
35
|
+
state.timer?.[type]?.();
|
|
36
|
+
return instance;
|
|
37
|
+
}
|
|
38
|
+
function startWhen(state, resolve) {
|
|
39
|
+
if (state.timer == null) throw new Error(MESSAGE_DESTROYED);
|
|
40
|
+
if (state.started) throw new Error(MESSAGE_STARTED);
|
|
41
|
+
state.started = true;
|
|
42
|
+
state.timer.start();
|
|
43
|
+
return state.promise.then(resolve);
|
|
44
|
+
}
|
|
132
45
|
/**
|
|
133
46
|
* Create a conditional timer
|
|
134
47
|
* @param condition Condition to check
|
|
@@ -136,7 +49,57 @@ var When = class {
|
|
|
136
49
|
* @returns Timer instance
|
|
137
50
|
*/
|
|
138
51
|
function when(condition, options) {
|
|
139
|
-
|
|
52
|
+
const state = {
|
|
53
|
+
promise: void 0,
|
|
54
|
+
result: false,
|
|
55
|
+
started: false,
|
|
56
|
+
timer: void 0
|
|
57
|
+
};
|
|
58
|
+
let instance;
|
|
59
|
+
state.promise = new Promise((resolve, reject) => {
|
|
60
|
+
state.resolver = resolve;
|
|
61
|
+
state.rejecter = reject;
|
|
62
|
+
});
|
|
63
|
+
state.timer = createTimer(TYPE_WHEN, {
|
|
64
|
+
callback: () => onCallback(condition, state),
|
|
65
|
+
trace: new TimerTrace().stack
|
|
66
|
+
}, {
|
|
67
|
+
onAfter: () => onAfter(instance, state),
|
|
68
|
+
onError: () => onError(instance, state),
|
|
69
|
+
count: getValidNumber(options?.count),
|
|
70
|
+
interval: getValidNumber(options?.interval),
|
|
71
|
+
timeout: getValidTimeout(options?.timeout)
|
|
72
|
+
}, false);
|
|
73
|
+
instance = {
|
|
74
|
+
continue: () => onWhen(WORK_CONTINUE, instance, state),
|
|
75
|
+
destroy: () => destroyWhen(state),
|
|
76
|
+
pause: () => onWhen("pause", instance, state),
|
|
77
|
+
start: (resolve) => startWhen(state, resolve),
|
|
78
|
+
stop: () => onWhen("stop", instance, state)
|
|
79
|
+
};
|
|
80
|
+
Object.defineProperties(instance, {
|
|
81
|
+
$timer: {
|
|
82
|
+
enumerable: false,
|
|
83
|
+
value: TYPE_WHEN
|
|
84
|
+
},
|
|
85
|
+
active: {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
get: () => state.timer?.active ?? false
|
|
88
|
+
},
|
|
89
|
+
destroyed: {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
get: () => state.timer == null
|
|
92
|
+
},
|
|
93
|
+
paused: {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
get: () => state.timer?.paused ?? false
|
|
96
|
+
},
|
|
97
|
+
trace: {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
get: () => globalThis._oscarpalmer_timer_debug ?? false ? state.timer?.trace : void 0
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
return Object.freeze(instance);
|
|
140
103
|
}
|
|
141
104
|
//#endregion
|
|
142
105
|
export { when };
|
package/dist/work.d.mts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { Timer } from "./
|
|
2
|
-
import { TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType } from "./models.mjs";
|
|
3
|
-
|
|
1
|
+
import { Timer, TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType } from "./models.mjs";
|
|
4
2
|
//#region src/work.d.ts
|
|
5
3
|
declare function stop(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
|
|
6
4
|
declare function work(type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
|
package/dist/work.mjs
CHANGED
|
@@ -6,7 +6,7 @@ function finish(timer, state, options, success) {
|
|
|
6
6
|
state.active = false;
|
|
7
7
|
state.elapsed = 0;
|
|
8
8
|
state.frame = void 0;
|
|
9
|
-
if (timer.
|
|
9
|
+
if (timer.name === "wait") state.callback();
|
|
10
10
|
else options.onAfter?.(success);
|
|
11
11
|
}
|
|
12
12
|
function ignore(type, state) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oscarpalmer/timer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.44.0",
|
|
4
4
|
"description": "A better solution for timeout- and interval-based timers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"requestAnimationFrame",
|
|
@@ -34,6 +34,10 @@
|
|
|
34
34
|
"types": "./dist/delay.d.mts",
|
|
35
35
|
"default": "./dist/delay.mjs"
|
|
36
36
|
},
|
|
37
|
+
"./models": {
|
|
38
|
+
"types": "./dist/models.d.mts",
|
|
39
|
+
"default": "./dist/models.mjs"
|
|
40
|
+
},
|
|
37
41
|
"./repeat": {
|
|
38
42
|
"types": "./dist/repeat.d.mts",
|
|
39
43
|
"default": "./dist/repeat.mjs"
|
|
@@ -56,14 +60,15 @@
|
|
|
56
60
|
"watch": "npx vite build --watch"
|
|
57
61
|
},
|
|
58
62
|
"dependencies": {
|
|
59
|
-
"@oscarpalmer/atoms": "^0.
|
|
63
|
+
"@oscarpalmer/atoms": "^0.193"
|
|
60
64
|
},
|
|
61
65
|
"devDependencies": {
|
|
62
|
-
"@
|
|
66
|
+
"@oxlint/plugins": "^1.80",
|
|
67
|
+
"@types/node": "^26.3",
|
|
63
68
|
"@vitest/coverage-istanbul": "^4.1",
|
|
64
|
-
"jsdom": "^
|
|
65
|
-
"tsdown": "^0.
|
|
66
|
-
"typescript": "^
|
|
69
|
+
"jsdom": "^30",
|
|
70
|
+
"tsdown": "^0.22",
|
|
71
|
+
"typescript": "^6",
|
|
67
72
|
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
|
|
68
73
|
"vite-plus": "latest",
|
|
69
74
|
"vitest": "npm:@voidzero-dev/vite-plus-test@latest"
|
package/src/constants.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {Timer} from './timer';
|
|
1
|
+
import type {Timer, TimerName, WorkHandlerType} from './models';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Buffer value to use when evaluating if a specific time is within a certain range
|
|
@@ -28,11 +27,11 @@ export const TIMERS_ACTIVE = new Set<Timer>();
|
|
|
28
27
|
*/
|
|
29
28
|
export const TIMERS_HIDDEN = new Set<Timer>();
|
|
30
29
|
|
|
31
|
-
export const TYPE_REPEAT:
|
|
30
|
+
export const TYPE_REPEAT: TimerName = 'repeat';
|
|
32
31
|
|
|
33
|
-
export const TYPE_WAIT:
|
|
32
|
+
export const TYPE_WAIT: TimerName = 'wait';
|
|
34
33
|
|
|
35
|
-
export const TYPE_WHEN:
|
|
34
|
+
export const TYPE_WHEN: TimerName = 'when';
|
|
36
35
|
|
|
37
36
|
export const WORK_CONTINUE: WorkHandlerType = 'continue';
|
|
38
37
|
|
package/src/delay.ts
CHANGED
package/src/get.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {GenericCallback} from '@oscarpalmer/atoms/models';
|
|
2
1
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
|
+
import type {GenericCallback} from '@oscarpalmer/atoms/models';
|
|
3
3
|
import {DEFAULT_TIMEOUT} from './constants';
|
|
4
4
|
|
|
5
5
|
export function getCallback(value: unknown): GenericCallback {
|
package/src/global.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import './global';
|
|
2
2
|
|
|
3
|
-
export
|
|
3
|
+
export {delay} from './delay';
|
|
4
4
|
export {isRepeated, isTimer, isWaited, isWhen} from './is';
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
5
|
+
export type {RepeatOptions, Timer, TimerOptions, When, WhenOptions} from './models';
|
|
6
|
+
export {repeat} from './repeat';
|
|
7
|
+
export {wait} from './wait';
|
|
8
|
+
export {when} from './when';
|
package/src/is.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type {PlainObject} from '@oscarpalmer/atoms/models';
|
|
2
2
|
import {TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN} from './constants';
|
|
3
|
-
import type {Timer} from './
|
|
4
|
-
import type {When} from './when';
|
|
3
|
+
import type {Timer, When} from './models';
|
|
5
4
|
|
|
6
5
|
function is(names: string[], value: unknown) {
|
|
7
6
|
return names.includes((value as PlainObject)?.$timer as string);
|
|
@@ -40,5 +39,5 @@ export function isWaited(value: unknown): value is Timer {
|
|
|
40
39
|
* @returns `true` if the value is a conditional timer
|
|
41
40
|
*/
|
|
42
41
|
export function isWhen(value: unknown): value is When {
|
|
43
|
-
return is([TYPE_WHEN], value) && typeof (value as When).
|
|
42
|
+
return is([TYPE_WHEN], value) && typeof (value as When).start === 'function';
|
|
44
43
|
}
|