@oscarpalmer/timer 0.28.0 → 0.30.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.cjs +32 -4
- package/dist/constants.js +26 -9
- package/dist/delay.cjs +24 -0
- package/dist/delay.js +20 -0
- package/dist/get.cjs +15 -0
- package/dist/get.js +10 -0
- package/dist/global.cjs +17 -2
- package/dist/global.js +15 -1
- package/dist/index.cjs +16 -44
- package/dist/index.js +6 -50
- package/dist/is.cjs +11 -8
- package/dist/is.js +8 -12
- package/dist/models.cjs +5 -2
- package/dist/models.js +2 -3
- package/dist/node_modules/@oscarpalmer/atoms/dist/function.cjs +8 -0
- package/dist/node_modules/@oscarpalmer/atoms/dist/function.js +4 -0
- package/dist/repeat.cjs +30 -0
- package/dist/repeat.js +26 -0
- package/dist/timer.cjs +59 -71
- package/dist/timer.full.js +472 -0
- package/dist/timer.js +56 -72
- package/dist/wait.cjs +30 -0
- package/dist/wait.js +26 -0
- package/dist/when.cjs +59 -44
- package/dist/when.js +55 -44
- package/dist/work.cjs +72 -0
- package/dist/work.js +68 -0
- package/package.json +49 -9
- package/src/constants.ts +51 -6
- package/src/delay.ts +25 -0
- package/src/get.ts +12 -0
- package/src/global.ts +16 -1
- package/src/index.ts +5 -48
- package/src/is.ts +6 -6
- package/src/models.ts +55 -47
- package/src/repeat.ts +32 -0
- package/src/timer.ts +67 -151
- package/src/wait.ts +31 -0
- package/src/when.ts +49 -24
- package/src/work.ts +129 -0
- package/types/constants.d.cts +49 -74
- package/types/constants.d.ts +15 -6
- package/types/delay.d.cts +8 -0
- package/types/delay.d.ts +4 -0
- package/types/get.d.cts +7 -0
- package/types/get.d.ts +3 -0
- package/types/index.d.cts +89 -94
- package/types/index.d.ts +5 -6
- package/types/is.d.cts +50 -69
- package/types/models.d.cts +61 -63
- package/types/models.d.ts +43 -40
- package/types/repeat.d.cts +98 -0
- package/types/repeat.d.ts +7 -0
- package/types/timer.d.cts +35 -97
- package/types/timer.d.ts +19 -52
- package/types/wait.d.cts +83 -0
- package/types/wait.d.ts +8 -0
- package/types/when.d.cts +65 -69
- package/types/when.d.ts +18 -5
- package/types/work.d.cts +78 -0
- package/types/work.d.ts +3 -0
- package/dist/functions.cjs +0 -99
- package/dist/functions.js +0 -99
- package/dist/timer.iife.js +0 -431
- package/src/functions.ts +0 -158
- package/types/functions.d.cts +0 -113
- package/types/functions.d.ts +0 -4
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stepper to calculate the average refresh rate of the display
|
|
3
|
+
*/
|
|
4
|
+
function stepper(now) {
|
|
5
|
+
if (values.length === 7) {
|
|
6
|
+
milliseconds = Math.floor((values.slice(2).reduce((first, second) => first + second) / 5) * 2) / 2;
|
|
7
|
+
last = undefined;
|
|
8
|
+
values.length = 0;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
last ??= now;
|
|
12
|
+
const difference = now - last;
|
|
13
|
+
if (difference > 0) {
|
|
14
|
+
values.push(difference);
|
|
15
|
+
}
|
|
16
|
+
last = now;
|
|
17
|
+
requestAnimationFrame(stepper);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A set of all active timers
|
|
22
|
+
*/
|
|
23
|
+
const activeTimers = new Set();
|
|
24
|
+
/**
|
|
25
|
+
* A set of types that allow work to begin
|
|
26
|
+
*/
|
|
27
|
+
const beginTypes = new Set(['continue', 'start']);
|
|
28
|
+
/**
|
|
29
|
+
* Message to show when a when-timer is destroyed
|
|
30
|
+
*/
|
|
31
|
+
const destroyedMessage = 'Timer has already been destroyed';
|
|
32
|
+
/**
|
|
33
|
+
* A set of types that allow work to end
|
|
34
|
+
*/
|
|
35
|
+
const endTypes = new Set(['pause', 'stop']);
|
|
36
|
+
/**
|
|
37
|
+
* A set of types that allow work to end or restart
|
|
38
|
+
*/
|
|
39
|
+
const endOrRestartTypes = new Set([
|
|
40
|
+
'pause',
|
|
41
|
+
'restart',
|
|
42
|
+
'stop',
|
|
43
|
+
]);
|
|
44
|
+
/**
|
|
45
|
+
* A set of timers that were paused due to the document being hidden
|
|
46
|
+
*/
|
|
47
|
+
const hiddenTimers = new Set();
|
|
48
|
+
const pauseTypes = new Set(['continue', 'pause']);
|
|
49
|
+
/**
|
|
50
|
+
* Message to show when a when-timer is started
|
|
51
|
+
*/
|
|
52
|
+
const startedMessage = 'Timer has already been started';
|
|
53
|
+
//
|
|
54
|
+
const values = [];
|
|
55
|
+
let last;
|
|
56
|
+
/**
|
|
57
|
+
* A calculated average of the refresh rate of the display _(in milliseconds)_
|
|
58
|
+
*/
|
|
59
|
+
let milliseconds = Math.floor((1000 / 60) * 2) / 2;
|
|
60
|
+
requestAnimationFrame(stepper);
|
|
61
|
+
|
|
62
|
+
if (globalThis._oscarpalmer_timers == null) {
|
|
63
|
+
Object.defineProperty(globalThis, '_oscarpalmer_timers', {
|
|
64
|
+
get() {
|
|
65
|
+
return globalThis._oscarpalmer_timer_debug ? [...activeTimers] : [];
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
document.addEventListener('visibilitychange', () => {
|
|
70
|
+
if (document.hidden) {
|
|
71
|
+
for (const timer of activeTimers) {
|
|
72
|
+
hiddenTimers.add(timer);
|
|
73
|
+
timer.pause();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
for (const timer of hiddenTimers) {
|
|
78
|
+
timer.continue();
|
|
79
|
+
}
|
|
80
|
+
hiddenTimers.clear();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
function noop() {
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getCallback(value) {
|
|
88
|
+
return typeof value === 'function' ? value : noop;
|
|
89
|
+
}
|
|
90
|
+
function getValidNumber(value, defaultValue) {
|
|
91
|
+
return typeof value === 'number' && value > (defaultValue ?? 0)
|
|
92
|
+
? value
|
|
93
|
+
: defaultValue ?? 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Create a delayed promise that resolves after a certain amount of time _(in milliseconds; defaults to screen refresh rate)_
|
|
98
|
+
*/
|
|
99
|
+
function delay(time) {
|
|
100
|
+
return new Promise(resolve => {
|
|
101
|
+
const interval = getValidNumber(time, milliseconds);
|
|
102
|
+
let start;
|
|
103
|
+
function step(now) {
|
|
104
|
+
start ??= now;
|
|
105
|
+
if (interval === milliseconds || now - start >= interval - 5) {
|
|
106
|
+
resolve();
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
requestAnimationFrame(step);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
requestAnimationFrame(step);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function is(names, value) {
|
|
117
|
+
return names.includes(value?.$timer);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Is the value a repeating timer?
|
|
121
|
+
*/
|
|
122
|
+
function isRepeated(value) {
|
|
123
|
+
return is(['repeat'], value);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Is the value a timer?
|
|
127
|
+
*/
|
|
128
|
+
function isTimer(value) {
|
|
129
|
+
return is(['repeat', 'wait'], value);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Is the value a waiting timer?
|
|
133
|
+
*/
|
|
134
|
+
function isWaited(value) {
|
|
135
|
+
return is(['wait'], value);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Is the value a conditional timer?
|
|
139
|
+
*/
|
|
140
|
+
function isWhen(value) {
|
|
141
|
+
return is(['when'], value) && typeof value.then === 'function';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
class TimerTrace extends Error {
|
|
145
|
+
constructor() {
|
|
146
|
+
super();
|
|
147
|
+
this.name = 'TimerTrace';
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
class Timer {
|
|
152
|
+
worker;
|
|
153
|
+
options;
|
|
154
|
+
state;
|
|
155
|
+
/**
|
|
156
|
+
* Is the timer active?
|
|
157
|
+
*/
|
|
158
|
+
get active() {
|
|
159
|
+
return this.state.active;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Is the timer destroyed?
|
|
163
|
+
*/
|
|
164
|
+
get destroyed() {
|
|
165
|
+
return this.state.destroyed;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Is the timer paused?
|
|
169
|
+
*/
|
|
170
|
+
get paused() {
|
|
171
|
+
return this.state.paused;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
175
|
+
*/
|
|
176
|
+
get trace() {
|
|
177
|
+
return globalThis._oscarpalmer_timer_debug ?? false
|
|
178
|
+
? this.state.trace
|
|
179
|
+
: undefined;
|
|
180
|
+
}
|
|
181
|
+
constructor(type, worker, state, options, start) {
|
|
182
|
+
this.worker = worker;
|
|
183
|
+
this.options = options;
|
|
184
|
+
this.$timer = type;
|
|
185
|
+
this.state = {
|
|
186
|
+
...state,
|
|
187
|
+
active: false,
|
|
188
|
+
destroyed: false,
|
|
189
|
+
elapsed: 0,
|
|
190
|
+
frame: undefined,
|
|
191
|
+
index: 0,
|
|
192
|
+
paused: false,
|
|
193
|
+
total: 0,
|
|
194
|
+
};
|
|
195
|
+
if (start) {
|
|
196
|
+
this.start();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Continue running the timer _(if it's paused)_
|
|
201
|
+
*/
|
|
202
|
+
continue() {
|
|
203
|
+
return this.#work('continue');
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Destroy the timer
|
|
207
|
+
*/
|
|
208
|
+
destroy() {
|
|
209
|
+
this.state.destroyed = true;
|
|
210
|
+
this.#work('stop');
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Pause the timer _(if it's running)_
|
|
214
|
+
*/
|
|
215
|
+
pause() {
|
|
216
|
+
return this.#work('pause');
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Restart the timer _(or start it, if it's not running)_
|
|
220
|
+
*/
|
|
221
|
+
restart() {
|
|
222
|
+
return this.#work('restart');
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Start the timer _(if it's not running)_
|
|
226
|
+
*/
|
|
227
|
+
start() {
|
|
228
|
+
return this.#work('start');
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Stop the timer _(if it's running)_
|
|
232
|
+
*/
|
|
233
|
+
stop() {
|
|
234
|
+
return this.#work('stop');
|
|
235
|
+
}
|
|
236
|
+
#work(type) {
|
|
237
|
+
return this.worker(type, {
|
|
238
|
+
instance: this,
|
|
239
|
+
type: this.$timer,
|
|
240
|
+
}, this.state, this.options);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function finish(timer, state, options, success) {
|
|
245
|
+
activeTimers.delete(timer.instance);
|
|
246
|
+
state.active = false;
|
|
247
|
+
state.elapsed = 0;
|
|
248
|
+
state.frame = undefined;
|
|
249
|
+
if (timer.type === 'wait') {
|
|
250
|
+
state.callback();
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
options.onAfter?.(success);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function work(type, timer, state, options) {
|
|
257
|
+
if ((state.destroyed && type !== 'stop') ||
|
|
258
|
+
(state.active ? beginTypes.has(type) : endTypes.has(type))) {
|
|
259
|
+
return timer.instance;
|
|
260
|
+
}
|
|
261
|
+
const pausable = pauseTypes.has(type);
|
|
262
|
+
state.elapsed = pausable ? state.elapsed : 0;
|
|
263
|
+
state.index = pausable ? state.index : 0;
|
|
264
|
+
state.total = pausable ? state.total : 0;
|
|
265
|
+
if (endOrRestartTypes.has(type)) {
|
|
266
|
+
activeTimers.delete(timer.instance);
|
|
267
|
+
cancelAnimationFrame(state.frame);
|
|
268
|
+
if (type === 'stop') {
|
|
269
|
+
options.onAfter?.(false);
|
|
270
|
+
}
|
|
271
|
+
state.active = false;
|
|
272
|
+
state.frame = undefined;
|
|
273
|
+
state.paused = type === 'pause';
|
|
274
|
+
return type === 'restart'
|
|
275
|
+
? work('start', timer, state, options)
|
|
276
|
+
: timer.instance;
|
|
277
|
+
}
|
|
278
|
+
state.active = true;
|
|
279
|
+
state.paused = false;
|
|
280
|
+
let start;
|
|
281
|
+
function step(now) {
|
|
282
|
+
if (!state.active) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
start ??= now;
|
|
286
|
+
const difference = now - start;
|
|
287
|
+
state.elapsed += difference;
|
|
288
|
+
state.total += difference;
|
|
289
|
+
if (options.timeout > 0 && state.total >= options.timeout) {
|
|
290
|
+
options.onError?.();
|
|
291
|
+
finish(timer, state, options, false);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (options.interval === milliseconds ||
|
|
295
|
+
state.elapsed >= options.interval - 5) {
|
|
296
|
+
if (options.count > -1) {
|
|
297
|
+
state.callback(state.index);
|
|
298
|
+
}
|
|
299
|
+
start = now;
|
|
300
|
+
state.elapsed = 0;
|
|
301
|
+
state.index += 1;
|
|
302
|
+
if (options.count === -1 ||
|
|
303
|
+
(options.count > 0 && state.index >= options.count)) {
|
|
304
|
+
finish(timer, state, options, true);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
state.frame = requestAnimationFrame(step);
|
|
309
|
+
}
|
|
310
|
+
activeTimers.add(timer.instance);
|
|
311
|
+
state.frame = requestAnimationFrame(step);
|
|
312
|
+
return timer.instance;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Create a repeating timer
|
|
317
|
+
*/
|
|
318
|
+
function repeat(callback, options) {
|
|
319
|
+
return new Timer('repeat', work, {
|
|
320
|
+
callback: getCallback(callback),
|
|
321
|
+
trace: new TimerTrace().stack,
|
|
322
|
+
}, {
|
|
323
|
+
onAfter: getCallback(options?.onAfter),
|
|
324
|
+
onError: undefined,
|
|
325
|
+
count: getValidNumber(options?.count),
|
|
326
|
+
interval: getValidNumber(options?.interval, milliseconds),
|
|
327
|
+
timeout: 0,
|
|
328
|
+
}, true);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Create a waiting timer
|
|
333
|
+
* @param callback Callback to run when the timer has finished
|
|
334
|
+
* @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
|
|
335
|
+
*/
|
|
336
|
+
function wait(callback, time) {
|
|
337
|
+
return new Timer('wait', work, {
|
|
338
|
+
callback: getCallback(callback),
|
|
339
|
+
trace: new TimerTrace().stack,
|
|
340
|
+
}, {
|
|
341
|
+
onAfter: undefined,
|
|
342
|
+
onError: undefined,
|
|
343
|
+
count: -1,
|
|
344
|
+
interval: getValidNumber(time, milliseconds),
|
|
345
|
+
timeout: 0,
|
|
346
|
+
}, true);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
class When {
|
|
350
|
+
$timer = 'when';
|
|
351
|
+
state;
|
|
352
|
+
/**
|
|
353
|
+
* Is the timer active?
|
|
354
|
+
*/
|
|
355
|
+
get active() {
|
|
356
|
+
return this.state.timer?.active ?? false;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Is the timer destroyed?
|
|
360
|
+
*/
|
|
361
|
+
get destroyed() {
|
|
362
|
+
return this.state.timer == null;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Is the timer paused?
|
|
366
|
+
*/
|
|
367
|
+
get paused() {
|
|
368
|
+
return this.state.timer?.paused ?? false;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
372
|
+
*/
|
|
373
|
+
get trace() {
|
|
374
|
+
return globalThis._oscarpalmer_timer_debug ?? false
|
|
375
|
+
? this.state.timer?.trace
|
|
376
|
+
: undefined;
|
|
377
|
+
}
|
|
378
|
+
constructor(state) {
|
|
379
|
+
this.state = state;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Continues the timer _(if it was paused)_
|
|
383
|
+
*/
|
|
384
|
+
continue() {
|
|
385
|
+
this.state.timer?.continue();
|
|
386
|
+
return this;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Destroys the timer _(and stops it,if it was running)_
|
|
390
|
+
*/
|
|
391
|
+
destroy() {
|
|
392
|
+
this.state.timer?.destroy();
|
|
393
|
+
this.state.promise = undefined;
|
|
394
|
+
this.state.resolver = noop;
|
|
395
|
+
this.state.rejecter = noop;
|
|
396
|
+
this.state.timer = undefined;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Pauses the timer _(if it was running)_
|
|
400
|
+
*/
|
|
401
|
+
pause() {
|
|
402
|
+
this.state.timer?.pause();
|
|
403
|
+
return this;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Stops the timer _(if it was running)_
|
|
407
|
+
*/
|
|
408
|
+
stop() {
|
|
409
|
+
this.state.timer?.stop();
|
|
410
|
+
return this;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Starts the timer and returns a promise that resolves when the condition is met
|
|
414
|
+
*/
|
|
415
|
+
// biome-ignore lint/suspicious/noThenProperty: returning a promise-like object, so it's ok ;)
|
|
416
|
+
then(resolve, reject) {
|
|
417
|
+
if (this.state.timer == null || this.state?.started) {
|
|
418
|
+
throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
|
|
419
|
+
}
|
|
420
|
+
this.state.started = true;
|
|
421
|
+
this.state.timer.start();
|
|
422
|
+
return this.state.promise.then(resolve ?? noop, reject ?? noop);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Create a conditional timer
|
|
427
|
+
*/
|
|
428
|
+
function when(condition, options) {
|
|
429
|
+
let called = false;
|
|
430
|
+
let result = false;
|
|
431
|
+
const state = {
|
|
432
|
+
started: false,
|
|
433
|
+
timer: new Timer('when', work, {
|
|
434
|
+
callback() {
|
|
435
|
+
if (condition()) {
|
|
436
|
+
result = true;
|
|
437
|
+
state.timer.stop();
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
trace: new TimerTrace().stack,
|
|
441
|
+
}, {
|
|
442
|
+
onAfter() {
|
|
443
|
+
if (!(state.timer?.paused ?? false) && !called) {
|
|
444
|
+
called = true;
|
|
445
|
+
if (result) {
|
|
446
|
+
state.resolver?.();
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
state.rejecter?.();
|
|
450
|
+
}
|
|
451
|
+
instance.destroy();
|
|
452
|
+
}
|
|
453
|
+
},
|
|
454
|
+
onError() {
|
|
455
|
+
state.rejecter?.();
|
|
456
|
+
instance.destroy();
|
|
457
|
+
},
|
|
458
|
+
count: getValidNumber(options?.count),
|
|
459
|
+
interval: getValidNumber(options?.interval, milliseconds),
|
|
460
|
+
timeout: getValidNumber(options?.timeout),
|
|
461
|
+
}, false),
|
|
462
|
+
};
|
|
463
|
+
const promise = new Promise((resolve, reject) => {
|
|
464
|
+
state.resolver = resolve;
|
|
465
|
+
state.rejecter = reject;
|
|
466
|
+
});
|
|
467
|
+
state.promise = promise;
|
|
468
|
+
const instance = new When(state);
|
|
469
|
+
return instance;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
export { delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
|
package/dist/timer.js
CHANGED
|
@@ -1,111 +1,95 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
constructor(type, state) {
|
|
1
|
+
class Timer {
|
|
2
|
+
constructor(type, worker, state, options, start) {
|
|
3
|
+
this.worker = worker;
|
|
4
|
+
this.options = options;
|
|
6
5
|
this.$timer = type;
|
|
7
|
-
this.state =
|
|
6
|
+
this.state = {
|
|
7
|
+
...state,
|
|
8
|
+
active: false,
|
|
9
|
+
destroyed: false,
|
|
10
|
+
elapsed: 0,
|
|
11
|
+
frame: void 0,
|
|
12
|
+
index: 0,
|
|
13
|
+
paused: false,
|
|
14
|
+
total: 0
|
|
15
|
+
};
|
|
16
|
+
if (start) {
|
|
17
|
+
this.start();
|
|
18
|
+
}
|
|
8
19
|
}
|
|
9
|
-
|
|
10
|
-
|
|
20
|
+
state;
|
|
21
|
+
/**
|
|
22
|
+
* Is the timer active?
|
|
23
|
+
*/
|
|
11
24
|
get active() {
|
|
12
25
|
return this.state.active;
|
|
13
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Is the timer destroyed?
|
|
29
|
+
*/
|
|
14
30
|
get destroyed() {
|
|
15
31
|
return this.state.destroyed;
|
|
16
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Is the timer paused?
|
|
35
|
+
*/
|
|
17
36
|
get paused() {
|
|
18
37
|
return this.state.paused;
|
|
19
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
41
|
+
*/
|
|
20
42
|
get trace() {
|
|
21
|
-
return globalThis._oscarpalmer_timer_debug ? this.state.trace : void 0;
|
|
22
|
-
}
|
|
23
|
-
constructor(type, state, options) {
|
|
24
|
-
super(type, state);
|
|
25
|
-
this.options = options;
|
|
43
|
+
return globalThis._oscarpalmer_timer_debug ?? false ? this.state.trace : void 0;
|
|
26
44
|
}
|
|
27
45
|
/**
|
|
28
|
-
*
|
|
46
|
+
* Continue running the timer _(if it's paused)_
|
|
29
47
|
*/
|
|
30
48
|
continue() {
|
|
31
|
-
return work("continue"
|
|
49
|
+
return this.#work("continue");
|
|
32
50
|
}
|
|
33
51
|
/**
|
|
34
|
-
*
|
|
52
|
+
* Destroy the timer
|
|
35
53
|
*/
|
|
36
54
|
destroy() {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this.stop();
|
|
40
|
-
this.options.afterCallback = void 0;
|
|
41
|
-
this.options.errorCallback = void 0;
|
|
42
|
-
this.state.callback = void 0;
|
|
43
|
-
this.state.trace = void 0;
|
|
44
|
-
}
|
|
55
|
+
this.state.destroyed = true;
|
|
56
|
+
this.#work("stop");
|
|
45
57
|
}
|
|
46
58
|
/**
|
|
47
|
-
*
|
|
59
|
+
* Pause the timer _(if it's running)_
|
|
48
60
|
*/
|
|
49
61
|
pause() {
|
|
50
|
-
return work("pause"
|
|
62
|
+
return this.#work("pause");
|
|
51
63
|
}
|
|
52
64
|
/**
|
|
53
|
-
*
|
|
65
|
+
* Restart the timer _(or start it, if it's not running)_
|
|
54
66
|
*/
|
|
55
67
|
restart() {
|
|
56
|
-
return work("restart"
|
|
68
|
+
return this.#work("restart");
|
|
57
69
|
}
|
|
58
70
|
/**
|
|
59
|
-
*
|
|
71
|
+
* Start the timer _(if it's not running)_
|
|
60
72
|
*/
|
|
61
73
|
start() {
|
|
62
|
-
return work("start"
|
|
74
|
+
return this.#work("start");
|
|
63
75
|
}
|
|
64
76
|
/**
|
|
65
|
-
*
|
|
77
|
+
* Stop the timer _(if it's running)_
|
|
66
78
|
*/
|
|
67
79
|
stop() {
|
|
68
|
-
return work("stop"
|
|
80
|
+
return this.#work("stop");
|
|
69
81
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
callback,
|
|
81
|
-
isRepeated,
|
|
82
|
-
active: false,
|
|
83
|
-
destroyed: false,
|
|
84
|
-
minimum: options.interval - options.interval % milliseconds / 2,
|
|
85
|
-
paused: false,
|
|
86
|
-
trace: new TimerTrace().stack
|
|
87
|
-
},
|
|
88
|
-
options
|
|
89
|
-
);
|
|
90
|
-
if (start) {
|
|
91
|
-
instance.start();
|
|
82
|
+
#work(type) {
|
|
83
|
+
return this.worker(
|
|
84
|
+
type,
|
|
85
|
+
{
|
|
86
|
+
instance: this,
|
|
87
|
+
type: this.$timer
|
|
88
|
+
},
|
|
89
|
+
this.state,
|
|
90
|
+
this.options
|
|
91
|
+
);
|
|
92
92
|
}
|
|
93
|
-
return instance;
|
|
94
|
-
}
|
|
95
|
-
function wait(callback, options) {
|
|
96
|
-
return timer(
|
|
97
|
-
"wait",
|
|
98
|
-
callback,
|
|
99
|
-
options == null || typeof options === "number" ? {
|
|
100
|
-
interval: options
|
|
101
|
-
} : options,
|
|
102
|
-
true
|
|
103
|
-
);
|
|
104
93
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
Timer,
|
|
108
|
-
repeat,
|
|
109
|
-
timer,
|
|
110
|
-
wait
|
|
111
|
-
};
|
|
94
|
+
|
|
95
|
+
export { Timer };
|
package/dist/wait.cjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const constants = require('./constants.cjs');
|
|
6
|
+
const get = require('./get.cjs');
|
|
7
|
+
const models = require('./models.cjs');
|
|
8
|
+
const timer = require('./timer.cjs');
|
|
9
|
+
const work = require('./work.cjs');
|
|
10
|
+
|
|
11
|
+
function wait(callback, time) {
|
|
12
|
+
return new timer.Timer(
|
|
13
|
+
"wait",
|
|
14
|
+
work.work,
|
|
15
|
+
{
|
|
16
|
+
callback: get.getCallback(callback),
|
|
17
|
+
trace: new models.TimerTrace().stack
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
onAfter: void 0,
|
|
21
|
+
onError: void 0,
|
|
22
|
+
count: -1,
|
|
23
|
+
interval: get.getValidNumber(time, constants.milliseconds),
|
|
24
|
+
timeout: 0
|
|
25
|
+
},
|
|
26
|
+
true
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
exports.wait = wait;
|
package/dist/wait.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { milliseconds } from './constants.js';
|
|
2
|
+
import { getCallback, getValidNumber } from './get.js';
|
|
3
|
+
import { TimerTrace } from './models.js';
|
|
4
|
+
import { Timer } from './timer.js';
|
|
5
|
+
import { work } from './work.js';
|
|
6
|
+
|
|
7
|
+
function wait(callback, time) {
|
|
8
|
+
return new Timer(
|
|
9
|
+
"wait",
|
|
10
|
+
work,
|
|
11
|
+
{
|
|
12
|
+
callback: getCallback(callback),
|
|
13
|
+
trace: new TimerTrace().stack
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
onAfter: void 0,
|
|
17
|
+
onError: void 0,
|
|
18
|
+
count: -1,
|
|
19
|
+
interval: getValidNumber(time, milliseconds),
|
|
20
|
+
timeout: 0
|
|
21
|
+
},
|
|
22
|
+
true
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { wait };
|