@oscarpalmer/timer 0.48.0 → 0.50.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 +15 -15
- package/dist/constants.mjs +2 -1
- package/dist/global.d.mts +2 -1
- package/dist/global.mjs +2 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +134 -105
- package/dist/is.d.mts +5 -6
- package/dist/is.mjs +11 -11
- package/dist/misc.d.mts +8 -7
- package/dist/misc.mjs +5 -1
- package/dist/models.d.mts +15 -15
- package/dist/repeat.d.mts +2 -3
- package/dist/timer.d.mts +3 -3
- package/dist/timer.mjs +51 -38
- package/dist/wait.d.mts +2 -3
- package/dist/when.d.mts +3 -3
- package/dist/when.mjs +61 -54
- package/dist/work.d.mts +3 -4
- package/dist/work.mjs +8 -7
- package/package.json +5 -5
- package/src/constants.ts +6 -0
- package/src/global.ts +12 -1
- package/src/is.ts +20 -12
- package/src/misc.ts +15 -0
- package/src/models.ts +9 -4
- package/src/repeat.ts +4 -0
- package/src/timer.ts +86 -43
- package/src/wait.ts +4 -0
- package/src/when.ts +113 -66
- package/src/work.ts +15 -9
package/dist/constants.d.mts
CHANGED
|
@@ -3,21 +3,21 @@ import { TimerName, TimerStates, WorkHandlerType } from "./models.mjs";
|
|
|
3
3
|
/**
|
|
4
4
|
* Buffer value to use when evaluating if a specific time is within a certain range
|
|
5
5
|
*/
|
|
6
|
-
declare const BUFFER_INTERVAL = 5;
|
|
7
|
-
declare const DEFAULT_TIMEOUT = 30000;
|
|
8
|
-
declare const GLOBAL_NAME = "_oscarpalmer_timers";
|
|
6
|
+
export declare const BUFFER_INTERVAL = 5;
|
|
7
|
+
export declare const DEFAULT_TIMEOUT = 30000;
|
|
8
|
+
export declare const GLOBAL_NAME = "_oscarpalmer_timers";
|
|
9
9
|
/**
|
|
10
10
|
* Message to show when a when-timer is started
|
|
11
11
|
*/
|
|
12
|
-
declare const MESSAGE_STARTED = "Timer has already been started";
|
|
13
|
-
declare const STATES: TimerStates;
|
|
14
|
-
declare const
|
|
15
|
-
declare const
|
|
16
|
-
declare const
|
|
17
|
-
declare const
|
|
18
|
-
declare const
|
|
19
|
-
declare const
|
|
20
|
-
declare const
|
|
21
|
-
declare const
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
export declare const MESSAGE_STARTED = "Timer has already been started";
|
|
13
|
+
export declare const STATES: TimerStates;
|
|
14
|
+
export declare const SYMBOL: unique symbol;
|
|
15
|
+
export declare const TYPE_REPEAT: TimerName;
|
|
16
|
+
export declare const TYPE_WAIT: TimerName;
|
|
17
|
+
export declare const TYPE_WHEN: TimerName;
|
|
18
|
+
export declare const WORK_CONTINUE: WorkHandlerType;
|
|
19
|
+
export declare const WORK_PAUSE: WorkHandlerType;
|
|
20
|
+
export declare const WORK_RESTART: WorkHandlerType;
|
|
21
|
+
export declare const WORK_START: WorkHandlerType;
|
|
22
|
+
export declare const WORK_STOP: WorkHandlerType;
|
|
23
|
+
//#endregion
|
package/dist/constants.mjs
CHANGED
|
@@ -13,6 +13,7 @@ const STATES = {
|
|
|
13
13
|
active: /* @__PURE__ */ new Set(),
|
|
14
14
|
hidden: /* @__PURE__ */ new Set()
|
|
15
15
|
};
|
|
16
|
+
const SYMBOL = Symbol("timer");
|
|
16
17
|
const TYPE_REPEAT = "repeat";
|
|
17
18
|
const TYPE_WAIT = "wait";
|
|
18
19
|
const TYPE_WHEN = "when";
|
|
@@ -22,4 +23,4 @@ const WORK_RESTART = "restart";
|
|
|
22
23
|
const WORK_START = "start";
|
|
23
24
|
const WORK_STOP = "stop";
|
|
24
25
|
//#endregion
|
|
25
|
-
export { BUFFER_INTERVAL, DEFAULT_TIMEOUT, GLOBAL_NAME, MESSAGE_STARTED, STATES, TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP };
|
|
26
|
+
export { BUFFER_INTERVAL, DEFAULT_TIMEOUT, GLOBAL_NAME, MESSAGE_STARTED, STATES, SYMBOL, TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP };
|
package/dist/global.d.mts
CHANGED
package/dist/global.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import { onVisibilityChange } from "./misc.mjs";
|
|
|
5
5
|
if (!("_oscarpalmer_timers" in globalThis)) Object.defineProperty(globalThis, GLOBAL_NAME, { get() {
|
|
6
6
|
return globalThis._oscarpalmer_timer_debug ? [...STATES.active].map((state) => state.timer) : [];
|
|
7
7
|
} });
|
|
8
|
-
|
|
8
|
+
/* istanbul ignore next */
|
|
9
|
+
if ("document" in globalThis) document.addEventListener("visibilitychange", onVisibilityChange);
|
|
9
10
|
//#endregion
|
|
10
11
|
export {};
|
package/dist/index.d.mts
CHANGED
|
@@ -200,6 +200,7 @@ export declare function repeat(callback: (index: number) => void, options?: Part
|
|
|
200
200
|
export declare function wait(callback: () => void, time?: number): Timer;
|
|
201
201
|
//#endregion
|
|
202
202
|
//#region src/when.d.ts
|
|
203
|
+
declare function When(this: any, condition: () => boolean, options: WhenOptions): void;
|
|
203
204
|
/**
|
|
204
205
|
* Create a conditional timer
|
|
205
206
|
* @param condition Condition to check
|
package/dist/index.mjs
CHANGED
|
@@ -9,6 +9,7 @@ const STATES = {
|
|
|
9
9
|
active: /* @__PURE__ */ new Set(),
|
|
10
10
|
hidden: /* @__PURE__ */ new Set()
|
|
11
11
|
};
|
|
12
|
+
const SYMBOL = Symbol("timer");
|
|
12
13
|
const TYPE_REPEAT = "repeat";
|
|
13
14
|
const TYPE_WAIT = "wait";
|
|
14
15
|
const TYPE_WHEN = "when";
|
|
@@ -27,7 +28,7 @@ function noop() {}
|
|
|
27
28
|
//#region src/work.ts
|
|
28
29
|
function finish(state, success) {
|
|
29
30
|
updateStates(state);
|
|
30
|
-
|
|
31
|
+
stopTimer$1(state.frame);
|
|
31
32
|
state.active = false;
|
|
32
33
|
state.elapsed = 0;
|
|
33
34
|
state.frame = void 0;
|
|
@@ -40,8 +41,9 @@ function ignore(type, state) {
|
|
|
40
41
|
}
|
|
41
42
|
function run(state) {
|
|
42
43
|
let last;
|
|
43
|
-
return function step(
|
|
44
|
+
return function step() {
|
|
44
45
|
if (!state.active) return;
|
|
46
|
+
const now = performance.now();
|
|
45
47
|
last ??= now;
|
|
46
48
|
const difference = now - last;
|
|
47
49
|
state.elapsed += difference;
|
|
@@ -61,7 +63,7 @@ function run(state) {
|
|
|
61
63
|
return;
|
|
62
64
|
}
|
|
63
65
|
}
|
|
64
|
-
state.frame =
|
|
66
|
+
state.frame = startTimer$1(step);
|
|
65
67
|
};
|
|
66
68
|
}
|
|
67
69
|
function setState(type, state) {
|
|
@@ -72,7 +74,7 @@ function setState(type, state) {
|
|
|
72
74
|
}
|
|
73
75
|
function stop(state) {
|
|
74
76
|
updateStates(state);
|
|
75
|
-
|
|
77
|
+
stopTimer$1(state.frame);
|
|
76
78
|
state.options.onAfter?.(false);
|
|
77
79
|
state.active = false;
|
|
78
80
|
state.frame = void 0;
|
|
@@ -84,7 +86,7 @@ function work(type, state, hide) {
|
|
|
84
86
|
setState(type, state);
|
|
85
87
|
if (type === "stop") return stop(state);
|
|
86
88
|
if (type === "pause" || type === "restart") {
|
|
87
|
-
|
|
89
|
+
stopTimer$1(state.frame);
|
|
88
90
|
state.frame = void 0;
|
|
89
91
|
}
|
|
90
92
|
state.active = true;
|
|
@@ -92,7 +94,7 @@ function work(type, state, hide) {
|
|
|
92
94
|
updateStates(state, state.paused ? hide : false);
|
|
93
95
|
if (state.paused) return state.timer;
|
|
94
96
|
const runner = run(state);
|
|
95
|
-
state.frame =
|
|
97
|
+
state.frame = startTimer$1(runner);
|
|
96
98
|
return state.timer;
|
|
97
99
|
}
|
|
98
100
|
//#endregion
|
|
@@ -128,53 +130,58 @@ function updateStates(state, hide) {
|
|
|
128
130
|
STATES.hidden.add(state.reference);
|
|
129
131
|
} else STATES.active.add(state);
|
|
130
132
|
}
|
|
133
|
+
/* istanbul ignore next */
|
|
134
|
+
const startTimer$1 = "requestAnimationFrame" in globalThis ? requestAnimationFrame : setTimeout;
|
|
135
|
+
/* istanbul ignore next */
|
|
136
|
+
const stopTimer$1 = "cancelAnimationFrame" in globalThis ? cancelAnimationFrame : clearTimeout;
|
|
131
137
|
//#endregion
|
|
132
138
|
//#region src/global.ts
|
|
133
139
|
/* istanbul ignore next */
|
|
134
140
|
if (!("_oscarpalmer_timers" in globalThis)) Object.defineProperty(globalThis, GLOBAL_NAME, { get() {
|
|
135
141
|
return globalThis._oscarpalmer_timer_debug ? [...STATES.active].map((state) => state.timer) : [];
|
|
136
142
|
} });
|
|
137
|
-
|
|
143
|
+
/* istanbul ignore next */
|
|
144
|
+
if ("document" in globalThis) document.addEventListener("visibilitychange", onVisibilityChange);
|
|
138
145
|
//#endregion
|
|
139
146
|
//#region src/is.ts
|
|
140
|
-
function
|
|
141
|
-
return names.includes(value
|
|
147
|
+
function isInstance(names, value) {
|
|
148
|
+
return typeof value === "object" && value !== null && SYMBOL in value && names.includes(value[SYMBOL].name);
|
|
142
149
|
}
|
|
143
150
|
/**
|
|
144
151
|
* Is the value a repeating timer?
|
|
145
|
-
*
|
|
152
|
+
*
|
|
146
153
|
* @param value Value to check
|
|
147
154
|
* @returns `true` if the value is a repeating timer, otherwise `false`
|
|
148
155
|
*/
|
|
149
156
|
function isRepeated(value) {
|
|
150
|
-
return
|
|
157
|
+
return isInstance([TYPE_REPEAT], value);
|
|
151
158
|
}
|
|
152
159
|
/**
|
|
153
160
|
* Is the value a timer?
|
|
154
|
-
*
|
|
161
|
+
*
|
|
155
162
|
* @param value Value to check
|
|
156
163
|
* @returns `true` if the value is a timer, otherwise `false`
|
|
157
164
|
*/
|
|
158
165
|
function isTimer(value) {
|
|
159
|
-
return
|
|
166
|
+
return isInstance([TYPE_REPEAT, TYPE_WAIT], value);
|
|
160
167
|
}
|
|
161
168
|
/**
|
|
162
169
|
* Is the value a waiting timer?
|
|
163
|
-
*
|
|
170
|
+
*
|
|
164
171
|
* @param value Value to check
|
|
165
172
|
* @returns `true` if the value is a waiting timer, otherwise `false`
|
|
166
173
|
*/
|
|
167
174
|
function isWaited(value) {
|
|
168
|
-
return
|
|
175
|
+
return isInstance([TYPE_WAIT], value);
|
|
169
176
|
}
|
|
170
177
|
/**
|
|
171
178
|
* Is the value a conditional timer?
|
|
172
|
-
*
|
|
179
|
+
*
|
|
173
180
|
* @param value Value to check
|
|
174
181
|
* @returns `true` if the value is a conditional timer, otherwise `false`
|
|
175
182
|
*/
|
|
176
183
|
function isWhen(value) {
|
|
177
|
-
return
|
|
184
|
+
return isInstance(["when"], value) && typeof value.start === "function";
|
|
178
185
|
}
|
|
179
186
|
//#endregion
|
|
180
187
|
//#region src/models.ts
|
|
@@ -186,9 +193,9 @@ var TimerTrace = class extends Error {
|
|
|
186
193
|
};
|
|
187
194
|
//#endregion
|
|
188
195
|
//#region src/timer.ts
|
|
189
|
-
function
|
|
190
|
-
|
|
191
|
-
...
|
|
196
|
+
function Timer(name, state, options, start) {
|
|
197
|
+
this[SYMBOL] = {
|
|
198
|
+
...state,
|
|
192
199
|
name,
|
|
193
200
|
options,
|
|
194
201
|
active: false,
|
|
@@ -200,39 +207,53 @@ function createTimer(name, pick, options, start) {
|
|
|
200
207
|
timer: void 0,
|
|
201
208
|
total: 0
|
|
202
209
|
};
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
210
|
+
if (start) startTimer.call(this);
|
|
211
|
+
}
|
|
212
|
+
Timer.prototype.continue = continueTimer;
|
|
213
|
+
Timer.prototype.pause = pauseTimer;
|
|
214
|
+
Timer.prototype.restart = restartTimer;
|
|
215
|
+
Timer.prototype.start = startTimer;
|
|
216
|
+
Timer.prototype.stop = stopTimer;
|
|
217
|
+
Object.defineProperties(Timer.prototype, {
|
|
218
|
+
active: {
|
|
219
|
+
enumerable: true,
|
|
220
|
+
get: getTimerActive
|
|
221
|
+
},
|
|
222
|
+
paused: {
|
|
223
|
+
enumerable: true,
|
|
224
|
+
get: getTimerPaused
|
|
225
|
+
},
|
|
226
|
+
trace: {
|
|
227
|
+
enumerable: true,
|
|
228
|
+
get: getTimerTrace
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
function continueTimer() {
|
|
232
|
+
return work(WORK_CONTINUE, this[SYMBOL]);
|
|
233
|
+
}
|
|
234
|
+
function createTimer(name, state, options, start) {
|
|
235
|
+
return new Timer(name, state, options, start);
|
|
236
|
+
}
|
|
237
|
+
function getTimerActive() {
|
|
238
|
+
return this[SYMBOL].active && !this[SYMBOL].paused;
|
|
239
|
+
}
|
|
240
|
+
function getTimerPaused() {
|
|
241
|
+
return this[SYMBOL].paused;
|
|
242
|
+
}
|
|
243
|
+
function getTimerTrace() {
|
|
244
|
+
return globalThis._oscarpalmer_timer_debug ?? false ? this[SYMBOL].trace : void 0;
|
|
245
|
+
}
|
|
246
|
+
function pauseTimer() {
|
|
247
|
+
return work(WORK_PAUSE, this[SYMBOL]);
|
|
248
|
+
}
|
|
249
|
+
function restartTimer() {
|
|
250
|
+
return work(WORK_RESTART, this[SYMBOL]);
|
|
251
|
+
}
|
|
252
|
+
function startTimer() {
|
|
253
|
+
return work(WORK_START, this[SYMBOL]);
|
|
254
|
+
}
|
|
255
|
+
function stopTimer() {
|
|
256
|
+
return work(WORK_STOP, this[SYMBOL]);
|
|
236
257
|
}
|
|
237
258
|
//#endregion
|
|
238
259
|
//#region src/repeat.ts
|
|
@@ -278,6 +299,57 @@ function wait(callback, time) {
|
|
|
278
299
|
}
|
|
279
300
|
//#endregion
|
|
280
301
|
//#region src/when.ts
|
|
302
|
+
function When(condition, options) {
|
|
303
|
+
const state = {
|
|
304
|
+
name: TYPE_WHEN,
|
|
305
|
+
promise: void 0,
|
|
306
|
+
result: false,
|
|
307
|
+
started: false,
|
|
308
|
+
timer: void 0
|
|
309
|
+
};
|
|
310
|
+
this[SYMBOL] = state;
|
|
311
|
+
state.promise = new Promise((resolve, reject) => {
|
|
312
|
+
state.resolver = resolve;
|
|
313
|
+
state.rejecter = reject;
|
|
314
|
+
});
|
|
315
|
+
state.timer = createTimer(TYPE_WHEN, {
|
|
316
|
+
callback: () => onCallback(condition, state),
|
|
317
|
+
trace: new TimerTrace().stack
|
|
318
|
+
}, {
|
|
319
|
+
...options,
|
|
320
|
+
onAfter: () => onAfter(state),
|
|
321
|
+
onError: () => state.rejecter?.()
|
|
322
|
+
}, false);
|
|
323
|
+
}
|
|
324
|
+
When.prototype.continue = continueWhen;
|
|
325
|
+
When.prototype.pause = pauseWhen;
|
|
326
|
+
When.prototype.start = startWhen;
|
|
327
|
+
When.prototype.stop = stopWhen;
|
|
328
|
+
Object.defineProperties(When.prototype, {
|
|
329
|
+
active: { get: getWhenActive },
|
|
330
|
+
paused: { get: getWhenPaused },
|
|
331
|
+
trace: { get: getWhenTrace }
|
|
332
|
+
});
|
|
333
|
+
function continueWhen() {
|
|
334
|
+
return onWhen(WORK_CONTINUE, this, this[SYMBOL]);
|
|
335
|
+
}
|
|
336
|
+
function getWhenOptions(input) {
|
|
337
|
+
const options = typeof input === "object" && input !== null ? input : {};
|
|
338
|
+
return {
|
|
339
|
+
count: getValidNumber(options?.count),
|
|
340
|
+
interval: getValidNumber(options?.interval),
|
|
341
|
+
timeout: getValidTimeout(options?.timeout)
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function getWhenActive() {
|
|
345
|
+
return this[SYMBOL].timer.active;
|
|
346
|
+
}
|
|
347
|
+
function getWhenPaused() {
|
|
348
|
+
return this[SYMBOL].timer.paused;
|
|
349
|
+
}
|
|
350
|
+
function getWhenTrace() {
|
|
351
|
+
return globalThis._oscarpalmer_timer_debug ?? false ? this[SYMBOL].timer?.trace : void 0;
|
|
352
|
+
}
|
|
281
353
|
function onAfter(state) {
|
|
282
354
|
if (state.result) state.resolver?.();
|
|
283
355
|
else state.rejecter?.();
|
|
@@ -296,12 +368,19 @@ function onWhen(type, instance, state) {
|
|
|
296
368
|
state.timer?.[type]?.();
|
|
297
369
|
return instance;
|
|
298
370
|
}
|
|
299
|
-
function
|
|
371
|
+
function pauseWhen() {
|
|
372
|
+
return onWhen(WORK_PAUSE, this, this[SYMBOL]);
|
|
373
|
+
}
|
|
374
|
+
function startWhen(resolve) {
|
|
375
|
+
const state = this[SYMBOL];
|
|
300
376
|
if (state.started) throw new Error(MESSAGE_STARTED);
|
|
301
377
|
state.started = true;
|
|
302
378
|
state.timer.start();
|
|
303
379
|
return state.promise.then(resolve);
|
|
304
380
|
}
|
|
381
|
+
function stopWhen() {
|
|
382
|
+
return onWhen(WORK_STOP, this, this[SYMBOL]);
|
|
383
|
+
}
|
|
305
384
|
/**
|
|
306
385
|
* Create a conditional timer
|
|
307
386
|
* @param condition Condition to check
|
|
@@ -309,57 +388,7 @@ function startWhen(state, resolve) {
|
|
|
309
388
|
* @returns Timer instance
|
|
310
389
|
*/
|
|
311
390
|
function when(condition, options) {
|
|
312
|
-
|
|
313
|
-
promise: void 0,
|
|
314
|
-
result: false,
|
|
315
|
-
started: false,
|
|
316
|
-
timer: void 0
|
|
317
|
-
};
|
|
318
|
-
let instance;
|
|
319
|
-
state.promise = new Promise((resolve, reject) => {
|
|
320
|
-
state.resolver = resolve;
|
|
321
|
-
state.rejecter = reject;
|
|
322
|
-
});
|
|
323
|
-
state.timer = createTimer(TYPE_WHEN, {
|
|
324
|
-
callback: () => onCallback(condition, state),
|
|
325
|
-
trace: new TimerTrace().stack
|
|
326
|
-
}, {
|
|
327
|
-
onAfter: () => onAfter(state),
|
|
328
|
-
onError: () => state.rejecter?.(),
|
|
329
|
-
count: getValidNumber(options?.count),
|
|
330
|
-
interval: getValidNumber(options?.interval),
|
|
331
|
-
timeout: getValidTimeout(options?.timeout)
|
|
332
|
-
}, false);
|
|
333
|
-
instance = {
|
|
334
|
-
continue: () => onWhen(WORK_CONTINUE, instance, state),
|
|
335
|
-
destroy: noop,
|
|
336
|
-
pause: () => onWhen(WORK_PAUSE, instance, state),
|
|
337
|
-
start: (resolve) => startWhen(state, resolve),
|
|
338
|
-
stop: () => onWhen(WORK_STOP, instance, state)
|
|
339
|
-
};
|
|
340
|
-
Object.defineProperties(instance, {
|
|
341
|
-
$timer: {
|
|
342
|
-
enumerable: false,
|
|
343
|
-
value: TYPE_WHEN
|
|
344
|
-
},
|
|
345
|
-
active: {
|
|
346
|
-
enumerable: true,
|
|
347
|
-
get: () => state.timer.active
|
|
348
|
-
},
|
|
349
|
-
destroyed: {
|
|
350
|
-
enumerable: true,
|
|
351
|
-
value: false
|
|
352
|
-
},
|
|
353
|
-
paused: {
|
|
354
|
-
enumerable: true,
|
|
355
|
-
get: () => state.timer.paused
|
|
356
|
-
},
|
|
357
|
-
trace: {
|
|
358
|
-
enumerable: true,
|
|
359
|
-
get: () => globalThis._oscarpalmer_timer_debug ?? false ? state.timer?.trace : void 0
|
|
360
|
-
}
|
|
361
|
-
});
|
|
362
|
-
return Object.freeze(instance);
|
|
391
|
+
return new When(condition, getWhenOptions(options));
|
|
363
392
|
}
|
|
364
393
|
//#endregion
|
|
365
394
|
export { isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
|
package/dist/is.d.mts
CHANGED
|
@@ -6,27 +6,26 @@ import { Timer, When } from "./models.mjs";
|
|
|
6
6
|
* @param value Value to check
|
|
7
7
|
* @returns `true` if the value is a repeating timer, otherwise `false`
|
|
8
8
|
*/
|
|
9
|
-
declare function isRepeated(value: unknown): value is Timer;
|
|
9
|
+
export declare function isRepeated(value: unknown): value is Timer;
|
|
10
10
|
/**
|
|
11
11
|
* Is the value a timer?
|
|
12
12
|
*
|
|
13
13
|
* @param value Value to check
|
|
14
14
|
* @returns `true` if the value is a timer, otherwise `false`
|
|
15
15
|
*/
|
|
16
|
-
declare function isTimer(value: unknown): value is Timer;
|
|
16
|
+
export declare function isTimer(value: unknown): value is Timer;
|
|
17
17
|
/**
|
|
18
18
|
* Is the value a waiting timer?
|
|
19
19
|
*
|
|
20
20
|
* @param value Value to check
|
|
21
21
|
* @returns `true` if the value is a waiting timer, otherwise `false`
|
|
22
22
|
*/
|
|
23
|
-
declare function isWaited(value: unknown): value is Timer;
|
|
23
|
+
export declare function isWaited(value: unknown): value is Timer;
|
|
24
24
|
/**
|
|
25
25
|
* Is the value a conditional timer?
|
|
26
26
|
*
|
|
27
27
|
* @param value Value to check
|
|
28
28
|
* @returns `true` if the value is a conditional timer, otherwise `false`
|
|
29
29
|
*/
|
|
30
|
-
declare function isWhen(value: unknown): value is When;
|
|
31
|
-
//#endregion
|
|
32
|
-
export { isRepeated, isTimer, isWaited, isWhen };
|
|
30
|
+
export declare function isWhen(value: unknown): value is When;
|
|
31
|
+
//#endregion
|
package/dist/is.mjs
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { TYPE_REPEAT, TYPE_WAIT } from "./constants.mjs";
|
|
1
|
+
import { SYMBOL, TYPE_REPEAT, TYPE_WAIT } from "./constants.mjs";
|
|
2
2
|
//#region src/is.ts
|
|
3
|
-
function
|
|
4
|
-
return names.includes(value
|
|
3
|
+
function isInstance(names, value) {
|
|
4
|
+
return typeof value === "object" && value !== null && SYMBOL in value && names.includes(value[SYMBOL].name);
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
7
|
* Is the value a repeating timer?
|
|
8
|
-
*
|
|
8
|
+
*
|
|
9
9
|
* @param value Value to check
|
|
10
10
|
* @returns `true` if the value is a repeating timer, otherwise `false`
|
|
11
11
|
*/
|
|
12
12
|
function isRepeated(value) {
|
|
13
|
-
return
|
|
13
|
+
return isInstance([TYPE_REPEAT], value);
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* Is the value a timer?
|
|
17
|
-
*
|
|
17
|
+
*
|
|
18
18
|
* @param value Value to check
|
|
19
19
|
* @returns `true` if the value is a timer, otherwise `false`
|
|
20
20
|
*/
|
|
21
21
|
function isTimer(value) {
|
|
22
|
-
return
|
|
22
|
+
return isInstance([TYPE_REPEAT, TYPE_WAIT], value);
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
25
|
* Is the value a waiting timer?
|
|
26
|
-
*
|
|
26
|
+
*
|
|
27
27
|
* @param value Value to check
|
|
28
28
|
* @returns `true` if the value is a waiting timer, otherwise `false`
|
|
29
29
|
*/
|
|
30
30
|
function isWaited(value) {
|
|
31
|
-
return
|
|
31
|
+
return isInstance([TYPE_WAIT], value);
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Is the value a conditional timer?
|
|
35
|
-
*
|
|
35
|
+
*
|
|
36
36
|
* @param value Value to check
|
|
37
37
|
* @returns `true` if the value is a conditional timer, otherwise `false`
|
|
38
38
|
*/
|
|
39
39
|
function isWhen(value) {
|
|
40
|
-
return
|
|
40
|
+
return isInstance(["when"], value) && typeof value.start === "function";
|
|
41
41
|
}
|
|
42
42
|
//#endregion
|
|
43
43
|
export { isRepeated, isTimer, isWaited, isWhen };
|
package/dist/misc.d.mts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { TimerState } from "./models.mjs";
|
|
2
2
|
import { GenericCallback } from "@oscarpalmer/atoms/models";
|
|
3
3
|
//#region src/misc.d.ts
|
|
4
|
-
declare function getCallback(value: unknown): GenericCallback;
|
|
5
|
-
declare function getValidNumber(value: unknown, defaultValue?: number): number;
|
|
6
|
-
declare function getValidTimeout(value: unknown): number;
|
|
7
|
-
declare function onVisibilityChange(): void;
|
|
8
|
-
declare function updateStates(state: TimerState, hide?: boolean): void;
|
|
9
|
-
|
|
10
|
-
export
|
|
4
|
+
export declare function getCallback(value: unknown): GenericCallback;
|
|
5
|
+
export declare function getValidNumber(value: unknown, defaultValue?: number): number;
|
|
6
|
+
export declare function getValidTimeout(value: unknown): number;
|
|
7
|
+
export declare function onVisibilityChange(): void;
|
|
8
|
+
export declare function updateStates(state: TimerState, hide?: boolean): void;
|
|
9
|
+
export declare const startTimer: typeof requestAnimationFrame | typeof setTimeout;
|
|
10
|
+
export declare const stopTimer: typeof cancelAnimationFrame;
|
|
11
|
+
//#endregion
|
package/dist/misc.mjs
CHANGED
|
@@ -33,5 +33,9 @@ function updateStates(state, hide) {
|
|
|
33
33
|
STATES.hidden.add(state.reference);
|
|
34
34
|
} else STATES.active.add(state);
|
|
35
35
|
}
|
|
36
|
+
/* istanbul ignore next */
|
|
37
|
+
const startTimer = "requestAnimationFrame" in globalThis ? requestAnimationFrame : setTimeout;
|
|
38
|
+
/* istanbul ignore next */
|
|
39
|
+
const stopTimer = "cancelAnimationFrame" in globalThis ? cancelAnimationFrame : clearTimeout;
|
|
36
40
|
//#endregion
|
|
37
|
-
export { getCallback, getValidNumber, getValidTimeout, onVisibilityChange, updateStates };
|
|
41
|
+
export { getCallback, getValidNumber, getValidTimeout, onVisibilityChange, startTimer, stopTimer, updateStates };
|
package/dist/models.d.mts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Options for a repeating timer
|
|
4
4
|
*/
|
|
5
|
-
type RepeatOptions = {
|
|
5
|
+
export type RepeatOptions = {
|
|
6
6
|
/**
|
|
7
7
|
* Callback to be called when the timer has stopped, either manually or by completing its work
|
|
8
8
|
*/
|
|
@@ -24,7 +24,7 @@ type RepeatOptions = {
|
|
|
24
24
|
*/
|
|
25
25
|
timeout: number;
|
|
26
26
|
};
|
|
27
|
-
type Timer = {
|
|
27
|
+
export type Timer = {
|
|
28
28
|
/**
|
|
29
29
|
* Is the timer active?
|
|
30
30
|
*/
|
|
@@ -70,15 +70,15 @@ type Timer = {
|
|
|
70
70
|
*/
|
|
71
71
|
stop(): Timer;
|
|
72
72
|
};
|
|
73
|
-
type TimerName = 'repeat' | 'wait' | 'when';
|
|
74
|
-
type TimerOptions = {
|
|
73
|
+
export type TimerName = 'repeat' | 'wait' | 'when';
|
|
74
|
+
export type TimerOptions = {
|
|
75
75
|
onAfter: ((finished: boolean) => void) | undefined;
|
|
76
76
|
onError: (() => void) | undefined;
|
|
77
77
|
count: number;
|
|
78
78
|
interval: number;
|
|
79
79
|
timeout: number;
|
|
80
80
|
};
|
|
81
|
-
type TimerState = {
|
|
81
|
+
export type TimerState = {
|
|
82
82
|
active: boolean;
|
|
83
83
|
callback: () => void;
|
|
84
84
|
destroyed: boolean;
|
|
@@ -93,7 +93,7 @@ type TimerState = {
|
|
|
93
93
|
total: number;
|
|
94
94
|
trace?: string;
|
|
95
95
|
};
|
|
96
|
-
type TimerStates = {
|
|
96
|
+
export type TimerStates = {
|
|
97
97
|
/**
|
|
98
98
|
* A set of all active timers
|
|
99
99
|
*/
|
|
@@ -103,10 +103,10 @@ type TimerStates = {
|
|
|
103
103
|
*/
|
|
104
104
|
hidden: Set<WeakRef<TimerState>>;
|
|
105
105
|
};
|
|
106
|
-
declare class TimerTrace extends Error {
|
|
106
|
+
export declare class TimerTrace extends Error {
|
|
107
107
|
constructor();
|
|
108
108
|
}
|
|
109
|
-
type When = {
|
|
109
|
+
export type When = {
|
|
110
110
|
/**
|
|
111
111
|
* Is the timer active?
|
|
112
112
|
*/
|
|
@@ -154,7 +154,7 @@ type When = {
|
|
|
154
154
|
/**
|
|
155
155
|
* Options for a conditional timer
|
|
156
156
|
*/
|
|
157
|
-
type WhenOptions = {
|
|
157
|
+
export type WhenOptions = {
|
|
158
158
|
/**
|
|
159
159
|
* How many times the timer should check the condition
|
|
160
160
|
*/
|
|
@@ -168,7 +168,8 @@ type WhenOptions = {
|
|
|
168
168
|
*/
|
|
169
169
|
timeout: number;
|
|
170
170
|
};
|
|
171
|
-
type WhenState = {
|
|
171
|
+
export type WhenState = {
|
|
172
|
+
name: TimerName;
|
|
172
173
|
promise: Promise<void>;
|
|
173
174
|
rejecter?: () => void;
|
|
174
175
|
resolver?: () => void;
|
|
@@ -176,11 +177,10 @@ type WhenState = {
|
|
|
176
177
|
started: boolean;
|
|
177
178
|
timer: Timer;
|
|
178
179
|
};
|
|
179
|
-
type WorkHandler = (type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions) => Timer;
|
|
180
|
-
type WorkHandlerTimer = {
|
|
180
|
+
export type WorkHandler = (type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions) => Timer;
|
|
181
|
+
export type WorkHandlerTimer = {
|
|
181
182
|
instance: Timer;
|
|
182
183
|
name: TimerName;
|
|
183
184
|
};
|
|
184
|
-
type WorkHandlerType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
|
|
185
|
-
//#endregion
|
|
186
|
-
export { RepeatOptions, Timer, TimerName, TimerOptions, TimerState, TimerStates, TimerTrace, When, WhenOptions, WhenState, WorkHandler, WorkHandlerTimer, WorkHandlerType };
|
|
185
|
+
export type WorkHandlerType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
|
|
186
|
+
//#endregion
|
package/dist/repeat.d.mts
CHANGED
|
@@ -8,6 +8,5 @@ import "./global.mjs";
|
|
|
8
8
|
* @param options Timer options
|
|
9
9
|
* @returns Repeating timer
|
|
10
10
|
*/
|
|
11
|
-
declare function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer;
|
|
12
|
-
//#endregion
|
|
13
|
-
export { repeat };
|
|
11
|
+
export declare function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer;
|
|
12
|
+
//#endregion
|
package/dist/timer.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Timer, TimerName, TimerOptions, TimerState } from "./models.mjs";
|
|
2
2
|
//#region src/timer.d.ts
|
|
3
|
-
declare function
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
declare function Timer(this: any, name: TimerName, state: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean): void;
|
|
4
|
+
export declare function createTimer(name: TimerName, state: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean): Timer;
|
|
5
|
+
//#endregion
|