@oscarpalmer/timer 0.43.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/is.d.mts CHANGED
@@ -1,6 +1,4 @@
1
- import { Timer } from "./timer.mjs";
2
- import { When } from "./when.mjs";
3
-
1
+ import { Timer, When } from "./models.mjs";
4
2
  //#region src/is.d.ts
5
3
  /**
6
4
  * Is the value a repeating timer?
@@ -0,0 +1,10 @@
1
+ import { TimerState, TimerStates } from "./models.mjs";
2
+ import { GenericCallback } from "@oscarpalmer/atoms/models";
3
+ //#region src/misc.d.ts
4
+ declare function getCallback(value: unknown): GenericCallback;
5
+ declare function getValidNumber(value: unknown, defaultValue?: number): number;
6
+ declare function getValidTimeout(value: unknown): number;
7
+ declare function onVisibilityChange(): void;
8
+ declare function updateStates(state: TimerState, key?: keyof TimerStates): void;
9
+ //#endregion
10
+ export { getCallback, getValidNumber, getValidTimeout, onVisibilityChange, updateStates };
package/dist/misc.mjs ADDED
@@ -0,0 +1,34 @@
1
+ import { DEFAULT_TIMEOUT, STATES, WORK_CONTINUE, WORK_PAUSE } from "./constants.mjs";
2
+ import { work } from "./work.mjs";
3
+ import { noop } from "@oscarpalmer/atoms/function";
4
+ //#region src/misc.ts
5
+ function getCallback(value) {
6
+ return typeof value === "function" ? value : noop;
7
+ }
8
+ function getValidNumber(value, defaultValue) {
9
+ const actualDefault = defaultValue ?? 0;
10
+ return typeof value === "number" && value > actualDefault ? value : actualDefault;
11
+ }
12
+ function getValidTimeout(value) {
13
+ return typeof value === "number" && value > 0 ? value : DEFAULT_TIMEOUT;
14
+ }
15
+ /* istanbul ignore next */
16
+ function onVisibilityChange() {
17
+ const from = document.hidden ? STATES.active : STATES.hidden;
18
+ const type = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
19
+ for (const stored of from) {
20
+ const state = stored instanceof WeakRef ? stored.deref() : stored;
21
+ if (state != null) work(type, state, true);
22
+ }
23
+ }
24
+ function updateStates(state, key) {
25
+ STATES.active.delete(state);
26
+ const hidden = [...STATES.hidden].find((stored) => stored.deref() === state);
27
+ /* istanbul ignore next */
28
+ if (hidden != null) STATES.hidden.delete(hidden);
29
+ if (key != null)
30
+ /* istanbul ignore next */
31
+ STATES[key].add(key === "hidden" ? new WeakRef(state) : state);
32
+ }
33
+ //#endregion
34
+ export { getCallback, getValidNumber, getValidTimeout, onVisibilityChange, updateStates };
package/dist/models.d.mts CHANGED
@@ -1,5 +1,3 @@
1
- import { Timer } from "./timer.mjs";
2
-
3
1
  //#region src/models.d.ts
4
2
  /**
5
3
  * Options for a repeating timer
@@ -26,6 +24,53 @@ type RepeatOptions = {
26
24
  */
27
25
  timeout: number;
28
26
  };
27
+ type Timer = {
28
+ /**
29
+ * Is the timer active?
30
+ */
31
+ get active(): boolean;
32
+ /**
33
+ * Is the timer destroyed?
34
+ *
35
+ * @deprecated Timers take care of their own cleanup; this always returns `false`
36
+ */
37
+ get destroyed(): boolean;
38
+ /**
39
+ * Is the timer paused?
40
+ */
41
+ get paused(): boolean;
42
+ /**
43
+ * Get the timer's origin _(if debugging is enabled)_
44
+ */
45
+ get trace(): string | undefined;
46
+ /**
47
+ * Continue running the timer _(if it's paused)_
48
+ */
49
+ continue(): Timer;
50
+ /**
51
+ * Destroy the timer
52
+ *
53
+ * @deprecated Timers take care of their own cleanup
54
+ */
55
+ destroy(): void;
56
+ /**
57
+ * Pause the timer _(if it's running)_
58
+ */
59
+ pause(): Timer;
60
+ /**
61
+ * Restart the timer _(or start it, if it's not running)_
62
+ */
63
+ restart(): Timer;
64
+ /**
65
+ * Start the timer _(if it's not running)_
66
+ */
67
+ start(): Timer;
68
+ /**
69
+ * Stop the timer _(if it's running)_
70
+ */
71
+ stop(): Timer;
72
+ };
73
+ type TimerName = 'repeat' | 'wait' | 'when';
29
74
  type TimerOptions = {
30
75
  onAfter: ((finished: boolean) => void) | undefined;
31
76
  onError: (() => void) | undefined;
@@ -38,16 +83,73 @@ type TimerState = {
38
83
  callback: () => void;
39
84
  destroyed: boolean;
40
85
  elapsed: number;
41
- frame: number | undefined;
86
+ frame?: number;
42
87
  index: number;
88
+ name: TimerName;
89
+ options: TimerOptions;
43
90
  paused: boolean;
91
+ timer: Timer;
44
92
  total: number;
45
- trace: string | undefined;
93
+ trace?: string;
94
+ };
95
+ type TimerStates = {
96
+ /**
97
+ * A set of all active timers
98
+ */
99
+ active: Set<TimerState>;
100
+ /**
101
+ * A set of timers that were paused due to the document being hidden
102
+ */
103
+ hidden: Set<WeakRef<TimerState>>;
46
104
  };
47
105
  declare class TimerTrace extends Error {
48
106
  constructor();
49
107
  }
50
- type TimerType = 'repeat' | 'wait' | 'when';
108
+ type When = {
109
+ /**
110
+ * Is the timer active?
111
+ */
112
+ get active(): boolean;
113
+ /**
114
+ * Is the timer destroyed?
115
+ *
116
+ * @deprecated Timers take care of their own cleanup; this always returns `false`
117
+ */
118
+ get destroyed(): boolean;
119
+ /**
120
+ * Is the timer paused?
121
+ */
122
+ get paused(): boolean;
123
+ /**
124
+ * Get the timer's origin _(if debugging is enabled)_
125
+ */
126
+ get trace(): string | undefined;
127
+ /**
128
+ * Continues the timer _(if it was paused)_
129
+ */
130
+ continue(): When;
131
+ /**
132
+ * Destroys the timer _(and stops it,if it was running)_
133
+ *
134
+ * @deprecated Timers take care of their own cleanup
135
+ */
136
+ destroy(): void;
137
+ /**
138
+ * Pauses the timer _(if it was running)_
139
+ */
140
+ pause(): When;
141
+ /**
142
+ * Start the timer
143
+ *
144
+ * @param resolve Optional resolve callback
145
+ * @returns Promise that resolves when the condition is met
146
+ */
147
+ start(resolve?: (() => void) | null): Promise<void>;
148
+ /**
149
+ * Stops the timer _(if it was running)_
150
+ */
151
+ stop(): When;
152
+ };
51
153
  /**
52
154
  * Options for a conditional timer
53
155
  */
@@ -69,14 +171,15 @@ type WhenState = {
69
171
  promise: Promise<void>;
70
172
  rejecter?: () => void;
71
173
  resolver?: () => void;
174
+ result: boolean;
72
175
  started: boolean;
73
176
  timer: Timer;
74
177
  };
75
178
  type WorkHandler = (type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions) => Timer;
76
179
  type WorkHandlerTimer = {
77
180
  instance: Timer;
78
- type: TimerType;
181
+ name: TimerName;
79
182
  };
80
183
  type WorkHandlerType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
81
184
  //#endregion
82
- export { RepeatOptions, TimerOptions, TimerState, TimerTrace, TimerType, WhenOptions, WhenState, WorkHandler, WorkHandlerTimer, WorkHandlerType };
185
+ export { RepeatOptions, Timer, TimerName, TimerOptions, TimerState, TimerStates, TimerTrace, When, WhenOptions, WhenState, WorkHandler, WorkHandlerTimer, WorkHandlerType };
package/dist/repeat.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { Timer } from "./timer.mjs";
2
- import { RepeatOptions } from "./models.mjs";
1
+ import { RepeatOptions, Timer } from "./models.mjs";
2
+ import "./global.mjs";
3
3
  //#region src/repeat.d.ts
4
4
  /**
5
5
  * Create a repeating timer
@@ -10,4 +10,4 @@ import { RepeatOptions } from "./models.mjs";
10
10
  */
11
11
  declare function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer;
12
12
  //#endregion
13
- export { type RepeatOptions, type Timer, repeat };
13
+ export { repeat };
package/dist/repeat.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import { TYPE_REPEAT } 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
- import { Timer } from "./timer.mjs";
5
+ import { createTimer } from "./timer.mjs";
6
6
  //#region src/repeat.ts
7
7
  /**
8
8
  * Create a repeating timer
@@ -12,7 +12,7 @@ import { Timer } from "./timer.mjs";
12
12
  * @returns Timer instance
13
13
  */
14
14
  function repeat(callback, options) {
15
- return new Timer(TYPE_REPEAT, {
15
+ return createTimer(TYPE_REPEAT, {
16
16
  callback: getCallback(callback),
17
17
  trace: new TimerTrace().stack
18
18
  }, {
package/dist/timer.d.mts CHANGED
@@ -1,52 +1,5 @@
1
- import { TimerOptions, TimerState, TimerType } from "./models.mjs";
2
-
1
+ import { Timer, TimerName, TimerOptions, TimerState } from "./models.mjs";
3
2
  //#region src/timer.d.ts
4
- declare class Timer {
5
- #private;
6
- protected readonly options: TimerOptions;
7
- private readonly $timer;
8
- protected readonly state: TimerState;
9
- /**
10
- * Is the timer active?
11
- */
12
- get active(): boolean;
13
- /**
14
- * Is the timer destroyed?
15
- */
16
- get destroyed(): boolean;
17
- /**
18
- * Is the timer paused?
19
- */
20
- get paused(): boolean;
21
- /**
22
- * Get the timer's origin _(if debugging is enabled)_
23
- */
24
- get trace(): string | undefined;
25
- constructor(type: TimerType, state: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean);
26
- /**
27
- * Continue running the timer _(if it's paused)_
28
- */
29
- continue(): Timer;
30
- /**
31
- * Destroy the timer
32
- */
33
- destroy(): void;
34
- /**
35
- * Pause the timer _(if it's running)_
36
- */
37
- pause(): Timer;
38
- /**
39
- * Restart the timer _(or start it, if it's not running)_
40
- */
41
- restart(): Timer;
42
- /**
43
- * Start the timer _(if it's not running)_
44
- */
45
- start(): Timer;
46
- /**
47
- * Stop the timer _(if it's running)_
48
- */
49
- stop(): Timer;
50
- }
3
+ declare function createTimer(name: TimerName, pick: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean): Timer;
51
4
  //#endregion
52
- export { Timer };
5
+ export { createTimer };
package/dist/timer.mjs CHANGED
@@ -1,98 +1,54 @@
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
- var Timer = class {
6
- state;
7
- /**
8
- * Is the timer active?
9
- */
10
- get active() {
11
- return this.state.active;
12
- }
13
- /**
14
- * Is the timer destroyed?
15
- */
16
- get destroyed() {
17
- return this.state.destroyed;
18
- }
19
- /**
20
- * Is the timer paused?
21
- */
22
- get paused() {
23
- return this.state.paused;
24
- }
25
- /**
26
- * Get the timer's origin _(if debugging is enabled)_
27
- */
28
- get trace() {
29
- return globalThis._oscarpalmer_timer_debug ?? false ? this.state.trace : void 0;
30
- }
31
- constructor(type, state, options, start) {
32
- this.options = options;
33
- Object.defineProperty(this, "$timer", { value: type });
34
- this.state = {
35
- ...state,
36
- active: false,
37
- destroyed: false,
38
- elapsed: 0,
39
- frame: void 0,
40
- index: 0,
41
- paused: false,
42
- total: 0
43
- };
44
- if (start) this.start();
45
- }
46
- /**
47
- * Continue running the timer _(if it's paused)_
48
- */
49
- continue() {
50
- return this.#work(WORK_CONTINUE);
51
- }
52
- /**
53
- * Destroy the timer
54
- */
55
- destroy() {
56
- this.state.destroyed = true;
57
- this.options.onAfter = noop;
58
- this.options.onError = noop;
59
- this.state.callback = noop;
60
- if (!globalThis._oscarpalmer_timer_debug) this.state.trace = void 0;
61
- stop({
62
- instance: this,
63
- type: this.$timer
64
- }, this.state, this.options);
65
- }
66
- /**
67
- * Pause the timer _(if it's running)_
68
- */
69
- pause() {
70
- return this.#work(WORK_PAUSE);
71
- }
72
- /**
73
- * Restart the timer _(or start it, if it's not running)_
74
- */
75
- restart() {
76
- return this.#work(WORK_RESTART);
77
- }
78
- /**
79
- * Start the timer _(if it's not running)_
80
- */
81
- start() {
82
- return this.#work(WORK_START);
83
- }
84
- /**
85
- * Stop the timer _(if it's running)_
86
- */
87
- stop() {
88
- return this.#work(WORK_STOP);
89
- }
90
- #work(type) {
91
- return work(type, {
92
- instance: this,
93
- type: this.$timer
94
- }, this.state, this.options);
95
- }
96
- };
5
+ function createTimer(name, pick, options, start) {
6
+ const state = {
7
+ ...pick,
8
+ name,
9
+ options,
10
+ active: false,
11
+ destroyed: false,
12
+ elapsed: 0,
13
+ frame: void 0,
14
+ index: 0,
15
+ paused: false,
16
+ timer: void 0,
17
+ total: 0
18
+ };
19
+ const instance = {
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)
26
+ };
27
+ Object.defineProperties(instance, {
28
+ $timer: {
29
+ enumerable: false,
30
+ value: name
31
+ },
32
+ active: {
33
+ enumerable: true,
34
+ get: () => state.active
35
+ },
36
+ destroyed: {
37
+ enumerable: true,
38
+ value: false
39
+ },
40
+ paused: {
41
+ enumerable: true,
42
+ get: () => state.paused
43
+ },
44
+ trace: {
45
+ enumerable: true,
46
+ get: () => globalThis._oscarpalmer_timer_debug ?? false ? state.trace : void 0
47
+ }
48
+ });
49
+ state.timer = Object.freeze(instance);
50
+ if (start) state.timer.start();
51
+ return state.timer;
52
+ }
97
53
  //#endregion
98
- export { Timer };
54
+ export { createTimer };
package/dist/wait.d.mts CHANGED
@@ -1,4 +1,5 @@
1
- import { Timer } from "./timer.mjs";
1
+ import { Timer } from "./models.mjs";
2
+ import "./global.mjs";
2
3
  //#region src/wait.d.ts
3
4
  /**
4
5
  * Create a waiting timer
@@ -8,4 +9,4 @@ import { Timer } from "./timer.mjs";
8
9
  */
9
10
  declare function wait(callback: () => void, time?: number): Timer;
10
11
  //#endregion
11
- export { type Timer, wait };
12
+ export { wait };
package/dist/wait.mjs CHANGED
@@ -1,8 +1,8 @@
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
- import { Timer } from "./timer.mjs";
5
+ import { createTimer } from "./timer.mjs";
6
6
  //#region src/wait.ts
7
7
  /**
8
8
  * Create a waiting timer
@@ -11,7 +11,7 @@ import { Timer } from "./timer.mjs";
11
11
  * @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
12
12
  */
13
13
  function wait(callback, time) {
14
- return new Timer(TYPE_WAIT, {
14
+ return createTimer(TYPE_WAIT, {
15
15
  callback: getCallback(callback),
16
16
  trace: new TimerTrace().stack
17
17
  }, {
package/dist/when.d.mts CHANGED
@@ -1,49 +1,6 @@
1
- import { WhenOptions } from "./models.mjs";
1
+ import { When, WhenOptions } from "./models.mjs";
2
+ import "./global.mjs";
2
3
  //#region src/when.d.ts
3
- declare class When {
4
- private readonly $timer;
5
- private readonly state;
6
- /**
7
- * Is the timer active?
8
- */
9
- get active(): boolean;
10
- /**
11
- * Is the timer destroyed?
12
- */
13
- get destroyed(): boolean;
14
- /**
15
- * Is the timer paused?
16
- */
17
- get paused(): boolean;
18
- /**
19
- * Get the timer's origin _(if debugging is enabled)_
20
- */
21
- get trace(): string | undefined;
22
- constructor(condition: () => boolean, options?: Partial<WhenOptions>);
23
- /**
24
- * Continues the timer _(if it was paused)_
25
- */
26
- continue(): When;
27
- /**
28
- * Destroys the timer _(and stops it,if it was running)_
29
- */
30
- destroy(): void;
31
- /**
32
- * Pauses the timer _(if it was running)_
33
- */
34
- pause(): When;
35
- /**
36
- * Start the timer
37
- *
38
- * @param resolve Optional resolve callback
39
- * @returns Promise that resolves when the condition is met
40
- */
41
- start(resolve?: (() => void) | null): Promise<void>;
42
- /**
43
- * Stops the timer _(if it was running)_
44
- */
45
- stop(): When;
46
- }
47
4
  /**
48
5
  * Create a conditional timer
49
6
  * @param condition Condition to check
@@ -52,4 +9,4 @@ declare class When {
52
9
  */
53
10
  declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
54
11
  //#endregion
55
- export { type When, when };
12
+ export { when };