@oscarpalmer/timer 0.25.0 → 0.26.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.
Files changed (46) hide show
  1. package/dist/constants.cjs +18 -0
  2. package/dist/constants.js +11 -12
  3. package/dist/{functions.mjs → functions.cjs} +35 -34
  4. package/dist/functions.js +25 -29
  5. package/dist/global.cjs +9 -0
  6. package/dist/global.js +1 -13
  7. package/dist/index.cjs +47 -0
  8. package/dist/index.js +30 -324
  9. package/dist/{is.mjs → is.cjs} +7 -8
  10. package/dist/is.js +4 -5
  11. package/dist/models.cjs +9 -0
  12. package/dist/{models.mjs → models.js} +0 -1
  13. package/dist/timer.cjs +111 -0
  14. package/dist/timer.js +64 -145
  15. package/dist/when.cjs +122 -0
  16. package/dist/when.js +86 -234
  17. package/package.json +19 -20
  18. package/src/constants.ts +2 -2
  19. package/src/functions.ts +3 -3
  20. package/src/global.ts +2 -2
  21. package/src/index.ts +4 -3
  22. package/src/is.ts +2 -2
  23. package/src/models.ts +1 -1
  24. package/src/timer.ts +3 -3
  25. package/src/when.ts +2 -2
  26. package/types/constants.d.cts +138 -0
  27. package/types/constants.d.ts +2 -2
  28. package/types/functions.d.cts +117 -0
  29. package/types/functions.d.ts +2 -2
  30. package/types/global.d.cts +3 -0
  31. package/types/global.d.ts +1 -1
  32. package/types/index.d.cts +78 -82
  33. package/types/index.d.ts +1 -1
  34. package/types/is.d.cts +163 -0
  35. package/types/is.d.ts +2 -2
  36. package/types/models.d.cts +123 -0
  37. package/types/models.d.ts +1 -1
  38. package/types/timer.d.cts +142 -0
  39. package/types/timer.d.ts +1 -1
  40. package/types/when.d.cts +153 -0
  41. package/types/when.d.ts +3 -3
  42. package/dist/constants.mjs +0 -19
  43. package/dist/global.mjs +0 -9
  44. package/dist/index.mjs +0 -46
  45. package/dist/timer.mjs +0 -89
  46. package/dist/when.mjs +0 -91
package/dist/index.js CHANGED
@@ -1,342 +1,48 @@
1
- // node_modules/@oscarpalmer/atoms/dist/js/function.mjs
2
- function noop() {
3
- }
4
-
5
- // src/constants.ts
6
- var activeTimers = new Set;
7
- var beginTypes = new Set(["continue", "start"]);
8
- var endTypes = new Set(["pause", "stop"]);
9
- var endOrRestartTypes = new Set([
10
- "pause",
11
- "restart",
12
- "stop"
13
- ]);
14
- var hiddenTimers = new Set;
15
- var milliseconds = 1000 / 60;
16
-
17
- // src/global.ts
18
- if (globalThis._oscarpalmer_timers == null) {
19
- Object.defineProperty(globalThis, "_oscarpalmer_timers", {
20
- get() {
21
- return globalThis._oscarpalmer_timer_debug ? [...activeTimers] : [];
22
- }
23
- });
24
- }
25
-
26
- // src/functions.ts
27
- function getOptions(options, isRepeated) {
28
- return {
29
- afterCallback: options.afterCallback,
30
- count: getValueOrDefault(options.count, isRepeated ? Number.POSITIVE_INFINITY : 1),
31
- errorCallback: options.errorCallback,
32
- interval: getValueOrDefault(options.interval, milliseconds, milliseconds),
33
- timeout: getValueOrDefault(options.timeout, isRepeated ? Number.POSITIVE_INFINITY : 30000)
34
- };
35
- }
36
- function getValueOrDefault(value, defaultValue, minimum) {
37
- return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
38
- }
39
- function work(type, timer, state, options) {
40
- if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
41
- return timer;
42
- }
43
- const { count, interval, timeout } = options;
44
- const { isRepeated, minimum } = state;
45
- if (endOrRestartTypes.has(type)) {
46
- const isStop = type === "stop";
47
- activeTimers.delete(timer);
48
- cancelAnimationFrame(state.frame);
49
- if (isStop) {
50
- options.afterCallback?.(false);
51
- }
52
- state.active = false;
53
- state.frame = undefined;
54
- state.paused = !isStop;
55
- if (isStop) {
56
- state.elapsed = undefined;
57
- state.index = undefined;
58
- }
59
- return type === "restart" ? work("start", timer, state, options) : timer;
60
- }
61
- state.active = true;
62
- state.paused = false;
63
- const elapsed = type === "continue" ? +(state.elapsed ?? 0) : 0;
64
- let index = type === "continue" ? +(state.index ?? 0) : 0;
65
- state.elapsed = elapsed;
66
- state.index = index;
67
- const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
68
- let current;
69
- let start;
70
- function finish(finished, error) {
71
- activeTimers.delete(timer);
72
- state.active = false;
73
- state.elapsed = undefined;
74
- state.frame = undefined;
75
- state.index = undefined;
76
- if (error) {
77
- options.errorCallback?.();
78
- }
79
- options.afterCallback?.(finished);
80
- }
81
- function step(timestamp) {
82
- if (!state.active) {
83
- return;
84
- }
85
- current ??= timestamp;
86
- start ??= timestamp;
87
- const time = timestamp - current;
88
- state.elapsed = elapsed + (current - start);
89
- const finished = time - elapsed >= total;
90
- if (timestamp - start >= timeout - elapsed) {
91
- finish(finished, !finished);
92
- return;
93
- }
94
- if (finished || time >= minimum) {
95
- if (state.active) {
96
- state.callback(isRepeated ? index : undefined);
97
- }
98
- index += 1;
99
- state.index = index;
100
- if (!finished && index < count) {
101
- current = null;
102
- } else {
103
- finish(true, false);
104
- return;
105
- }
106
- }
107
- state.frame = requestAnimationFrame(step);
108
- }
109
- activeTimers.add(timer);
110
- state.frame = requestAnimationFrame(step);
111
- return timer;
112
- }
113
-
114
- // src/models.ts
115
- class TimerTrace extends Error {
116
- constructor() {
117
- super();
118
- this.name = "TimerTrace";
119
- }
120
- }
121
-
122
- // src/timer.ts
123
- function repeat(callback, options) {
124
- return timer("repeat", callback, options ?? {}, true);
125
- }
126
- function timer(type, callback, partial, start) {
127
- const isRepeated = type === "repeat";
128
- const options = getOptions(partial, isRepeated);
129
- const instance = new Timer(type, {
130
- callback,
131
- isRepeated,
132
- active: false,
133
- destroyed: false,
134
- minimum: options.interval - options.interval % milliseconds / 2,
135
- paused: false,
136
- trace: new TimerTrace
137
- }, options);
138
- if (start) {
139
- instance.start();
140
- }
141
- return instance;
142
- }
143
- function wait(callback, options) {
144
- return timer("wait", callback, options == null || typeof options === "number" ? {
145
- interval: options
146
- } : options, true);
147
- }
148
-
149
- class BasicTimer {
150
- constructor(type, state) {
151
- this.$timer = type;
152
- this.state = state;
153
- }
154
- }
155
-
156
- class Timer extends BasicTimer {
157
- get active() {
158
- return this.state.active;
159
- }
160
- get destroyed() {
161
- return this.state.destroyed;
162
- }
163
- get paused() {
164
- return this.state.paused;
165
- }
166
- get trace() {
167
- return globalThis._oscarpalmer_timer_debug ? this.state.trace : undefined;
168
- }
169
- constructor(type, state, options) {
170
- super(type, state);
171
- this.options = options;
172
- }
173
- continue() {
174
- return work("continue", this, this.state, this.options);
175
- }
176
- destroy() {
177
- if (!this.state.destroyed) {
178
- this.state.destroyed = true;
179
- this.stop();
180
- this.options.afterCallback = undefined;
181
- this.options.errorCallback = undefined;
182
- this.state.callback = undefined;
183
- this.state.trace = undefined;
184
- }
185
- }
186
- pause() {
187
- return work("pause", this, this.state, this.options);
188
- }
189
- restart() {
190
- return work("restart", this, this.state, this.options);
191
- }
192
- start() {
193
- return work("start", this, this.state, this.options);
194
- }
195
- stop() {
196
- return work("stop", this, this.state, this.options);
197
- }
198
- }
199
-
200
- // src/index.ts
1
+ import { activeTimers, hiddenTimers } from "./constants.js";
2
+ import "./global.js";
3
+ import { noop } from "@oscarpalmer/atoms/function";
4
+ import { wait } from "./timer.js";
5
+ import { repeat } from "./timer.js";
6
+ import { isRepeated, isTimer, isWaited, isWhen } from "./is.js";
7
+ import { when } from "./when.js";
201
8
  function delay(time, timeout) {
202
9
  return new Promise((resolve, reject) => {
203
- const delayed = wait(() => {
204
- delayed.destroy();
205
- (resolve ?? noop)();
206
- }, {
207
- timeout,
208
- errorCallback: () => {
10
+ const delayed = wait(
11
+ () => {
209
12
  delayed.destroy();
210
- (reject ?? noop)();
13
+ (resolve ?? noop)();
211
14
  },
212
- interval: time
213
- });
214
- });
215
- }
216
-
217
- // src/is.ts
218
- function is(pattern, value) {
219
- return pattern.test(value?.$timer);
220
- }
221
- function isRepeated(value) {
222
- return is(/^repeat$/, value);
223
- }
224
- function isTimer(value) {
225
- return is(/^repeat|wait$/, value);
226
- }
227
- function isWaited(value) {
228
- return is(/^wait$/, value);
229
- }
230
- function isWhen(value) {
231
- return is(/^when$/, value) && typeof value.then === "function";
232
- }
233
- // src/when.ts
234
- function when(condition, options) {
235
- let result = false;
236
- const state = {
237
- started: false,
238
- timer: timer("repeat", () => {
239
- if (condition()) {
240
- result = true;
241
- state.timer.stop();
15
+ {
16
+ timeout,
17
+ errorCallback: () => {
18
+ delayed.destroy();
19
+ (reject ?? noop)();
20
+ },
21
+ interval: time
242
22
  }
243
- }, {
244
- afterCallback() {
245
- if (!state.timer.paused) {
246
- if (result) {
247
- state.resolver?.();
248
- } else {
249
- state.rejecter?.();
250
- }
251
- instance.destroy();
252
- }
253
- },
254
- errorCallback() {
255
- state.rejecter?.();
256
- instance.destroy();
257
- },
258
- count: options?.count,
259
- interval: options?.interval,
260
- timeout: options?.timeout
261
- }, false)
262
- };
263
- const promise = new Promise((resolve, reject) => {
264
- state.resolver = resolve;
265
- state.rejecter = reject;
23
+ );
266
24
  });
267
- state.promise = promise;
268
- const instance = new When(state);
269
- return instance;
270
25
  }
271
- var destroyedMessage = "Timer has already been destroyed";
272
- var startedMessage = "Timer has already been started";
273
-
274
- class When extends BasicTimer {
275
- get active() {
276
- return this.state.timer?.active ?? false;
277
- }
278
- get destroyed() {
279
- return this.state.timer == null;
280
- }
281
- get paused() {
282
- return this.state.timer?.paused ?? false;
283
- }
284
- get trace() {
285
- return this.state.timer?.trace;
286
- }
287
- constructor(state) {
288
- super("when", state);
289
- }
290
- continue() {
291
- this.state.timer?.continue();
292
- return this;
293
- }
294
- destroy() {
295
- this.state.timer?.destroy();
296
- this.state.promise = undefined;
297
- this.state.resolver = noop;
298
- this.state.rejecter = noop;
299
- this.state.timer = undefined;
300
- }
301
- pause() {
302
- this.state.timer?.pause();
303
- return this;
304
- }
305
- stop() {
306
- this.state.timer?.stop();
307
- return this;
308
- }
309
- then(resolve, reject) {
310
- if (this.state.timer == null || this.state?.started) {
311
- throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
312
- }
313
- this.state.started = true;
314
- this.state.timer.start();
315
- return this.state.promise.then(resolve ?? noop, reject ?? noop);
316
- }
317
- }
318
-
319
- // src/index.ts
320
26
  document.addEventListener("visibilitychange", () => {
321
27
  if (document.hidden) {
322
- for (const timer2 of activeTimers) {
323
- hiddenTimers.add(timer2);
324
- timer2.pause();
28
+ for (const timer of activeTimers) {
29
+ hiddenTimers.add(timer);
30
+ timer.pause();
325
31
  }
326
32
  } else {
327
- for (const timer2 of hiddenTimers) {
328
- timer2.continue();
33
+ for (const timer of hiddenTimers) {
34
+ timer.continue();
329
35
  }
330
36
  hiddenTimers.clear();
331
37
  }
332
38
  });
333
39
  export {
334
- when,
335
- wait,
336
- repeat,
337
- isWhen,
338
- isWaited,
339
- isTimer,
40
+ delay,
340
41
  isRepeated,
341
- delay
42
+ isTimer,
43
+ isWaited,
44
+ isWhen,
45
+ repeat,
46
+ wait,
47
+ when
342
48
  };
@@ -1,6 +1,7 @@
1
- // src/is.ts
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
3
  function is(pattern, value) {
3
- return pattern.test(value?.$timer);
4
+ return pattern.test(value == null ? void 0 : value.$timer);
4
5
  }
5
6
  function isRepeated(value) {
6
7
  return is(/^repeat$/, value);
@@ -14,9 +15,7 @@ function isWaited(value) {
14
15
  function isWhen(value) {
15
16
  return is(/^when$/, value) && typeof value.then === "function";
16
17
  }
17
- export {
18
- isWhen,
19
- isWaited,
20
- isTimer,
21
- isRepeated
22
- };
18
+ exports.isRepeated = isRepeated;
19
+ exports.isTimer = isTimer;
20
+ exports.isWaited = isWaited;
21
+ exports.isWhen = isWhen;
package/dist/is.js CHANGED
@@ -1,6 +1,5 @@
1
- // src/is.ts
2
1
  function is(pattern, value) {
3
- return pattern.test(value?.$timer);
2
+ return pattern.test(value == null ? void 0 : value.$timer);
4
3
  }
5
4
  function isRepeated(value) {
6
5
  return is(/^repeat$/, value);
@@ -15,8 +14,8 @@ function isWhen(value) {
15
14
  return is(/^when$/, value) && typeof value.then === "function";
16
15
  }
17
16
  export {
18
- isWhen,
19
- isWaited,
17
+ isRepeated,
20
18
  isTimer,
21
- isRepeated
19
+ isWaited,
20
+ isWhen
22
21
  };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ class TimerTrace extends Error {
4
+ constructor() {
5
+ super();
6
+ this.name = "TimerTrace";
7
+ }
8
+ }
9
+ exports.TimerTrace = TimerTrace;
@@ -1,4 +1,3 @@
1
- // src/models.ts
2
1
  class TimerTrace extends Error {
3
2
  constructor() {
4
3
  super();
package/dist/timer.cjs ADDED
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const constants = require("./constants.cjs");
4
+ const functions = require("./functions.cjs");
5
+ const models = require("./models.cjs");
6
+ class BasicTimer {
7
+ constructor(type, state) {
8
+ this.$timer = type;
9
+ this.state = state;
10
+ }
11
+ }
12
+ class Timer extends BasicTimer {
13
+ get active() {
14
+ return this.state.active;
15
+ }
16
+ get destroyed() {
17
+ return this.state.destroyed;
18
+ }
19
+ get paused() {
20
+ return this.state.paused;
21
+ }
22
+ get trace() {
23
+ return globalThis._oscarpalmer_timer_debug ? this.state.trace : void 0;
24
+ }
25
+ constructor(type, state, options) {
26
+ super(type, state);
27
+ this.options = options;
28
+ }
29
+ /**
30
+ * Continues the timer _(if it was paused)_
31
+ */
32
+ continue() {
33
+ return functions.work("continue", this, this.state, this.options);
34
+ }
35
+ /**
36
+ * Destroys the timer _(after stopping it, if it was running)_
37
+ */
38
+ destroy() {
39
+ if (!this.state.destroyed) {
40
+ this.state.destroyed = true;
41
+ this.stop();
42
+ this.options.afterCallback = void 0;
43
+ this.options.errorCallback = void 0;
44
+ this.state.callback = void 0;
45
+ this.state.trace = void 0;
46
+ }
47
+ }
48
+ /**
49
+ * Pauses the timer _(if it was running)_
50
+ */
51
+ pause() {
52
+ return functions.work("pause", this, this.state, this.options);
53
+ }
54
+ /**
55
+ * Restarts the timer _(if it was running)_
56
+ */
57
+ restart() {
58
+ return functions.work("restart", this, this.state, this.options);
59
+ }
60
+ /**
61
+ * Starts the timer _(if it was stopped)_
62
+ */
63
+ start() {
64
+ return functions.work("start", this, this.state, this.options);
65
+ }
66
+ /**
67
+ * Stops the timer _(if it was running)_
68
+ */
69
+ stop() {
70
+ return functions.work("stop", this, this.state, this.options);
71
+ }
72
+ }
73
+ function repeat(callback, options) {
74
+ return timer("repeat", callback, options ?? {}, true);
75
+ }
76
+ function timer(type, callback, partial, start) {
77
+ const isRepeated = type === "repeat";
78
+ const options = functions.getOptions(partial, isRepeated);
79
+ const instance = new Timer(
80
+ type,
81
+ {
82
+ callback,
83
+ isRepeated,
84
+ active: false,
85
+ destroyed: false,
86
+ minimum: options.interval - options.interval % constants.milliseconds / 2,
87
+ paused: false,
88
+ trace: new models.TimerTrace()
89
+ },
90
+ options
91
+ );
92
+ if (start) {
93
+ instance.start();
94
+ }
95
+ return instance;
96
+ }
97
+ function wait(callback, options) {
98
+ return timer(
99
+ "wait",
100
+ callback,
101
+ options == null || typeof options === "number" ? {
102
+ interval: options
103
+ } : options,
104
+ true
105
+ );
106
+ }
107
+ exports.BasicTimer = BasicTimer;
108
+ exports.Timer = Timer;
109
+ exports.repeat = repeat;
110
+ exports.timer = timer;
111
+ exports.wait = wait;