@oscarpalmer/timer 0.32.0 → 0.34.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/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(
@@ -67,6 +63,7 @@ function run(
67
63
  state: TimerState,
68
64
  options: TimerOptions,
69
65
  ): (now: DOMHighResTimeStamp) => void {
66
+ let last: DOMHighResTimeStamp | undefined;
70
67
  let start: DOMHighResTimeStamp | undefined;
71
68
 
72
69
  return function step(now: DOMHighResTimeStamp): void {
@@ -74,13 +71,16 @@ function run(
74
71
  return;
75
72
  }
76
73
 
74
+ last ??= now;
77
75
  start ??= now;
78
76
 
79
- const difference = now - start;
77
+ const difference = now - last;
80
78
 
81
79
  state.elapsed += difference;
82
80
  state.total += difference;
83
81
 
82
+ last = now;
83
+
84
84
  if (options.timeout > 0 && state.total >= options.timeout) {
85
85
  options.onError?.();
86
86
 
@@ -117,7 +117,7 @@ function run(
117
117
  }
118
118
 
119
119
  function setState(type: WorkHandlerType, state: TimerState): void {
120
- const pausable = pauseTypes.has(type);
120
+ const pausable = type === WORK_CONTINUE || type === WORK_PAUSE;
121
121
 
122
122
  state.elapsed = pausable ? state.elapsed : 0;
123
123
  state.index = pausable ? state.index : 0;
@@ -125,12 +125,22 @@ function setState(type: WorkHandlerType, state: TimerState): void {
125
125
  }
126
126
 
127
127
  export function stop(
128
- timer: WorkHandlerTimer,
129
- state: TimerState,
130
- options: TimerOptions,
131
- ): void {
132
- endOrRestart('stop', timer, state, options);
133
- }
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
+ }
134
144
 
135
145
  export function work(
136
146
  type: WorkHandlerType,
@@ -144,12 +154,22 @@ export function work(
144
154
 
145
155
  setState(type, state);
146
156
 
147
- if (endOrRestartTypes.has(type)) {
148
- 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;
149
165
  }
150
166
 
151
167
  state.active = true;
152
- state.paused = false;
168
+ state.paused = type === WORK_PAUSE;
169
+
170
+ if (state.paused) {
171
+ return pause(timer, state);
172
+ }
153
173
 
154
174
  activeTimers.add(timer.instance);
155
175
 
@@ -71,10 +71,6 @@ export type WorkHandlerType = "continue" | "pause" | "restart" | "start" | "stop
71
71
  * A set of all active timers
72
72
  */
73
73
  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
74
  /**
79
75
  * Buffer value to use when evaluating if a specific time is within a certain range
80
76
  */
@@ -83,23 +79,22 @@ export declare const intervalBuffer = 5;
83
79
  * Message to show when a when-timer is destroyed
84
80
  */
85
81
  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
82
  /**
95
83
  * A set of timers that were paused due to the document being hidden
96
84
  */
97
85
  export declare const hiddenTimers: Set<Timer>;
98
- export declare const pauseTypes: Set<WorkHandlerType>;
99
86
  /**
100
87
  * Message to show when a when-timer is started
101
88
  */
102
89
  export declare const startedMessage = "Timer has already been started";
90
+ export declare const TYPE_REPEAT: TimerType;
91
+ export declare const TYPE_WAIT: TimerType;
92
+ export declare const TYPE_WHEN: TimerType;
93
+ export declare const WORK_CONTINUE: WorkHandlerType;
94
+ export declare const WORK_PAUSE: WorkHandlerType;
95
+ export declare const WORK_RESTART: WorkHandlerType;
96
+ export declare const WORK_START: WorkHandlerType;
97
+ export declare const WORK_STOP: WorkHandlerType;
103
98
  /**
104
99
  * A calculated average of the refresh rate of the display _(in milliseconds)_
105
100
  */
@@ -1,13 +1,9 @@
1
- import type { WorkHandlerType } from './models';
1
+ import type { TimerType, WorkHandlerType } from './models';
2
2
  import type { Timer } from './timer';
3
3
  /**
4
4
  * A set of all active timers
5
5
  */
6
6
  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
7
  /**
12
8
  * Buffer value to use when evaluating if a specific time is within a certain range
13
9
  */
@@ -16,23 +12,22 @@ export declare const intervalBuffer = 5;
16
12
  * Message to show when a when-timer is destroyed
17
13
  */
18
14
  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
15
  /**
28
16
  * A set of timers that were paused due to the document being hidden
29
17
  */
30
18
  export declare const hiddenTimers: Set<Timer>;
31
- export declare const pauseTypes: Set<WorkHandlerType>;
32
19
  /**
33
20
  * Message to show when a when-timer is started
34
21
  */
35
22
  export declare const startedMessage = "Timer has already been started";
23
+ export declare const TYPE_REPEAT: TimerType;
24
+ export declare const TYPE_WAIT: TimerType;
25
+ export declare const TYPE_WHEN: TimerType;
26
+ export declare const WORK_CONTINUE: WorkHandlerType;
27
+ export declare const WORK_PAUSE: WorkHandlerType;
28
+ export declare const WORK_RESTART: WorkHandlerType;
29
+ export declare const WORK_START: WorkHandlerType;
30
+ export declare const WORK_STOP: WorkHandlerType;
36
31
  /**
37
32
  * A calculated average of the refresh rate of the display _(in milliseconds)_
38
33
  */
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;