@oscarpalmer/timer 0.24.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 -1
  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/timer.js CHANGED
@@ -1,145 +1,12 @@
1
- // src/constants.ts
2
- var activeTimers = new Set;
3
- var beginTypes = new Set(["continue", "start"]);
4
- var endTypes = new Set(["pause", "stop"]);
5
- var endOrRestartTypes = new Set([
6
- "pause",
7
- "restart",
8
- "stop"
9
- ]);
10
- var hiddenTimers = new Set;
11
- var milliseconds = 1000 / 60;
12
-
13
- // src/functions.ts
14
- function getOptions(options, isRepeated) {
15
- return {
16
- afterCallback: options.afterCallback,
17
- count: getValueOrDefault(options.count, isRepeated ? Number.POSITIVE_INFINITY : 1),
18
- errorCallback: options.errorCallback,
19
- interval: getValueOrDefault(options.interval, milliseconds, milliseconds),
20
- timeout: getValueOrDefault(options.timeout, isRepeated ? Number.POSITIVE_INFINITY : 30000)
21
- };
22
- }
23
- function getValueOrDefault(value, defaultValue, minimum) {
24
- return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
25
- }
26
- function work(type, timer, state, options) {
27
- if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
28
- return timer;
29
- }
30
- const { count, interval, timeout } = options;
31
- const { isRepeated, minimum } = state;
32
- if (endOrRestartTypes.has(type)) {
33
- const isStop = type === "stop";
34
- activeTimers.delete(timer);
35
- cancelAnimationFrame(state.frame);
36
- if (isStop) {
37
- options.afterCallback?.(false);
38
- }
39
- state.active = false;
40
- state.frame = undefined;
41
- state.paused = !isStop;
42
- if (isStop) {
43
- state.elapsed = undefined;
44
- state.index = undefined;
45
- }
46
- return type === "restart" ? work("start", timer, state, options) : timer;
47
- }
48
- state.active = true;
49
- state.paused = false;
50
- const elapsed = type === "continue" ? +(state.elapsed ?? 0) : 0;
51
- let index = type === "continue" ? +(state.index ?? 0) : 0;
52
- state.elapsed = elapsed;
53
- state.index = index;
54
- const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
55
- let current;
56
- let start;
57
- function finish(finished, error) {
58
- activeTimers.delete(timer);
59
- state.active = false;
60
- state.elapsed = undefined;
61
- state.frame = undefined;
62
- state.index = undefined;
63
- if (error) {
64
- options.errorCallback?.();
65
- }
66
- options.afterCallback?.(finished);
67
- }
68
- function step(timestamp) {
69
- if (!state.active) {
70
- return;
71
- }
72
- current ??= timestamp;
73
- start ??= timestamp;
74
- const time = timestamp - current;
75
- state.elapsed = elapsed + (current - start);
76
- const finished = time - elapsed >= total;
77
- if (timestamp - start >= timeout - elapsed) {
78
- finish(finished, !finished);
79
- return;
80
- }
81
- if (finished || time >= minimum) {
82
- if (state.active) {
83
- state.callback(isRepeated ? index : undefined);
84
- }
85
- index += 1;
86
- state.index = index;
87
- if (!finished && index < count) {
88
- current = null;
89
- } else {
90
- finish(true, false);
91
- return;
92
- }
93
- }
94
- state.frame = requestAnimationFrame(step);
95
- }
96
- activeTimers.add(timer);
97
- state.frame = requestAnimationFrame(step);
98
- return timer;
99
- }
100
-
101
- // src/models.ts
102
- class TimerTrace extends Error {
103
- constructor() {
104
- super();
105
- this.name = "TimerTrace";
106
- }
107
- }
108
-
109
- // src/timer.ts
110
- function repeat(callback, options) {
111
- return timer("repeat", callback, options ?? {}, true);
112
- }
113
- function timer(type, callback, partial, start) {
114
- const isRepeated = type === "repeat";
115
- const options = getOptions(partial, isRepeated);
116
- const instance = new Timer(type, {
117
- callback,
118
- isRepeated,
119
- active: false,
120
- destroyed: false,
121
- minimum: options.interval - options.interval % milliseconds / 2,
122
- paused: false,
123
- trace: new TimerTrace
124
- }, options);
125
- if (start) {
126
- instance.start();
127
- }
128
- return instance;
129
- }
130
- function wait(callback, options) {
131
- return timer("wait", callback, options == null || typeof options === "number" ? {
132
- interval: options
133
- } : options, true);
134
- }
135
-
1
+ import { milliseconds } from "./constants.js";
2
+ import { work, getOptions } from "./functions.js";
3
+ import { TimerTrace } from "./models.js";
136
4
  class BasicTimer {
137
5
  constructor(type, state) {
138
6
  this.$timer = type;
139
7
  this.state = state;
140
8
  }
141
9
  }
142
-
143
10
  class Timer extends BasicTimer {
144
11
  get active() {
145
12
  return this.state.active;
@@ -151,42 +18,94 @@ class Timer extends BasicTimer {
151
18
  return this.state.paused;
152
19
  }
153
20
  get trace() {
154
- return globalThis._oscarpalmer_timer_debug ? this.state.trace : undefined;
21
+ return globalThis._oscarpalmer_timer_debug ? this.state.trace : void 0;
155
22
  }
156
23
  constructor(type, state, options) {
157
24
  super(type, state);
158
25
  this.options = options;
159
26
  }
27
+ /**
28
+ * Continues the timer _(if it was paused)_
29
+ */
160
30
  continue() {
161
31
  return work("continue", this, this.state, this.options);
162
32
  }
33
+ /**
34
+ * Destroys the timer _(after stopping it, if it was running)_
35
+ */
163
36
  destroy() {
164
37
  if (!this.state.destroyed) {
165
38
  this.state.destroyed = true;
166
39
  this.stop();
167
- this.options.afterCallback = undefined;
168
- this.options.errorCallback = undefined;
169
- this.state.callback = undefined;
170
- this.state.trace = undefined;
40
+ this.options.afterCallback = void 0;
41
+ this.options.errorCallback = void 0;
42
+ this.state.callback = void 0;
43
+ this.state.trace = void 0;
171
44
  }
172
45
  }
46
+ /**
47
+ * Pauses the timer _(if it was running)_
48
+ */
173
49
  pause() {
174
50
  return work("pause", this, this.state, this.options);
175
51
  }
52
+ /**
53
+ * Restarts the timer _(if it was running)_
54
+ */
176
55
  restart() {
177
56
  return work("restart", this, this.state, this.options);
178
57
  }
58
+ /**
59
+ * Starts the timer _(if it was stopped)_
60
+ */
179
61
  start() {
180
62
  return work("start", this, this.state, this.options);
181
63
  }
64
+ /**
65
+ * Stops the timer _(if it was running)_
66
+ */
182
67
  stop() {
183
68
  return work("stop", this, this.state, this.options);
184
69
  }
185
70
  }
71
+ function repeat(callback, options) {
72
+ return timer("repeat", callback, options ?? {}, true);
73
+ }
74
+ function timer(type, callback, partial, start) {
75
+ const isRepeated = type === "repeat";
76
+ const options = getOptions(partial, isRepeated);
77
+ const instance = new Timer(
78
+ type,
79
+ {
80
+ callback,
81
+ isRepeated,
82
+ active: false,
83
+ destroyed: false,
84
+ minimum: options.interval - options.interval % milliseconds / 2,
85
+ paused: false,
86
+ trace: new TimerTrace()
87
+ },
88
+ options
89
+ );
90
+ if (start) {
91
+ instance.start();
92
+ }
93
+ return instance;
94
+ }
95
+ function wait(callback, options) {
96
+ return timer(
97
+ "wait",
98
+ callback,
99
+ options == null || typeof options === "number" ? {
100
+ interval: options
101
+ } : options,
102
+ true
103
+ );
104
+ }
186
105
  export {
187
- wait,
188
- timer,
189
- repeat,
106
+ BasicTimer,
190
107
  Timer,
191
- BasicTimer
108
+ repeat,
109
+ timer,
110
+ wait
192
111
  };
package/dist/when.cjs ADDED
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const _function = require("@oscarpalmer/atoms/function");
4
+ const timer = require("./timer.cjs");
5
+ const destroyedMessage = "Timer has already been destroyed";
6
+ const startedMessage = "Timer has already been started";
7
+ class When extends timer.BasicTimer {
8
+ get active() {
9
+ var _a;
10
+ return ((_a = this.state.timer) == null ? void 0 : _a.active) ?? false;
11
+ }
12
+ get destroyed() {
13
+ return this.state.timer == null;
14
+ }
15
+ get paused() {
16
+ var _a;
17
+ return ((_a = this.state.timer) == null ? void 0 : _a.paused) ?? false;
18
+ }
19
+ get trace() {
20
+ var _a;
21
+ return (_a = this.state.timer) == null ? void 0 : _a.trace;
22
+ }
23
+ constructor(state) {
24
+ super("when", state);
25
+ }
26
+ /**
27
+ * Continues the timer _(if it was paused)_
28
+ */
29
+ continue() {
30
+ var _a;
31
+ (_a = this.state.timer) == null ? void 0 : _a.continue();
32
+ return this;
33
+ }
34
+ /**
35
+ * Destroys the timer _(and stops it,if it was running)_
36
+ */
37
+ destroy() {
38
+ var _a;
39
+ (_a = this.state.timer) == null ? void 0 : _a.destroy();
40
+ this.state.promise = void 0;
41
+ this.state.resolver = _function.noop;
42
+ this.state.rejecter = _function.noop;
43
+ this.state.timer = void 0;
44
+ }
45
+ /**
46
+ * Pauses the timer _(if it was running)_
47
+ */
48
+ pause() {
49
+ var _a;
50
+ (_a = this.state.timer) == null ? void 0 : _a.pause();
51
+ return this;
52
+ }
53
+ /**
54
+ * Stops the timer _(if it was running)_
55
+ */
56
+ stop() {
57
+ var _a;
58
+ (_a = this.state.timer) == null ? void 0 : _a.stop();
59
+ return this;
60
+ }
61
+ /**
62
+ * Starts the timer and returns a promise that resolves when the condition is met
63
+ */
64
+ // biome-ignore lint/suspicious/noThenProperty: returning a promise-like object, so it's ok ;)
65
+ then(resolve, reject) {
66
+ var _a;
67
+ if (this.state.timer == null || ((_a = this.state) == null ? void 0 : _a.started)) {
68
+ throw new Error(
69
+ this.state.timer == null ? destroyedMessage : startedMessage
70
+ );
71
+ }
72
+ this.state.started = true;
73
+ this.state.timer.start();
74
+ return this.state.promise.then(resolve ?? _function.noop, reject ?? _function.noop);
75
+ }
76
+ }
77
+ function when(condition, options) {
78
+ let result = false;
79
+ const state = {
80
+ started: false,
81
+ timer: timer.timer(
82
+ "repeat",
83
+ () => {
84
+ if (condition()) {
85
+ result = true;
86
+ state.timer.stop();
87
+ }
88
+ },
89
+ {
90
+ afterCallback() {
91
+ var _a, _b;
92
+ if (!state.timer.paused) {
93
+ if (result) {
94
+ (_a = state.resolver) == null ? void 0 : _a.call(state);
95
+ } else {
96
+ (_b = state.rejecter) == null ? void 0 : _b.call(state);
97
+ }
98
+ instance.destroy();
99
+ }
100
+ },
101
+ errorCallback() {
102
+ var _a;
103
+ (_a = state.rejecter) == null ? void 0 : _a.call(state);
104
+ instance.destroy();
105
+ },
106
+ count: options == null ? void 0 : options.count,
107
+ interval: options == null ? void 0 : options.interval,
108
+ timeout: options == null ? void 0 : options.timeout
109
+ },
110
+ false
111
+ )
112
+ };
113
+ const promise = new Promise((resolve, reject) => {
114
+ state.resolver = resolve;
115
+ state.rejecter = reject;
116
+ });
117
+ state.promise = promise;
118
+ const instance = new When(state);
119
+ return instance;
120
+ }
121
+ exports.When = When;
122
+ exports.when = when;