@oscarpalmer/timer 0.44.0 → 0.45.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/timer.mjs CHANGED
@@ -1,31 +1,28 @@
1
1
  import { WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP } from "./constants.mjs";
2
- import { stop, work } from "./work.mjs";
2
+ import { work } from "./work.mjs";
3
3
  import { noop } from "@oscarpalmer/atoms/function";
4
4
  //#region src/timer.ts
5
5
  function createTimer(name, pick, options, start) {
6
- function worker(type) {
7
- return work(type, {
8
- name,
9
- instance
10
- }, state, options);
11
- }
12
6
  const state = {
13
7
  ...pick,
8
+ name,
9
+ options,
14
10
  active: false,
15
11
  destroyed: false,
16
12
  elapsed: 0,
17
13
  frame: void 0,
18
14
  index: 0,
19
15
  paused: false,
16
+ timer: void 0,
20
17
  total: 0
21
18
  };
22
19
  const instance = {
23
- continue: () => worker(WORK_CONTINUE),
24
- destroy: () => destroyTimer(name, instance, state, options),
25
- pause: () => worker(WORK_PAUSE),
26
- restart: () => worker(WORK_RESTART),
27
- start: () => worker(WORK_START),
28
- stop: () => worker(WORK_STOP)
20
+ continue: () => work(WORK_CONTINUE, state),
21
+ destroy: noop,
22
+ pause: () => work(WORK_PAUSE, state),
23
+ restart: () => work(WORK_RESTART, state),
24
+ start: () => work(WORK_START, state),
25
+ stop: () => work(WORK_STOP, state)
29
26
  };
30
27
  Object.defineProperties(instance, {
31
28
  $timer: {
@@ -38,7 +35,7 @@ function createTimer(name, pick, options, start) {
38
35
  },
39
36
  destroyed: {
40
37
  enumerable: true,
41
- get: () => state.destroyed
38
+ value: false
42
39
  },
43
40
  paused: {
44
41
  enumerable: true,
@@ -49,19 +46,9 @@ function createTimer(name, pick, options, start) {
49
46
  get: () => globalThis._oscarpalmer_timer_debug ?? false ? state.trace : void 0
50
47
  }
51
48
  });
52
- if (start) instance.start();
53
- return Object.freeze(instance);
54
- }
55
- function destroyTimer(name, instance, state, options) {
56
- state.destroyed = true;
57
- options.onAfter = noop;
58
- options.onError = noop;
59
- state.callback = noop;
60
- if (!globalThis._oscarpalmer_timer_debug) state.trace = void 0;
61
- stop({
62
- instance,
63
- name
64
- }, state, options);
49
+ state.timer = Object.freeze(instance);
50
+ if (start) state.timer.start();
51
+ return state.timer;
65
52
  }
66
53
  //#endregion
67
54
  export { createTimer };
package/dist/wait.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { TYPE_WAIT } from "./constants.mjs";
2
- import { getCallback, getValidNumber } from "./get.mjs";
2
+ import { getCallback, getValidNumber } from "./misc.mjs";
3
3
  import "./global.mjs";
4
4
  import { TimerTrace } from "./models.mjs";
5
5
  import { createTimer } from "./timer.mjs";
package/dist/when.mjs CHANGED
@@ -1,21 +1,13 @@
1
- import { MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN, WORK_CONTINUE } from "./constants.mjs";
2
- import { getValidNumber, getValidTimeout } from "./get.mjs";
1
+ import { MESSAGE_STARTED, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_STOP } from "./constants.mjs";
2
+ import { getValidNumber, getValidTimeout } from "./misc.mjs";
3
3
  import "./global.mjs";
4
4
  import { TimerTrace } from "./models.mjs";
5
5
  import { createTimer } from "./timer.mjs";
6
6
  import { noop } from "@oscarpalmer/atoms/function";
7
7
  //#region src/when.ts
8
- function destroyWhen(state) {
9
- state.timer?.destroy();
10
- state.promise = void 0;
11
- state.resolver = noop;
12
- state.rejecter = noop;
13
- state.timer = void 0;
14
- }
15
- function onAfter(instance, state) {
8
+ function onAfter(state) {
16
9
  if (state.result) state.resolver?.();
17
10
  else state.rejecter?.();
18
- instance.destroy();
19
11
  }
20
12
  function onCallback(condition, state) {
21
13
  try {
@@ -27,16 +19,11 @@ function onCallback(condition, state) {
27
19
  state.timer.stop();
28
20
  }
29
21
  }
30
- function onError(instance, state) {
31
- state.rejecter?.();
32
- instance.destroy();
33
- }
34
22
  function onWhen(type, instance, state) {
35
23
  state.timer?.[type]?.();
36
24
  return instance;
37
25
  }
38
26
  function startWhen(state, resolve) {
39
- if (state.timer == null) throw new Error(MESSAGE_DESTROYED);
40
27
  if (state.started) throw new Error(MESSAGE_STARTED);
41
28
  state.started = true;
42
29
  state.timer.start();
@@ -64,18 +51,18 @@ function when(condition, options) {
64
51
  callback: () => onCallback(condition, state),
65
52
  trace: new TimerTrace().stack
66
53
  }, {
67
- onAfter: () => onAfter(instance, state),
68
- onError: () => onError(instance, state),
54
+ onAfter: () => onAfter(state),
55
+ onError: () => state.rejecter?.(),
69
56
  count: getValidNumber(options?.count),
70
57
  interval: getValidNumber(options?.interval),
71
58
  timeout: getValidTimeout(options?.timeout)
72
59
  }, false);
73
60
  instance = {
74
61
  continue: () => onWhen(WORK_CONTINUE, instance, state),
75
- destroy: () => destroyWhen(state),
76
- pause: () => onWhen("pause", instance, state),
62
+ destroy: noop,
63
+ pause: () => onWhen(WORK_PAUSE, instance, state),
77
64
  start: (resolve) => startWhen(state, resolve),
78
- stop: () => onWhen("stop", instance, state)
65
+ stop: () => onWhen(WORK_STOP, instance, state)
79
66
  };
80
67
  Object.defineProperties(instance, {
81
68
  $timer: {
@@ -84,15 +71,15 @@ function when(condition, options) {
84
71
  },
85
72
  active: {
86
73
  enumerable: true,
87
- get: () => state.timer?.active ?? false
74
+ get: () => state.timer.active
88
75
  },
89
76
  destroyed: {
90
77
  enumerable: true,
91
- get: () => state.timer == null
78
+ value: false
92
79
  },
93
80
  paused: {
94
81
  enumerable: true,
95
- get: () => state.timer?.paused ?? false
82
+ get: () => state.timer.paused
96
83
  },
97
84
  trace: {
98
85
  enumerable: true,
package/dist/work.d.mts CHANGED
@@ -1,6 +1,6 @@
1
- import { Timer, TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType } from "./models.mjs";
1
+ import { Timer, TimerState, WorkHandlerType } from "./models.mjs";
2
2
  //#region src/work.d.ts
3
- declare function stop(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
4
- declare function work(type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
3
+ declare function stop(state: TimerState): Timer;
4
+ declare function work(type: WorkHandlerType, state: TimerState, hide?: boolean): Timer;
5
5
  //#endregion
6
6
  export { stop, work };
package/dist/work.mjs CHANGED
@@ -1,25 +1,20 @@
1
- import { TIMERS_ACTIVE, WORK_PAUSE, WORK_STOP } from "./constants.mjs";
1
+ import { WORK_PAUSE } from "./constants.mjs";
2
+ import { updateStates } from "./misc.mjs";
2
3
  //#region src/work.ts
3
- function finish(timer, state, options, success) {
4
+ function finish(state, success) {
5
+ updateStates(state);
4
6
  cancelAnimationFrame(state.frame);
5
- TIMERS_ACTIVE.delete(timer.instance);
6
7
  state.active = false;
7
8
  state.elapsed = 0;
8
9
  state.frame = void 0;
9
- if (timer.name === "wait") state.callback();
10
- else options.onAfter?.(success);
10
+ if (state.name === "wait") state.callback();
11
+ else state.options.onAfter?.(success);
11
12
  }
12
13
  function ignore(type, state) {
13
- if (state.destroyed) return type !== WORK_STOP;
14
14
  if (state.paused) return type === "pause" || type === "start";
15
15
  return state.active && type === "start";
16
16
  }
17
- function pause(timer, state) {
18
- cancelAnimationFrame(state.frame);
19
- state.frame = void 0;
20
- return timer.instance;
21
- }
22
- function run(timer, state, options) {
17
+ function run(state) {
23
18
  let last;
24
19
  let start;
25
20
  return function step(now) {
@@ -30,18 +25,18 @@ function run(timer, state, options) {
30
25
  state.elapsed += difference;
31
26
  state.total += difference;
32
27
  last = now;
33
- if (options.timeout > 0 && state.total >= options.timeout) {
34
- options.onError?.();
35
- finish(timer, state, options, false);
28
+ if (state.options.timeout > 0 && state.total >= state.options.timeout) {
29
+ state.options.onError?.();
30
+ finish(state, false);
36
31
  return;
37
32
  }
38
- if (options.interval === 0 || state.elapsed >= options.interval - 5) {
39
- if (options.count > -1) state.callback(state.index);
33
+ if (state.options.interval === 0 || state.elapsed >= state.options.interval - 5) {
34
+ if (state.options.count > -1) state.callback(state.index);
40
35
  start = now;
41
36
  state.elapsed = 0;
42
37
  state.index += 1;
43
- if (options.count === -1 || options.count > 0 && state.index >= options.count) {
44
- finish(timer, state, options, true);
38
+ if (state.options.count === -1 || state.options.count > 0 && state.index >= state.options.count) {
39
+ finish(state, true);
45
40
  return;
46
41
  }
47
42
  }
@@ -54,30 +49,30 @@ function setState(type, state) {
54
49
  state.index = pausable ? state.index : 0;
55
50
  state.total = pausable ? state.total : 0;
56
51
  }
57
- function stop(timer, state, options) {
52
+ function stop(state) {
53
+ updateStates(state);
58
54
  cancelAnimationFrame(state.frame);
59
- TIMERS_ACTIVE.delete(timer.instance);
60
- options.onAfter?.(false);
61
- state.active = !stop;
55
+ state.options.onAfter?.(false);
56
+ state.active = false;
62
57
  state.frame = void 0;
63
58
  state.paused = false;
64
- return timer.instance;
59
+ return state.timer;
65
60
  }
66
- function work(type, timer, state, options) {
67
- if (ignore(type, state)) return timer.instance;
61
+ function work(type, state, hide) {
62
+ if (ignore(type, state)) return state.timer;
68
63
  setState(type, state);
69
- if (type === "stop") return stop(timer, state, options);
64
+ if (type === "stop") return stop(state);
70
65
  if (type === "pause" || type === "restart") {
71
66
  cancelAnimationFrame(state.frame);
72
67
  state.frame = void 0;
73
68
  }
74
69
  state.active = true;
75
70
  state.paused = type === WORK_PAUSE;
76
- if (state.paused) return pause(timer, state);
77
- TIMERS_ACTIVE.add(timer.instance);
78
- const runner = run(timer, state, options);
71
+ updateStates(state, state.paused ? hide ?? false ? "hidden" : void 0 : "active");
72
+ if (state.paused) return state.timer;
73
+ const runner = run(state);
79
74
  state.frame = requestAnimationFrame(runner);
80
- return timer.instance;
75
+ return state.timer;
81
76
  }
82
77
  //#endregion
83
78
  export { stop, work };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/timer",
3
- "version": "0.44.0",
3
+ "version": "0.45.0",
4
4
  "description": "A better solution for timeout- and interval-based timers.",
5
5
  "keywords": [
6
6
  "requestAnimationFrame",
@@ -30,10 +30,6 @@
30
30
  "types": "./dist/index.d.mts",
31
31
  "default": "./dist/index.mjs"
32
32
  },
33
- "./delay": {
34
- "types": "./dist/delay.d.mts",
35
- "default": "./dist/delay.mjs"
36
- },
37
33
  "./models": {
38
34
  "types": "./dist/models.d.mts",
39
35
  "default": "./dist/models.mjs"
@@ -64,7 +60,7 @@
64
60
  },
65
61
  "devDependencies": {
66
62
  "@oxlint/plugins": "^1.80",
67
- "@types/node": "^26.3",
63
+ "@types/node": "^26.4",
68
64
  "@vitest/coverage-istanbul": "^4.1",
69
65
  "jsdom": "^30",
70
66
  "tsdown": "^0.22",
package/src/constants.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {Timer, TimerName, WorkHandlerType} from './models';
1
+ import type {TimerName, TimerStates, WorkHandlerType} from './models';
2
2
 
3
3
  /**
4
4
  * Buffer value to use when evaluating if a specific time is within a certain range
@@ -7,25 +7,15 @@ export const BUFFER_INTERVAL = 5;
7
7
 
8
8
  export const DEFAULT_TIMEOUT = 30_000;
9
9
 
10
- /**
11
- * Message to show when a when-timer is destroyed
12
- */
13
- export const MESSAGE_DESTROYED = 'Timer has already been destroyed';
14
-
15
10
  /**
16
11
  * Message to show when a when-timer is started
17
12
  */
18
13
  export const MESSAGE_STARTED = 'Timer has already been started';
19
14
 
20
- /**
21
- * A set of all active timers
22
- */
23
- export const TIMERS_ACTIVE = new Set<Timer>();
24
-
25
- /**
26
- * A set of timers that were paused due to the document being hidden
27
- */
28
- export const TIMERS_HIDDEN = new Set<Timer>();
15
+ export const STATES: TimerStates = {
16
+ active: new Set(),
17
+ hidden: new Set(),
18
+ };
29
19
 
30
20
  export const TYPE_REPEAT: TimerName = 'repeat';
31
21
 
package/src/global.ts CHANGED
@@ -1,29 +1,19 @@
1
- import {TIMERS_ACTIVE, TIMERS_HIDDEN, WORK_CONTINUE, WORK_PAUSE} from './constants';
1
+ import {STATES} from './constants';
2
+ import {onVisibilityChange} from './misc';
2
3
  import type {Timer} from './models';
3
4
 
4
5
  declare global {
5
6
  var _oscarpalmer_timer_debug: boolean | undefined;
7
+ /**
8
+ * All active timers _(or `undefined` if debugging is not enabled)_
9
+ */
6
10
  var _oscarpalmer_timers: Timer[] | undefined;
7
11
  }
8
12
 
9
- if (globalThis._oscarpalmer_timers == null) {
10
- Object.defineProperty(globalThis, '_oscarpalmer_timers', {
11
- get() {
12
- return globalThis._oscarpalmer_timer_debug ? [...TIMERS_ACTIVE] : [];
13
- },
14
- });
15
- }
16
-
17
- /* istanbul ignore next */
18
- document.addEventListener('visibilitychange', () => {
19
- const from = document.hidden ? TIMERS_ACTIVE : TIMERS_HIDDEN;
20
- const method = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
21
- const to = document.hidden ? TIMERS_HIDDEN : TIMERS_ACTIVE;
22
-
23
- for (const timer of from) {
24
- timer[method]();
25
- to.add(timer);
26
- }
27
-
28
- from.clear();
13
+ Object.defineProperty(globalThis, '_oscarpalmer_timers', {
14
+ get() {
15
+ return globalThis._oscarpalmer_timer_debug ? [...STATES.active].map(state => state.timer) : [];
16
+ },
29
17
  });
18
+
19
+ document.addEventListener('visibilitychange', onVisibilityChange);
package/src/index.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import './global';
2
2
 
3
- export {delay} from './delay';
4
3
  export {isRepeated, isTimer, isWaited, isWhen} from './is';
5
4
  export type {RepeatOptions, Timer, TimerOptions, When, WhenOptions} from './models';
6
5
  export {repeat} from './repeat';
package/src/misc.ts ADDED
@@ -0,0 +1,49 @@
1
+ import {noop} from '@oscarpalmer/atoms/function';
2
+ import type {GenericCallback} from '@oscarpalmer/atoms/models';
3
+ import {DEFAULT_TIMEOUT, STATES, WORK_CONTINUE, WORK_PAUSE} from './constants';
4
+ import type {TimerStates, TimerState} from './models';
5
+ import {work} from './work';
6
+
7
+ export function getCallback(value: unknown): GenericCallback {
8
+ return typeof value === 'function' ? (value as GenericCallback) : noop;
9
+ }
10
+
11
+ export function getValidNumber(value: unknown, defaultValue?: number): number {
12
+ const actualDefault = defaultValue ?? 0;
13
+
14
+ return typeof value === 'number' && value > actualDefault ? value : actualDefault;
15
+ }
16
+
17
+ export function getValidTimeout(value: unknown): number {
18
+ return typeof value === 'number' && value > 0 ? value : DEFAULT_TIMEOUT;
19
+ }
20
+
21
+ /* istanbul ignore next */
22
+ export function onVisibilityChange(): void {
23
+ const from = document.hidden ? STATES.active : STATES.hidden;
24
+ const type = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
25
+
26
+ for (const stored of from) {
27
+ const state = stored instanceof WeakRef ? stored.deref() : stored;
28
+
29
+ if (state != null) {
30
+ work(type, state, true);
31
+ }
32
+ }
33
+ }
34
+
35
+ export function updateStates(state: TimerState, key?: keyof TimerStates): void {
36
+ STATES.active.delete(state);
37
+
38
+ const hidden = [...STATES.hidden].find(stored => stored.deref() === state);
39
+
40
+ /* istanbul ignore next */
41
+ if (hidden != null) {
42
+ STATES.hidden.delete(hidden);
43
+ }
44
+
45
+ if (key != null) {
46
+ /* istanbul ignore next */
47
+ STATES[key].add((key === 'hidden' ? new WeakRef(state) : state) as never);
48
+ }
49
+ }
package/src/models.ts CHANGED
@@ -32,6 +32,8 @@ export type Timer = {
32
32
 
33
33
  /**
34
34
  * Is the timer destroyed?
35
+ *
36
+ * @deprecated Timers take care of their own cleanup; this always returns `false`
35
37
  */
36
38
  get destroyed(): boolean;
37
39
 
@@ -52,6 +54,8 @@ export type Timer = {
52
54
 
53
55
  /**
54
56
  * Destroy the timer
57
+ *
58
+ * @deprecated Timers take care of their own cleanup
55
59
  */
56
60
  destroy(): void;
57
61
 
@@ -91,11 +95,25 @@ export type TimerState = {
91
95
  callback: () => void;
92
96
  destroyed: boolean;
93
97
  elapsed: number;
94
- frame: number | undefined;
98
+ frame?: number;
95
99
  index: number;
100
+ name: TimerName;
101
+ options: TimerOptions;
96
102
  paused: boolean;
103
+ timer: Timer;
97
104
  total: number;
98
- trace: string | undefined;
105
+ trace?: string;
106
+ };
107
+
108
+ export type TimerStates = {
109
+ /**
110
+ * A set of all active timers
111
+ */
112
+ active: Set<TimerState>;
113
+ /**
114
+ * A set of timers that were paused due to the document being hidden
115
+ */
116
+ hidden: Set<WeakRef<TimerState>>;
99
117
  };
100
118
 
101
119
  export class TimerTrace extends Error {
@@ -114,6 +132,8 @@ export type When = {
114
132
 
115
133
  /**
116
134
  * Is the timer destroyed?
135
+ *
136
+ * @deprecated Timers take care of their own cleanup; this always returns `false`
117
137
  */
118
138
  get destroyed(): boolean;
119
139
 
@@ -134,6 +154,8 @@ export type When = {
134
154
 
135
155
  /**
136
156
  * Destroys the timer _(and stops it,if it was running)_
157
+ *
158
+ * @deprecated Timers take care of their own cleanup
137
159
  */
138
160
  destroy(): void;
139
161
 
package/src/repeat.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {TYPE_REPEAT} from './constants';
2
- import {getCallback, getValidNumber} from './get';
2
+ import {getCallback, getValidNumber} from './misc';
3
3
  import './global';
4
4
  import {type RepeatOptions, type Timer, TimerTrace} from './models';
5
5
  import {createTimer} from './timer';
package/src/timer.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import {noop} from '@oscarpalmer/atoms/function';
2
2
  import {WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP} from './constants';
3
- import type {Timer, TimerName, TimerOptions, TimerState, WorkHandlerType} from './models';
4
- import {stop, work} from './work';
3
+ import type {Timer, TimerName, TimerOptions, TimerState} from './models';
4
+ import {work} from './work';
5
5
 
6
6
  export function createTimer(
7
7
  name: TimerName,
@@ -9,36 +9,27 @@ export function createTimer(
9
9
  options: TimerOptions,
10
10
  start: boolean,
11
11
  ): Timer {
12
- function worker(type: WorkHandlerType): Timer {
13
- return work(
14
- type,
15
- {
16
- name,
17
- instance: instance as Timer,
18
- },
19
- state,
20
- options,
21
- );
22
- }
23
-
24
12
  const state: TimerState = {
25
13
  ...pick,
14
+ name,
15
+ options,
26
16
  active: false,
27
17
  destroyed: false,
28
18
  elapsed: 0,
29
19
  frame: undefined,
30
20
  index: 0,
31
21
  paused: false,
22
+ timer: undefined as never,
32
23
  total: 0,
33
24
  };
34
25
 
35
26
  const instance = {
36
- continue: () => worker(WORK_CONTINUE),
37
- destroy: () => destroyTimer(name, instance as Timer, state, options),
38
- pause: () => worker(WORK_PAUSE),
39
- restart: () => worker(WORK_RESTART),
40
- start: () => worker(WORK_START),
41
- stop: () => worker(WORK_STOP),
27
+ continue: () => work(WORK_CONTINUE, state),
28
+ destroy: noop,
29
+ pause: () => work(WORK_PAUSE, state),
30
+ restart: () => work(WORK_RESTART, state),
31
+ start: () => work(WORK_START, state),
32
+ stop: () => work(WORK_STOP, state),
42
33
  };
43
34
 
44
35
  Object.defineProperties(instance, {
@@ -52,7 +43,7 @@ export function createTimer(
52
43
  },
53
44
  destroyed: {
54
45
  enumerable: true,
55
- get: () => state.destroyed,
46
+ value: false,
56
47
  },
57
48
  paused: {
58
49
  enumerable: true,
@@ -64,28 +55,11 @@ export function createTimer(
64
55
  },
65
56
  });
66
57
 
67
- if (start) {
68
- instance.start();
69
- }
70
-
71
- return Object.freeze(instance) as Timer;
72
- }
58
+ state.timer = Object.freeze(instance) as Timer;
73
59
 
74
- function destroyTimer(
75
- name: TimerName,
76
- instance: Timer,
77
- state: TimerState,
78
- options: TimerOptions,
79
- ): void {
80
- state.destroyed = true;
81
-
82
- options.onAfter = noop;
83
- options.onError = noop;
84
- state.callback = noop;
85
-
86
- if (!globalThis._oscarpalmer_timer_debug) {
87
- state.trace = undefined;
60
+ if (start) {
61
+ state.timer.start();
88
62
  }
89
63
 
90
- stop({instance, name}, state, options);
64
+ return state.timer;
91
65
  }
package/src/wait.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {TYPE_WAIT} from './constants';
2
- import {getCallback, getValidNumber} from './get';
2
+ import {getCallback, getValidNumber} from './misc';
3
3
  import './global';
4
4
  import {type Timer, TimerTrace} from './models';
5
5
  import {createTimer} from './timer';