@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.
@@ -1,35 +1,22 @@
1
- import { Timer } from "./timer.mjs";
2
- import { TimerType, WorkHandlerType } from "./models.mjs";
3
-
1
+ import { TimerName, TimerStates, WorkHandlerType } from "./models.mjs";
4
2
  //#region src/constants.d.ts
5
3
  /**
6
4
  * Buffer value to use when evaluating if a specific time is within a certain range
7
5
  */
8
6
  declare const BUFFER_INTERVAL = 5;
9
7
  declare const DEFAULT_TIMEOUT = 30000;
10
- /**
11
- * Message to show when a when-timer is destroyed
12
- */
13
- declare const MESSAGE_DESTROYED = "Timer has already been destroyed";
14
8
  /**
15
9
  * Message to show when a when-timer is started
16
10
  */
17
11
  declare const MESSAGE_STARTED = "Timer has already been started";
18
- /**
19
- * A set of all active timers
20
- */
21
- declare const TIMERS_ACTIVE: Set<Timer>;
22
- /**
23
- * A set of timers that were paused due to the document being hidden
24
- */
25
- declare const TIMERS_HIDDEN: Set<Timer>;
26
- declare const TYPE_REPEAT: TimerType;
27
- declare const TYPE_WAIT: TimerType;
28
- declare const TYPE_WHEN: TimerType;
12
+ declare const STATES: TimerStates;
13
+ declare const TYPE_REPEAT: TimerName;
14
+ declare const TYPE_WAIT: TimerName;
15
+ declare const TYPE_WHEN: TimerName;
29
16
  declare const WORK_CONTINUE: WorkHandlerType;
30
17
  declare const WORK_PAUSE: WorkHandlerType;
31
18
  declare const WORK_RESTART: WorkHandlerType;
32
19
  declare const WORK_START: WorkHandlerType;
33
20
  declare const WORK_STOP: WorkHandlerType;
34
21
  //#endregion
35
- export { BUFFER_INTERVAL, DEFAULT_TIMEOUT, MESSAGE_DESTROYED, MESSAGE_STARTED, TIMERS_ACTIVE, TIMERS_HIDDEN, TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP };
22
+ export { BUFFER_INTERVAL, DEFAULT_TIMEOUT, MESSAGE_STARTED, STATES, TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP };
@@ -5,21 +5,13 @@
5
5
  const BUFFER_INTERVAL = 5;
6
6
  const DEFAULT_TIMEOUT = 3e4;
7
7
  /**
8
- * Message to show when a when-timer is destroyed
9
- */
10
- const MESSAGE_DESTROYED = "Timer has already been destroyed";
11
- /**
12
8
  * Message to show when a when-timer is started
13
9
  */
14
10
  const MESSAGE_STARTED = "Timer has already been started";
15
- /**
16
- * A set of all active timers
17
- */
18
- const TIMERS_ACTIVE = /* @__PURE__ */ new Set();
19
- /**
20
- * A set of timers that were paused due to the document being hidden
21
- */
22
- const TIMERS_HIDDEN = /* @__PURE__ */ new Set();
11
+ const STATES = {
12
+ active: /* @__PURE__ */ new Set(),
13
+ hidden: /* @__PURE__ */ new Set()
14
+ };
23
15
  const TYPE_REPEAT = "repeat";
24
16
  const TYPE_WAIT = "wait";
25
17
  const TYPE_WHEN = "when";
@@ -29,4 +21,4 @@ const WORK_RESTART = "restart";
29
21
  const WORK_START = "start";
30
22
  const WORK_STOP = "stop";
31
23
  //#endregion
32
- export { BUFFER_INTERVAL, DEFAULT_TIMEOUT, MESSAGE_DESTROYED, MESSAGE_STARTED, TIMERS_ACTIVE, TIMERS_HIDDEN, TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP };
24
+ export { BUFFER_INTERVAL, DEFAULT_TIMEOUT, MESSAGE_STARTED, STATES, TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP };
package/dist/global.d.mts CHANGED
@@ -1,7 +1,9 @@
1
- import { Timer } from "./timer.mjs";
2
-
1
+ import { Timer } from "./models.mjs";
3
2
  //#region src/global.d.ts
4
3
  declare global {
5
4
  var _oscarpalmer_timer_debug: boolean | undefined;
5
+ /**
6
+ * All active timers _(or `undefined` if debugging is not enabled)_
7
+ */
6
8
  var _oscarpalmer_timers: Timer[] | undefined;
7
9
  }
package/dist/global.mjs CHANGED
@@ -1,18 +1,9 @@
1
- import { TIMERS_ACTIVE, TIMERS_HIDDEN, WORK_CONTINUE, WORK_PAUSE } from "./constants.mjs";
1
+ import { STATES } from "./constants.mjs";
2
+ import { onVisibilityChange } from "./misc.mjs";
2
3
  //#region src/global.ts
3
- if (globalThis._oscarpalmer_timers == null) Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
4
- return globalThis._oscarpalmer_timer_debug ? [...TIMERS_ACTIVE] : [];
4
+ Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
5
+ return globalThis._oscarpalmer_timer_debug ? [...STATES.active].map((state) => state.timer) : [];
5
6
  } });
6
- /* istanbul ignore next */
7
- document.addEventListener("visibilitychange", () => {
8
- const from = document.hidden ? TIMERS_ACTIVE : TIMERS_HIDDEN;
9
- const method = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
10
- const to = document.hidden ? TIMERS_HIDDEN : TIMERS_ACTIVE;
11
- for (const timer of from) {
12
- timer[method]();
13
- to.add(timer);
14
- }
15
- from.clear();
16
- });
7
+ document.addEventListener("visibilitychange", onVisibilityChange);
17
8
  //#endregion
18
9
  export {};
package/dist/index.d.mts CHANGED
@@ -24,55 +24,15 @@ type RepeatOptions = {
24
24
  */
25
25
  timeout: number;
26
26
  };
27
- type TimerOptions = {
28
- onAfter: ((finished: boolean) => void) | undefined;
29
- onError: (() => void) | undefined;
30
- count: number;
31
- interval: number;
32
- timeout: number;
33
- };
34
- type TimerState = {
35
- active: boolean;
36
- callback: () => void;
37
- destroyed: boolean;
38
- elapsed: number;
39
- frame: number | undefined;
40
- index: number;
41
- paused: boolean;
42
- total: number;
43
- trace: string | undefined;
44
- };
45
- type TimerType = 'repeat' | 'wait' | 'when';
46
- /**
47
- * Options for a conditional timer
48
- */
49
- type WhenOptions = {
50
- /**
51
- * How many times the timer should check the condition
52
- */
53
- count: number;
54
- /**
55
- * Then interval between each condtional check
56
- */
57
- interval: number;
58
- /**
59
- * The timeout for the timer _(any value above `0` will enable the timeout)_
60
- */
61
- timeout: number;
62
- };
63
- //#endregion
64
- //#region src/timer.d.ts
65
- declare class Timer {
66
- #private;
67
- protected readonly options: TimerOptions;
68
- private readonly $timer;
69
- protected readonly state: TimerState;
27
+ type Timer = {
70
28
  /**
71
29
  * Is the timer active?
72
30
  */
73
31
  get active(): boolean;
74
32
  /**
75
33
  * Is the timer destroyed?
34
+ *
35
+ * @deprecated Timers take care of their own cleanup; this always returns `false`
76
36
  */
77
37
  get destroyed(): boolean;
78
38
  /**
@@ -83,13 +43,14 @@ declare class Timer {
83
43
  * Get the timer's origin _(if debugging is enabled)_
84
44
  */
85
45
  get trace(): string | undefined;
86
- constructor(type: TimerType, state: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean);
87
46
  /**
88
47
  * Continue running the timer _(if it's paused)_
89
48
  */
90
49
  continue(): Timer;
91
50
  /**
92
51
  * Destroy the timer
52
+ *
53
+ * @deprecated Timers take care of their own cleanup
93
54
  */
94
55
  destroy(): void;
95
56
  /**
@@ -108,56 +69,23 @@ declare class Timer {
108
69
  * Stop the timer _(if it's running)_
109
70
  */
110
71
  stop(): Timer;
111
- }
112
- //#endregion
113
- //#region src/global.d.ts
114
- declare global {
115
- var _oscarpalmer_timer_debug: boolean | undefined;
116
- var _oscarpalmer_timers: Timer[] | undefined;
117
- }
118
- //#endregion
119
- //#region node_modules/@oscarpalmer/atoms/dist/promise/models.d.mts
120
- /**
121
- * Options for a _Promise_-handling function
122
- */
123
- type PromiseOptions = {
124
- /**
125
- * AbortSignal for aborting the _Promise_; when aborted, the _Promise_ will reject with the reason of the signal
126
- */
127
- signal?: AbortSignal;
128
- /**
129
- * How long to wait for _(in milliseconds; defaults to `0`)_
130
- */
131
- time?: number;
132
72
  };
133
- //#endregion
134
- //#region node_modules/@oscarpalmer/atoms/dist/promise/delay.d.mts
135
- //#region src/promise/delay.d.ts
136
- /**
137
- * Create a delayed promise that resolves after a certain amount of time, or rejects if aborted
138
- *
139
- * @param options Options for the delay
140
- * @returns Delayed promise
141
- */
142
- declare function delay(options?: PromiseOptions): Promise<void>;
143
- /**
144
- * Create a delayed promise that resolves after a certain amount of time
145
- *
146
- * @param time How long to wait for _(in milliseconds; defaults to `0`)_
147
- * @returns Delayed promise
148
- */
149
- declare function delay(time?: number): Promise<void>; //#endregion
150
- //#endregion
151
- //#region src/when.d.ts
152
- declare class When {
153
- private readonly $timer;
154
- private readonly state;
73
+ type TimerOptions = {
74
+ onAfter: ((finished: boolean) => void) | undefined;
75
+ onError: (() => void) | undefined;
76
+ count: number;
77
+ interval: number;
78
+ timeout: number;
79
+ };
80
+ type When = {
155
81
  /**
156
82
  * Is the timer active?
157
83
  */
158
84
  get active(): boolean;
159
85
  /**
160
86
  * Is the timer destroyed?
87
+ *
88
+ * @deprecated Timers take care of their own cleanup; this always returns `false`
161
89
  */
162
90
  get destroyed(): boolean;
163
91
  /**
@@ -168,13 +96,14 @@ declare class When {
168
96
  * Get the timer's origin _(if debugging is enabled)_
169
97
  */
170
98
  get trace(): string | undefined;
171
- constructor(condition: () => boolean, options?: Partial<WhenOptions>);
172
99
  /**
173
100
  * Continues the timer _(if it was paused)_
174
101
  */
175
102
  continue(): When;
176
103
  /**
177
104
  * Destroys the timer _(and stops it,if it was running)_
105
+ *
106
+ * @deprecated Timers take care of their own cleanup
178
107
  */
179
108
  destroy(): void;
180
109
  /**
@@ -192,14 +121,33 @@ declare class When {
192
121
  * Stops the timer _(if it was running)_
193
122
  */
194
123
  stop(): When;
195
- }
124
+ };
196
125
  /**
197
- * Create a conditional timer
198
- * @param condition Condition to check
199
- * @param options Timer options
200
- * @returns Timer instance
126
+ * Options for a conditional timer
201
127
  */
202
- declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
128
+ type WhenOptions = {
129
+ /**
130
+ * How many times the timer should check the condition
131
+ */
132
+ count: number;
133
+ /**
134
+ * Then interval between each condtional check
135
+ */
136
+ interval: number;
137
+ /**
138
+ * The timeout for the timer _(any value above `0` will enable the timeout)_
139
+ */
140
+ timeout: number;
141
+ };
142
+ //#endregion
143
+ //#region src/global.d.ts
144
+ declare global {
145
+ var _oscarpalmer_timer_debug: boolean | undefined;
146
+ /**
147
+ * All active timers _(or `undefined` if debugging is not enabled)_
148
+ */
149
+ var _oscarpalmer_timers: Timer[] | undefined;
150
+ }
203
151
  //#endregion
204
152
  //#region src/is.d.ts
205
153
  /**
@@ -246,4 +194,13 @@ declare function repeat(callback: (index: number) => void, options?: Partial<Rep
246
194
  */
247
195
  declare function wait(callback: () => void, time?: number): Timer;
248
196
  //#endregion
249
- export { type PromiseOptions, type RepeatOptions, type Timer, type When, delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
197
+ //#region src/when.d.ts
198
+ /**
199
+ * Create a conditional timer
200
+ * @param condition Condition to check
201
+ * @param options Timer options
202
+ * @returns Timer instance
203
+ */
204
+ declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
205
+ //#endregion
206
+ export { type RepeatOptions, type Timer, type TimerOptions, type When, type WhenOptions, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };