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