@oscarpalmer/timer 0.41.4 → 0.43.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 +35 -0
- package/dist/{constants.js → constants.mjs} +2 -0
- package/dist/delay.d.mts +3 -0
- package/dist/delay.mjs +2 -0
- package/dist/get.d.mts +8 -0
- package/dist/{get.js → get.mjs} +3 -1
- package/dist/global.d.mts +7 -0
- package/dist/{global.js → global.mjs} +4 -1
- package/dist/index.d.mts +249 -0
- package/dist/{timer.full.js → index.mjs} +84 -55
- package/{types/is.d.ts → dist/is.d.mts} +10 -6
- package/dist/{is.js → is.mjs} +4 -2
- package/dist/models.d.mts +82 -0
- package/dist/{models.js → models.mjs} +2 -0
- package/dist/repeat.d.mts +13 -0
- package/dist/{repeat.js → repeat.mjs} +8 -5
- package/dist/timer.d.mts +52 -0
- package/dist/{timer.js → timer.mjs} +4 -2
- package/{types/wait.d.ts → dist/wait.d.mts} +6 -4
- package/dist/{wait.js → wait.mjs} +8 -5
- package/dist/when.d.mts +55 -0
- package/dist/{when.js → when.mjs} +10 -18
- package/dist/work.d.mts +8 -0
- package/dist/{work.js → work.mjs} +3 -1
- package/package.json +50 -52
- package/src/delay.ts +2 -1
- package/src/get.ts +1 -1
- package/src/is.ts +2 -2
- package/src/repeat.ts +1 -0
- package/src/wait.ts +1 -0
- package/src/when.ts +3 -15
- package/dist/delay.js +0 -2
- package/dist/index.js +0 -7
- package/types/constants.d.ts +0 -31
- package/types/delay.d.ts +0 -1
- package/types/get.d.ts +0 -4
- package/types/global.d.ts +0 -5
- package/types/index.d.ts +0 -235
- package/types/models.d.ts +0 -78
- package/types/repeat.d.ts +0 -11
- package/types/timer.d.ts +0 -48
- package/types/when.d.ts +0 -62
- package/types/work.d.ts +0 -4
package/README.md
CHANGED
|
@@ -64,7 +64,7 @@ function callback(index) {
|
|
|
64
64
|
// 'index' is the current step
|
|
65
65
|
// starts at 0, goes up to a maximum of count - 1
|
|
66
66
|
// for this example: 0 → 9
|
|
67
|
-
}
|
|
67
|
+
}
|
|
68
68
|
```
|
|
69
69
|
|
|
70
70
|
When you create a repeated timer, you can also provide a callback to run when the timer stops, as below:
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Timer } from "./timer.mjs";
|
|
2
|
+
import { TimerType, WorkHandlerType } from "./models.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/constants.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Buffer value to use when evaluating if a specific time is within a certain range
|
|
7
|
+
*/
|
|
8
|
+
declare const BUFFER_INTERVAL = 5;
|
|
9
|
+
declare const DEFAULT_TIMEOUT = 30000;
|
|
10
|
+
/**
|
|
11
|
+
* Message to show when a when-timer is destroyed
|
|
12
|
+
*/
|
|
13
|
+
declare const MESSAGE_DESTROYED = "Timer has already been destroyed";
|
|
14
|
+
/**
|
|
15
|
+
* Message to show when a when-timer is started
|
|
16
|
+
*/
|
|
17
|
+
declare const MESSAGE_STARTED = "Timer has already been started";
|
|
18
|
+
/**
|
|
19
|
+
* A set of all active timers
|
|
20
|
+
*/
|
|
21
|
+
declare const TIMERS_ACTIVE: Set<Timer>;
|
|
22
|
+
/**
|
|
23
|
+
* A set of timers that were paused due to the document being hidden
|
|
24
|
+
*/
|
|
25
|
+
declare const TIMERS_HIDDEN: Set<Timer>;
|
|
26
|
+
declare const TYPE_REPEAT: TimerType;
|
|
27
|
+
declare const TYPE_WAIT: TimerType;
|
|
28
|
+
declare const TYPE_WHEN: TimerType;
|
|
29
|
+
declare const WORK_CONTINUE: WorkHandlerType;
|
|
30
|
+
declare const WORK_PAUSE: WorkHandlerType;
|
|
31
|
+
declare const WORK_RESTART: WorkHandlerType;
|
|
32
|
+
declare const WORK_START: WorkHandlerType;
|
|
33
|
+
declare const WORK_STOP: WorkHandlerType;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { BUFFER_INTERVAL, DEFAULT_TIMEOUT, MESSAGE_DESTROYED, MESSAGE_STARTED, TIMERS_ACTIVE, TIMERS_HIDDEN, TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/constants.ts
|
|
1
2
|
/**
|
|
2
3
|
* Buffer value to use when evaluating if a specific time is within a certain range
|
|
3
4
|
*/
|
|
@@ -27,4 +28,5 @@ const WORK_PAUSE = "pause";
|
|
|
27
28
|
const WORK_RESTART = "restart";
|
|
28
29
|
const WORK_START = "start";
|
|
29
30
|
const WORK_STOP = "stop";
|
|
31
|
+
//#endregion
|
|
30
32
|
export { BUFFER_INTERVAL, DEFAULT_TIMEOUT, MESSAGE_DESTROYED, MESSAGE_STARTED, TIMERS_ACTIVE, TIMERS_HIDDEN, TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP };
|
package/dist/delay.d.mts
ADDED
package/dist/delay.mjs
ADDED
package/dist/get.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GenericCallback } from "@oscarpalmer/atoms/models";
|
|
2
|
+
|
|
3
|
+
//#region src/get.d.ts
|
|
4
|
+
declare function getCallback(value: unknown): GenericCallback;
|
|
5
|
+
declare function getValidTimeout(value: unknown): number;
|
|
6
|
+
declare function getValidNumber(value: unknown, defaultValue?: number): number;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { getCallback, getValidNumber, getValidTimeout };
|
package/dist/{get.js → get.mjs}
RENAMED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { DEFAULT_TIMEOUT } from "./constants.
|
|
1
|
+
import { DEFAULT_TIMEOUT } from "./constants.mjs";
|
|
2
2
|
import { noop } from "@oscarpalmer/atoms/function";
|
|
3
|
+
//#region src/get.ts
|
|
3
4
|
function getCallback(value) {
|
|
4
5
|
return typeof value === "function" ? value : noop;
|
|
5
6
|
}
|
|
@@ -10,4 +11,5 @@ function getValidNumber(value, defaultValue) {
|
|
|
10
11
|
const actualDefault = defaultValue ?? 0;
|
|
11
12
|
return typeof value === "number" && value > actualDefault ? value : actualDefault;
|
|
12
13
|
}
|
|
14
|
+
//#endregion
|
|
13
15
|
export { getCallback, getValidNumber, getValidTimeout };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { TIMERS_ACTIVE, TIMERS_HIDDEN, WORK_CONTINUE, WORK_PAUSE } from "./constants.
|
|
1
|
+
import { TIMERS_ACTIVE, TIMERS_HIDDEN, WORK_CONTINUE, WORK_PAUSE } from "./constants.mjs";
|
|
2
|
+
//#region src/global.ts
|
|
2
3
|
if (globalThis._oscarpalmer_timers == null) Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
|
|
3
4
|
return globalThis._oscarpalmer_timer_debug ? [...TIMERS_ACTIVE] : [];
|
|
4
5
|
} });
|
|
@@ -13,3 +14,5 @@ document.addEventListener("visibilitychange", () => {
|
|
|
13
14
|
}
|
|
14
15
|
from.clear();
|
|
15
16
|
});
|
|
17
|
+
//#endregion
|
|
18
|
+
export {};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
//#region src/models.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Options for a repeating timer
|
|
4
|
+
*/
|
|
5
|
+
type RepeatOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Callback to be called when the timer has stopped, either manually or by completing its work
|
|
8
|
+
*/
|
|
9
|
+
onAfter: (finished: boolean) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Callback to be called after the timer has timed out
|
|
12
|
+
*/
|
|
13
|
+
onTimeout: () => void;
|
|
14
|
+
/**
|
|
15
|
+
* How many times the timer should repeat
|
|
16
|
+
*/
|
|
17
|
+
count: number;
|
|
18
|
+
/**
|
|
19
|
+
* The interval between each repeat
|
|
20
|
+
*/
|
|
21
|
+
interval: number;
|
|
22
|
+
/**
|
|
23
|
+
* The timeout for the timer _(any value above `0` will enable the timeout)_
|
|
24
|
+
*/
|
|
25
|
+
timeout: number;
|
|
26
|
+
};
|
|
27
|
+
type TimerOptions = {
|
|
28
|
+
onAfter: ((finished: boolean) => void) | undefined;
|
|
29
|
+
onError: (() => void) | undefined;
|
|
30
|
+
count: number;
|
|
31
|
+
interval: number;
|
|
32
|
+
timeout: number;
|
|
33
|
+
};
|
|
34
|
+
type TimerState = {
|
|
35
|
+
active: boolean;
|
|
36
|
+
callback: () => void;
|
|
37
|
+
destroyed: boolean;
|
|
38
|
+
elapsed: number;
|
|
39
|
+
frame: number | undefined;
|
|
40
|
+
index: number;
|
|
41
|
+
paused: boolean;
|
|
42
|
+
total: number;
|
|
43
|
+
trace: string | undefined;
|
|
44
|
+
};
|
|
45
|
+
type TimerType = 'repeat' | 'wait' | 'when';
|
|
46
|
+
/**
|
|
47
|
+
* Options for a conditional timer
|
|
48
|
+
*/
|
|
49
|
+
type WhenOptions = {
|
|
50
|
+
/**
|
|
51
|
+
* How many times the timer should check the condition
|
|
52
|
+
*/
|
|
53
|
+
count: number;
|
|
54
|
+
/**
|
|
55
|
+
* Then interval between each condtional check
|
|
56
|
+
*/
|
|
57
|
+
interval: number;
|
|
58
|
+
/**
|
|
59
|
+
* The timeout for the timer _(any value above `0` will enable the timeout)_
|
|
60
|
+
*/
|
|
61
|
+
timeout: number;
|
|
62
|
+
};
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/timer.d.ts
|
|
65
|
+
declare class Timer {
|
|
66
|
+
#private;
|
|
67
|
+
protected readonly options: TimerOptions;
|
|
68
|
+
private readonly $timer;
|
|
69
|
+
protected readonly state: TimerState;
|
|
70
|
+
/**
|
|
71
|
+
* Is the timer active?
|
|
72
|
+
*/
|
|
73
|
+
get active(): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Is the timer destroyed?
|
|
76
|
+
*/
|
|
77
|
+
get destroyed(): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Is the timer paused?
|
|
80
|
+
*/
|
|
81
|
+
get paused(): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
84
|
+
*/
|
|
85
|
+
get trace(): string | undefined;
|
|
86
|
+
constructor(type: TimerType, state: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean);
|
|
87
|
+
/**
|
|
88
|
+
* Continue running the timer _(if it's paused)_
|
|
89
|
+
*/
|
|
90
|
+
continue(): Timer;
|
|
91
|
+
/**
|
|
92
|
+
* Destroy the timer
|
|
93
|
+
*/
|
|
94
|
+
destroy(): void;
|
|
95
|
+
/**
|
|
96
|
+
* Pause the timer _(if it's running)_
|
|
97
|
+
*/
|
|
98
|
+
pause(): Timer;
|
|
99
|
+
/**
|
|
100
|
+
* Restart the timer _(or start it, if it's not running)_
|
|
101
|
+
*/
|
|
102
|
+
restart(): Timer;
|
|
103
|
+
/**
|
|
104
|
+
* Start the timer _(if it's not running)_
|
|
105
|
+
*/
|
|
106
|
+
start(): Timer;
|
|
107
|
+
/**
|
|
108
|
+
* Stop the timer _(if it's running)_
|
|
109
|
+
*/
|
|
110
|
+
stop(): Timer;
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/global.d.ts
|
|
114
|
+
declare global {
|
|
115
|
+
var _oscarpalmer_timer_debug: boolean | undefined;
|
|
116
|
+
var _oscarpalmer_timers: Timer[] | undefined;
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/models.d.mts
|
|
120
|
+
/**
|
|
121
|
+
* Options for a _Promise_-handling function
|
|
122
|
+
*/
|
|
123
|
+
type PromiseOptions = {
|
|
124
|
+
/**
|
|
125
|
+
* AbortSignal for aborting the _Promise_; when aborted, the _Promise_ will reject with the reason of the signal
|
|
126
|
+
*/
|
|
127
|
+
signal?: AbortSignal;
|
|
128
|
+
/**
|
|
129
|
+
* How long to wait for _(in milliseconds; defaults to `0`)_
|
|
130
|
+
*/
|
|
131
|
+
time?: number;
|
|
132
|
+
};
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/delay.d.mts
|
|
135
|
+
//#region src/promise/delay.d.ts
|
|
136
|
+
/**
|
|
137
|
+
* Create a delayed promise that resolves after a certain amount of time, or rejects if aborted
|
|
138
|
+
*
|
|
139
|
+
* @param options Options for the delay
|
|
140
|
+
* @returns Delayed promise
|
|
141
|
+
*/
|
|
142
|
+
declare function delay(options?: PromiseOptions): Promise<void>;
|
|
143
|
+
/**
|
|
144
|
+
* Create a delayed promise that resolves after a certain amount of time
|
|
145
|
+
*
|
|
146
|
+
* @param time How long to wait for _(in milliseconds; defaults to `0`)_
|
|
147
|
+
* @returns Delayed promise
|
|
148
|
+
*/
|
|
149
|
+
declare function delay(time?: number): Promise<void>; //#endregion
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/when.d.ts
|
|
152
|
+
declare class When {
|
|
153
|
+
private readonly $timer;
|
|
154
|
+
private readonly state;
|
|
155
|
+
/**
|
|
156
|
+
* Is the timer active?
|
|
157
|
+
*/
|
|
158
|
+
get active(): boolean;
|
|
159
|
+
/**
|
|
160
|
+
* Is the timer destroyed?
|
|
161
|
+
*/
|
|
162
|
+
get destroyed(): boolean;
|
|
163
|
+
/**
|
|
164
|
+
* Is the timer paused?
|
|
165
|
+
*/
|
|
166
|
+
get paused(): boolean;
|
|
167
|
+
/**
|
|
168
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
169
|
+
*/
|
|
170
|
+
get trace(): string | undefined;
|
|
171
|
+
constructor(condition: () => boolean, options?: Partial<WhenOptions>);
|
|
172
|
+
/**
|
|
173
|
+
* Continues the timer _(if it was paused)_
|
|
174
|
+
*/
|
|
175
|
+
continue(): When;
|
|
176
|
+
/**
|
|
177
|
+
* Destroys the timer _(and stops it,if it was running)_
|
|
178
|
+
*/
|
|
179
|
+
destroy(): void;
|
|
180
|
+
/**
|
|
181
|
+
* Pauses the timer _(if it was running)_
|
|
182
|
+
*/
|
|
183
|
+
pause(): When;
|
|
184
|
+
/**
|
|
185
|
+
* Start the timer
|
|
186
|
+
*
|
|
187
|
+
* @param resolve Optional resolve callback
|
|
188
|
+
* @returns Promise that resolves when the condition is met
|
|
189
|
+
*/
|
|
190
|
+
start(resolve?: (() => void) | null): Promise<void>;
|
|
191
|
+
/**
|
|
192
|
+
* Stops the timer _(if it was running)_
|
|
193
|
+
*/
|
|
194
|
+
stop(): When;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Create a conditional timer
|
|
198
|
+
* @param condition Condition to check
|
|
199
|
+
* @param options Timer options
|
|
200
|
+
* @returns Timer instance
|
|
201
|
+
*/
|
|
202
|
+
declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
|
|
203
|
+
//#endregion
|
|
204
|
+
//#region src/is.d.ts
|
|
205
|
+
/**
|
|
206
|
+
* Is the value a repeating timer?
|
|
207
|
+
* @param value Value to check
|
|
208
|
+
* @returns `true` if the value is a repeating timer
|
|
209
|
+
*/
|
|
210
|
+
declare function isRepeated(value: unknown): value is Timer;
|
|
211
|
+
/**
|
|
212
|
+
* Is the value a timer?
|
|
213
|
+
* @param value Value to check
|
|
214
|
+
* @returns `true` if the value is a timer
|
|
215
|
+
*/
|
|
216
|
+
declare function isTimer(value: unknown): value is Timer;
|
|
217
|
+
/**
|
|
218
|
+
* Is the value a waiting timer?
|
|
219
|
+
* @param value Value to check
|
|
220
|
+
* @returns `true` if the value is a waiting timer
|
|
221
|
+
*/
|
|
222
|
+
declare function isWaited(value: unknown): value is Timer;
|
|
223
|
+
/**
|
|
224
|
+
* Is the value a conditional timer?
|
|
225
|
+
* @param value Value to check
|
|
226
|
+
* @returns `true` if the value is a conditional timer
|
|
227
|
+
*/
|
|
228
|
+
declare function isWhen(value: unknown): value is When;
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/repeat.d.ts
|
|
231
|
+
/**
|
|
232
|
+
* Create a repeating timer
|
|
233
|
+
*
|
|
234
|
+
* @param callback Callback to run on each interval
|
|
235
|
+
* @param options Timer options
|
|
236
|
+
* @returns Timer instance
|
|
237
|
+
*/
|
|
238
|
+
declare function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer;
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/wait.d.ts
|
|
241
|
+
/**
|
|
242
|
+
* Create a waiting timer
|
|
243
|
+
*
|
|
244
|
+
* @param callback Callback to run when the timer has finished
|
|
245
|
+
* @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
|
|
246
|
+
*/
|
|
247
|
+
declare function wait(callback: () => void, time?: number): Timer;
|
|
248
|
+
//#endregion
|
|
249
|
+
export { type PromiseOptions, type RepeatOptions, type Timer, type When, delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Buffer value to use when evaluating if a specific time is within a certain range
|
|
3
|
-
*/
|
|
4
|
-
const BUFFER_INTERVAL = 5;
|
|
1
|
+
//#region src/constants.ts
|
|
5
2
|
const DEFAULT_TIMEOUT = 3e4;
|
|
6
3
|
/**
|
|
7
4
|
* Message to show when a when-timer is destroyed
|
|
@@ -27,6 +24,8 @@ const WORK_PAUSE = "pause";
|
|
|
27
24
|
const WORK_RESTART = "restart";
|
|
28
25
|
const WORK_START = "start";
|
|
29
26
|
const WORK_STOP = "stop";
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/global.ts
|
|
30
29
|
if (globalThis._oscarpalmer_timers == null) Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
|
|
31
30
|
return globalThis._oscarpalmer_timer_debug ? [...TIMERS_ACTIVE] : [];
|
|
32
31
|
} });
|
|
@@ -41,56 +40,74 @@ document.addEventListener("visibilitychange", () => {
|
|
|
41
40
|
}
|
|
42
41
|
from.clear();
|
|
43
42
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
function getNumberOrDefault(value) {
|
|
47
|
-
return typeof value === "number" && value > 0 ? value : 0;
|
|
48
|
-
}
|
|
49
|
-
function getPromiseOptions(input) {
|
|
50
|
-
if (typeof input === "number") return { time: getNumberOrDefault(input) };
|
|
51
|
-
if (input instanceof AbortSignal) return {
|
|
52
|
-
signal: input,
|
|
53
|
-
time: 0
|
|
54
|
-
};
|
|
55
|
-
const options = typeof input === "object" && input !== null ? input : {};
|
|
56
|
-
return {
|
|
57
|
-
signal: options.signal instanceof AbortSignal ? options.signal : void 0,
|
|
58
|
-
time: getNumberOrDefault(options.time)
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
function settlePromise(aborter, settler, value, signal) {
|
|
62
|
-
signal?.removeEventListener(PROMISE_EVENT_NAME, aborter);
|
|
63
|
-
settler(value);
|
|
64
|
-
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region node_modules/@oscarpalmer/atoms/dist/internal/function/timer.mjs
|
|
65
45
|
function getInterval(value) {
|
|
66
46
|
return typeof value === "number" && value > 0 ? value : 0;
|
|
67
47
|
}
|
|
68
48
|
function getTimer(type, callback, time) {
|
|
69
|
-
|
|
70
|
-
|
|
49
|
+
function run() {
|
|
50
|
+
const now = performance.now();
|
|
71
51
|
start ??= now;
|
|
72
52
|
if (interval === 0 || now - start >= interval - OFFSET) {
|
|
73
|
-
|
|
53
|
+
start = throttle ? now : void 0;
|
|
74
54
|
callback(...args);
|
|
75
|
-
} else
|
|
55
|
+
} else id = startTimer(run);
|
|
76
56
|
}
|
|
57
|
+
const interval = getInterval(time);
|
|
77
58
|
const throttle = type === TIMER_THROTTLE;
|
|
78
59
|
let args;
|
|
79
|
-
let
|
|
60
|
+
let id;
|
|
80
61
|
let start;
|
|
81
62
|
const timer = (...parameters) => {
|
|
82
63
|
timer.cancel();
|
|
83
64
|
args = parameters;
|
|
84
|
-
|
|
65
|
+
if (throttle) run();
|
|
66
|
+
else id = startTimer(run);
|
|
85
67
|
};
|
|
86
68
|
timer.cancel = () => {
|
|
87
|
-
|
|
69
|
+
clearTimer(id);
|
|
88
70
|
};
|
|
89
71
|
return timer;
|
|
90
72
|
}
|
|
91
|
-
|
|
73
|
+
const OFFSET = 5;
|
|
92
74
|
const TIMER_THROTTLE = "throttle";
|
|
93
75
|
const TIMER_WAIT = "wait";
|
|
76
|
+
// istanbul ignore next
|
|
77
|
+
const clearTimer = typeof cancelAnimationFrame === "function" ? cancelAnimationFrame : clearTimeout;
|
|
78
|
+
// istanbul ignore next
|
|
79
|
+
const startTimer = typeof requestAnimationFrame === "function" ? requestAnimationFrame : setTimeout;
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/models.mjs
|
|
82
|
+
const PROMISE_ABORT_EVENT = "abort";
|
|
83
|
+
const PROMISE_ABORT_OPTIONS = { once: true };
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region node_modules/@oscarpalmer/atoms/dist/internal/number.mjs
|
|
86
|
+
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
87
|
+
return typeof value === "number" && !Number.isNaN(value) && value >= (minimum ?? 0) ? Math.floor(value) : defaultValue;
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/helpers.mjs
|
|
91
|
+
function getPromiseOptions(input) {
|
|
92
|
+
if (typeof input === "number") return { time: getNumberOrDefault(input, 0) };
|
|
93
|
+
if (input instanceof AbortSignal) return {
|
|
94
|
+
signal: input,
|
|
95
|
+
time: 0
|
|
96
|
+
};
|
|
97
|
+
const options = typeof input === "object" && input !== null ? input : {};
|
|
98
|
+
return {
|
|
99
|
+
signal: options.signal instanceof AbortSignal ? options.signal : void 0,
|
|
100
|
+
time: getNumberOrDefault(options.time, 0)
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/misc.mjs
|
|
105
|
+
function settlePromise(aborter, settler, value, signal) {
|
|
106
|
+
signal?.removeEventListener(PROMISE_ABORT_EVENT, aborter);
|
|
107
|
+
settler(value);
|
|
108
|
+
}
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/delay.mjs
|
|
94
111
|
function delay(options) {
|
|
95
112
|
const { signal, time } = getPromiseOptions(options);
|
|
96
113
|
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
@@ -101,7 +118,7 @@ function delay(options) {
|
|
|
101
118
|
const timer = getTimer(TIMER_WAIT, () => {
|
|
102
119
|
settlePromise(abort, resolver, void 0, signal);
|
|
103
120
|
}, time);
|
|
104
|
-
signal?.addEventListener(
|
|
121
|
+
signal?.addEventListener(PROMISE_ABORT_EVENT, abort, PROMISE_ABORT_OPTIONS);
|
|
105
122
|
let rejector;
|
|
106
123
|
let resolver;
|
|
107
124
|
return new Promise((resolve, reject) => {
|
|
@@ -111,6 +128,8 @@ function delay(options) {
|
|
|
111
128
|
else timer();
|
|
112
129
|
});
|
|
113
130
|
}
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/is.ts
|
|
114
133
|
function is(names, value) {
|
|
115
134
|
return names.includes(value?.$timer);
|
|
116
135
|
}
|
|
@@ -144,12 +163,16 @@ function isWaited(value) {
|
|
|
144
163
|
* @returns `true` if the value is a conditional timer
|
|
145
164
|
*/
|
|
146
165
|
function isWhen(value) {
|
|
147
|
-
return is([
|
|
166
|
+
return is(["when"], value) && typeof value.start === "function";
|
|
148
167
|
}
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region node_modules/@oscarpalmer/atoms/dist/internal/function/misc.mjs
|
|
149
170
|
/**
|
|
150
171
|
* A function that does nothing, which can be useful, I guess…
|
|
151
172
|
*/
|
|
152
173
|
function noop() {}
|
|
174
|
+
//#endregion
|
|
175
|
+
//#region src/get.ts
|
|
153
176
|
function getCallback(value) {
|
|
154
177
|
return typeof value === "function" ? value : noop;
|
|
155
178
|
}
|
|
@@ -160,25 +183,29 @@ function getValidNumber(value, defaultValue) {
|
|
|
160
183
|
const actualDefault = defaultValue ?? 0;
|
|
161
184
|
return typeof value === "number" && value > actualDefault ? value : actualDefault;
|
|
162
185
|
}
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region src/models.ts
|
|
163
188
|
var TimerTrace = class extends Error {
|
|
164
189
|
constructor() {
|
|
165
190
|
super();
|
|
166
191
|
this.name = "TimerTrace";
|
|
167
192
|
}
|
|
168
193
|
};
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/work.ts
|
|
169
196
|
function finish(timer, state, options, success) {
|
|
170
197
|
cancelAnimationFrame(state.frame);
|
|
171
198
|
TIMERS_ACTIVE.delete(timer.instance);
|
|
172
199
|
state.active = false;
|
|
173
200
|
state.elapsed = 0;
|
|
174
201
|
state.frame = void 0;
|
|
175
|
-
if (timer.type ===
|
|
202
|
+
if (timer.type === "wait") state.callback();
|
|
176
203
|
else options.onAfter?.(success);
|
|
177
204
|
}
|
|
178
205
|
function ignore(type, state) {
|
|
179
206
|
if (state.destroyed) return type !== WORK_STOP;
|
|
180
|
-
if (state.paused) return type ===
|
|
181
|
-
return state.active && type ===
|
|
207
|
+
if (state.paused) return type === "pause" || type === "start";
|
|
208
|
+
return state.active && type === "start";
|
|
182
209
|
}
|
|
183
210
|
function pause(timer, state) {
|
|
184
211
|
cancelAnimationFrame(state.frame);
|
|
@@ -201,7 +228,7 @@ function run(timer, state, options) {
|
|
|
201
228
|
finish(timer, state, options, false);
|
|
202
229
|
return;
|
|
203
230
|
}
|
|
204
|
-
if (options.interval === 0 || state.elapsed >= options.interval -
|
|
231
|
+
if (options.interval === 0 || state.elapsed >= options.interval - 5) {
|
|
205
232
|
if (options.count > -1) state.callback(state.index);
|
|
206
233
|
start = now;
|
|
207
234
|
state.elapsed = 0;
|
|
@@ -215,7 +242,7 @@ function run(timer, state, options) {
|
|
|
215
242
|
};
|
|
216
243
|
}
|
|
217
244
|
function setState(type, state) {
|
|
218
|
-
const pausable = type ===
|
|
245
|
+
const pausable = type === "continue" || type === "pause";
|
|
219
246
|
state.elapsed = pausable ? state.elapsed : 0;
|
|
220
247
|
state.index = pausable ? state.index : 0;
|
|
221
248
|
state.total = pausable ? state.total : 0;
|
|
@@ -232,8 +259,8 @@ function stop(timer, state, options) {
|
|
|
232
259
|
function work(type, timer, state, options) {
|
|
233
260
|
if (ignore(type, state)) return timer.instance;
|
|
234
261
|
setState(type, state);
|
|
235
|
-
if (type ===
|
|
236
|
-
if (type ===
|
|
262
|
+
if (type === "stop") return stop(timer, state, options);
|
|
263
|
+
if (type === "pause" || type === "restart") {
|
|
237
264
|
cancelAnimationFrame(state.frame);
|
|
238
265
|
state.frame = void 0;
|
|
239
266
|
}
|
|
@@ -245,7 +272,10 @@ function work(type, timer, state, options) {
|
|
|
245
272
|
state.frame = requestAnimationFrame(runner);
|
|
246
273
|
return timer.instance;
|
|
247
274
|
}
|
|
275
|
+
//#endregion
|
|
276
|
+
//#region src/timer.ts
|
|
248
277
|
var Timer = class {
|
|
278
|
+
options;
|
|
249
279
|
state;
|
|
250
280
|
/**
|
|
251
281
|
* Is the timer active?
|
|
@@ -337,8 +367,11 @@ var Timer = class {
|
|
|
337
367
|
}, this.state, this.options);
|
|
338
368
|
}
|
|
339
369
|
};
|
|
370
|
+
//#endregion
|
|
371
|
+
//#region src/repeat.ts
|
|
340
372
|
/**
|
|
341
373
|
* Create a repeating timer
|
|
374
|
+
*
|
|
342
375
|
* @param callback Callback to run on each interval
|
|
343
376
|
* @param options Timer options
|
|
344
377
|
* @returns Timer instance
|
|
@@ -355,8 +388,11 @@ function repeat(callback, options) {
|
|
|
355
388
|
timeout: getValidNumber(options?.timeout)
|
|
356
389
|
}, true);
|
|
357
390
|
}
|
|
391
|
+
//#endregion
|
|
392
|
+
//#region src/wait.ts
|
|
358
393
|
/**
|
|
359
394
|
* Create a waiting timer
|
|
395
|
+
*
|
|
360
396
|
* @param callback Callback to run when the timer has finished
|
|
361
397
|
* @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
|
|
362
398
|
*/
|
|
@@ -372,6 +408,8 @@ function wait(callback, time) {
|
|
|
372
408
|
timeout: 0
|
|
373
409
|
}, true);
|
|
374
410
|
}
|
|
411
|
+
//#endregion
|
|
412
|
+
//#region src/when.ts
|
|
375
413
|
var When = class {
|
|
376
414
|
state = {
|
|
377
415
|
promise: void 0,
|
|
@@ -466,17 +504,17 @@ var When = class {
|
|
|
466
504
|
}
|
|
467
505
|
/**
|
|
468
506
|
* Start the timer
|
|
507
|
+
*
|
|
469
508
|
* @param resolve Optional resolve callback
|
|
470
|
-
* @param reject Optional reject callback
|
|
471
509
|
* @returns Promise that resolves when the condition is met
|
|
472
510
|
*/
|
|
473
|
-
start(resolve
|
|
511
|
+
start(resolve) {
|
|
474
512
|
const { state } = this;
|
|
475
513
|
if (state.timer == null) throw new Error(MESSAGE_DESTROYED);
|
|
476
514
|
if (state.started) throw new Error(MESSAGE_STARTED);
|
|
477
515
|
state.started = true;
|
|
478
516
|
state.timer.start();
|
|
479
|
-
return state.promise.then(resolve
|
|
517
|
+
return state.promise.then(resolve);
|
|
480
518
|
}
|
|
481
519
|
/**
|
|
482
520
|
* Stops the timer _(if it was running)_
|
|
@@ -485,16 +523,6 @@ var When = class {
|
|
|
485
523
|
this.state.timer?.stop();
|
|
486
524
|
return this;
|
|
487
525
|
}
|
|
488
|
-
/**
|
|
489
|
-
* Start the timer
|
|
490
|
-
* @deprecated Use `start()` instead
|
|
491
|
-
* @param resolve Optional resolve callback
|
|
492
|
-
* @param reject Optional reject callback
|
|
493
|
-
* @returns Promise that resolves when the condition is met
|
|
494
|
-
*/
|
|
495
|
-
then(resolve, reject) {
|
|
496
|
-
return this.start(resolve, reject);
|
|
497
|
-
}
|
|
498
526
|
};
|
|
499
527
|
/**
|
|
500
528
|
* Create a conditional timer
|
|
@@ -505,4 +533,5 @@ var When = class {
|
|
|
505
533
|
function when(condition, options) {
|
|
506
534
|
return new When(condition, options);
|
|
507
535
|
}
|
|
536
|
+
//#endregion
|
|
508
537
|
export { delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
|