@oscarpalmer/timer 0.22.0 → 0.23.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.js +10 -0
- package/dist/constants.mjs +10 -0
- package/dist/functions.js +18 -3
- package/dist/functions.mjs +18 -4
- package/dist/global.js +7 -0
- package/dist/index.js +95 -43
- package/dist/index.mjs +8 -2
- package/dist/models.mjs +10 -0
- package/dist/timer.js +192 -0
- package/dist/timer.mjs +17 -7
- package/dist/when.js +87 -41
- package/dist/when.mjs +50 -32
- package/package.json +8 -8
- package/src/constants.ts +20 -0
- package/src/functions.ts +21 -5
- package/src/index.ts +16 -6
- package/src/models.ts +11 -1
- package/src/timer.ts +40 -18
- package/src/when.ts +63 -39
- package/types/constants.d.ts +13 -0
- package/types/functions.d.ts +2 -1
- package/types/index.d.cts +26 -5
- package/types/models.d.ts +6 -1
- package/types/timer.d.ts +15 -5
- package/types/when.d.ts +6 -0
package/src/when.ts
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
|
+
import {destroyWhen} from './functions';
|
|
2
3
|
import type {WhenOptions, WhenState} from './models';
|
|
3
4
|
import {BasicTimer, timer} from './timer';
|
|
4
5
|
|
|
5
6
|
export class When extends BasicTimer<WhenState> {
|
|
6
7
|
get active() {
|
|
7
|
-
return this.state.timer
|
|
8
|
+
return this.state.timer?.active ?? false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get destroyed() {
|
|
12
|
+
return this.state.timer == null;
|
|
8
13
|
}
|
|
9
14
|
|
|
10
15
|
get paused() {
|
|
11
|
-
return this.state.timer
|
|
16
|
+
return this.state.timer?.paused ?? false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get trace() {
|
|
20
|
+
return this.state.timer?.trace;
|
|
12
21
|
}
|
|
13
22
|
|
|
14
23
|
constructor(state: WhenState) {
|
|
@@ -19,16 +28,25 @@ export class When extends BasicTimer<WhenState> {
|
|
|
19
28
|
* Continues the timer _(if it was paused)_
|
|
20
29
|
*/
|
|
21
30
|
continue(): When {
|
|
22
|
-
this.state.timer
|
|
31
|
+
this.state.timer?.continue();
|
|
23
32
|
|
|
24
33
|
return this;
|
|
25
34
|
}
|
|
26
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Destroys the timer _
|
|
38
|
+
*/
|
|
39
|
+
destroy(): void {
|
|
40
|
+
if (this.state.timer != null) {
|
|
41
|
+
this.state.timer.destroy();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
27
45
|
/**
|
|
28
46
|
* Pauses the timer _(if it was running)_
|
|
29
47
|
*/
|
|
30
48
|
pause(): When {
|
|
31
|
-
this.state.timer
|
|
49
|
+
this.state.timer?.pause();
|
|
32
50
|
|
|
33
51
|
return this;
|
|
34
52
|
}
|
|
@@ -37,11 +55,7 @@ export class When extends BasicTimer<WhenState> {
|
|
|
37
55
|
* Stops the timer _(if it was running)_
|
|
38
56
|
*/
|
|
39
57
|
stop(): When {
|
|
40
|
-
|
|
41
|
-
this.state.timer.stop();
|
|
42
|
-
|
|
43
|
-
this.state.rejecter?.();
|
|
44
|
-
}
|
|
58
|
+
this.state.timer?.stop();
|
|
45
59
|
|
|
46
60
|
return this;
|
|
47
61
|
}
|
|
@@ -49,12 +63,19 @@ export class When extends BasicTimer<WhenState> {
|
|
|
49
63
|
/**
|
|
50
64
|
* Starts the timer and returns a promise that resolves when the condition is met
|
|
51
65
|
*/
|
|
52
|
-
|
|
53
66
|
// biome-ignore lint/suspicious/noThenProperty: returning a promise-like object, so it's ok ;)
|
|
54
67
|
then(
|
|
55
68
|
resolve?: (() => void) | null,
|
|
56
69
|
reject?: (() => void) | null,
|
|
57
70
|
): Promise<void> {
|
|
71
|
+
if (this.state.timer == null || this.state.started) {
|
|
72
|
+
return new Promise(() => {
|
|
73
|
+
(reject ?? noop)();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
this.state.started = true;
|
|
78
|
+
|
|
58
79
|
this.state.timer.start();
|
|
59
80
|
|
|
60
81
|
return this.state.promise.then(resolve ?? noop, reject ?? noop);
|
|
@@ -69,43 +90,46 @@ export function when(
|
|
|
69
90
|
condition: () => boolean,
|
|
70
91
|
options?: Partial<WhenOptions>,
|
|
71
92
|
): When {
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
afterCallback() {
|
|
83
|
-
if (!repeated.paused) {
|
|
84
|
-
if (condition()) {
|
|
85
|
-
state.resolver?.();
|
|
86
|
-
} else {
|
|
87
|
-
state.rejecter?.();
|
|
88
|
-
}
|
|
93
|
+
const state: WhenState = {
|
|
94
|
+
started: false,
|
|
95
|
+
timer: timer(
|
|
96
|
+
'repeat',
|
|
97
|
+
() => {
|
|
98
|
+
if (condition()) {
|
|
99
|
+
state.timer.stop();
|
|
89
100
|
}
|
|
90
101
|
},
|
|
91
|
-
|
|
92
|
-
|
|
102
|
+
{
|
|
103
|
+
afterCallback() {
|
|
104
|
+
if (!state.timer.paused) {
|
|
105
|
+
if (condition()) {
|
|
106
|
+
state.resolver?.();
|
|
107
|
+
} else {
|
|
108
|
+
state.rejecter?.();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
destroyWhen(state);
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
errorCallback() {
|
|
115
|
+
state.rejecter?.();
|
|
116
|
+
|
|
117
|
+
destroyWhen(state);
|
|
118
|
+
},
|
|
119
|
+
count: options?.count,
|
|
120
|
+
interval: options?.interval,
|
|
121
|
+
timeout: options?.timeout,
|
|
93
122
|
},
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
},
|
|
98
|
-
false,
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
const state: WhenState = {} as never;
|
|
123
|
+
false,
|
|
124
|
+
),
|
|
125
|
+
} as never;
|
|
102
126
|
|
|
103
|
-
|
|
127
|
+
const promise = new Promise<void>((resolve, reject) => {
|
|
104
128
|
state.resolver = resolve;
|
|
105
129
|
state.rejecter = reject;
|
|
106
130
|
});
|
|
107
131
|
|
|
108
|
-
state.
|
|
132
|
+
state.promise = promise;
|
|
109
133
|
|
|
110
134
|
return new When(state);
|
|
111
135
|
}
|
package/types/constants.d.ts
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
|
+
import type { WorkType } from './models';
|
|
1
2
|
import type { Timer } from './timer';
|
|
2
3
|
/**
|
|
3
4
|
* A set of all active timers
|
|
4
5
|
*/
|
|
5
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<WorkType>;
|
|
11
|
+
/**
|
|
12
|
+
* A set of types that allow work to end
|
|
13
|
+
*/
|
|
14
|
+
export declare const endTypes: Set<WorkType>;
|
|
15
|
+
/**
|
|
16
|
+
* A set of types that allow work to end or restart
|
|
17
|
+
*/
|
|
18
|
+
export declare const endOrRestartTypes: Set<WorkType>;
|
|
6
19
|
/**
|
|
7
20
|
* A set of timers that were paused due to the document being hidden
|
|
8
21
|
*/
|
package/types/functions.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { TimerOptions, TimerState, WorkType } from './models';
|
|
1
|
+
import type { TimerOptions, TimerState, WhenState, WorkType } from './models';
|
|
2
2
|
import type { Timer } from './timer';
|
|
3
|
+
export declare function destroyWhen(state: WhenState): void;
|
|
3
4
|
export declare function getOptions(options: Partial<TimerOptions>, isRepeated: boolean): TimerOptions;
|
|
4
5
|
export declare function getValueOrDefault(value: unknown, defaultValue: number, minimum?: number): number;
|
|
5
6
|
export declare function work(type: WorkType, timer: Timer, state: TimerState, options: TimerOptions): Timer;
|
package/types/index.d.cts
CHANGED
|
@@ -43,6 +43,7 @@ export type TimerOptions = {} & RepeatOptions;
|
|
|
43
43
|
export type TimerState = {
|
|
44
44
|
active: boolean;
|
|
45
45
|
callback: AnyCallback;
|
|
46
|
+
destroyed: boolean;
|
|
46
47
|
count?: number;
|
|
47
48
|
elapsed?: number;
|
|
48
49
|
frame?: number;
|
|
@@ -50,14 +51,18 @@ export type TimerState = {
|
|
|
50
51
|
isRepeated: boolean;
|
|
51
52
|
minimum: number;
|
|
52
53
|
paused: boolean;
|
|
53
|
-
trace:
|
|
54
|
+
trace: TimerTrace;
|
|
54
55
|
};
|
|
56
|
+
declare class TimerTrace extends Error {
|
|
57
|
+
constructor();
|
|
58
|
+
}
|
|
55
59
|
export type WaitOptions = {} & BaseOptions & OptionsWithError;
|
|
56
60
|
export type WhenOptions = {} & OptionsWithCount;
|
|
57
61
|
export type WhenState = {
|
|
58
62
|
promise: Promise<void>;
|
|
59
63
|
rejecter?: () => void;
|
|
60
64
|
resolver?: () => void;
|
|
65
|
+
started: boolean;
|
|
61
66
|
timer: Timer$1;
|
|
62
67
|
};
|
|
63
68
|
declare abstract class BasicTimer<State> {
|
|
@@ -68,10 +73,18 @@ declare abstract class BasicTimer<State> {
|
|
|
68
73
|
* Is the timer running?
|
|
69
74
|
*/
|
|
70
75
|
abstract readonly active: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Is the timer destroyed?
|
|
78
|
+
*/
|
|
79
|
+
abstract readonly destroyed: boolean;
|
|
71
80
|
/**
|
|
72
81
|
* Is the timer paused?
|
|
73
82
|
*/
|
|
74
83
|
abstract readonly paused: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Gets the traced location of the timer
|
|
86
|
+
*/
|
|
87
|
+
abstract readonly trace: TimerTrace | undefined;
|
|
75
88
|
}
|
|
76
89
|
/**
|
|
77
90
|
* A timer that can be started, stopped, and restarted as neeeded
|
|
@@ -79,16 +92,18 @@ declare abstract class BasicTimer<State> {
|
|
|
79
92
|
declare class Timer$1 extends BasicTimer<TimerState> {
|
|
80
93
|
private readonly options;
|
|
81
94
|
get active(): boolean;
|
|
95
|
+
get destroyed(): boolean;
|
|
82
96
|
get paused(): boolean;
|
|
83
|
-
|
|
84
|
-
* Gets the traced location of the timer
|
|
85
|
-
*/
|
|
86
|
-
get trace(): unknown;
|
|
97
|
+
get trace(): TimerTrace | undefined;
|
|
87
98
|
constructor(type: "repeat" | "wait", state: TimerState, options: TimerOptions);
|
|
88
99
|
/**
|
|
89
100
|
* Continues the timer _(if it was paused)_
|
|
90
101
|
*/
|
|
91
102
|
continue(): Timer$1;
|
|
103
|
+
/**
|
|
104
|
+
* Destroys the timer _(after stopping it, if it was running)_
|
|
105
|
+
*/
|
|
106
|
+
destroy(): void;
|
|
92
107
|
/**
|
|
93
108
|
* Pauses the timer _(if it was running)_
|
|
94
109
|
*/
|
|
@@ -132,12 +147,18 @@ export declare function wait(callback: () => void, time: number): Timer$1;
|
|
|
132
147
|
export declare function wait(callback: () => void, options: Partial<WaitOptions>): Timer$1;
|
|
133
148
|
export declare class When extends BasicTimer<WhenState> {
|
|
134
149
|
get active(): boolean;
|
|
150
|
+
get destroyed(): boolean;
|
|
135
151
|
get paused(): boolean;
|
|
152
|
+
get trace(): TimerTrace | undefined;
|
|
136
153
|
constructor(state: WhenState);
|
|
137
154
|
/**
|
|
138
155
|
* Continues the timer _(if it was paused)_
|
|
139
156
|
*/
|
|
140
157
|
continue(): When;
|
|
158
|
+
/**
|
|
159
|
+
* Destroys the timer _
|
|
160
|
+
*/
|
|
161
|
+
destroy(): void;
|
|
141
162
|
/**
|
|
142
163
|
* Pauses the timer _(if it was running)_
|
|
143
164
|
*/
|
package/types/models.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ export type TimerOptions = {} & RepeatOptions;
|
|
|
42
42
|
export type TimerState = {
|
|
43
43
|
active: boolean;
|
|
44
44
|
callback: AnyCallback;
|
|
45
|
+
destroyed: boolean;
|
|
45
46
|
count?: number;
|
|
46
47
|
elapsed?: number;
|
|
47
48
|
frame?: number;
|
|
@@ -49,14 +50,18 @@ export type TimerState = {
|
|
|
49
50
|
isRepeated: boolean;
|
|
50
51
|
minimum: number;
|
|
51
52
|
paused: boolean;
|
|
52
|
-
trace:
|
|
53
|
+
trace: TimerTrace;
|
|
53
54
|
};
|
|
55
|
+
export declare class TimerTrace extends Error {
|
|
56
|
+
constructor();
|
|
57
|
+
}
|
|
54
58
|
export type WaitOptions = {} & BaseOptions & OptionsWithError;
|
|
55
59
|
export type WhenOptions = {} & OptionsWithCount;
|
|
56
60
|
export type WhenState = {
|
|
57
61
|
promise: Promise<void>;
|
|
58
62
|
rejecter?: () => void;
|
|
59
63
|
resolver?: () => void;
|
|
64
|
+
started: boolean;
|
|
60
65
|
timer: Timer;
|
|
61
66
|
};
|
|
62
67
|
export type WorkType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
|
package/types/timer.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { TimerTrace, type AnyCallback, type IndexedCallback, type RepeatOptions, type TimerOptions, type TimerState, type WaitOptions } from './models';
|
|
2
2
|
export declare abstract class BasicTimer<State> {
|
|
3
3
|
protected readonly $timer: string;
|
|
4
4
|
protected readonly state: State;
|
|
@@ -7,10 +7,18 @@ export declare abstract class BasicTimer<State> {
|
|
|
7
7
|
* Is the timer running?
|
|
8
8
|
*/
|
|
9
9
|
abstract readonly active: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Is the timer destroyed?
|
|
12
|
+
*/
|
|
13
|
+
abstract readonly destroyed: boolean;
|
|
10
14
|
/**
|
|
11
15
|
* Is the timer paused?
|
|
12
16
|
*/
|
|
13
17
|
abstract readonly paused: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Gets the traced location of the timer
|
|
20
|
+
*/
|
|
21
|
+
abstract readonly trace: TimerTrace | undefined;
|
|
14
22
|
}
|
|
15
23
|
/**
|
|
16
24
|
* A timer that can be started, stopped, and restarted as neeeded
|
|
@@ -18,16 +26,18 @@ export declare abstract class BasicTimer<State> {
|
|
|
18
26
|
export declare class Timer extends BasicTimer<TimerState> {
|
|
19
27
|
private readonly options;
|
|
20
28
|
get active(): boolean;
|
|
29
|
+
get destroyed(): boolean;
|
|
21
30
|
get paused(): boolean;
|
|
22
|
-
|
|
23
|
-
* Gets the traced location of the timer
|
|
24
|
-
*/
|
|
25
|
-
get trace(): unknown;
|
|
31
|
+
get trace(): TimerTrace | undefined;
|
|
26
32
|
constructor(type: 'repeat' | 'wait', state: TimerState, options: TimerOptions);
|
|
27
33
|
/**
|
|
28
34
|
* Continues the timer _(if it was paused)_
|
|
29
35
|
*/
|
|
30
36
|
continue(): Timer;
|
|
37
|
+
/**
|
|
38
|
+
* Destroys the timer _(after stopping it, if it was running)_
|
|
39
|
+
*/
|
|
40
|
+
destroy(): void;
|
|
31
41
|
/**
|
|
32
42
|
* Pauses the timer _(if it was running)_
|
|
33
43
|
*/
|
package/types/when.d.ts
CHANGED
|
@@ -2,12 +2,18 @@ import type { WhenOptions, WhenState } from './models';
|
|
|
2
2
|
import { BasicTimer } from './timer';
|
|
3
3
|
export declare class When extends BasicTimer<WhenState> {
|
|
4
4
|
get active(): boolean;
|
|
5
|
+
get destroyed(): boolean;
|
|
5
6
|
get paused(): boolean;
|
|
7
|
+
get trace(): import("./models").TimerTrace | undefined;
|
|
6
8
|
constructor(state: WhenState);
|
|
7
9
|
/**
|
|
8
10
|
* Continues the timer _(if it was paused)_
|
|
9
11
|
*/
|
|
10
12
|
continue(): When;
|
|
13
|
+
/**
|
|
14
|
+
* Destroys the timer _
|
|
15
|
+
*/
|
|
16
|
+
destroy(): void;
|
|
11
17
|
/**
|
|
12
18
|
* Pauses the timer _(if it was running)_
|
|
13
19
|
*/
|