@oscarpalmer/timer 0.23.0 → 0.24.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/functions.js +1 -9
- package/dist/functions.mjs +1 -9
- package/dist/index.js +16 -18
- package/dist/when.js +16 -18
- package/dist/when.mjs +16 -12
- package/package.json +1 -1
- package/src/functions.ts +0 -9
- package/src/when.ts +24 -13
- package/types/functions.d.ts +1 -2
- package/types/index.d.cts +1 -1
- package/types/when.d.ts +1 -1
package/dist/functions.js
CHANGED
|
@@ -11,13 +11,6 @@ var hiddenTimers = new Set;
|
|
|
11
11
|
var milliseconds = 1000 / 60;
|
|
12
12
|
|
|
13
13
|
// src/functions.ts
|
|
14
|
-
function destroyWhen(state) {
|
|
15
|
-
state.timer?.destroy();
|
|
16
|
-
state.promise = undefined;
|
|
17
|
-
state.rejecter = undefined;
|
|
18
|
-
state.resolver = undefined;
|
|
19
|
-
state.timer = undefined;
|
|
20
|
-
}
|
|
21
14
|
function getOptions(options, isRepeated) {
|
|
22
15
|
return {
|
|
23
16
|
afterCallback: options.afterCallback,
|
|
@@ -107,6 +100,5 @@ function work(type, timer, state, options) {
|
|
|
107
100
|
export {
|
|
108
101
|
work,
|
|
109
102
|
getValueOrDefault,
|
|
110
|
-
getOptions
|
|
111
|
-
destroyWhen
|
|
103
|
+
getOptions
|
|
112
104
|
};
|
package/dist/functions.mjs
CHANGED
|
@@ -6,13 +6,6 @@ endOrRestartTypes,
|
|
|
6
6
|
endTypes,
|
|
7
7
|
milliseconds
|
|
8
8
|
} from "./constants";
|
|
9
|
-
function destroyWhen(state) {
|
|
10
|
-
state.timer?.destroy();
|
|
11
|
-
state.promise = undefined;
|
|
12
|
-
state.rejecter = undefined;
|
|
13
|
-
state.resolver = undefined;
|
|
14
|
-
state.timer = undefined;
|
|
15
|
-
}
|
|
16
9
|
function getOptions(options, isRepeated) {
|
|
17
10
|
return {
|
|
18
11
|
afterCallback: options.afterCallback,
|
|
@@ -102,6 +95,5 @@ function work(type, timer, state, options) {
|
|
|
102
95
|
export {
|
|
103
96
|
work,
|
|
104
97
|
getValueOrDefault,
|
|
105
|
-
getOptions
|
|
106
|
-
destroyWhen
|
|
98
|
+
getOptions
|
|
107
99
|
};
|
package/dist/index.js
CHANGED
|
@@ -24,13 +24,6 @@ if (globalThis._oscarpalmer_timers == null) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
// src/functions.ts
|
|
27
|
-
function destroyWhen(state) {
|
|
28
|
-
state.timer?.destroy();
|
|
29
|
-
state.promise = undefined;
|
|
30
|
-
state.rejecter = undefined;
|
|
31
|
-
state.resolver = undefined;
|
|
32
|
-
state.timer = undefined;
|
|
33
|
-
}
|
|
34
27
|
function getOptions(options, isRepeated) {
|
|
35
28
|
return {
|
|
36
29
|
afterCallback: options.afterCallback,
|
|
@@ -239,26 +232,28 @@ function isWhen(value) {
|
|
|
239
232
|
}
|
|
240
233
|
// src/when.ts
|
|
241
234
|
function when(condition, options) {
|
|
235
|
+
let result = false;
|
|
242
236
|
const state = {
|
|
243
237
|
started: false,
|
|
244
238
|
timer: timer("repeat", () => {
|
|
245
239
|
if (condition()) {
|
|
240
|
+
result = true;
|
|
246
241
|
state.timer.stop();
|
|
247
242
|
}
|
|
248
243
|
}, {
|
|
249
244
|
afterCallback() {
|
|
250
245
|
if (!state.timer.paused) {
|
|
251
|
-
if (
|
|
246
|
+
if (result) {
|
|
252
247
|
state.resolver?.();
|
|
253
248
|
} else {
|
|
254
249
|
state.rejecter?.();
|
|
255
250
|
}
|
|
256
|
-
|
|
251
|
+
instance.destroy();
|
|
257
252
|
}
|
|
258
253
|
},
|
|
259
254
|
errorCallback() {
|
|
260
255
|
state.rejecter?.();
|
|
261
|
-
|
|
256
|
+
instance.destroy();
|
|
262
257
|
},
|
|
263
258
|
count: options?.count,
|
|
264
259
|
interval: options?.interval,
|
|
@@ -270,8 +265,11 @@ function when(condition, options) {
|
|
|
270
265
|
state.rejecter = reject;
|
|
271
266
|
});
|
|
272
267
|
state.promise = promise;
|
|
273
|
-
|
|
268
|
+
const instance = new When(state);
|
|
269
|
+
return instance;
|
|
274
270
|
}
|
|
271
|
+
var destroyedMessage = "Timer has already been destroyed";
|
|
272
|
+
var startedMessage = "Timer has already been started";
|
|
275
273
|
|
|
276
274
|
class When extends BasicTimer {
|
|
277
275
|
get active() {
|
|
@@ -294,9 +292,11 @@ class When extends BasicTimer {
|
|
|
294
292
|
return this;
|
|
295
293
|
}
|
|
296
294
|
destroy() {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
295
|
+
this.state.timer?.destroy();
|
|
296
|
+
this.state.promise = undefined;
|
|
297
|
+
this.state.resolver = noop;
|
|
298
|
+
this.state.rejecter = noop;
|
|
299
|
+
this.state.timer = undefined;
|
|
300
300
|
}
|
|
301
301
|
pause() {
|
|
302
302
|
this.state.timer?.pause();
|
|
@@ -307,10 +307,8 @@ class When extends BasicTimer {
|
|
|
307
307
|
return this;
|
|
308
308
|
}
|
|
309
309
|
then(resolve, reject) {
|
|
310
|
-
if (this.state.timer == null || this.state
|
|
311
|
-
|
|
312
|
-
(reject ?? noop)();
|
|
313
|
-
});
|
|
310
|
+
if (this.state.timer == null || this.state?.started) {
|
|
311
|
+
throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
|
|
314
312
|
}
|
|
315
313
|
this.state.started = true;
|
|
316
314
|
this.state.timer.start();
|
package/dist/when.js
CHANGED
|
@@ -15,13 +15,6 @@ var hiddenTimers = new Set;
|
|
|
15
15
|
var milliseconds = 1000 / 60;
|
|
16
16
|
|
|
17
17
|
// src/functions.ts
|
|
18
|
-
function destroyWhen(state) {
|
|
19
|
-
state.timer?.destroy();
|
|
20
|
-
state.promise = undefined;
|
|
21
|
-
state.rejecter = undefined;
|
|
22
|
-
state.resolver = undefined;
|
|
23
|
-
state.timer = undefined;
|
|
24
|
-
}
|
|
25
18
|
function getOptions(options, isRepeated) {
|
|
26
19
|
return {
|
|
27
20
|
afterCallback: options.afterCallback,
|
|
@@ -188,26 +181,28 @@ class Timer extends BasicTimer {
|
|
|
188
181
|
|
|
189
182
|
// src/when.ts
|
|
190
183
|
function when(condition, options) {
|
|
184
|
+
let result = false;
|
|
191
185
|
const state = {
|
|
192
186
|
started: false,
|
|
193
187
|
timer: timer("repeat", () => {
|
|
194
188
|
if (condition()) {
|
|
189
|
+
result = true;
|
|
195
190
|
state.timer.stop();
|
|
196
191
|
}
|
|
197
192
|
}, {
|
|
198
193
|
afterCallback() {
|
|
199
194
|
if (!state.timer.paused) {
|
|
200
|
-
if (
|
|
195
|
+
if (result) {
|
|
201
196
|
state.resolver?.();
|
|
202
197
|
} else {
|
|
203
198
|
state.rejecter?.();
|
|
204
199
|
}
|
|
205
|
-
|
|
200
|
+
instance.destroy();
|
|
206
201
|
}
|
|
207
202
|
},
|
|
208
203
|
errorCallback() {
|
|
209
204
|
state.rejecter?.();
|
|
210
|
-
|
|
205
|
+
instance.destroy();
|
|
211
206
|
},
|
|
212
207
|
count: options?.count,
|
|
213
208
|
interval: options?.interval,
|
|
@@ -219,8 +214,11 @@ function when(condition, options) {
|
|
|
219
214
|
state.rejecter = reject;
|
|
220
215
|
});
|
|
221
216
|
state.promise = promise;
|
|
222
|
-
|
|
217
|
+
const instance = new When(state);
|
|
218
|
+
return instance;
|
|
223
219
|
}
|
|
220
|
+
var destroyedMessage = "Timer has already been destroyed";
|
|
221
|
+
var startedMessage = "Timer has already been started";
|
|
224
222
|
|
|
225
223
|
class When extends BasicTimer {
|
|
226
224
|
get active() {
|
|
@@ -243,9 +241,11 @@ class When extends BasicTimer {
|
|
|
243
241
|
return this;
|
|
244
242
|
}
|
|
245
243
|
destroy() {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
244
|
+
this.state.timer?.destroy();
|
|
245
|
+
this.state.promise = undefined;
|
|
246
|
+
this.state.resolver = noop;
|
|
247
|
+
this.state.rejecter = noop;
|
|
248
|
+
this.state.timer = undefined;
|
|
249
249
|
}
|
|
250
250
|
pause() {
|
|
251
251
|
this.state.timer?.pause();
|
|
@@ -256,10 +256,8 @@ class When extends BasicTimer {
|
|
|
256
256
|
return this;
|
|
257
257
|
}
|
|
258
258
|
then(resolve, reject) {
|
|
259
|
-
if (this.state.timer == null || this.state
|
|
260
|
-
|
|
261
|
-
(reject ?? noop)();
|
|
262
|
-
});
|
|
259
|
+
if (this.state.timer == null || this.state?.started) {
|
|
260
|
+
throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
|
|
263
261
|
}
|
|
264
262
|
this.state.started = true;
|
|
265
263
|
this.state.timer.start();
|
package/dist/when.mjs
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
// src/when.ts
|
|
2
2
|
import {noop} from "@oscarpalmer/atoms/function";
|
|
3
|
-
import {destroyWhen} from "./functions";
|
|
4
3
|
import {BasicTimer, timer as timer2} from "./timer";
|
|
5
4
|
function when(condition, options) {
|
|
5
|
+
let result = false;
|
|
6
6
|
const state = {
|
|
7
7
|
started: false,
|
|
8
8
|
timer: timer2("repeat", () => {
|
|
9
9
|
if (condition()) {
|
|
10
|
+
result = true;
|
|
10
11
|
state.timer.stop();
|
|
11
12
|
}
|
|
12
13
|
}, {
|
|
13
14
|
afterCallback() {
|
|
14
15
|
if (!state.timer.paused) {
|
|
15
|
-
if (
|
|
16
|
+
if (result) {
|
|
16
17
|
state.resolver?.();
|
|
17
18
|
} else {
|
|
18
19
|
state.rejecter?.();
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
+
instance.destroy();
|
|
21
22
|
}
|
|
22
23
|
},
|
|
23
24
|
errorCallback() {
|
|
24
25
|
state.rejecter?.();
|
|
25
|
-
|
|
26
|
+
instance.destroy();
|
|
26
27
|
},
|
|
27
28
|
count: options?.count,
|
|
28
29
|
interval: options?.interval,
|
|
@@ -34,8 +35,11 @@ function when(condition, options) {
|
|
|
34
35
|
state.rejecter = reject;
|
|
35
36
|
});
|
|
36
37
|
state.promise = promise;
|
|
37
|
-
|
|
38
|
+
const instance = new When(state);
|
|
39
|
+
return instance;
|
|
38
40
|
}
|
|
41
|
+
var destroyedMessage = "Timer has already been destroyed";
|
|
42
|
+
var startedMessage = "Timer has already been started";
|
|
39
43
|
|
|
40
44
|
class When extends BasicTimer {
|
|
41
45
|
get active() {
|
|
@@ -58,9 +62,11 @@ class When extends BasicTimer {
|
|
|
58
62
|
return this;
|
|
59
63
|
}
|
|
60
64
|
destroy() {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
this.state.timer?.destroy();
|
|
66
|
+
this.state.promise = undefined;
|
|
67
|
+
this.state.resolver = noop;
|
|
68
|
+
this.state.rejecter = noop;
|
|
69
|
+
this.state.timer = undefined;
|
|
64
70
|
}
|
|
65
71
|
pause() {
|
|
66
72
|
this.state.timer?.pause();
|
|
@@ -71,10 +77,8 @@ class When extends BasicTimer {
|
|
|
71
77
|
return this;
|
|
72
78
|
}
|
|
73
79
|
then(resolve, reject) {
|
|
74
|
-
if (this.state.timer == null || this.state
|
|
75
|
-
|
|
76
|
-
(reject ?? noop)();
|
|
77
|
-
});
|
|
80
|
+
if (this.state.timer == null || this.state?.started) {
|
|
81
|
+
throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
|
|
78
82
|
}
|
|
79
83
|
this.state.started = true;
|
|
80
84
|
this.state.timer.start();
|
package/package.json
CHANGED
package/src/functions.ts
CHANGED
|
@@ -8,15 +8,6 @@ import {
|
|
|
8
8
|
import type {TimerOptions, TimerState, WhenState, WorkType} from './models';
|
|
9
9
|
import type {Timer} from './timer';
|
|
10
10
|
|
|
11
|
-
export function destroyWhen(state: WhenState): void {
|
|
12
|
-
state.timer?.destroy();
|
|
13
|
-
|
|
14
|
-
state.promise = undefined as never;
|
|
15
|
-
state.rejecter = undefined;
|
|
16
|
-
state.resolver = undefined;
|
|
17
|
-
state.timer = undefined as never;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
11
|
export function getOptions(
|
|
21
12
|
options: Partial<TimerOptions>,
|
|
22
13
|
isRepeated: boolean,
|
package/src/when.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
|
-
import {destroyWhen} from './functions';
|
|
3
2
|
import type {WhenOptions, WhenState} from './models';
|
|
4
3
|
import {BasicTimer, timer} from './timer';
|
|
5
4
|
|
|
5
|
+
const destroyedMessage = 'Timer has already been destroyed';
|
|
6
|
+
const startedMessage = 'Timer has already been started';
|
|
7
|
+
|
|
6
8
|
export class When extends BasicTimer<WhenState> {
|
|
7
9
|
get active() {
|
|
8
10
|
return this.state.timer?.active ?? false;
|
|
@@ -34,12 +36,15 @@ export class When extends BasicTimer<WhenState> {
|
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
/**
|
|
37
|
-
* Destroys the timer _
|
|
39
|
+
* Destroys the timer _(and stops it,if it was running)_
|
|
38
40
|
*/
|
|
39
41
|
destroy(): void {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
this.state.timer?.destroy();
|
|
43
|
+
|
|
44
|
+
this.state.promise = undefined as never;
|
|
45
|
+
this.state.resolver = noop;
|
|
46
|
+
this.state.rejecter = noop;
|
|
47
|
+
this.state.timer = undefined as never;
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
/**
|
|
@@ -68,10 +73,10 @@ export class When extends BasicTimer<WhenState> {
|
|
|
68
73
|
resolve?: (() => void) | null,
|
|
69
74
|
reject?: (() => void) | null,
|
|
70
75
|
): Promise<void> {
|
|
71
|
-
if (this.state.timer == null || this.state
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
if (this.state.timer == null || this.state?.started) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
this.state.timer == null ? destroyedMessage : startedMessage,
|
|
79
|
+
);
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
this.state.started = true;
|
|
@@ -90,31 +95,35 @@ export function when(
|
|
|
90
95
|
condition: () => boolean,
|
|
91
96
|
options?: Partial<WhenOptions>,
|
|
92
97
|
): When {
|
|
98
|
+
let result = false;
|
|
99
|
+
|
|
93
100
|
const state: WhenState = {
|
|
94
101
|
started: false,
|
|
95
102
|
timer: timer(
|
|
96
103
|
'repeat',
|
|
97
104
|
() => {
|
|
98
105
|
if (condition()) {
|
|
106
|
+
result = true;
|
|
107
|
+
|
|
99
108
|
state.timer.stop();
|
|
100
109
|
}
|
|
101
110
|
},
|
|
102
111
|
{
|
|
103
112
|
afterCallback() {
|
|
104
113
|
if (!state.timer.paused) {
|
|
105
|
-
if (
|
|
114
|
+
if (result) {
|
|
106
115
|
state.resolver?.();
|
|
107
116
|
} else {
|
|
108
117
|
state.rejecter?.();
|
|
109
118
|
}
|
|
110
119
|
|
|
111
|
-
|
|
120
|
+
instance.destroy();
|
|
112
121
|
}
|
|
113
122
|
},
|
|
114
123
|
errorCallback() {
|
|
115
124
|
state.rejecter?.();
|
|
116
125
|
|
|
117
|
-
|
|
126
|
+
instance.destroy();
|
|
118
127
|
},
|
|
119
128
|
count: options?.count,
|
|
120
129
|
interval: options?.interval,
|
|
@@ -131,5 +140,7 @@ export function when(
|
|
|
131
140
|
|
|
132
141
|
state.promise = promise;
|
|
133
142
|
|
|
134
|
-
|
|
143
|
+
const instance = new When(state);
|
|
144
|
+
|
|
145
|
+
return instance;
|
|
135
146
|
}
|
package/types/functions.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { TimerOptions, TimerState,
|
|
1
|
+
import type { TimerOptions, TimerState, WorkType } from './models';
|
|
2
2
|
import type { Timer } from './timer';
|
|
3
|
-
export declare function destroyWhen(state: WhenState): void;
|
|
4
3
|
export declare function getOptions(options: Partial<TimerOptions>, isRepeated: boolean): TimerOptions;
|
|
5
4
|
export declare function getValueOrDefault(value: unknown, defaultValue: number, minimum?: number): number;
|
|
6
5
|
export declare function work(type: WorkType, timer: Timer, state: TimerState, options: TimerOptions): Timer;
|
package/types/index.d.cts
CHANGED