@oscarpalmer/timer 0.37.0 → 0.38.1

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