@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.
@@ -1,26 +1,15 @@
1
- import { Timer, TimerName, WorkHandlerType } from "./models.mjs";
1
+ import { TimerName, TimerStates, WorkHandlerType } from "./models.mjs";
2
2
  //#region src/constants.d.ts
3
3
  /**
4
4
  * Buffer value to use when evaluating if a specific time is within a certain range
5
5
  */
6
6
  declare const BUFFER_INTERVAL = 5;
7
7
  declare const DEFAULT_TIMEOUT = 30000;
8
- /**
9
- * Message to show when a when-timer is destroyed
10
- */
11
- declare const MESSAGE_DESTROYED = "Timer has already been destroyed";
12
8
  /**
13
9
  * Message to show when a when-timer is started
14
10
  */
15
11
  declare const MESSAGE_STARTED = "Timer has already been started";
16
- /**
17
- * A set of all active timers
18
- */
19
- declare const TIMERS_ACTIVE: Set<Timer>;
20
- /**
21
- * A set of timers that were paused due to the document being hidden
22
- */
23
- declare const TIMERS_HIDDEN: Set<Timer>;
12
+ declare const STATES: TimerStates;
24
13
  declare const TYPE_REPEAT: TimerName;
25
14
  declare const TYPE_WAIT: TimerName;
26
15
  declare const TYPE_WHEN: TimerName;
@@ -30,4 +19,4 @@ declare const WORK_RESTART: WorkHandlerType;
30
19
  declare const WORK_START: WorkHandlerType;
31
20
  declare const WORK_STOP: WorkHandlerType;
32
21
  //#endregion
33
- 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
@@ -2,5 +2,8 @@ import { Timer } from "./models.mjs";
2
2
  //#region src/global.d.ts
3
3
  declare global {
4
4
  var _oscarpalmer_timer_debug: boolean | undefined;
5
+ /**
6
+ * All active timers _(or `undefined` if debugging is not enabled)_
7
+ */
5
8
  var _oscarpalmer_timers: Timer[] | undefined;
6
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
@@ -31,6 +31,8 @@ type Timer = {
31
31
  get active(): boolean;
32
32
  /**
33
33
  * Is the timer destroyed?
34
+ *
35
+ * @deprecated Timers take care of their own cleanup; this always returns `false`
34
36
  */
35
37
  get destroyed(): boolean;
36
38
  /**
@@ -47,6 +49,8 @@ type Timer = {
47
49
  continue(): Timer;
48
50
  /**
49
51
  * Destroy the timer
52
+ *
53
+ * @deprecated Timers take care of their own cleanup
50
54
  */
51
55
  destroy(): void;
52
56
  /**
@@ -80,6 +84,8 @@ type When = {
80
84
  get active(): boolean;
81
85
  /**
82
86
  * Is the timer destroyed?
87
+ *
88
+ * @deprecated Timers take care of their own cleanup; this always returns `false`
83
89
  */
84
90
  get destroyed(): boolean;
85
91
  /**
@@ -96,6 +102,8 @@ type When = {
96
102
  continue(): When;
97
103
  /**
98
104
  * Destroys the timer _(and stops it,if it was running)_
105
+ *
106
+ * @deprecated Timers take care of their own cleanup
99
107
  */
100
108
  destroy(): void;
101
109
  /**
@@ -135,40 +143,11 @@ type WhenOptions = {
135
143
  //#region src/global.d.ts
136
144
  declare global {
137
145
  var _oscarpalmer_timer_debug: boolean | undefined;
138
- var _oscarpalmer_timers: Timer[] | undefined;
139
- }
140
- //#endregion
141
- //#region node_modules/@oscarpalmer/atoms/dist/promise/models.d.mts
142
- /**
143
- * Options for a _Promise_-handling function
144
- */
145
- type PromiseOptions = {
146
- /**
147
- * AbortSignal for aborting the _Promise_; when aborted, the _Promise_ will reject with the reason of the signal
148
- */
149
- signal?: AbortSignal;
150
146
  /**
151
- * How long to wait for _(in milliseconds; defaults to `0`)_
147
+ * All active timers _(or `undefined` if debugging is not enabled)_
152
148
  */
153
- time?: number;
154
- };
155
- //#endregion
156
- //#region node_modules/@oscarpalmer/atoms/dist/promise/delay.d.mts
157
- //#region src/promise/delay.d.ts
158
- /**
159
- * Create a delayed promise that resolves after a certain amount of time, or rejects if aborted
160
- *
161
- * @param options Options for the delay
162
- * @returns Delayed promise
163
- */
164
- declare function delay(options?: PromiseOptions): Promise<void>;
165
- /**
166
- * Create a delayed promise that resolves after a certain amount of time
167
- *
168
- * @param time How long to wait for _(in milliseconds; defaults to `0`)_
169
- * @returns Delayed promise
170
- */
171
- declare function delay(time?: number): Promise<void>;
149
+ var _oscarpalmer_timers: Timer[] | undefined;
150
+ }
172
151
  //#endregion
173
152
  //#region src/is.d.ts
174
153
  /**
@@ -224,4 +203,4 @@ declare function wait(callback: () => void, time?: number): Timer;
224
203
  */
225
204
  declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
226
205
  //#endregion
227
- export { type RepeatOptions, type Timer, type TimerOptions, type When, type WhenOptions, delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
206
+ export { type RepeatOptions, type Timer, type TimerOptions, type When, type WhenOptions, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };