@oscarpalmer/timer 0.44.0 → 0.46.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/when.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import {noop} from '@oscarpalmer/atoms/function';
2
- import {MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN, WORK_CONTINUE} from './constants';
3
- import {getValidNumber, getValidTimeout} from './get';
2
+ import {MESSAGE_STARTED, TYPE_WHEN, WORK_CONTINUE, WORK_PAUSE, WORK_STOP} from './constants';
3
+ import {getValidNumber, getValidTimeout} from './misc';
4
4
  import './global';
5
5
  import {
6
6
  TimerTrace,
@@ -11,23 +11,12 @@ import {
11
11
  } from './models';
12
12
  import {createTimer} from './timer';
13
13
 
14
- function destroyWhen(state: WhenState): void {
15
- state.timer?.destroy();
16
-
17
- state.promise = undefined as never;
18
- state.resolver = noop;
19
- state.rejecter = noop;
20
- state.timer = undefined as never;
21
- }
22
-
23
- function onAfter(instance: When, state: WhenState): void {
14
+ function onAfter(state: WhenState): void {
24
15
  if (state.result) {
25
16
  state.resolver?.();
26
17
  } else {
27
18
  state.rejecter?.();
28
19
  }
29
-
30
- instance.destroy();
31
20
  }
32
21
 
33
22
  function onCallback(condition: () => boolean, state: WhenState): void {
@@ -42,12 +31,6 @@ function onCallback(condition: () => boolean, state: WhenState): void {
42
31
  }
43
32
  }
44
33
 
45
- function onError(instance: When, state: WhenState): void {
46
- state.rejecter?.();
47
-
48
- instance.destroy();
49
- }
50
-
51
34
  function onWhen(type: WorkHandlerType, instance: When, state: WhenState): When {
52
35
  state.timer?.[type]?.();
53
36
 
@@ -55,10 +38,6 @@ function onWhen(type: WorkHandlerType, instance: When, state: WhenState): When {
55
38
  }
56
39
 
57
40
  function startWhen(state: WhenState, resolve?: (() => void) | null): Promise<void> {
58
- if (state.timer == null) {
59
- throw new Error(MESSAGE_DESTROYED);
60
- }
61
-
62
41
  if (state.started) {
63
42
  throw new Error(MESSAGE_STARTED);
64
43
  }
@@ -98,8 +77,8 @@ export function when(condition: () => boolean, options?: Partial<WhenOptions>):
98
77
  trace: new TimerTrace().stack,
99
78
  },
100
79
  {
101
- onAfter: () => onAfter(instance, state),
102
- onError: () => onError(instance, state),
80
+ onAfter: () => onAfter(state),
81
+ onError: () => state.rejecter?.(),
103
82
  count: getValidNumber(options?.count),
104
83
  interval: getValidNumber(options?.interval),
105
84
  timeout: getValidTimeout(options?.timeout),
@@ -109,10 +88,10 @@ export function when(condition: () => boolean, options?: Partial<WhenOptions>):
109
88
 
110
89
  instance = {
111
90
  continue: () => onWhen(WORK_CONTINUE, instance, state),
112
- destroy: () => destroyWhen(state),
113
- pause: () => onWhen('pause', instance, state),
114
- start: (resolve?: (() => void) | null) => startWhen(state, resolve),
115
- stop: () => onWhen('stop', instance, state),
91
+ destroy: noop,
92
+ pause: () => onWhen(WORK_PAUSE, instance, state),
93
+ start: (resolve: never) => startWhen(state, resolve),
94
+ stop: () => onWhen(WORK_STOP, instance, state),
116
95
  } as When;
117
96
 
118
97
  Object.defineProperties(instance, {
@@ -122,15 +101,15 @@ export function when(condition: () => boolean, options?: Partial<WhenOptions>):
122
101
  },
123
102
  active: {
124
103
  enumerable: true,
125
- get: () => state.timer?.active ?? false,
104
+ get: () => state.timer.active,
126
105
  },
127
106
  destroyed: {
128
107
  enumerable: true,
129
- get: () => state.timer == null,
108
+ value: false,
130
109
  },
131
110
  paused: {
132
111
  enumerable: true,
133
- get: () => state.timer?.paused ?? false,
112
+ get: () => state.timer.paused,
134
113
  },
135
114
  trace: {
136
115
  enumerable: true,
package/src/work.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import {
2
2
  BUFFER_INTERVAL,
3
- TIMERS_ACTIVE,
4
3
  TYPE_WAIT,
5
4
  WORK_CONTINUE,
6
5
  WORK_PAUSE,
@@ -8,56 +7,35 @@ import {
8
7
  WORK_START,
9
8
  WORK_STOP,
10
9
  } from './constants';
11
- import type {Timer, TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType} from './models';
12
-
13
- function finish(
14
- timer: WorkHandlerTimer,
15
- state: TimerState,
16
- options: TimerOptions,
17
- success: boolean,
18
- ): void {
19
- cancelAnimationFrame(state.frame as never);
10
+ import {updateStates} from './misc';
11
+ import type {Timer, TimerState, WorkHandlerType} from './models';
12
+
13
+ function finish(state: TimerState, success: boolean): void {
14
+ updateStates(state);
20
15
 
21
- TIMERS_ACTIVE.delete(timer.instance);
16
+ cancelAnimationFrame(state.frame as never);
22
17
 
23
18
  state.active = false;
24
19
  state.elapsed = 0;
25
20
  state.frame = undefined;
26
21
 
27
- if (timer.name === TYPE_WAIT) {
22
+ if (state.name === TYPE_WAIT) {
28
23
  state.callback();
29
24
  } else {
30
- options.onAfter?.(success);
25
+ state.options.onAfter?.(success);
31
26
  }
32
27
  }
33
28
 
34
29
  function ignore(type: WorkHandlerType, state: TimerState): boolean {
35
- if (state.destroyed) {
36
- return type !== WORK_STOP;
37
- }
38
-
39
30
  if (state.paused) {
40
31
  return type === WORK_PAUSE || type === WORK_START;
41
32
  }
42
33
 
43
- return state.active && type === WORK_START;
34
+ return state.active && (type === WORK_CONTINUE || type === WORK_START);
44
35
  }
45
36
 
46
- function pause(timer: WorkHandlerTimer, state: TimerState): Timer {
47
- cancelAnimationFrame(state.frame as never);
48
-
49
- state.frame = undefined;
50
-
51
- return timer.instance;
52
- }
53
-
54
- function run(
55
- timer: WorkHandlerTimer,
56
- state: TimerState,
57
- options: TimerOptions,
58
- ): (now: DOMHighResTimeStamp) => void {
37
+ function run(state: TimerState): (now: DOMHighResTimeStamp) => void {
59
38
  let last: DOMHighResTimeStamp | undefined;
60
- let start: DOMHighResTimeStamp | undefined;
61
39
 
62
40
  return function step(now: DOMHighResTimeStamp): void {
63
41
  if (!state.active) {
@@ -65,7 +43,6 @@ function run(
65
43
  }
66
44
 
67
45
  last ??= now;
68
- start ??= now;
69
46
 
70
47
  const difference = now - last;
71
48
 
@@ -74,26 +51,27 @@ function run(
74
51
 
75
52
  last = now;
76
53
 
77
- if (options.timeout > 0 && state.total >= options.timeout) {
78
- options.onError?.();
54
+ if (state.options.timeout > 0 && state.total >= state.options.timeout) {
55
+ state.options.onError?.();
79
56
 
80
- finish(timer, state, options, false);
57
+ finish(state, false);
81
58
 
82
59
  return;
83
60
  }
84
61
 
85
- if (options.interval === 0 || state.elapsed >= options.interval - BUFFER_INTERVAL) {
86
- if (options.count > -1) {
62
+ if (state.options.interval === 0 || state.elapsed >= state.options.interval - BUFFER_INTERVAL) {
63
+ if (state.options.count > -1) {
87
64
  (state.callback as (index: number) => void)(state.index);
88
65
  }
89
66
 
90
- start = now;
91
-
92
67
  state.elapsed = 0;
93
68
  state.index += 1;
94
69
 
95
- if (options.count === -1 || (options.count > 0 && state.index >= options.count)) {
96
- finish(timer, state, options, true);
70
+ if (
71
+ state.options.count === -1 ||
72
+ (state.options.count > 0 && state.index >= state.options.count)
73
+ ) {
74
+ finish(state, true);
97
75
 
98
76
  return;
99
77
  }
@@ -111,34 +89,29 @@ function setState(type: WorkHandlerType, state: TimerState): void {
111
89
  state.total = pausable ? state.total : 0;
112
90
  }
113
91
 
114
- export function stop(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer {
115
- cancelAnimationFrame(state.frame as never);
92
+ export function stop(state: TimerState): Timer {
93
+ updateStates(state);
116
94
 
117
- TIMERS_ACTIVE.delete(timer.instance);
95
+ cancelAnimationFrame(state.frame as never);
118
96
 
119
- options.onAfter?.(false);
97
+ state.options.onAfter?.(false);
120
98
 
121
- state.active = !stop;
99
+ state.active = false;
122
100
  state.frame = undefined;
123
101
  state.paused = false;
124
102
 
125
- return timer.instance;
103
+ return state.timer;
126
104
  }
127
105
 
128
- export function work(
129
- type: WorkHandlerType,
130
- timer: WorkHandlerTimer,
131
- state: TimerState,
132
- options: TimerOptions,
133
- ): Timer {
106
+ export function work(type: WorkHandlerType, state: TimerState, hide?: boolean): Timer {
134
107
  if (ignore(type, state)) {
135
- return timer.instance;
108
+ return state.timer;
136
109
  }
137
110
 
138
111
  setState(type, state);
139
112
 
140
113
  if (type === WORK_STOP) {
141
- return stop(timer, state, options);
114
+ return stop(state);
142
115
  }
143
116
 
144
117
  if (type === WORK_PAUSE || type === WORK_RESTART) {
@@ -150,15 +123,15 @@ export function work(
150
123
  state.active = true;
151
124
  state.paused = type === WORK_PAUSE;
152
125
 
126
+ updateStates(state, state.paused ? hide : false);
127
+
153
128
  if (state.paused) {
154
- return pause(timer, state);
129
+ return state.timer;
155
130
  }
156
131
 
157
- TIMERS_ACTIVE.add(timer.instance);
158
-
159
- const runner = run(timer, state, options);
132
+ const runner = run(state);
160
133
 
161
134
  state.frame = requestAnimationFrame(runner);
162
135
 
163
- return timer.instance;
136
+ return state.timer;
164
137
  }
package/dist/delay.d.mts DELETED
@@ -1,2 +0,0 @@
1
- import { delay } from "@oscarpalmer/atoms/promise/delay";
2
- export { delay };
package/dist/delay.mjs DELETED
@@ -1,2 +0,0 @@
1
- import { delay } from "@oscarpalmer/atoms/promise/delay";
2
- export { delay };
package/dist/get.mjs DELETED
@@ -1,15 +0,0 @@
1
- import { DEFAULT_TIMEOUT } from "./constants.mjs";
2
- import { noop } from "@oscarpalmer/atoms/function";
3
- //#region src/get.ts
4
- function getCallback(value) {
5
- return typeof value === "function" ? value : noop;
6
- }
7
- function getValidTimeout(value) {
8
- return typeof value === "number" && value > 0 ? value : DEFAULT_TIMEOUT;
9
- }
10
- function getValidNumber(value, defaultValue) {
11
- const actualDefault = defaultValue ?? 0;
12
- return typeof value === "number" && value > actualDefault ? value : actualDefault;
13
- }
14
- //#endregion
15
- export { getCallback, getValidNumber, getValidTimeout };
package/src/delay.ts DELETED
@@ -1 +0,0 @@
1
- export {delay} from '@oscarpalmer/atoms/promise/delay';
package/src/get.ts DELETED
@@ -1,17 +0,0 @@
1
- import {noop} from '@oscarpalmer/atoms/function';
2
- import type {GenericCallback} from '@oscarpalmer/atoms/models';
3
- import {DEFAULT_TIMEOUT} from './constants';
4
-
5
- export function getCallback(value: unknown): GenericCallback {
6
- return typeof value === 'function' ? (value as GenericCallback) : noop;
7
- }
8
-
9
- export function getValidTimeout(value: unknown): number {
10
- return typeof value === 'number' && value > 0 ? value : DEFAULT_TIMEOUT;
11
- }
12
-
13
- export function getValidNumber(value: unknown, defaultValue?: number): number {
14
- const actualDefault = defaultValue ?? 0;
15
-
16
- return typeof value === 'number' && value > actualDefault ? value : actualDefault;
17
- }