@oscarpalmer/timer 0.43.0 → 0.44.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.mts +4 -6
- package/dist/delay.d.mts +1 -2
- package/dist/get.d.mts +0 -1
- package/dist/global.d.mts +1 -2
- package/dist/index.d.mts +72 -94
- package/dist/index.mjs +149 -208
- package/dist/is.d.mts +1 -3
- package/dist/models.d.mts +87 -5
- package/dist/repeat.d.mts +3 -3
- package/dist/repeat.mjs +2 -2
- package/dist/timer.d.mts +3 -50
- package/dist/timer.mjs +60 -91
- package/dist/wait.d.mts +3 -2
- package/dist/wait.mjs +2 -2
- package/dist/when.d.mts +3 -46
- package/dist/when.mjs +89 -116
- package/dist/work.d.mts +1 -3
- package/dist/work.mjs +1 -1
- package/package.json +9 -5
- package/src/constants.ts +4 -5
- package/src/delay.ts +0 -1
- package/src/get.ts +1 -1
- package/src/global.ts +1 -1
- package/src/index.ts +5 -5
- package/src/is.ts +1 -2
- package/src/models.ts +105 -4
- package/src/repeat.ts +3 -5
- package/src/timer.ts +75 -119
- package/src/wait.ts +3 -5
- package/src/when.ts +112 -146
- package/src/work.ts +3 -4
package/dist/constants.d.mts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { Timer } from "./
|
|
2
|
-
import { TimerType, WorkHandlerType } from "./models.mjs";
|
|
3
|
-
|
|
1
|
+
import { Timer, TimerName, WorkHandlerType } from "./models.mjs";
|
|
4
2
|
//#region src/constants.d.ts
|
|
5
3
|
/**
|
|
6
4
|
* Buffer value to use when evaluating if a specific time is within a certain range
|
|
@@ -23,9 +21,9 @@ declare const TIMERS_ACTIVE: Set<Timer>;
|
|
|
23
21
|
* A set of timers that were paused due to the document being hidden
|
|
24
22
|
*/
|
|
25
23
|
declare const TIMERS_HIDDEN: Set<Timer>;
|
|
26
|
-
declare const TYPE_REPEAT:
|
|
27
|
-
declare const TYPE_WAIT:
|
|
28
|
-
declare const TYPE_WHEN:
|
|
24
|
+
declare const TYPE_REPEAT: TimerName;
|
|
25
|
+
declare const TYPE_WAIT: TimerName;
|
|
26
|
+
declare const TYPE_WHEN: TimerName;
|
|
29
27
|
declare const WORK_CONTINUE: WorkHandlerType;
|
|
30
28
|
declare const WORK_PAUSE: WorkHandlerType;
|
|
31
29
|
declare const WORK_RESTART: WorkHandlerType;
|
package/dist/delay.d.mts
CHANGED
package/dist/get.d.mts
CHANGED
package/dist/global.d.mts
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -24,49 +24,7 @@ type RepeatOptions = {
|
|
|
24
24
|
*/
|
|
25
25
|
timeout: number;
|
|
26
26
|
};
|
|
27
|
-
type
|
|
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;
|
|
27
|
+
type Timer = {
|
|
70
28
|
/**
|
|
71
29
|
* Is the timer active?
|
|
72
30
|
*/
|
|
@@ -83,7 +41,6 @@ declare class Timer {
|
|
|
83
41
|
* Get the timer's origin _(if debugging is enabled)_
|
|
84
42
|
*/
|
|
85
43
|
get trace(): string | undefined;
|
|
86
|
-
constructor(type: TimerType, state: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean);
|
|
87
44
|
/**
|
|
88
45
|
* Continue running the timer _(if it's paused)_
|
|
89
46
|
*/
|
|
@@ -108,50 +65,15 @@ declare class Timer {
|
|
|
108
65
|
* Stop the timer _(if it's running)_
|
|
109
66
|
*/
|
|
110
67
|
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
68
|
};
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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;
|
|
69
|
+
type TimerOptions = {
|
|
70
|
+
onAfter: ((finished: boolean) => void) | undefined;
|
|
71
|
+
onError: (() => void) | undefined;
|
|
72
|
+
count: number;
|
|
73
|
+
interval: number;
|
|
74
|
+
timeout: number;
|
|
75
|
+
};
|
|
76
|
+
type When = {
|
|
155
77
|
/**
|
|
156
78
|
* Is the timer active?
|
|
157
79
|
*/
|
|
@@ -168,7 +90,6 @@ declare class When {
|
|
|
168
90
|
* Get the timer's origin _(if debugging is enabled)_
|
|
169
91
|
*/
|
|
170
92
|
get trace(): string | undefined;
|
|
171
|
-
constructor(condition: () => boolean, options?: Partial<WhenOptions>);
|
|
172
93
|
/**
|
|
173
94
|
* Continues the timer _(if it was paused)_
|
|
174
95
|
*/
|
|
@@ -192,14 +113,62 @@ declare class When {
|
|
|
192
113
|
* Stops the timer _(if it was running)_
|
|
193
114
|
*/
|
|
194
115
|
stop(): When;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Options for a conditional timer
|
|
119
|
+
*/
|
|
120
|
+
type WhenOptions = {
|
|
121
|
+
/**
|
|
122
|
+
* How many times the timer should check the condition
|
|
123
|
+
*/
|
|
124
|
+
count: number;
|
|
125
|
+
/**
|
|
126
|
+
* Then interval between each condtional check
|
|
127
|
+
*/
|
|
128
|
+
interval: number;
|
|
129
|
+
/**
|
|
130
|
+
* The timeout for the timer _(any value above `0` will enable the timeout)_
|
|
131
|
+
*/
|
|
132
|
+
timeout: number;
|
|
133
|
+
};
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/global.d.ts
|
|
136
|
+
declare global {
|
|
137
|
+
var _oscarpalmer_timer_debug: boolean | undefined;
|
|
138
|
+
var _oscarpalmer_timers: Timer[] | undefined;
|
|
195
139
|
}
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/models.d.mts
|
|
196
142
|
/**
|
|
197
|
-
*
|
|
198
|
-
* @param condition Condition to check
|
|
199
|
-
* @param options Timer options
|
|
200
|
-
* @returns Timer instance
|
|
143
|
+
* Options for a _Promise_-handling function
|
|
201
144
|
*/
|
|
202
|
-
|
|
145
|
+
type PromiseOptions = {
|
|
146
|
+
/**
|
|
147
|
+
* AbortSignal for aborting the _Promise_; when aborted, the _Promise_ will reject with the reason of the signal
|
|
148
|
+
*/
|
|
149
|
+
signal?: AbortSignal;
|
|
150
|
+
/**
|
|
151
|
+
* How long to wait for _(in milliseconds; defaults to `0`)_
|
|
152
|
+
*/
|
|
153
|
+
time?: number;
|
|
154
|
+
};
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region node_modules/@oscarpalmer/atoms/dist/promise/delay.d.mts
|
|
157
|
+
//#region src/promise/delay.d.ts
|
|
158
|
+
/**
|
|
159
|
+
* Create a delayed promise that resolves after a certain amount of time, or rejects if aborted
|
|
160
|
+
*
|
|
161
|
+
* @param options Options for the delay
|
|
162
|
+
* @returns Delayed promise
|
|
163
|
+
*/
|
|
164
|
+
declare function delay(options?: PromiseOptions): Promise<void>;
|
|
165
|
+
/**
|
|
166
|
+
* Create a delayed promise that resolves after a certain amount of time
|
|
167
|
+
*
|
|
168
|
+
* @param time How long to wait for _(in milliseconds; defaults to `0`)_
|
|
169
|
+
* @returns Delayed promise
|
|
170
|
+
*/
|
|
171
|
+
declare function delay(time?: number): Promise<void>;
|
|
203
172
|
//#endregion
|
|
204
173
|
//#region src/is.d.ts
|
|
205
174
|
/**
|
|
@@ -246,4 +215,13 @@ declare function repeat(callback: (index: number) => void, options?: Partial<Rep
|
|
|
246
215
|
*/
|
|
247
216
|
declare function wait(callback: () => void, time?: number): Timer;
|
|
248
217
|
//#endregion
|
|
249
|
-
|
|
218
|
+
//#region src/when.d.ts
|
|
219
|
+
/**
|
|
220
|
+
* Create a conditional timer
|
|
221
|
+
* @param condition Condition to check
|
|
222
|
+
* @param options Timer options
|
|
223
|
+
* @returns Timer instance
|
|
224
|
+
*/
|
|
225
|
+
declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
|
|
226
|
+
//#endregion
|
|
227
|
+
export { type RepeatOptions, type Timer, type TimerOptions, type When, type WhenOptions, delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
|
package/dist/index.mjs
CHANGED
|
@@ -199,7 +199,7 @@ function finish(timer, state, options, success) {
|
|
|
199
199
|
state.active = false;
|
|
200
200
|
state.elapsed = 0;
|
|
201
201
|
state.frame = void 0;
|
|
202
|
-
if (timer.
|
|
202
|
+
if (timer.name === "wait") state.callback();
|
|
203
203
|
else options.onAfter?.(success);
|
|
204
204
|
}
|
|
205
205
|
function ignore(type, state) {
|
|
@@ -274,99 +274,67 @@ function work(type, timer, state, options) {
|
|
|
274
274
|
}
|
|
275
275
|
//#endregion
|
|
276
276
|
//#region src/timer.ts
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
state;
|
|
280
|
-
/**
|
|
281
|
-
* Is the timer active?
|
|
282
|
-
*/
|
|
283
|
-
get active() {
|
|
284
|
-
return this.state.active;
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Is the timer destroyed?
|
|
288
|
-
*/
|
|
289
|
-
get destroyed() {
|
|
290
|
-
return this.state.destroyed;
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* Is the timer paused?
|
|
294
|
-
*/
|
|
295
|
-
get paused() {
|
|
296
|
-
return this.state.paused;
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Get the timer's origin _(if debugging is enabled)_
|
|
300
|
-
*/
|
|
301
|
-
get trace() {
|
|
302
|
-
return globalThis._oscarpalmer_timer_debug ?? false ? this.state.trace : void 0;
|
|
303
|
-
}
|
|
304
|
-
constructor(type, state, options, start) {
|
|
305
|
-
this.options = options;
|
|
306
|
-
Object.defineProperty(this, "$timer", { value: type });
|
|
307
|
-
this.state = {
|
|
308
|
-
...state,
|
|
309
|
-
active: false,
|
|
310
|
-
destroyed: false,
|
|
311
|
-
elapsed: 0,
|
|
312
|
-
frame: void 0,
|
|
313
|
-
index: 0,
|
|
314
|
-
paused: false,
|
|
315
|
-
total: 0
|
|
316
|
-
};
|
|
317
|
-
if (start) this.start();
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Continue running the timer _(if it's paused)_
|
|
321
|
-
*/
|
|
322
|
-
continue() {
|
|
323
|
-
return this.#work(WORK_CONTINUE);
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Destroy the timer
|
|
327
|
-
*/
|
|
328
|
-
destroy() {
|
|
329
|
-
this.state.destroyed = true;
|
|
330
|
-
this.options.onAfter = noop;
|
|
331
|
-
this.options.onError = noop;
|
|
332
|
-
this.state.callback = noop;
|
|
333
|
-
if (!globalThis._oscarpalmer_timer_debug) this.state.trace = void 0;
|
|
334
|
-
stop({
|
|
335
|
-
instance: this,
|
|
336
|
-
type: this.$timer
|
|
337
|
-
}, this.state, this.options);
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Pause the timer _(if it's running)_
|
|
341
|
-
*/
|
|
342
|
-
pause() {
|
|
343
|
-
return this.#work(WORK_PAUSE);
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Restart the timer _(or start it, if it's not running)_
|
|
347
|
-
*/
|
|
348
|
-
restart() {
|
|
349
|
-
return this.#work(WORK_RESTART);
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Start the timer _(if it's not running)_
|
|
353
|
-
*/
|
|
354
|
-
start() {
|
|
355
|
-
return this.#work(WORK_START);
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* Stop the timer _(if it's running)_
|
|
359
|
-
*/
|
|
360
|
-
stop() {
|
|
361
|
-
return this.#work(WORK_STOP);
|
|
362
|
-
}
|
|
363
|
-
#work(type) {
|
|
277
|
+
function createTimer(name, pick, options, start) {
|
|
278
|
+
function worker(type) {
|
|
364
279
|
return work(type, {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
},
|
|
280
|
+
name,
|
|
281
|
+
instance
|
|
282
|
+
}, state, options);
|
|
368
283
|
}
|
|
369
|
-
|
|
284
|
+
const state = {
|
|
285
|
+
...pick,
|
|
286
|
+
active: false,
|
|
287
|
+
destroyed: false,
|
|
288
|
+
elapsed: 0,
|
|
289
|
+
frame: void 0,
|
|
290
|
+
index: 0,
|
|
291
|
+
paused: false,
|
|
292
|
+
total: 0
|
|
293
|
+
};
|
|
294
|
+
const instance = {
|
|
295
|
+
continue: () => worker(WORK_CONTINUE),
|
|
296
|
+
destroy: () => destroyTimer(name, instance, state, options),
|
|
297
|
+
pause: () => worker(WORK_PAUSE),
|
|
298
|
+
restart: () => worker(WORK_RESTART),
|
|
299
|
+
start: () => worker(WORK_START),
|
|
300
|
+
stop: () => worker(WORK_STOP)
|
|
301
|
+
};
|
|
302
|
+
Object.defineProperties(instance, {
|
|
303
|
+
$timer: {
|
|
304
|
+
enumerable: false,
|
|
305
|
+
value: name
|
|
306
|
+
},
|
|
307
|
+
active: {
|
|
308
|
+
enumerable: true,
|
|
309
|
+
get: () => state.active
|
|
310
|
+
},
|
|
311
|
+
destroyed: {
|
|
312
|
+
enumerable: true,
|
|
313
|
+
get: () => state.destroyed
|
|
314
|
+
},
|
|
315
|
+
paused: {
|
|
316
|
+
enumerable: true,
|
|
317
|
+
get: () => state.paused
|
|
318
|
+
},
|
|
319
|
+
trace: {
|
|
320
|
+
enumerable: true,
|
|
321
|
+
get: () => globalThis._oscarpalmer_timer_debug ?? false ? state.trace : void 0
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
if (start) instance.start();
|
|
325
|
+
return Object.freeze(instance);
|
|
326
|
+
}
|
|
327
|
+
function destroyTimer(name, instance, state, options) {
|
|
328
|
+
state.destroyed = true;
|
|
329
|
+
options.onAfter = noop;
|
|
330
|
+
options.onError = noop;
|
|
331
|
+
state.callback = noop;
|
|
332
|
+
if (!globalThis._oscarpalmer_timer_debug) state.trace = void 0;
|
|
333
|
+
stop({
|
|
334
|
+
instance,
|
|
335
|
+
name
|
|
336
|
+
}, state, options);
|
|
337
|
+
}
|
|
370
338
|
//#endregion
|
|
371
339
|
//#region src/repeat.ts
|
|
372
340
|
/**
|
|
@@ -377,7 +345,7 @@ var Timer = class {
|
|
|
377
345
|
* @returns Timer instance
|
|
378
346
|
*/
|
|
379
347
|
function repeat(callback, options) {
|
|
380
|
-
return
|
|
348
|
+
return createTimer(TYPE_REPEAT, {
|
|
381
349
|
callback: getCallback(callback),
|
|
382
350
|
trace: new TimerTrace().stack
|
|
383
351
|
}, {
|
|
@@ -397,7 +365,7 @@ function repeat(callback, options) {
|
|
|
397
365
|
* @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
|
|
398
366
|
*/
|
|
399
367
|
function wait(callback, time) {
|
|
400
|
-
return
|
|
368
|
+
return createTimer(TYPE_WAIT, {
|
|
401
369
|
callback: getCallback(callback),
|
|
402
370
|
trace: new TimerTrace().stack
|
|
403
371
|
}, {
|
|
@@ -410,120 +378,43 @@ function wait(callback, time) {
|
|
|
410
378
|
}
|
|
411
379
|
//#endregion
|
|
412
380
|
//#region src/when.ts
|
|
413
|
-
|
|
414
|
-
state
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
* Is the timer paused?
|
|
435
|
-
*/
|
|
436
|
-
get paused() {
|
|
437
|
-
return this.state.timer?.paused ?? false;
|
|
438
|
-
}
|
|
439
|
-
/**
|
|
440
|
-
* Get the timer's origin _(if debugging is enabled)_
|
|
441
|
-
*/
|
|
442
|
-
get trace() {
|
|
443
|
-
return globalThis._oscarpalmer_timer_debug ?? false ? this.state.timer?.trace : void 0;
|
|
444
|
-
}
|
|
445
|
-
constructor(condition, options) {
|
|
446
|
-
Object.defineProperty(this, "$timer", { value: TYPE_WHEN });
|
|
447
|
-
const { state } = this;
|
|
448
|
-
state.promise = new Promise((resolve, reject) => {
|
|
449
|
-
state.resolver = resolve;
|
|
450
|
-
state.rejecter = reject;
|
|
451
|
-
});
|
|
452
|
-
let result = false;
|
|
453
|
-
this.state.timer = new Timer(TYPE_WHEN, {
|
|
454
|
-
callback() {
|
|
455
|
-
try {
|
|
456
|
-
if (condition()) {
|
|
457
|
-
result = true;
|
|
458
|
-
state.timer.stop();
|
|
459
|
-
}
|
|
460
|
-
} catch {
|
|
461
|
-
state.timer.stop();
|
|
462
|
-
}
|
|
463
|
-
},
|
|
464
|
-
trace: new TimerTrace().stack
|
|
465
|
-
}, {
|
|
466
|
-
onAfter: () => {
|
|
467
|
-
if (result) state.resolver?.();
|
|
468
|
-
else state.rejecter?.();
|
|
469
|
-
this.destroy();
|
|
470
|
-
},
|
|
471
|
-
onError: () => {
|
|
472
|
-
state.rejecter?.();
|
|
473
|
-
this.destroy();
|
|
474
|
-
},
|
|
475
|
-
count: getValidNumber(options?.count),
|
|
476
|
-
interval: getValidNumber(options?.interval),
|
|
477
|
-
timeout: getValidTimeout(options?.timeout)
|
|
478
|
-
}, false);
|
|
479
|
-
}
|
|
480
|
-
/**
|
|
481
|
-
* Continues the timer _(if it was paused)_
|
|
482
|
-
*/
|
|
483
|
-
continue() {
|
|
484
|
-
this.state.timer?.continue();
|
|
485
|
-
return this;
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* Destroys the timer _(and stops it,if it was running)_
|
|
489
|
-
*/
|
|
490
|
-
destroy() {
|
|
491
|
-
const { state } = this;
|
|
492
|
-
state.timer?.destroy();
|
|
493
|
-
state.promise = void 0;
|
|
494
|
-
state.resolver = noop;
|
|
495
|
-
state.rejecter = noop;
|
|
496
|
-
state.timer = void 0;
|
|
497
|
-
}
|
|
498
|
-
/**
|
|
499
|
-
* Pauses the timer _(if it was running)_
|
|
500
|
-
*/
|
|
501
|
-
pause() {
|
|
502
|
-
this.state.timer?.pause();
|
|
503
|
-
return this;
|
|
504
|
-
}
|
|
505
|
-
/**
|
|
506
|
-
* Start the timer
|
|
507
|
-
*
|
|
508
|
-
* @param resolve Optional resolve callback
|
|
509
|
-
* @returns Promise that resolves when the condition is met
|
|
510
|
-
*/
|
|
511
|
-
start(resolve) {
|
|
512
|
-
const { state } = this;
|
|
513
|
-
if (state.timer == null) throw new Error(MESSAGE_DESTROYED);
|
|
514
|
-
if (state.started) throw new Error(MESSAGE_STARTED);
|
|
515
|
-
state.started = true;
|
|
516
|
-
state.timer.start();
|
|
517
|
-
return state.promise.then(resolve);
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* Stops the timer _(if it was running)_
|
|
521
|
-
*/
|
|
522
|
-
stop() {
|
|
523
|
-
this.state.timer?.stop();
|
|
524
|
-
return this;
|
|
381
|
+
function destroyWhen(state) {
|
|
382
|
+
state.timer?.destroy();
|
|
383
|
+
state.promise = void 0;
|
|
384
|
+
state.resolver = noop;
|
|
385
|
+
state.rejecter = noop;
|
|
386
|
+
state.timer = void 0;
|
|
387
|
+
}
|
|
388
|
+
function onAfter(instance, state) {
|
|
389
|
+
if (state.result) state.resolver?.();
|
|
390
|
+
else state.rejecter?.();
|
|
391
|
+
instance.destroy();
|
|
392
|
+
}
|
|
393
|
+
function onCallback(condition, state) {
|
|
394
|
+
try {
|
|
395
|
+
if (condition()) {
|
|
396
|
+
state.result = true;
|
|
397
|
+
state.timer.stop();
|
|
398
|
+
}
|
|
399
|
+
} catch {
|
|
400
|
+
state.timer.stop();
|
|
525
401
|
}
|
|
526
|
-
}
|
|
402
|
+
}
|
|
403
|
+
function onError(instance, state) {
|
|
404
|
+
state.rejecter?.();
|
|
405
|
+
instance.destroy();
|
|
406
|
+
}
|
|
407
|
+
function onWhen(type, instance, state) {
|
|
408
|
+
state.timer?.[type]?.();
|
|
409
|
+
return instance;
|
|
410
|
+
}
|
|
411
|
+
function startWhen(state, resolve) {
|
|
412
|
+
if (state.timer == null) throw new Error(MESSAGE_DESTROYED);
|
|
413
|
+
if (state.started) throw new Error(MESSAGE_STARTED);
|
|
414
|
+
state.started = true;
|
|
415
|
+
state.timer.start();
|
|
416
|
+
return state.promise.then(resolve);
|
|
417
|
+
}
|
|
527
418
|
/**
|
|
528
419
|
* Create a conditional timer
|
|
529
420
|
* @param condition Condition to check
|
|
@@ -531,7 +422,57 @@ var When = class {
|
|
|
531
422
|
* @returns Timer instance
|
|
532
423
|
*/
|
|
533
424
|
function when(condition, options) {
|
|
534
|
-
|
|
425
|
+
const state = {
|
|
426
|
+
promise: void 0,
|
|
427
|
+
result: false,
|
|
428
|
+
started: false,
|
|
429
|
+
timer: void 0
|
|
430
|
+
};
|
|
431
|
+
let instance;
|
|
432
|
+
state.promise = new Promise((resolve, reject) => {
|
|
433
|
+
state.resolver = resolve;
|
|
434
|
+
state.rejecter = reject;
|
|
435
|
+
});
|
|
436
|
+
state.timer = createTimer(TYPE_WHEN, {
|
|
437
|
+
callback: () => onCallback(condition, state),
|
|
438
|
+
trace: new TimerTrace().stack
|
|
439
|
+
}, {
|
|
440
|
+
onAfter: () => onAfter(instance, state),
|
|
441
|
+
onError: () => onError(instance, state),
|
|
442
|
+
count: getValidNumber(options?.count),
|
|
443
|
+
interval: getValidNumber(options?.interval),
|
|
444
|
+
timeout: getValidTimeout(options?.timeout)
|
|
445
|
+
}, false);
|
|
446
|
+
instance = {
|
|
447
|
+
continue: () => onWhen(WORK_CONTINUE, instance, state),
|
|
448
|
+
destroy: () => destroyWhen(state),
|
|
449
|
+
pause: () => onWhen("pause", instance, state),
|
|
450
|
+
start: (resolve) => startWhen(state, resolve),
|
|
451
|
+
stop: () => onWhen("stop", instance, state)
|
|
452
|
+
};
|
|
453
|
+
Object.defineProperties(instance, {
|
|
454
|
+
$timer: {
|
|
455
|
+
enumerable: false,
|
|
456
|
+
value: TYPE_WHEN
|
|
457
|
+
},
|
|
458
|
+
active: {
|
|
459
|
+
enumerable: true,
|
|
460
|
+
get: () => state.timer?.active ?? false
|
|
461
|
+
},
|
|
462
|
+
destroyed: {
|
|
463
|
+
enumerable: true,
|
|
464
|
+
get: () => state.timer == null
|
|
465
|
+
},
|
|
466
|
+
paused: {
|
|
467
|
+
enumerable: true,
|
|
468
|
+
get: () => state.timer?.paused ?? false
|
|
469
|
+
},
|
|
470
|
+
trace: {
|
|
471
|
+
enumerable: true,
|
|
472
|
+
get: () => globalThis._oscarpalmer_timer_debug ?? false ? state.timer?.trace : void 0
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
return Object.freeze(instance);
|
|
535
476
|
}
|
|
536
477
|
//#endregion
|
|
537
478
|
export { delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
|