@oscarpalmer/timer 0.43.0 → 0.44.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 +4 -6
- package/dist/delay.d.mts +1 -2
- package/dist/get.d.mts +0 -1
- package/dist/global.d.mts +1 -2
- package/dist/index.d.mts +72 -94
- package/dist/index.mjs +149 -208
- package/dist/is.d.mts +1 -3
- package/dist/models.d.mts +87 -5
- package/dist/repeat.d.mts +3 -3
- package/dist/repeat.mjs +2 -2
- package/dist/timer.d.mts +3 -50
- package/dist/timer.mjs +60 -91
- package/dist/wait.d.mts +3 -2
- package/dist/wait.mjs +2 -2
- package/dist/when.d.mts +3 -46
- package/dist/when.mjs +89 -116
- package/dist/work.d.mts +1 -3
- package/dist/work.mjs +1 -1
- package/package.json +9 -5
- package/src/constants.ts +4 -5
- package/src/delay.ts +0 -1
- package/src/get.ts +1 -1
- package/src/global.ts +1 -1
- package/src/index.ts +5 -5
- package/src/is.ts +1 -2
- package/src/models.ts +105 -4
- package/src/repeat.ts +3 -5
- package/src/timer.ts +75 -119
- package/src/wait.ts +3 -5
- package/src/when.ts +112 -146
- package/src/work.ts +3 -4
package/src/when.ts
CHANGED
|
@@ -1,166 +1,73 @@
|
|
|
1
1
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
|
-
import {MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN} from './constants';
|
|
2
|
+
import {MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN, WORK_CONTINUE} from './constants';
|
|
3
3
|
import {getValidNumber, getValidTimeout} from './get';
|
|
4
4
|
import './global';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
get active() {
|
|
23
|
-
return this.state.timer?.active ?? false;
|
|
24
|
-
}
|
|
5
|
+
import {
|
|
6
|
+
TimerTrace,
|
|
7
|
+
type When,
|
|
8
|
+
type WhenOptions,
|
|
9
|
+
type WhenState,
|
|
10
|
+
type WorkHandlerType,
|
|
11
|
+
} from './models';
|
|
12
|
+
import {createTimer} from './timer';
|
|
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
|
+
}
|
|
25
22
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
function onAfter(instance: When, state: WhenState): void {
|
|
24
|
+
if (state.result) {
|
|
25
|
+
state.resolver?.();
|
|
26
|
+
} else {
|
|
27
|
+
state.rejecter?.();
|
|
31
28
|
}
|
|
32
29
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*/
|
|
36
|
-
get paused() {
|
|
37
|
-
return this.state.timer?.paused ?? false;
|
|
38
|
-
}
|
|
30
|
+
instance.destroy();
|
|
31
|
+
}
|
|
39
32
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return (globalThis._oscarpalmer_timer_debug ?? false) ? this.state.timer?.trace : undefined;
|
|
45
|
-
}
|
|
33
|
+
function onCallback(condition: () => boolean, state: WhenState): void {
|
|
34
|
+
try {
|
|
35
|
+
if (condition()) {
|
|
36
|
+
state.result = true;
|
|
46
37
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const {state} = this;
|
|
53
|
-
|
|
54
|
-
state.promise = new Promise<void>((resolve, reject) => {
|
|
55
|
-
state.resolver = resolve;
|
|
56
|
-
state.rejecter = reject;
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
let result = false;
|
|
60
|
-
|
|
61
|
-
this.state.timer = new Timer(
|
|
62
|
-
TYPE_WHEN,
|
|
63
|
-
{
|
|
64
|
-
callback(): void {
|
|
65
|
-
try {
|
|
66
|
-
if (condition()) {
|
|
67
|
-
result = true;
|
|
68
|
-
|
|
69
|
-
state.timer.stop();
|
|
70
|
-
}
|
|
71
|
-
} catch {
|
|
72
|
-
state.timer.stop();
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
trace: new TimerTrace().stack,
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
onAfter: () => {
|
|
79
|
-
if (result) {
|
|
80
|
-
state.resolver?.();
|
|
81
|
-
} else {
|
|
82
|
-
state.rejecter?.();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
this.destroy();
|
|
86
|
-
},
|
|
87
|
-
onError: () => {
|
|
88
|
-
state.rejecter?.();
|
|
89
|
-
|
|
90
|
-
this.destroy();
|
|
91
|
-
},
|
|
92
|
-
count: getValidNumber(options?.count),
|
|
93
|
-
interval: getValidNumber(options?.interval),
|
|
94
|
-
timeout: getValidTimeout(options?.timeout),
|
|
95
|
-
},
|
|
96
|
-
false,
|
|
97
|
-
);
|
|
38
|
+
state.timer.stop();
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
state.timer.stop();
|
|
98
42
|
}
|
|
43
|
+
}
|
|
99
44
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
*/
|
|
103
|
-
continue(): When {
|
|
104
|
-
this.state.timer?.continue();
|
|
45
|
+
function onError(instance: When, state: WhenState): void {
|
|
46
|
+
state.rejecter?.();
|
|
105
47
|
|
|
106
|
-
|
|
107
|
-
|
|
48
|
+
instance.destroy();
|
|
49
|
+
}
|
|
108
50
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
*/
|
|
112
|
-
destroy(): void {
|
|
113
|
-
const {state} = this;
|
|
51
|
+
function onWhen(type: WorkHandlerType, instance: When, state: WhenState): When {
|
|
52
|
+
state.timer?.[type]?.();
|
|
114
53
|
|
|
115
|
-
|
|
54
|
+
return instance;
|
|
55
|
+
}
|
|
116
56
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
state.timer = undefined as never;
|
|
57
|
+
function startWhen(state: WhenState, resolve?: (() => void) | null): Promise<void> {
|
|
58
|
+
if (state.timer == null) {
|
|
59
|
+
throw new Error(MESSAGE_DESTROYED);
|
|
121
60
|
}
|
|
122
61
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
*/
|
|
126
|
-
pause(): When {
|
|
127
|
-
this.state.timer?.pause();
|
|
128
|
-
|
|
129
|
-
return this;
|
|
62
|
+
if (state.started) {
|
|
63
|
+
throw new Error(MESSAGE_STARTED);
|
|
130
64
|
}
|
|
131
65
|
|
|
132
|
-
|
|
133
|
-
* Start the timer
|
|
134
|
-
*
|
|
135
|
-
* @param resolve Optional resolve callback
|
|
136
|
-
* @returns Promise that resolves when the condition is met
|
|
137
|
-
*/
|
|
138
|
-
start(resolve?: (() => void) | null): Promise<void> {
|
|
139
|
-
const {state} = this;
|
|
140
|
-
|
|
141
|
-
if (state.timer == null) {
|
|
142
|
-
throw new Error(MESSAGE_DESTROYED);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (state.started) {
|
|
146
|
-
throw new Error(MESSAGE_STARTED);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
state.started = true;
|
|
150
|
-
|
|
151
|
-
state.timer.start();
|
|
152
|
-
|
|
153
|
-
return state.promise.then(resolve);
|
|
154
|
-
}
|
|
66
|
+
state.started = true;
|
|
155
67
|
|
|
156
|
-
|
|
157
|
-
* Stops the timer _(if it was running)_
|
|
158
|
-
*/
|
|
159
|
-
stop(): When {
|
|
160
|
-
this.state.timer?.stop();
|
|
68
|
+
state.timer.start();
|
|
161
69
|
|
|
162
|
-
|
|
163
|
-
}
|
|
70
|
+
return state.promise.then(resolve);
|
|
164
71
|
}
|
|
165
72
|
|
|
166
73
|
/**
|
|
@@ -170,7 +77,66 @@ class When {
|
|
|
170
77
|
* @returns Timer instance
|
|
171
78
|
*/
|
|
172
79
|
export function when(condition: () => boolean, options?: Partial<WhenOptions>): When {
|
|
173
|
-
|
|
174
|
-
|
|
80
|
+
const state: WhenState = {
|
|
81
|
+
promise: undefined as never,
|
|
82
|
+
result: false,
|
|
83
|
+
started: false,
|
|
84
|
+
timer: undefined as never,
|
|
85
|
+
};
|
|
175
86
|
|
|
176
|
-
|
|
87
|
+
let instance: When;
|
|
88
|
+
|
|
89
|
+
state.promise = new Promise<void>((resolve, reject) => {
|
|
90
|
+
state.resolver = resolve;
|
|
91
|
+
state.rejecter = reject;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
state.timer = createTimer(
|
|
95
|
+
TYPE_WHEN,
|
|
96
|
+
{
|
|
97
|
+
callback: () => onCallback(condition, state),
|
|
98
|
+
trace: new TimerTrace().stack,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
onAfter: () => onAfter(instance, state),
|
|
102
|
+
onError: () => onError(instance, state),
|
|
103
|
+
count: getValidNumber(options?.count),
|
|
104
|
+
interval: getValidNumber(options?.interval),
|
|
105
|
+
timeout: getValidTimeout(options?.timeout),
|
|
106
|
+
},
|
|
107
|
+
false,
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
instance = {
|
|
111
|
+
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),
|
|
116
|
+
} as When;
|
|
117
|
+
|
|
118
|
+
Object.defineProperties(instance, {
|
|
119
|
+
$timer: {
|
|
120
|
+
enumerable: false,
|
|
121
|
+
value: TYPE_WHEN,
|
|
122
|
+
},
|
|
123
|
+
active: {
|
|
124
|
+
enumerable: true,
|
|
125
|
+
get: () => state.timer?.active ?? false,
|
|
126
|
+
},
|
|
127
|
+
destroyed: {
|
|
128
|
+
enumerable: true,
|
|
129
|
+
get: () => state.timer == null,
|
|
130
|
+
},
|
|
131
|
+
paused: {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
get: () => state.timer?.paused ?? false,
|
|
134
|
+
},
|
|
135
|
+
trace: {
|
|
136
|
+
enumerable: true,
|
|
137
|
+
get: () => ((globalThis._oscarpalmer_timer_debug ?? false) ? state.timer?.trace : undefined),
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
return Object.freeze(instance) as When;
|
|
142
|
+
}
|
package/src/work.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
TIMERS_ACTIVE,
|
|
3
2
|
BUFFER_INTERVAL,
|
|
3
|
+
TIMERS_ACTIVE,
|
|
4
4
|
TYPE_WAIT,
|
|
5
5
|
WORK_CONTINUE,
|
|
6
6
|
WORK_PAUSE,
|
|
@@ -8,8 +8,7 @@ import {
|
|
|
8
8
|
WORK_START,
|
|
9
9
|
WORK_STOP,
|
|
10
10
|
} from './constants';
|
|
11
|
-
import type {TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType} from './models';
|
|
12
|
-
import type {Timer} from './timer';
|
|
11
|
+
import type {Timer, TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType} from './models';
|
|
13
12
|
|
|
14
13
|
function finish(
|
|
15
14
|
timer: WorkHandlerTimer,
|
|
@@ -25,7 +24,7 @@ function finish(
|
|
|
25
24
|
state.elapsed = 0;
|
|
26
25
|
state.frame = undefined;
|
|
27
26
|
|
|
28
|
-
if (timer.
|
|
27
|
+
if (timer.name === TYPE_WAIT) {
|
|
29
28
|
state.callback();
|
|
30
29
|
} else {
|
|
31
30
|
options.onAfter?.(success);
|