@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.
- package/dist/constants.d.mts +3 -14
- package/dist/constants.mjs +5 -13
- package/dist/global.d.mts +3 -0
- package/dist/global.mjs +5 -14
- package/dist/index.d.mts +12 -33
- package/dist/index.mjs +132 -252
- package/dist/misc.d.mts +10 -0
- package/dist/misc.mjs +34 -0
- package/dist/models.d.mts +24 -3
- package/dist/repeat.mjs +1 -1
- package/dist/timer.mjs +14 -27
- package/dist/wait.mjs +1 -1
- package/dist/when.mjs +11 -24
- package/dist/work.d.mts +3 -3
- package/dist/work.mjs +26 -31
- package/package.json +2 -6
- package/src/constants.ts +5 -15
- package/src/global.ts +11 -21
- package/src/index.ts +0 -1
- package/src/misc.ts +49 -0
- package/src/models.ts +24 -2
- package/src/repeat.ts +1 -1
- package/src/timer.ts +16 -42
- package/src/wait.ts +1 -1
- package/src/when.ts +12 -33
- package/src/work.ts +33 -56
- package/dist/delay.d.mts +0 -2
- package/dist/delay.mjs +0 -2
- package/dist/get.d.mts +0 -7
- package/dist/get.mjs +0 -15
- package/src/delay.ts +0 -1
- package/src/get.ts +0 -17
package/src/when.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
|
-
import {
|
|
3
|
-
import {getValidNumber, getValidTimeout} from './
|
|
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
|
|
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(
|
|
102
|
-
onError: () =>
|
|
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:
|
|
113
|
-
pause: () => onWhen(
|
|
114
|
-
start: (resolve
|
|
115
|
-
stop: () => onWhen(
|
|
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
|
|
104
|
+
get: () => state.timer.active,
|
|
126
105
|
},
|
|
127
106
|
destroyed: {
|
|
128
107
|
enumerable: true,
|
|
129
|
-
|
|
108
|
+
value: false,
|
|
130
109
|
},
|
|
131
110
|
paused: {
|
|
132
111
|
enumerable: true,
|
|
133
|
-
get: () => state.timer
|
|
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,34 +7,26 @@ import {
|
|
|
8
7
|
WORK_START,
|
|
9
8
|
WORK_STOP,
|
|
10
9
|
} from './constants';
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
state
|
|
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
|
-
|
|
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 (
|
|
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
|
}
|
|
@@ -43,19 +34,7 @@ function ignore(type: WorkHandlerType, state: TimerState): boolean {
|
|
|
43
34
|
return state.active && type === WORK_START;
|
|
44
35
|
}
|
|
45
36
|
|
|
46
|
-
function
|
|
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
39
|
let start: DOMHighResTimeStamp | undefined;
|
|
61
40
|
|
|
@@ -74,16 +53,16 @@ function run(
|
|
|
74
53
|
|
|
75
54
|
last = now;
|
|
76
55
|
|
|
77
|
-
if (options.timeout > 0 && state.total >= options.timeout) {
|
|
78
|
-
options.onError?.();
|
|
56
|
+
if (state.options.timeout > 0 && state.total >= state.options.timeout) {
|
|
57
|
+
state.options.onError?.();
|
|
79
58
|
|
|
80
|
-
finish(
|
|
59
|
+
finish(state, false);
|
|
81
60
|
|
|
82
61
|
return;
|
|
83
62
|
}
|
|
84
63
|
|
|
85
|
-
if (options.interval === 0 || state.elapsed >= options.interval - BUFFER_INTERVAL) {
|
|
86
|
-
if (options.count > -1) {
|
|
64
|
+
if (state.options.interval === 0 || state.elapsed >= state.options.interval - BUFFER_INTERVAL) {
|
|
65
|
+
if (state.options.count > -1) {
|
|
87
66
|
(state.callback as (index: number) => void)(state.index);
|
|
88
67
|
}
|
|
89
68
|
|
|
@@ -92,8 +71,11 @@ function run(
|
|
|
92
71
|
state.elapsed = 0;
|
|
93
72
|
state.index += 1;
|
|
94
73
|
|
|
95
|
-
if (
|
|
96
|
-
|
|
74
|
+
if (
|
|
75
|
+
state.options.count === -1 ||
|
|
76
|
+
(state.options.count > 0 && state.index >= state.options.count)
|
|
77
|
+
) {
|
|
78
|
+
finish(state, true);
|
|
97
79
|
|
|
98
80
|
return;
|
|
99
81
|
}
|
|
@@ -111,34 +93,29 @@ function setState(type: WorkHandlerType, state: TimerState): void {
|
|
|
111
93
|
state.total = pausable ? state.total : 0;
|
|
112
94
|
}
|
|
113
95
|
|
|
114
|
-
export function stop(
|
|
115
|
-
|
|
96
|
+
export function stop(state: TimerState): Timer {
|
|
97
|
+
updateStates(state);
|
|
116
98
|
|
|
117
|
-
|
|
99
|
+
cancelAnimationFrame(state.frame as never);
|
|
118
100
|
|
|
119
|
-
options.onAfter?.(false);
|
|
101
|
+
state.options.onAfter?.(false);
|
|
120
102
|
|
|
121
|
-
state.active =
|
|
103
|
+
state.active = false;
|
|
122
104
|
state.frame = undefined;
|
|
123
105
|
state.paused = false;
|
|
124
106
|
|
|
125
|
-
return timer
|
|
107
|
+
return state.timer;
|
|
126
108
|
}
|
|
127
109
|
|
|
128
|
-
export function work(
|
|
129
|
-
type: WorkHandlerType,
|
|
130
|
-
timer: WorkHandlerTimer,
|
|
131
|
-
state: TimerState,
|
|
132
|
-
options: TimerOptions,
|
|
133
|
-
): Timer {
|
|
110
|
+
export function work(type: WorkHandlerType, state: TimerState, hide?: boolean): Timer {
|
|
134
111
|
if (ignore(type, state)) {
|
|
135
|
-
return timer
|
|
112
|
+
return state.timer;
|
|
136
113
|
}
|
|
137
114
|
|
|
138
115
|
setState(type, state);
|
|
139
116
|
|
|
140
117
|
if (type === WORK_STOP) {
|
|
141
|
-
return stop(
|
|
118
|
+
return stop(state);
|
|
142
119
|
}
|
|
143
120
|
|
|
144
121
|
if (type === WORK_PAUSE || type === WORK_RESTART) {
|
|
@@ -150,15 +127,15 @@ export function work(
|
|
|
150
127
|
state.active = true;
|
|
151
128
|
state.paused = type === WORK_PAUSE;
|
|
152
129
|
|
|
130
|
+
updateStates(state, state.paused ? ((hide ?? false) ? 'hidden' : undefined) : 'active');
|
|
131
|
+
|
|
153
132
|
if (state.paused) {
|
|
154
|
-
return
|
|
133
|
+
return state.timer;
|
|
155
134
|
}
|
|
156
135
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const runner = run(timer, state, options);
|
|
136
|
+
const runner = run(state);
|
|
160
137
|
|
|
161
138
|
state.frame = requestAnimationFrame(runner);
|
|
162
139
|
|
|
163
|
-
return timer
|
|
140
|
+
return state.timer;
|
|
164
141
|
}
|
package/dist/delay.d.mts
DELETED
package/dist/delay.mjs
DELETED
package/dist/get.d.mts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { GenericCallback } from "@oscarpalmer/atoms/models";
|
|
2
|
-
//#region src/get.d.ts
|
|
3
|
-
declare function getCallback(value: unknown): GenericCallback;
|
|
4
|
-
declare function getValidTimeout(value: unknown): number;
|
|
5
|
-
declare function getValidNumber(value: unknown, defaultValue?: number): number;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { getCallback, getValidNumber, getValidTimeout };
|
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
|
-
}
|