@oscarpalmer/timer 0.22.1 → 0.24.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.js CHANGED
@@ -1,9 +1,19 @@
1
1
  // src/constants.ts
2
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
+ ]);
3
10
  var hiddenTimers = new Set;
4
11
  var milliseconds = 1000 / 60;
5
12
  export {
6
13
  milliseconds,
7
14
  hiddenTimers,
15
+ endTypes,
16
+ endOrRestartTypes,
17
+ beginTypes,
8
18
  activeTimers
9
19
  };
@@ -1,9 +1,19 @@
1
1
  // src/constants.ts
2
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
+ ]);
3
10
  var hiddenTimers = new Set;
4
11
  var milliseconds = 1000 / 60;
5
12
  export {
6
13
  milliseconds,
7
14
  hiddenTimers,
15
+ endTypes,
16
+ endOrRestartTypes,
17
+ beginTypes,
8
18
  activeTimers
9
19
  };
package/dist/functions.js CHANGED
@@ -1,5 +1,12 @@
1
1
  // src/constants.ts
2
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
+ ]);
3
10
  var hiddenTimers = new Set;
4
11
  var milliseconds = 1000 / 60;
5
12
 
@@ -17,12 +24,12 @@ function getValueOrDefault(value, defaultValue, minimum) {
17
24
  return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
18
25
  }
19
26
  function work(type, timer, state, options) {
20
- if (["continue", "start"].includes(type) && state.active || ["pause", "stop"].includes(type) && !state.active) {
27
+ if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
21
28
  return timer;
22
29
  }
23
30
  const { count, interval, timeout } = options;
24
31
  const { isRepeated, minimum } = state;
25
- if (["pause", "restart", "stop"].includes(type)) {
32
+ if (endOrRestartTypes.has(type)) {
26
33
  const isStop = type === "stop";
27
34
  activeTimers.delete(timer);
28
35
  cancelAnimationFrame(state.frame);
@@ -1,5 +1,11 @@
1
1
  // src/functions.ts
2
- import {activeTimers, milliseconds} from "./constants";
2
+ import {
3
+ activeTimers,
4
+ beginTypes,
5
+ endOrRestartTypes,
6
+ endTypes,
7
+ milliseconds
8
+ } from "./constants";
3
9
  function getOptions(options, isRepeated) {
4
10
  return {
5
11
  afterCallback: options.afterCallback,
@@ -13,12 +19,12 @@ function getValueOrDefault(value, defaultValue, minimum) {
13
19
  return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
14
20
  }
15
21
  function work(type, timer, state, options) {
16
- if (["continue", "start"].includes(type) && state.active || ["pause", "stop"].includes(type) && !state.active) {
22
+ if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
17
23
  return timer;
18
24
  }
19
25
  const { count, interval, timeout } = options;
20
26
  const { isRepeated, minimum } = state;
21
- if (["pause", "restart", "stop"].includes(type)) {
27
+ if (endOrRestartTypes.has(type)) {
22
28
  const isStop = type === "stop";
23
29
  activeTimers.delete(timer);
24
30
  cancelAnimationFrame(state.frame);
package/dist/global.js CHANGED
@@ -1,5 +1,12 @@
1
1
  // src/constants.ts
2
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
+ ]);
3
10
  var hiddenTimers = new Set;
4
11
  var milliseconds = 1000 / 60;
5
12
 
package/dist/index.js CHANGED
@@ -4,6 +4,13 @@ function noop() {
4
4
 
5
5
  // src/constants.ts
6
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
+ ]);
7
14
  var hiddenTimers = new Set;
8
15
  var milliseconds = 1000 / 60;
9
16
 
@@ -30,12 +37,12 @@ function getValueOrDefault(value, defaultValue, minimum) {
30
37
  return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
31
38
  }
32
39
  function work(type, timer, state, options) {
33
- if (["continue", "start"].includes(type) && state.active || ["pause", "stop"].includes(type) && !state.active) {
40
+ if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
34
41
  return timer;
35
42
  }
36
43
  const { count, interval, timeout } = options;
37
44
  const { isRepeated, minimum } = state;
38
- if (["pause", "restart", "stop"].includes(type)) {
45
+ if (endOrRestartTypes.has(type)) {
39
46
  const isStop = type === "stop";
40
47
  activeTimers.delete(timer);
41
48
  cancelAnimationFrame(state.frame);
@@ -104,6 +111,14 @@ function work(type, timer, state, options) {
104
111
  return timer;
105
112
  }
106
113
 
114
+ // src/models.ts
115
+ class TimerTrace extends Error {
116
+ constructor() {
117
+ super();
118
+ this.name = "TimerTrace";
119
+ }
120
+ }
121
+
107
122
  // src/timer.ts
108
123
  function repeat(callback, options) {
109
124
  return timer("repeat", callback, options ?? {}, true);
@@ -115,6 +130,7 @@ function timer(type, callback, partial, start) {
115
130
  callback,
116
131
  isRepeated,
117
132
  active: false,
133
+ destroyed: false,
118
134
  minimum: options.interval - options.interval % milliseconds / 2,
119
135
  paused: false,
120
136
  trace: new TimerTrace
@@ -141,6 +157,9 @@ class Timer extends BasicTimer {
141
157
  get active() {
142
158
  return this.state.active;
143
159
  }
160
+ get destroyed() {
161
+ return this.state.destroyed;
162
+ }
144
163
  get paused() {
145
164
  return this.state.paused;
146
165
  }
@@ -154,6 +173,16 @@ class Timer extends BasicTimer {
154
173
  continue() {
155
174
  return work("continue", this, this.state, this.options);
156
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
+ }
157
186
  pause() {
158
187
  return work("pause", this, this.state, this.options);
159
188
  }
@@ -168,19 +197,18 @@ class Timer extends BasicTimer {
168
197
  }
169
198
  }
170
199
 
171
- class TimerTrace extends Error {
172
- constructor() {
173
- super();
174
- this.name = "TimerTrace";
175
- }
176
- }
177
-
178
200
  // src/index.ts
179
201
  function delay(time, timeout) {
180
202
  return new Promise((resolve, reject) => {
181
- wait(resolve ?? noop, {
203
+ const delayed = wait(() => {
204
+ delayed.destroy();
205
+ (resolve ?? noop)();
206
+ }, {
182
207
  timeout,
183
- errorCallback: reject ?? noop,
208
+ errorCallback: () => {
209
+ delayed.destroy();
210
+ (reject ?? noop)();
211
+ },
184
212
  interval: time
185
213
  });
186
214
  });
@@ -204,63 +232,85 @@ function isWhen(value) {
204
232
  }
205
233
  // src/when.ts
206
234
  function when(condition, options) {
207
- const repeated = timer("repeat", () => {
208
- if (condition()) {
209
- repeated.stop();
210
- state.resolver?.();
211
- }
212
- }, {
213
- afterCallback() {
214
- if (!repeated.paused) {
215
- if (condition()) {
216
- state.resolver?.();
217
- } else {
218
- state.rejecter?.();
219
- }
235
+ let result = false;
236
+ const state = {
237
+ started: false,
238
+ timer: timer("repeat", () => {
239
+ if (condition()) {
240
+ result = true;
241
+ state.timer.stop();
220
242
  }
221
- },
222
- errorCallback() {
223
- state.rejecter?.();
224
- },
225
- count: options?.count,
226
- interval: options?.interval,
227
- timeout: options?.timeout
228
- }, false);
229
- const state = {};
230
- state.promise = new Promise((resolve, reject) => {
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) => {
231
264
  state.resolver = resolve;
232
265
  state.rejecter = reject;
233
266
  });
234
- state.timer = repeated;
235
- return new When(state);
267
+ state.promise = promise;
268
+ const instance = new When(state);
269
+ return instance;
236
270
  }
271
+ var destroyedMessage = "Timer has already been destroyed";
272
+ var startedMessage = "Timer has already been started";
237
273
 
238
274
  class When extends BasicTimer {
239
275
  get active() {
240
- return this.state.timer.active;
276
+ return this.state.timer?.active ?? false;
277
+ }
278
+ get destroyed() {
279
+ return this.state.timer == null;
241
280
  }
242
281
  get paused() {
243
- return this.state.timer.paused;
282
+ return this.state.timer?.paused ?? false;
283
+ }
284
+ get trace() {
285
+ return this.state.timer?.trace;
244
286
  }
245
287
  constructor(state) {
246
288
  super("when", state);
247
289
  }
248
290
  continue() {
249
- this.state.timer.continue();
291
+ this.state.timer?.continue();
250
292
  return this;
251
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
+ }
252
301
  pause() {
253
- this.state.timer.pause();
302
+ this.state.timer?.pause();
254
303
  return this;
255
304
  }
256
305
  stop() {
257
- if (this.state.timer.active) {
258
- this.state.timer.stop();
259
- this.state.rejecter?.();
260
- }
306
+ this.state.timer?.stop();
261
307
  return this;
262
308
  }
263
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;
264
314
  this.state.timer.start();
265
315
  return this.state.promise.then(resolve ?? noop, reject ?? noop);
266
316
  }
package/dist/index.mjs CHANGED
@@ -5,9 +5,15 @@ import"./global";
5
5
  import {wait} from "./timer";
6
6
  function delay(time, timeout) {
7
7
  return new Promise((resolve, reject) => {
8
- wait(resolve ?? noop, {
8
+ const delayed = wait(() => {
9
+ delayed.destroy();
10
+ (resolve ?? noop)();
11
+ }, {
9
12
  timeout,
10
- errorCallback: reject ?? noop,
13
+ errorCallback: () => {
14
+ delayed.destroy();
15
+ (reject ?? noop)();
16
+ },
11
17
  interval: time
12
18
  });
13
19
  });
package/dist/models.mjs CHANGED
@@ -0,0 +1,10 @@
1
+ // src/models.ts
2
+ class TimerTrace extends Error {
3
+ constructor() {
4
+ super();
5
+ this.name = "TimerTrace";
6
+ }
7
+ }
8
+ export {
9
+ TimerTrace
10
+ };
package/dist/timer.js CHANGED
@@ -1,5 +1,12 @@
1
1
  // src/constants.ts
2
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
+ ]);
3
10
  var hiddenTimers = new Set;
4
11
  var milliseconds = 1000 / 60;
5
12
 
@@ -17,12 +24,12 @@ function getValueOrDefault(value, defaultValue, minimum) {
17
24
  return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
18
25
  }
19
26
  function work(type, timer, state, options) {
20
- if (["continue", "start"].includes(type) && state.active || ["pause", "stop"].includes(type) && !state.active) {
27
+ if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
21
28
  return timer;
22
29
  }
23
30
  const { count, interval, timeout } = options;
24
31
  const { isRepeated, minimum } = state;
25
- if (["pause", "restart", "stop"].includes(type)) {
32
+ if (endOrRestartTypes.has(type)) {
26
33
  const isStop = type === "stop";
27
34
  activeTimers.delete(timer);
28
35
  cancelAnimationFrame(state.frame);
@@ -91,6 +98,14 @@ function work(type, timer, state, options) {
91
98
  return timer;
92
99
  }
93
100
 
101
+ // src/models.ts
102
+ class TimerTrace extends Error {
103
+ constructor() {
104
+ super();
105
+ this.name = "TimerTrace";
106
+ }
107
+ }
108
+
94
109
  // src/timer.ts
95
110
  function repeat(callback, options) {
96
111
  return timer("repeat", callback, options ?? {}, true);
@@ -102,6 +117,7 @@ function timer(type, callback, partial, start) {
102
117
  callback,
103
118
  isRepeated,
104
119
  active: false,
120
+ destroyed: false,
105
121
  minimum: options.interval - options.interval % milliseconds / 2,
106
122
  paused: false,
107
123
  trace: new TimerTrace
@@ -128,6 +144,9 @@ class Timer extends BasicTimer {
128
144
  get active() {
129
145
  return this.state.active;
130
146
  }
147
+ get destroyed() {
148
+ return this.state.destroyed;
149
+ }
131
150
  get paused() {
132
151
  return this.state.paused;
133
152
  }
@@ -141,6 +160,16 @@ class Timer extends BasicTimer {
141
160
  continue() {
142
161
  return work("continue", this, this.state, this.options);
143
162
  }
163
+ destroy() {
164
+ if (!this.state.destroyed) {
165
+ this.state.destroyed = true;
166
+ this.stop();
167
+ this.options.afterCallback = undefined;
168
+ this.options.errorCallback = undefined;
169
+ this.state.callback = undefined;
170
+ this.state.trace = undefined;
171
+ }
172
+ }
144
173
  pause() {
145
174
  return work("pause", this, this.state, this.options);
146
175
  }
@@ -154,13 +183,6 @@ class Timer extends BasicTimer {
154
183
  return work("stop", this, this.state, this.options);
155
184
  }
156
185
  }
157
-
158
- class TimerTrace extends Error {
159
- constructor() {
160
- super();
161
- this.name = "TimerTrace";
162
- }
163
- }
164
186
  export {
165
187
  wait,
166
188
  timer,
package/dist/timer.mjs CHANGED
@@ -1,6 +1,9 @@
1
1
  // src/timer.ts
2
2
  import {milliseconds} from "./constants";
3
3
  import {getOptions, work} from "./functions";
4
+ import {
5
+ TimerTrace
6
+ } from "./models";
4
7
  function repeat(callback, options) {
5
8
  return timer("repeat", callback, options ?? {}, true);
6
9
  }
@@ -11,6 +14,7 @@ function timer(type, callback, partial, start) {
11
14
  callback,
12
15
  isRepeated,
13
16
  active: false,
17
+ destroyed: false,
14
18
  minimum: options.interval - options.interval % milliseconds / 2,
15
19
  paused: false,
16
20
  trace: new TimerTrace
@@ -37,6 +41,9 @@ class Timer extends BasicTimer {
37
41
  get active() {
38
42
  return this.state.active;
39
43
  }
44
+ get destroyed() {
45
+ return this.state.destroyed;
46
+ }
40
47
  get paused() {
41
48
  return this.state.paused;
42
49
  }
@@ -50,6 +57,16 @@ class Timer extends BasicTimer {
50
57
  continue() {
51
58
  return work("continue", this, this.state, this.options);
52
59
  }
60
+ destroy() {
61
+ if (!this.state.destroyed) {
62
+ this.state.destroyed = true;
63
+ this.stop();
64
+ this.options.afterCallback = undefined;
65
+ this.options.errorCallback = undefined;
66
+ this.state.callback = undefined;
67
+ this.state.trace = undefined;
68
+ }
69
+ }
53
70
  pause() {
54
71
  return work("pause", this, this.state, this.options);
55
72
  }
@@ -63,13 +80,6 @@ class Timer extends BasicTimer {
63
80
  return work("stop", this, this.state, this.options);
64
81
  }
65
82
  }
66
-
67
- class TimerTrace extends Error {
68
- constructor() {
69
- super();
70
- this.name = "TimerTrace";
71
- }
72
- }
73
83
  export {
74
84
  wait,
75
85
  timer,