@oscarpalmer/timer 0.33.0 → 0.35.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/src/global.ts CHANGED
@@ -1,4 +1,9 @@
1
- import {activeTimers, hiddenTimers} from './constants';
1
+ import {
2
+ activeTimers,
3
+ hiddenTimers,
4
+ WORK_CONTINUE,
5
+ WORK_PAUSE,
6
+ } from './constants';
2
7
  import type {Timer} from './timer';
3
8
 
4
9
  declare global {
@@ -15,16 +20,14 @@ if (globalThis._oscarpalmer_timers == null) {
15
20
  }
16
21
 
17
22
  document.addEventListener('visibilitychange', () => {
18
- if (document.hidden) {
19
- for (const timer of activeTimers) {
20
- hiddenTimers.add(timer);
21
- timer.pause();
22
- }
23
- } else {
24
- for (const timer of hiddenTimers) {
25
- timer.continue();
26
- }
23
+ const from = document.hidden ? activeTimers : hiddenTimers;
24
+ const method = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
25
+ const to = document.hidden ? hiddenTimers : activeTimers;
27
26
 
28
- hiddenTimers.clear();
27
+ for (const timer of from) {
28
+ timer[method]();
29
+ to.add(timer);
29
30
  }
31
+
32
+ from.clear();
30
33
  });
package/src/is.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type {PlainObject} from '@oscarpalmer/atoms/models';
2
+ import {TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN} from './constants';
2
3
  import type {Timer} from './timer';
3
4
  import type {When} from './when';
4
5
 
@@ -10,26 +11,26 @@ function is(names: string[], value: unknown) {
10
11
  * Is the value a repeating timer?
11
12
  */
12
13
  export function isRepeated(value: unknown): value is Timer {
13
- return is(['repeat'], value);
14
+ return is([TYPE_REPEAT], value);
14
15
  }
15
16
 
16
17
  /**
17
18
  * Is the value a timer?
18
19
  */
19
20
  export function isTimer(value: unknown): value is Timer {
20
- return is(['repeat', 'wait'], value);
21
+ return is([TYPE_REPEAT, TYPE_WAIT], value);
21
22
  }
22
23
 
23
24
  /**
24
25
  * Is the value a waiting timer?
25
26
  */
26
27
  export function isWaited(value: unknown): value is Timer {
27
- return is(['wait'], value);
28
+ return is([TYPE_WAIT], value);
28
29
  }
29
30
 
30
31
  /**
31
32
  * Is the value a conditional timer?
32
33
  */
33
34
  export function isWhen(value: unknown): value is When {
34
- return is(['when'], value) && typeof (value as When).then === 'function';
35
+ return is([TYPE_WHEN], value) && typeof (value as When).then === 'function';
35
36
  }
package/src/repeat.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {milliseconds} from './constants';
1
+ import {milliseconds, TYPE_REPEAT} from './constants';
2
2
  import {getCallback, getValidNumber} from './get';
3
3
  import './global';
4
4
  import {type RepeatOptions, TimerTrace} from './models';
@@ -12,7 +12,7 @@ export function repeat(
12
12
  options?: Partial<RepeatOptions>,
13
13
  ): Timer {
14
14
  return new Timer(
15
- 'repeat',
15
+ TYPE_REPEAT,
16
16
  {
17
17
  callback: getCallback(callback),
18
18
  trace: new TimerTrace().stack,
package/src/timer.ts CHANGED
@@ -1,4 +1,11 @@
1
1
  import {noop} from '@oscarpalmer/atoms/function';
2
+ import {
3
+ WORK_CONTINUE,
4
+ WORK_PAUSE,
5
+ WORK_RESTART,
6
+ WORK_START,
7
+ WORK_STOP,
8
+ } from './constants';
2
9
  import type {
3
10
  TimerOptions,
4
11
  TimerState,
@@ -37,7 +44,7 @@ export class Timer {
37
44
  * Get the timer's origin _(if debugging is enabled)_
38
45
  */
39
46
  get trace(): string | undefined {
40
- return globalThis._oscarpalmer_timer_debug ?? false
47
+ return (globalThis._oscarpalmer_timer_debug ?? false)
41
48
  ? this.state.trace
42
49
  : undefined;
43
50
  }
@@ -70,7 +77,7 @@ export class Timer {
70
77
  * Continue running the timer _(if it's paused)_
71
78
  */
72
79
  continue(): Timer {
73
- return this.#work('continue');
80
+ return this.#work(WORK_CONTINUE);
74
81
  }
75
82
 
76
83
  /**
@@ -101,28 +108,28 @@ export class Timer {
101
108
  * Pause the timer _(if it's running)_
102
109
  */
103
110
  pause(): Timer {
104
- return this.#work('pause');
111
+ return this.#work(WORK_PAUSE);
105
112
  }
106
113
 
107
114
  /**
108
115
  * Restart the timer _(or start it, if it's not running)_
109
116
  */
110
117
  restart(): Timer {
111
- return this.#work('restart');
118
+ return this.#work(WORK_RESTART);
112
119
  }
113
120
 
114
121
  /**
115
122
  * Start the timer _(if it's not running)_
116
123
  */
117
124
  start(): Timer {
118
- return this.#work('start');
125
+ return this.#work(WORK_START);
119
126
  }
120
127
 
121
128
  /**
122
129
  * Stop the timer _(if it's running)_
123
130
  */
124
131
  stop(): Timer {
125
- return this.#work('stop');
132
+ return this.#work(WORK_STOP);
126
133
  }
127
134
 
128
135
  #work(type: WorkHandlerType): Timer {
package/src/wait.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {milliseconds} from './constants';
1
+ import {milliseconds, TYPE_WAIT} from './constants';
2
2
  import {getCallback, getValidNumber} from './get';
3
3
  import './global';
4
4
  import {TimerTrace} from './models';
@@ -11,7 +11,7 @@ import {Timer} from './timer';
11
11
  */
12
12
  export function wait(callback: () => void, time?: number): Timer {
13
13
  return new Timer(
14
- 'wait',
14
+ TYPE_WAIT,
15
15
  {
16
16
  callback: getCallback(callback),
17
17
  trace: new TimerTrace().stack,
package/src/when.ts CHANGED
@@ -1,12 +1,19 @@
1
1
  import {noop} from '@oscarpalmer/atoms/function';
2
- import {destroyedMessage, milliseconds, startedMessage} from './constants';
2
+ import {
3
+ defaultTimeout,
4
+ destroyedMessage,
5
+ milliseconds,
6
+ startedMessage,
7
+ TYPE_WHEN,
8
+ } from './constants';
3
9
  import {getValidNumber} from './get';
4
10
  import './global';
5
11
  import {TimerTrace, type WhenOptions, type WhenState} from './models';
6
12
  import {Timer} from './timer';
7
13
 
8
14
  class When {
9
- private readonly $timer = 'when';
15
+ private readonly $timer = TYPE_WHEN;
16
+
10
17
  private readonly state: WhenState = {
11
18
  promise: undefined as never,
12
19
  rejecter: undefined as never,
@@ -40,7 +47,7 @@ class When {
40
47
  * Get the timer's origin _(if debugging is enabled)_
41
48
  */
42
49
  get trace(): string | undefined {
43
- return globalThis._oscarpalmer_timer_debug ?? false
50
+ return (globalThis._oscarpalmer_timer_debug ?? false)
44
51
  ? this.state.timer?.trace
45
52
  : undefined;
46
53
  }
@@ -56,7 +63,7 @@ class When {
56
63
  let result = false;
57
64
 
58
65
  this.state.timer = new Timer(
59
- 'when',
66
+ TYPE_WHEN,
60
67
  {
61
68
  callback(): void {
62
69
  try {
@@ -88,7 +95,7 @@ class When {
88
95
  },
89
96
  count: getValidNumber(options?.count),
90
97
  interval: getValidNumber(options?.interval, milliseconds),
91
- timeout: getValidNumber(options?.timeout),
98
+ timeout: getValidNumber(options?.timeout, defaultTimeout),
92
99
  },
93
100
  false,
94
101
  );
package/src/work.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import {
2
2
  activeTimers,
3
- beginTypes,
4
- endOrRestartTypes,
5
- endTypes,
6
3
  intervalBuffer,
7
4
  milliseconds,
8
- pauseTypes,
5
+ TYPE_WAIT,
6
+ WORK_CONTINUE,
7
+ WORK_PAUSE,
8
+ WORK_RESTART,
9
+ WORK_START,
10
+ WORK_STOP,
9
11
  } from './constants';
10
12
  import type {
11
13
  TimerOptions,
@@ -15,40 +17,21 @@ import type {
15
17
  } from './models';
16
18
  import type {Timer} from './timer';
17
19
 
18
- function endOrRestart(
19
- type: WorkHandlerType,
20
- timer: WorkHandlerTimer,
21
- state: TimerState,
22
- options: TimerOptions,
23
- ): Timer {
24
- cancelAnimationFrame(state.frame as never);
25
-
26
- if (type === 'stop') {
27
- options.onAfter?.(false);
28
- }
29
-
30
- state.active = false;
31
- state.frame = undefined;
32
- state.paused = type === 'pause';
33
-
34
- return type === 'restart'
35
- ? work('start', timer, state, options)
36
- : timer.instance;
37
- }
38
-
39
20
  function finish(
40
21
  timer: WorkHandlerTimer,
41
22
  state: TimerState,
42
23
  options: TimerOptions,
43
24
  success: boolean,
44
25
  ): void {
26
+ cancelAnimationFrame(state.frame as never);
27
+
45
28
  activeTimers.delete(timer.instance);
46
29
 
47
30
  state.active = false;
48
31
  state.elapsed = 0;
49
32
  state.frame = undefined;
50
33
 
51
- if (timer.type === 'wait') {
34
+ if (timer.type === TYPE_WAIT) {
52
35
  state.callback();
53
36
  } else {
54
37
  options.onAfter?.(success);
@@ -56,10 +39,23 @@ function finish(
56
39
  }
57
40
 
58
41
  function ignore(type: WorkHandlerType, state: TimerState): boolean {
59
- return (
60
- (state.destroyed && type !== 'stop') ||
61
- (state.active ? beginTypes.has(type) : endTypes.has(type))
62
- );
42
+ if (state.destroyed) {
43
+ return type !== WORK_STOP;
44
+ }
45
+
46
+ if (state.paused) {
47
+ return type === WORK_PAUSE || type === WORK_START;
48
+ }
49
+
50
+ return state.active && type === WORK_START;
51
+ }
52
+
53
+ function pause(timer: WorkHandlerTimer, state: TimerState): Timer {
54
+ cancelAnimationFrame(state.frame as never);
55
+
56
+ state.frame = undefined;
57
+
58
+ return timer.instance;
63
59
  }
64
60
 
65
61
  function run(
@@ -121,7 +117,7 @@ function run(
121
117
  }
122
118
 
123
119
  function setState(type: WorkHandlerType, state: TimerState): void {
124
- const pausable = pauseTypes.has(type);
120
+ const pausable = type === WORK_CONTINUE || type === WORK_PAUSE;
125
121
 
126
122
  state.elapsed = pausable ? state.elapsed : 0;
127
123
  state.index = pausable ? state.index : 0;
@@ -129,12 +125,22 @@ function setState(type: WorkHandlerType, state: TimerState): void {
129
125
  }
130
126
 
131
127
  export function stop(
132
- timer: WorkHandlerTimer,
133
- state: TimerState,
134
- options: TimerOptions,
135
- ): void {
136
- endOrRestart('stop', timer, state, options);
137
- }
128
+ timer: WorkHandlerTimer,
129
+ state: TimerState,
130
+ options: TimerOptions,
131
+ ): Timer {
132
+ cancelAnimationFrame(state.frame as never);
133
+
134
+ activeTimers.delete(timer.instance);
135
+
136
+ options.onAfter?.(false);
137
+
138
+ state.active = !stop;
139
+ state.frame = undefined;
140
+ state.paused = false;
141
+
142
+ return timer.instance;
143
+ }
138
144
 
139
145
  export function work(
140
146
  type: WorkHandlerType,
@@ -148,12 +154,22 @@ export function work(
148
154
 
149
155
  setState(type, state);
150
156
 
151
- if (endOrRestartTypes.has(type)) {
152
- return endOrRestart(type, timer, state, options);
157
+ if (type === WORK_STOP) {
158
+ return stop(timer, state, options);
159
+ }
160
+
161
+ if (type === WORK_PAUSE || type === WORK_RESTART) {
162
+ cancelAnimationFrame(state.frame as never);
163
+
164
+ state.frame = undefined;
153
165
  }
154
166
 
155
167
  state.active = true;
156
- state.paused = false;
168
+ state.paused = type === WORK_PAUSE;
169
+
170
+ if (state.paused) {
171
+ return pause(timer, state);
172
+ }
157
173
 
158
174
  activeTimers.add(timer.instance);
159
175
 
@@ -67,14 +67,11 @@ export type TimerState = {
67
67
  };
68
68
  export type TimerType = "repeat" | "wait" | "when";
69
69
  export type WorkHandlerType = "continue" | "pause" | "restart" | "start" | "stop";
70
+ export declare const defaultTimeout = 30000;
70
71
  /**
71
72
  * A set of all active timers
72
73
  */
73
74
  export declare const activeTimers: Set<Timer>;
74
- /**
75
- * A set of types that allow work to begin
76
- */
77
- export declare const beginTypes: Set<WorkHandlerType>;
78
75
  /**
79
76
  * Buffer value to use when evaluating if a specific time is within a certain range
80
77
  */
@@ -83,23 +80,22 @@ export declare const intervalBuffer = 5;
83
80
  * Message to show when a when-timer is destroyed
84
81
  */
85
82
  export declare const destroyedMessage = "Timer has already been destroyed";
86
- /**
87
- * A set of types that allow work to end
88
- */
89
- export declare const endTypes: Set<WorkHandlerType>;
90
- /**
91
- * A set of types that allow work to end or restart
92
- */
93
- export declare const endOrRestartTypes: Set<WorkHandlerType>;
94
83
  /**
95
84
  * A set of timers that were paused due to the document being hidden
96
85
  */
97
86
  export declare const hiddenTimers: Set<Timer>;
98
- export declare const pauseTypes: Set<WorkHandlerType>;
99
87
  /**
100
88
  * Message to show when a when-timer is started
101
89
  */
102
90
  export declare const startedMessage = "Timer has already been started";
91
+ export declare const TYPE_REPEAT: TimerType;
92
+ export declare const TYPE_WAIT: TimerType;
93
+ export declare const TYPE_WHEN: TimerType;
94
+ export declare const WORK_CONTINUE: WorkHandlerType;
95
+ export declare const WORK_PAUSE: WorkHandlerType;
96
+ export declare const WORK_RESTART: WorkHandlerType;
97
+ export declare const WORK_START: WorkHandlerType;
98
+ export declare const WORK_STOP: WorkHandlerType;
103
99
  /**
104
100
  * A calculated average of the refresh rate of the display _(in milliseconds)_
105
101
  */
@@ -1,13 +1,10 @@
1
- import type { WorkHandlerType } from './models';
1
+ import type { TimerType, WorkHandlerType } from './models';
2
2
  import type { Timer } from './timer';
3
+ export declare const defaultTimeout = 30000;
3
4
  /**
4
5
  * A set of all active timers
5
6
  */
6
7
  export declare const activeTimers: Set<Timer>;
7
- /**
8
- * A set of types that allow work to begin
9
- */
10
- export declare const beginTypes: Set<WorkHandlerType>;
11
8
  /**
12
9
  * Buffer value to use when evaluating if a specific time is within a certain range
13
10
  */
@@ -16,23 +13,22 @@ export declare const intervalBuffer = 5;
16
13
  * Message to show when a when-timer is destroyed
17
14
  */
18
15
  export declare const destroyedMessage = "Timer has already been destroyed";
19
- /**
20
- * A set of types that allow work to end
21
- */
22
- export declare const endTypes: Set<WorkHandlerType>;
23
- /**
24
- * A set of types that allow work to end or restart
25
- */
26
- export declare const endOrRestartTypes: Set<WorkHandlerType>;
27
16
  /**
28
17
  * A set of timers that were paused due to the document being hidden
29
18
  */
30
19
  export declare const hiddenTimers: Set<Timer>;
31
- export declare const pauseTypes: Set<WorkHandlerType>;
32
20
  /**
33
21
  * Message to show when a when-timer is started
34
22
  */
35
23
  export declare const startedMessage = "Timer has already been started";
24
+ export declare const TYPE_REPEAT: TimerType;
25
+ export declare const TYPE_WAIT: TimerType;
26
+ export declare const TYPE_WHEN: TimerType;
27
+ export declare const WORK_CONTINUE: WorkHandlerType;
28
+ export declare const WORK_PAUSE: WorkHandlerType;
29
+ export declare const WORK_RESTART: WorkHandlerType;
30
+ export declare const WORK_START: WorkHandlerType;
31
+ export declare const WORK_STOP: WorkHandlerType;
36
32
  /**
37
33
  * A calculated average of the refresh rate of the display _(in milliseconds)_
38
34
  */
package/types/get.d.cts CHANGED
@@ -1,5 +1,8 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
+ /**
4
+ * A generic callback function
5
+ */
3
6
  export type GenericCallback = (...args: any[]) => any;
4
7
  export declare function getCallback(value: unknown): GenericCallback;
5
8
  export declare function getValidNumber(value: unknown, defaultValue?: number): number;
package/types/work.d.cts CHANGED
@@ -71,7 +71,7 @@ export type WorkHandlerTimer = {
71
71
  type: TimerType;
72
72
  };
73
73
  export type WorkHandlerType = "continue" | "pause" | "restart" | "start" | "stop";
74
- declare function stop$1(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): void;
74
+ declare function stop$1(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
75
75
  export declare function work(type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
76
76
 
77
77
  export {
package/types/work.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import type { TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType } from './models';
2
2
  import type { Timer } from './timer';
3
- export declare function stop(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): void;
3
+ export declare function stop(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
4
4
  export declare function work(type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;