@oscarpalmer/timer 0.23.0 → 0.25.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/functions.js CHANGED
@@ -11,13 +11,6 @@ var hiddenTimers = new Set;
11
11
  var milliseconds = 1000 / 60;
12
12
 
13
13
  // src/functions.ts
14
- function destroyWhen(state) {
15
- state.timer?.destroy();
16
- state.promise = undefined;
17
- state.rejecter = undefined;
18
- state.resolver = undefined;
19
- state.timer = undefined;
20
- }
21
14
  function getOptions(options, isRepeated) {
22
15
  return {
23
16
  afterCallback: options.afterCallback,
@@ -107,6 +100,5 @@ function work(type, timer, state, options) {
107
100
  export {
108
101
  work,
109
102
  getValueOrDefault,
110
- getOptions,
111
- destroyWhen
103
+ getOptions
112
104
  };
@@ -1,18 +1,11 @@
1
1
  // src/functions.ts
2
2
  import {
3
- activeTimers,
4
- beginTypes,
5
- endOrRestartTypes,
6
- endTypes,
7
- milliseconds
3
+ activeTimers,
4
+ beginTypes,
5
+ endOrRestartTypes,
6
+ endTypes,
7
+ milliseconds
8
8
  } from "./constants";
9
- function destroyWhen(state) {
10
- state.timer?.destroy();
11
- state.promise = undefined;
12
- state.rejecter = undefined;
13
- state.resolver = undefined;
14
- state.timer = undefined;
15
- }
16
9
  function getOptions(options, isRepeated) {
17
10
  return {
18
11
  afterCallback: options.afterCallback,
@@ -102,6 +95,5 @@ function work(type, timer, state, options) {
102
95
  export {
103
96
  work,
104
97
  getValueOrDefault,
105
- getOptions,
106
- destroyWhen
98
+ getOptions
107
99
  };
package/dist/global.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/global.ts
2
- import {activeTimers} from "./constants";
2
+ import { activeTimers } from "./constants";
3
3
  if (globalThis._oscarpalmer_timers == null) {
4
4
  Object.defineProperty(globalThis, "_oscarpalmer_timers", {
5
5
  get() {
package/dist/index.js CHANGED
@@ -24,13 +24,6 @@ if (globalThis._oscarpalmer_timers == null) {
24
24
  }
25
25
 
26
26
  // src/functions.ts
27
- function destroyWhen(state) {
28
- state.timer?.destroy();
29
- state.promise = undefined;
30
- state.rejecter = undefined;
31
- state.resolver = undefined;
32
- state.timer = undefined;
33
- }
34
27
  function getOptions(options, isRepeated) {
35
28
  return {
36
29
  afterCallback: options.afterCallback,
@@ -239,26 +232,28 @@ function isWhen(value) {
239
232
  }
240
233
  // src/when.ts
241
234
  function when(condition, options) {
235
+ let result = false;
242
236
  const state = {
243
237
  started: false,
244
238
  timer: timer("repeat", () => {
245
239
  if (condition()) {
240
+ result = true;
246
241
  state.timer.stop();
247
242
  }
248
243
  }, {
249
244
  afterCallback() {
250
245
  if (!state.timer.paused) {
251
- if (condition()) {
246
+ if (result) {
252
247
  state.resolver?.();
253
248
  } else {
254
249
  state.rejecter?.();
255
250
  }
256
- destroyWhen(state);
251
+ instance.destroy();
257
252
  }
258
253
  },
259
254
  errorCallback() {
260
255
  state.rejecter?.();
261
- destroyWhen(state);
256
+ instance.destroy();
262
257
  },
263
258
  count: options?.count,
264
259
  interval: options?.interval,
@@ -270,8 +265,11 @@ function when(condition, options) {
270
265
  state.rejecter = reject;
271
266
  });
272
267
  state.promise = promise;
273
- return new When(state);
268
+ const instance = new When(state);
269
+ return instance;
274
270
  }
271
+ var destroyedMessage = "Timer has already been destroyed";
272
+ var startedMessage = "Timer has already been started";
275
273
 
276
274
  class When extends BasicTimer {
277
275
  get active() {
@@ -294,9 +292,11 @@ class When extends BasicTimer {
294
292
  return this;
295
293
  }
296
294
  destroy() {
297
- if (this.state.timer != null) {
298
- this.state.timer.destroy();
299
- }
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
300
  }
301
301
  pause() {
302
302
  this.state.timer?.pause();
@@ -307,10 +307,8 @@ class When extends BasicTimer {
307
307
  return this;
308
308
  }
309
309
  then(resolve, reject) {
310
- if (this.state.timer == null || this.state.started) {
311
- return new Promise(() => {
312
- (reject ?? noop)();
313
- });
310
+ if (this.state.timer == null || this.state?.started) {
311
+ throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
314
312
  }
315
313
  this.state.started = true;
316
314
  this.state.timer.start();
@@ -321,13 +319,13 @@ class When extends BasicTimer {
321
319
  // src/index.ts
322
320
  document.addEventListener("visibilitychange", () => {
323
321
  if (document.hidden) {
324
- for (const timer4 of activeTimers) {
325
- hiddenTimers.add(timer4);
326
- timer4.pause();
322
+ for (const timer2 of activeTimers) {
323
+ hiddenTimers.add(timer2);
324
+ timer2.pause();
327
325
  }
328
326
  } else {
329
- for (const timer4 of hiddenTimers) {
330
- timer4.continue();
327
+ for (const timer2 of hiddenTimers) {
328
+ timer2.continue();
331
329
  }
332
330
  hiddenTimers.clear();
333
331
  }
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  // src/index.ts
2
- import {noop} from "@oscarpalmer/atoms/function";
3
- import {activeTimers, hiddenTimers} from "./constants";
2
+ import { noop } from "@oscarpalmer/atoms/function";
3
+ import { activeTimers, hiddenTimers } from "./constants";
4
4
  import"./global";
5
- import {wait} from "./timer";
5
+ import { wait } from "./timer";
6
6
  function delay(time, timeout) {
7
7
  return new Promise((resolve, reject) => {
8
8
  const delayed = wait(() => {
@@ -18,18 +18,18 @@ function delay(time, timeout) {
18
18
  });
19
19
  });
20
20
  }
21
- import {isRepeated, isTimer, isWaited, isWhen} from "./is";
22
- import {repeat, wait as wait2} from "./timer";
23
- import {when} from "./when";
21
+ import { isRepeated, isTimer, isWaited, isWhen } from "./is";
22
+ import { repeat, wait as wait2 } from "./timer";
23
+ import { when } from "./when";
24
24
  document.addEventListener("visibilitychange", () => {
25
25
  if (document.hidden) {
26
- for (const timer2 of activeTimers) {
27
- hiddenTimers.add(timer2);
28
- timer2.pause();
26
+ for (const timer of activeTimers) {
27
+ hiddenTimers.add(timer);
28
+ timer.pause();
29
29
  }
30
30
  } else {
31
- for (const timer2 of hiddenTimers) {
32
- timer2.continue();
31
+ for (const timer of hiddenTimers) {
32
+ timer.continue();
33
33
  }
34
34
  hiddenTimers.clear();
35
35
  }
package/dist/timer.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  // src/timer.ts
2
- import {milliseconds} from "./constants";
3
- import {getOptions, work} from "./functions";
2
+ import { milliseconds } from "./constants";
3
+ import { getOptions, work } from "./functions";
4
4
  import {
5
- TimerTrace
5
+ TimerTrace
6
6
  } from "./models";
7
7
  function repeat(callback, options) {
8
8
  return timer("repeat", callback, options ?? {}, true);
package/dist/when.js CHANGED
@@ -15,13 +15,6 @@ var hiddenTimers = new Set;
15
15
  var milliseconds = 1000 / 60;
16
16
 
17
17
  // src/functions.ts
18
- function destroyWhen(state) {
19
- state.timer?.destroy();
20
- state.promise = undefined;
21
- state.rejecter = undefined;
22
- state.resolver = undefined;
23
- state.timer = undefined;
24
- }
25
18
  function getOptions(options, isRepeated) {
26
19
  return {
27
20
  afterCallback: options.afterCallback,
@@ -188,26 +181,28 @@ class Timer extends BasicTimer {
188
181
 
189
182
  // src/when.ts
190
183
  function when(condition, options) {
184
+ let result = false;
191
185
  const state = {
192
186
  started: false,
193
187
  timer: timer("repeat", () => {
194
188
  if (condition()) {
189
+ result = true;
195
190
  state.timer.stop();
196
191
  }
197
192
  }, {
198
193
  afterCallback() {
199
194
  if (!state.timer.paused) {
200
- if (condition()) {
195
+ if (result) {
201
196
  state.resolver?.();
202
197
  } else {
203
198
  state.rejecter?.();
204
199
  }
205
- destroyWhen(state);
200
+ instance.destroy();
206
201
  }
207
202
  },
208
203
  errorCallback() {
209
204
  state.rejecter?.();
210
- destroyWhen(state);
205
+ instance.destroy();
211
206
  },
212
207
  count: options?.count,
213
208
  interval: options?.interval,
@@ -219,8 +214,11 @@ function when(condition, options) {
219
214
  state.rejecter = reject;
220
215
  });
221
216
  state.promise = promise;
222
- return new When(state);
217
+ const instance = new When(state);
218
+ return instance;
223
219
  }
220
+ var destroyedMessage = "Timer has already been destroyed";
221
+ var startedMessage = "Timer has already been started";
224
222
 
225
223
  class When extends BasicTimer {
226
224
  get active() {
@@ -243,9 +241,11 @@ class When extends BasicTimer {
243
241
  return this;
244
242
  }
245
243
  destroy() {
246
- if (this.state.timer != null) {
247
- this.state.timer.destroy();
248
- }
244
+ this.state.timer?.destroy();
245
+ this.state.promise = undefined;
246
+ this.state.resolver = noop;
247
+ this.state.rejecter = noop;
248
+ this.state.timer = undefined;
249
249
  }
250
250
  pause() {
251
251
  this.state.timer?.pause();
@@ -256,10 +256,8 @@ class When extends BasicTimer {
256
256
  return this;
257
257
  }
258
258
  then(resolve, reject) {
259
- if (this.state.timer == null || this.state.started) {
260
- return new Promise(() => {
261
- (reject ?? noop)();
262
- });
259
+ if (this.state.timer == null || this.state?.started) {
260
+ throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
263
261
  }
264
262
  this.state.started = true;
265
263
  this.state.timer.start();
package/dist/when.mjs CHANGED
@@ -1,28 +1,29 @@
1
1
  // src/when.ts
2
- import {noop} from "@oscarpalmer/atoms/function";
3
- import {destroyWhen} from "./functions";
4
- import {BasicTimer, timer as timer2} from "./timer";
2
+ import { noop } from "@oscarpalmer/atoms/function";
3
+ import { BasicTimer, timer } from "./timer";
5
4
  function when(condition, options) {
5
+ let result = false;
6
6
  const state = {
7
7
  started: false,
8
- timer: timer2("repeat", () => {
8
+ timer: timer("repeat", () => {
9
9
  if (condition()) {
10
+ result = true;
10
11
  state.timer.stop();
11
12
  }
12
13
  }, {
13
14
  afterCallback() {
14
15
  if (!state.timer.paused) {
15
- if (condition()) {
16
+ if (result) {
16
17
  state.resolver?.();
17
18
  } else {
18
19
  state.rejecter?.();
19
20
  }
20
- destroyWhen(state);
21
+ instance.destroy();
21
22
  }
22
23
  },
23
24
  errorCallback() {
24
25
  state.rejecter?.();
25
- destroyWhen(state);
26
+ instance.destroy();
26
27
  },
27
28
  count: options?.count,
28
29
  interval: options?.interval,
@@ -34,8 +35,11 @@ function when(condition, options) {
34
35
  state.rejecter = reject;
35
36
  });
36
37
  state.promise = promise;
37
- return new When(state);
38
+ const instance = new When(state);
39
+ return instance;
38
40
  }
41
+ var destroyedMessage = "Timer has already been destroyed";
42
+ var startedMessage = "Timer has already been started";
39
43
 
40
44
  class When extends BasicTimer {
41
45
  get active() {
@@ -58,9 +62,11 @@ class When extends BasicTimer {
58
62
  return this;
59
63
  }
60
64
  destroy() {
61
- if (this.state.timer != null) {
62
- this.state.timer.destroy();
63
- }
65
+ this.state.timer?.destroy();
66
+ this.state.promise = undefined;
67
+ this.state.resolver = noop;
68
+ this.state.rejecter = noop;
69
+ this.state.timer = undefined;
64
70
  }
65
71
  pause() {
66
72
  this.state.timer?.pause();
@@ -71,10 +77,8 @@ class When extends BasicTimer {
71
77
  return this;
72
78
  }
73
79
  then(resolve, reject) {
74
- if (this.state.timer == null || this.state.started) {
75
- return new Promise(() => {
76
- (reject ?? noop)();
77
- });
80
+ if (this.state.timer == null || this.state?.started) {
81
+ throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
78
82
  }
79
83
  this.state.started = true;
80
84
  this.state.timer.start();
package/package.json CHANGED
@@ -4,14 +4,14 @@
4
4
  "url": "https://oscarpalmer.se"
5
5
  },
6
6
  "dependencies": {
7
- "@oscarpalmer/atoms": "0.69.0"
7
+ "@oscarpalmer/atoms": "0.74.1"
8
8
  },
9
9
  "description": "A better solution for timeout- and interval-based timers.",
10
10
  "devDependencies": {
11
- "@biomejs/biome": "^1.9.0",
11
+ "@biomejs/biome": "^1.9.2",
12
12
  "@happy-dom/global-registrator": "^15.7.4",
13
- "@types/bun": "^1.1.9",
14
- "bun": "^1.1.27",
13
+ "@types/bun": "^1.1.10",
14
+ "bun": "^1.1.29",
15
15
  "dts-bundle-generator": "9.5.1",
16
16
  "typescript": "^5.6.2"
17
17
  },
@@ -50,5 +50,5 @@
50
50
  },
51
51
  "type": "module",
52
52
  "types": "types/index.d.cts",
53
- "version": "0.23.0"
53
+ "version": "0.25.0"
54
54
  }
package/src/functions.ts CHANGED
@@ -8,15 +8,6 @@ import {
8
8
  import type {TimerOptions, TimerState, WhenState, WorkType} from './models';
9
9
  import type {Timer} from './timer';
10
10
 
11
- export function destroyWhen(state: WhenState): void {
12
- state.timer?.destroy();
13
-
14
- state.promise = undefined as never;
15
- state.rejecter = undefined;
16
- state.resolver = undefined;
17
- state.timer = undefined as never;
18
- }
19
-
20
11
  export function getOptions(
21
12
  options: Partial<TimerOptions>,
22
13
  isRepeated: boolean,
package/src/global.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type {Timer} from './timer';
1
2
  import {activeTimers} from './constants';
2
3
 
3
4
  declare global {
package/src/when.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import {noop} from '@oscarpalmer/atoms/function';
2
- import {destroyWhen} from './functions';
3
2
  import type {WhenOptions, WhenState} from './models';
4
3
  import {BasicTimer, timer} from './timer';
5
4
 
5
+ const destroyedMessage = 'Timer has already been destroyed';
6
+ const startedMessage = 'Timer has already been started';
7
+
6
8
  export class When extends BasicTimer<WhenState> {
7
9
  get active() {
8
10
  return this.state.timer?.active ?? false;
@@ -34,12 +36,15 @@ export class When extends BasicTimer<WhenState> {
34
36
  }
35
37
 
36
38
  /**
37
- * Destroys the timer _
39
+ * Destroys the timer _(and stops it,if it was running)_
38
40
  */
39
41
  destroy(): void {
40
- if (this.state.timer != null) {
41
- this.state.timer.destroy();
42
- }
42
+ this.state.timer?.destroy();
43
+
44
+ this.state.promise = undefined as never;
45
+ this.state.resolver = noop;
46
+ this.state.rejecter = noop;
47
+ this.state.timer = undefined as never;
43
48
  }
44
49
 
45
50
  /**
@@ -68,10 +73,10 @@ export class When extends BasicTimer<WhenState> {
68
73
  resolve?: (() => void) | null,
69
74
  reject?: (() => void) | null,
70
75
  ): Promise<void> {
71
- if (this.state.timer == null || this.state.started) {
72
- return new Promise(() => {
73
- (reject ?? noop)();
74
- });
76
+ if (this.state.timer == null || this.state?.started) {
77
+ throw new Error(
78
+ this.state.timer == null ? destroyedMessage : startedMessage,
79
+ );
75
80
  }
76
81
 
77
82
  this.state.started = true;
@@ -90,31 +95,35 @@ export function when(
90
95
  condition: () => boolean,
91
96
  options?: Partial<WhenOptions>,
92
97
  ): When {
98
+ let result = false;
99
+
93
100
  const state: WhenState = {
94
101
  started: false,
95
102
  timer: timer(
96
103
  'repeat',
97
104
  () => {
98
105
  if (condition()) {
106
+ result = true;
107
+
99
108
  state.timer.stop();
100
109
  }
101
110
  },
102
111
  {
103
112
  afterCallback() {
104
113
  if (!state.timer.paused) {
105
- if (condition()) {
114
+ if (result) {
106
115
  state.resolver?.();
107
116
  } else {
108
117
  state.rejecter?.();
109
118
  }
110
119
 
111
- destroyWhen(state);
120
+ instance.destroy();
112
121
  }
113
122
  },
114
123
  errorCallback() {
115
124
  state.rejecter?.();
116
125
 
117
- destroyWhen(state);
126
+ instance.destroy();
118
127
  },
119
128
  count: options?.count,
120
129
  interval: options?.interval,
@@ -131,5 +140,7 @@ export function when(
131
140
 
132
141
  state.promise = promise;
133
142
 
134
- return new When(state);
143
+ const instance = new When(state);
144
+
145
+ return instance;
135
146
  }
@@ -1,6 +1,5 @@
1
- import type { TimerOptions, TimerState, WhenState, WorkType } from './models';
1
+ import type { TimerOptions, TimerState, WorkType } from './models';
2
2
  import type { Timer } from './timer';
3
- export declare function destroyWhen(state: WhenState): void;
4
3
  export declare function getOptions(options: Partial<TimerOptions>, isRepeated: boolean): TimerOptions;
5
4
  export declare function getValueOrDefault(value: unknown, defaultValue: number, minimum?: number): number;
6
5
  export declare function work(type: WorkType, timer: Timer, state: TimerState, options: TimerOptions): Timer;
package/types/global.d.ts CHANGED
@@ -1,5 +1,5 @@
1
+ import type { Timer } from './timer';
1
2
  declare global {
2
3
  var _oscarpalmer_timer_debug: boolean | undefined;
3
4
  var _oscarpalmer_timers: Timer[] | undefined;
4
5
  }
5
- export {};
package/types/index.d.cts CHANGED
@@ -156,7 +156,7 @@ export declare class When extends BasicTimer<WhenState> {
156
156
  */
157
157
  continue(): When;
158
158
  /**
159
- * Destroys the timer _
159
+ * Destroys the timer _(and stops it,if it was running)_
160
160
  */
161
161
  destroy(): void;
162
162
  /**
package/types/when.d.ts CHANGED
@@ -11,7 +11,7 @@ export declare class When extends BasicTimer<WhenState> {
11
11
  */
12
12
  continue(): When;
13
13
  /**
14
- * Destroys the timer _
14
+ * Destroys the timer _(and stops it,if it was running)_
15
15
  */
16
16
  destroy(): void;
17
17
  /**