@oscarpalmer/timer 0.41.4 → 0.42.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/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 +250 -0
- package/dist/{timer.full.js → index.mjs} +63 -35
- package/{types/is.d.ts → dist/is.d.mts} +10 -6
- package/dist/{is.js → is.mjs} +3 -1
- package/dist/models.d.mts +82 -0
- package/dist/{models.js → models.mjs} +2 -0
- package/dist/repeat.d.mts +12 -0
- package/dist/{repeat.js → repeat.mjs} +7 -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} +5 -4
- package/dist/{wait.js → wait.mjs} +7 -5
- package/dist/when.d.mts +63 -0
- package/dist/{when.js → when.mjs} +7 -5
- package/dist/work.d.mts +8 -0
- package/dist/{work.js → work.mjs} +3 -1
- package/package.json +49 -52
- package/src/delay.ts +2 -1
- package/src/get.ts +1 -1
- package/src/is.ts +1 -1
- 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
|
@@ -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,250 @@
|
|
|
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
|
+
type PromiseOptions = {
|
|
121
|
+
/**
|
|
122
|
+
* AbortSignal for aborting the promise; when aborted, the promise will reject with the reason of the signal
|
|
123
|
+
*/
|
|
124
|
+
signal?: AbortSignal;
|
|
125
|
+
/**
|
|
126
|
+
* How long to wait for (in milliseconds; defaults to `0`)
|
|
127
|
+
*/
|
|
128
|
+
time?: number;
|
|
129
|
+
};
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/delay.d.mts
|
|
132
|
+
//#region src/promise/delay.d.ts
|
|
133
|
+
/**
|
|
134
|
+
* Create a delayed promise that resolves after a certain amount of time, or rejects if aborted
|
|
135
|
+
* @param options Options for the delay
|
|
136
|
+
* @returns Delayed promise
|
|
137
|
+
*/
|
|
138
|
+
declare function delay(options?: PromiseOptions): Promise<void>;
|
|
139
|
+
/**
|
|
140
|
+
* Create a delayed promise that resolves after a certain amount of time
|
|
141
|
+
* @param time How long to wait for _(in milliseconds; defaults to `0`)_
|
|
142
|
+
* @returns Delayed promise
|
|
143
|
+
*/
|
|
144
|
+
declare function delay(time?: number): Promise<void>; //#endregion
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/when.d.ts
|
|
147
|
+
declare class When {
|
|
148
|
+
private readonly $timer;
|
|
149
|
+
private readonly state;
|
|
150
|
+
/**
|
|
151
|
+
* Is the timer active?
|
|
152
|
+
*/
|
|
153
|
+
get active(): boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Is the timer destroyed?
|
|
156
|
+
*/
|
|
157
|
+
get destroyed(): boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Is the timer paused?
|
|
160
|
+
*/
|
|
161
|
+
get paused(): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
164
|
+
*/
|
|
165
|
+
get trace(): string | undefined;
|
|
166
|
+
constructor(condition: () => boolean, options?: Partial<WhenOptions>);
|
|
167
|
+
/**
|
|
168
|
+
* Continues the timer _(if it was paused)_
|
|
169
|
+
*/
|
|
170
|
+
continue(): When;
|
|
171
|
+
/**
|
|
172
|
+
* Destroys the timer _(and stops it,if it was running)_
|
|
173
|
+
*/
|
|
174
|
+
destroy(): void;
|
|
175
|
+
/**
|
|
176
|
+
* Pauses the timer _(if it was running)_
|
|
177
|
+
*/
|
|
178
|
+
pause(): When;
|
|
179
|
+
/**
|
|
180
|
+
* Start the timer
|
|
181
|
+
* @param resolve Optional resolve callback
|
|
182
|
+
* @param reject Optional reject callback
|
|
183
|
+
* @returns Promise that resolves when the condition is met
|
|
184
|
+
*/
|
|
185
|
+
start(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
|
|
186
|
+
/**
|
|
187
|
+
* Stops the timer _(if it was running)_
|
|
188
|
+
*/
|
|
189
|
+
stop(): When;
|
|
190
|
+
/**
|
|
191
|
+
* Start the timer
|
|
192
|
+
* @deprecated Use `start()` instead
|
|
193
|
+
* @param resolve Optional resolve callback
|
|
194
|
+
* @param reject Optional reject callback
|
|
195
|
+
* @returns Promise that resolves when the condition is met
|
|
196
|
+
*/
|
|
197
|
+
then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Create a conditional timer
|
|
201
|
+
* @param condition Condition to check
|
|
202
|
+
* @param options Timer options
|
|
203
|
+
* @returns Timer instance
|
|
204
|
+
*/
|
|
205
|
+
declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/is.d.ts
|
|
208
|
+
/**
|
|
209
|
+
* Is the value a repeating timer?
|
|
210
|
+
* @param value Value to check
|
|
211
|
+
* @returns `true` if the value is a repeating timer
|
|
212
|
+
*/
|
|
213
|
+
declare function isRepeated(value: unknown): value is Timer;
|
|
214
|
+
/**
|
|
215
|
+
* Is the value a timer?
|
|
216
|
+
* @param value Value to check
|
|
217
|
+
* @returns `true` if the value is a timer
|
|
218
|
+
*/
|
|
219
|
+
declare function isTimer(value: unknown): value is Timer;
|
|
220
|
+
/**
|
|
221
|
+
* Is the value a waiting timer?
|
|
222
|
+
* @param value Value to check
|
|
223
|
+
* @returns `true` if the value is a waiting timer
|
|
224
|
+
*/
|
|
225
|
+
declare function isWaited(value: unknown): value is Timer;
|
|
226
|
+
/**
|
|
227
|
+
* Is the value a conditional timer?
|
|
228
|
+
* @param value Value to check
|
|
229
|
+
* @returns `true` if the value is a conditional timer
|
|
230
|
+
*/
|
|
231
|
+
declare function isWhen(value: unknown): value is When;
|
|
232
|
+
//#endregion
|
|
233
|
+
//#region src/repeat.d.ts
|
|
234
|
+
/**
|
|
235
|
+
* Create a repeating timer
|
|
236
|
+
* @param callback Callback to run on each interval
|
|
237
|
+
* @param options Timer options
|
|
238
|
+
* @returns Timer instance
|
|
239
|
+
*/
|
|
240
|
+
declare function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer;
|
|
241
|
+
//#endregion
|
|
242
|
+
//#region src/wait.d.ts
|
|
243
|
+
/**
|
|
244
|
+
* Create a waiting timer
|
|
245
|
+
* @param callback Callback to run when the timer has finished
|
|
246
|
+
* @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
|
|
247
|
+
*/
|
|
248
|
+
declare function wait(callback: () => void, time?: number): Timer;
|
|
249
|
+
//#endregion
|
|
250
|
+
export { 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,27 +40,8 @@ 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
|
}
|
|
@@ -88,9 +68,38 @@ function getTimer(type, callback, time) {
|
|
|
88
68
|
};
|
|
89
69
|
return timer;
|
|
90
70
|
}
|
|
91
|
-
|
|
71
|
+
const OFFSET = 5;
|
|
92
72
|
const TIMER_THROTTLE = "throttle";
|
|
93
73
|
const TIMER_WAIT = "wait";
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/models.mjs
|
|
76
|
+
const PROMISE_ABORT_EVENT = "abort";
|
|
77
|
+
const PROMISE_ABORT_OPTIONS = { once: true };
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/helpers.mjs
|
|
80
|
+
function getNumberOrDefault(value) {
|
|
81
|
+
return typeof value === "number" && value > 0 ? value : 0;
|
|
82
|
+
}
|
|
83
|
+
function getPromiseOptions(input) {
|
|
84
|
+
if (typeof input === "number") return { time: getNumberOrDefault(input) };
|
|
85
|
+
if (input instanceof AbortSignal) return {
|
|
86
|
+
signal: input,
|
|
87
|
+
time: 0
|
|
88
|
+
};
|
|
89
|
+
const options = typeof input === "object" && input !== null ? input : {};
|
|
90
|
+
return {
|
|
91
|
+
signal: options.signal instanceof AbortSignal ? options.signal : void 0,
|
|
92
|
+
time: getNumberOrDefault(options.time)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/misc.mjs
|
|
97
|
+
function settlePromise(aborter, settler, value, signal) {
|
|
98
|
+
signal?.removeEventListener(PROMISE_ABORT_EVENT, aborter);
|
|
99
|
+
settler(value);
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/delay.mjs
|
|
94
103
|
function delay(options) {
|
|
95
104
|
const { signal, time } = getPromiseOptions(options);
|
|
96
105
|
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
@@ -101,7 +110,7 @@ function delay(options) {
|
|
|
101
110
|
const timer = getTimer(TIMER_WAIT, () => {
|
|
102
111
|
settlePromise(abort, resolver, void 0, signal);
|
|
103
112
|
}, time);
|
|
104
|
-
signal?.addEventListener(
|
|
113
|
+
signal?.addEventListener(PROMISE_ABORT_EVENT, abort, PROMISE_ABORT_OPTIONS);
|
|
105
114
|
let rejector;
|
|
106
115
|
let resolver;
|
|
107
116
|
return new Promise((resolve, reject) => {
|
|
@@ -111,6 +120,8 @@ function delay(options) {
|
|
|
111
120
|
else timer();
|
|
112
121
|
});
|
|
113
122
|
}
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/is.ts
|
|
114
125
|
function is(names, value) {
|
|
115
126
|
return names.includes(value?.$timer);
|
|
116
127
|
}
|
|
@@ -144,12 +155,16 @@ function isWaited(value) {
|
|
|
144
155
|
* @returns `true` if the value is a conditional timer
|
|
145
156
|
*/
|
|
146
157
|
function isWhen(value) {
|
|
147
|
-
return is([
|
|
158
|
+
return is(["when"], value) && typeof value.then === "function";
|
|
148
159
|
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region node_modules/@oscarpalmer/atoms/dist/internal/function/misc.mjs
|
|
149
162
|
/**
|
|
150
163
|
* A function that does nothing, which can be useful, I guess…
|
|
151
164
|
*/
|
|
152
165
|
function noop() {}
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/get.ts
|
|
153
168
|
function getCallback(value) {
|
|
154
169
|
return typeof value === "function" ? value : noop;
|
|
155
170
|
}
|
|
@@ -160,25 +175,29 @@ function getValidNumber(value, defaultValue) {
|
|
|
160
175
|
const actualDefault = defaultValue ?? 0;
|
|
161
176
|
return typeof value === "number" && value > actualDefault ? value : actualDefault;
|
|
162
177
|
}
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/models.ts
|
|
163
180
|
var TimerTrace = class extends Error {
|
|
164
181
|
constructor() {
|
|
165
182
|
super();
|
|
166
183
|
this.name = "TimerTrace";
|
|
167
184
|
}
|
|
168
185
|
};
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region src/work.ts
|
|
169
188
|
function finish(timer, state, options, success) {
|
|
170
189
|
cancelAnimationFrame(state.frame);
|
|
171
190
|
TIMERS_ACTIVE.delete(timer.instance);
|
|
172
191
|
state.active = false;
|
|
173
192
|
state.elapsed = 0;
|
|
174
193
|
state.frame = void 0;
|
|
175
|
-
if (timer.type ===
|
|
194
|
+
if (timer.type === "wait") state.callback();
|
|
176
195
|
else options.onAfter?.(success);
|
|
177
196
|
}
|
|
178
197
|
function ignore(type, state) {
|
|
179
198
|
if (state.destroyed) return type !== WORK_STOP;
|
|
180
|
-
if (state.paused) return type ===
|
|
181
|
-
return state.active && type ===
|
|
199
|
+
if (state.paused) return type === "pause" || type === "start";
|
|
200
|
+
return state.active && type === "start";
|
|
182
201
|
}
|
|
183
202
|
function pause(timer, state) {
|
|
184
203
|
cancelAnimationFrame(state.frame);
|
|
@@ -201,7 +220,7 @@ function run(timer, state, options) {
|
|
|
201
220
|
finish(timer, state, options, false);
|
|
202
221
|
return;
|
|
203
222
|
}
|
|
204
|
-
if (options.interval === 0 || state.elapsed >= options.interval -
|
|
223
|
+
if (options.interval === 0 || state.elapsed >= options.interval - 5) {
|
|
205
224
|
if (options.count > -1) state.callback(state.index);
|
|
206
225
|
start = now;
|
|
207
226
|
state.elapsed = 0;
|
|
@@ -215,7 +234,7 @@ function run(timer, state, options) {
|
|
|
215
234
|
};
|
|
216
235
|
}
|
|
217
236
|
function setState(type, state) {
|
|
218
|
-
const pausable = type ===
|
|
237
|
+
const pausable = type === "continue" || type === "pause";
|
|
219
238
|
state.elapsed = pausable ? state.elapsed : 0;
|
|
220
239
|
state.index = pausable ? state.index : 0;
|
|
221
240
|
state.total = pausable ? state.total : 0;
|
|
@@ -232,8 +251,8 @@ function stop(timer, state, options) {
|
|
|
232
251
|
function work(type, timer, state, options) {
|
|
233
252
|
if (ignore(type, state)) return timer.instance;
|
|
234
253
|
setState(type, state);
|
|
235
|
-
if (type ===
|
|
236
|
-
if (type ===
|
|
254
|
+
if (type === "stop") return stop(timer, state, options);
|
|
255
|
+
if (type === "pause" || type === "restart") {
|
|
237
256
|
cancelAnimationFrame(state.frame);
|
|
238
257
|
state.frame = void 0;
|
|
239
258
|
}
|
|
@@ -245,6 +264,8 @@ function work(type, timer, state, options) {
|
|
|
245
264
|
state.frame = requestAnimationFrame(runner);
|
|
246
265
|
return timer.instance;
|
|
247
266
|
}
|
|
267
|
+
//#endregion
|
|
268
|
+
//#region src/timer.ts
|
|
248
269
|
var Timer = class {
|
|
249
270
|
state;
|
|
250
271
|
/**
|
|
@@ -337,6 +358,8 @@ var Timer = class {
|
|
|
337
358
|
}, this.state, this.options);
|
|
338
359
|
}
|
|
339
360
|
};
|
|
361
|
+
//#endregion
|
|
362
|
+
//#region src/repeat.ts
|
|
340
363
|
/**
|
|
341
364
|
* Create a repeating timer
|
|
342
365
|
* @param callback Callback to run on each interval
|
|
@@ -355,6 +378,8 @@ function repeat(callback, options) {
|
|
|
355
378
|
timeout: getValidNumber(options?.timeout)
|
|
356
379
|
}, true);
|
|
357
380
|
}
|
|
381
|
+
//#endregion
|
|
382
|
+
//#region src/wait.ts
|
|
358
383
|
/**
|
|
359
384
|
* Create a waiting timer
|
|
360
385
|
* @param callback Callback to run when the timer has finished
|
|
@@ -372,6 +397,8 @@ function wait(callback, time) {
|
|
|
372
397
|
timeout: 0
|
|
373
398
|
}, true);
|
|
374
399
|
}
|
|
400
|
+
//#endregion
|
|
401
|
+
//#region src/when.ts
|
|
375
402
|
var When = class {
|
|
376
403
|
state = {
|
|
377
404
|
promise: void 0,
|
|
@@ -505,4 +532,5 @@ var When = class {
|
|
|
505
532
|
function when(condition, options) {
|
|
506
533
|
return new When(condition, options);
|
|
507
534
|
}
|
|
535
|
+
//#endregion
|
|
508
536
|
export { delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
|
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { Timer } from "./timer.mjs";
|
|
2
|
+
import { When } from "./when.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/is.d.ts
|
|
3
5
|
/**
|
|
4
6
|
* Is the value a repeating timer?
|
|
5
7
|
* @param value Value to check
|
|
6
8
|
* @returns `true` if the value is a repeating timer
|
|
7
9
|
*/
|
|
8
|
-
|
|
10
|
+
declare function isRepeated(value: unknown): value is Timer;
|
|
9
11
|
/**
|
|
10
12
|
* Is the value a timer?
|
|
11
13
|
* @param value Value to check
|
|
12
14
|
* @returns `true` if the value is a timer
|
|
13
15
|
*/
|
|
14
|
-
|
|
16
|
+
declare function isTimer(value: unknown): value is Timer;
|
|
15
17
|
/**
|
|
16
18
|
* Is the value a waiting timer?
|
|
17
19
|
* @param value Value to check
|
|
18
20
|
* @returns `true` if the value is a waiting timer
|
|
19
21
|
*/
|
|
20
|
-
|
|
22
|
+
declare function isWaited(value: unknown): value is Timer;
|
|
21
23
|
/**
|
|
22
24
|
* Is the value a conditional timer?
|
|
23
25
|
* @param value Value to check
|
|
24
26
|
* @returns `true` if the value is a conditional timer
|
|
25
27
|
*/
|
|
26
|
-
|
|
28
|
+
declare function isWhen(value: unknown): value is When;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { isRepeated, isTimer, isWaited, isWhen };
|
package/dist/{is.js → is.mjs}
RENAMED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { TYPE_REPEAT, TYPE_WAIT } from "./constants.
|
|
1
|
+
import { TYPE_REPEAT, TYPE_WAIT } from "./constants.mjs";
|
|
2
|
+
//#region src/is.ts
|
|
2
3
|
function is(names, value) {
|
|
3
4
|
return names.includes(value?.$timer);
|
|
4
5
|
}
|
|
@@ -34,4 +35,5 @@ function isWaited(value) {
|
|
|
34
35
|
function isWhen(value) {
|
|
35
36
|
return is(["when"], value) && typeof value.then === "function";
|
|
36
37
|
}
|
|
38
|
+
//#endregion
|
|
37
39
|
export { isRepeated, isTimer, isWaited, isWhen };
|