@oscarpalmer/timer 0.22.1 → 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/constants.js +10 -0
- package/dist/constants.mjs +10 -0
- package/dist/functions.js +9 -2
- package/dist/functions.mjs +9 -3
- package/dist/global.js +7 -0
- package/dist/index.js +94 -44
- package/dist/index.mjs +8 -2
- package/dist/models.mjs +10 -0
- package/dist/timer.js +31 -9
- package/dist/timer.mjs +17 -7
- package/dist/when.js +86 -42
- package/dist/when.mjs +55 -33
- package/package.json +6 -6
- package/src/constants.ts +20 -0
- package/src/functions.ts +12 -5
- package/src/index.ts +16 -6
- package/src/models.ts +11 -1
- package/src/timer.ts +40 -18
- package/src/when.ts +75 -40
- package/types/constants.d.ts +13 -0
- 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
|
@@ -2,13 +2,24 @@ import {noop} from '@oscarpalmer/atoms/function';
|
|
|
2
2
|
import type {WhenOptions, WhenState} from './models';
|
|
3
3
|
import {BasicTimer, timer} from './timer';
|
|
4
4
|
|
|
5
|
+
const destroyedMessage = 'Timer has already been destroyed';
|
|
6
|
+
const startedMessage = 'Timer has already been started';
|
|
7
|
+
|
|
5
8
|
export class When extends BasicTimer<WhenState> {
|
|
6
9
|
get active() {
|
|
7
|
-
return this.state.timer
|
|
10
|
+
return this.state.timer?.active ?? false;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get destroyed() {
|
|
14
|
+
return this.state.timer == null;
|
|
8
15
|
}
|
|
9
16
|
|
|
10
17
|
get paused() {
|
|
11
|
-
return this.state.timer
|
|
18
|
+
return this.state.timer?.paused ?? false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get trace() {
|
|
22
|
+
return this.state.timer?.trace;
|
|
12
23
|
}
|
|
13
24
|
|
|
14
25
|
constructor(state: WhenState) {
|
|
@@ -19,16 +30,28 @@ export class When extends BasicTimer<WhenState> {
|
|
|
19
30
|
* Continues the timer _(if it was paused)_
|
|
20
31
|
*/
|
|
21
32
|
continue(): When {
|
|
22
|
-
this.state.timer
|
|
33
|
+
this.state.timer?.continue();
|
|
23
34
|
|
|
24
35
|
return this;
|
|
25
36
|
}
|
|
26
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Destroys the timer _(and stops it,if it was running)_
|
|
40
|
+
*/
|
|
41
|
+
destroy(): void {
|
|
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;
|
|
48
|
+
}
|
|
49
|
+
|
|
27
50
|
/**
|
|
28
51
|
* Pauses the timer _(if it was running)_
|
|
29
52
|
*/
|
|
30
53
|
pause(): When {
|
|
31
|
-
this.state.timer
|
|
54
|
+
this.state.timer?.pause();
|
|
32
55
|
|
|
33
56
|
return this;
|
|
34
57
|
}
|
|
@@ -37,11 +60,7 @@ export class When extends BasicTimer<WhenState> {
|
|
|
37
60
|
* Stops the timer _(if it was running)_
|
|
38
61
|
*/
|
|
39
62
|
stop(): When {
|
|
40
|
-
|
|
41
|
-
this.state.timer.stop();
|
|
42
|
-
|
|
43
|
-
this.state.rejecter?.();
|
|
44
|
-
}
|
|
63
|
+
this.state.timer?.stop();
|
|
45
64
|
|
|
46
65
|
return this;
|
|
47
66
|
}
|
|
@@ -49,12 +68,19 @@ export class When extends BasicTimer<WhenState> {
|
|
|
49
68
|
/**
|
|
50
69
|
* Starts the timer and returns a promise that resolves when the condition is met
|
|
51
70
|
*/
|
|
52
|
-
|
|
53
71
|
// biome-ignore lint/suspicious/noThenProperty: returning a promise-like object, so it's ok ;)
|
|
54
72
|
then(
|
|
55
73
|
resolve?: (() => void) | null,
|
|
56
74
|
reject?: (() => void) | null,
|
|
57
75
|
): Promise<void> {
|
|
76
|
+
if (this.state.timer == null || this.state?.started) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
this.state.timer == null ? destroyedMessage : startedMessage,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.state.started = true;
|
|
83
|
+
|
|
58
84
|
this.state.timer.start();
|
|
59
85
|
|
|
60
86
|
return this.state.promise.then(resolve ?? noop, reject ?? noop);
|
|
@@ -69,43 +95,52 @@ export function when(
|
|
|
69
95
|
condition: () => boolean,
|
|
70
96
|
options?: Partial<WhenOptions>,
|
|
71
97
|
): When {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (!repeated.paused) {
|
|
84
|
-
if (condition()) {
|
|
85
|
-
state.resolver?.();
|
|
86
|
-
} else {
|
|
87
|
-
state.rejecter?.();
|
|
88
|
-
}
|
|
98
|
+
let result = false;
|
|
99
|
+
|
|
100
|
+
const state: WhenState = {
|
|
101
|
+
started: false,
|
|
102
|
+
timer: timer(
|
|
103
|
+
'repeat',
|
|
104
|
+
() => {
|
|
105
|
+
if (condition()) {
|
|
106
|
+
result = true;
|
|
107
|
+
|
|
108
|
+
state.timer.stop();
|
|
89
109
|
}
|
|
90
110
|
},
|
|
91
|
-
|
|
92
|
-
|
|
111
|
+
{
|
|
112
|
+
afterCallback() {
|
|
113
|
+
if (!state.timer.paused) {
|
|
114
|
+
if (result) {
|
|
115
|
+
state.resolver?.();
|
|
116
|
+
} else {
|
|
117
|
+
state.rejecter?.();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
instance.destroy();
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
errorCallback() {
|
|
124
|
+
state.rejecter?.();
|
|
125
|
+
|
|
126
|
+
instance.destroy();
|
|
127
|
+
},
|
|
128
|
+
count: options?.count,
|
|
129
|
+
interval: options?.interval,
|
|
130
|
+
timeout: options?.timeout,
|
|
93
131
|
},
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
},
|
|
98
|
-
false,
|
|
99
|
-
);
|
|
132
|
+
false,
|
|
133
|
+
),
|
|
134
|
+
} as never;
|
|
100
135
|
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
state.promise = new Promise((resolve, reject) => {
|
|
136
|
+
const promise = new Promise<void>((resolve, reject) => {
|
|
104
137
|
state.resolver = resolve;
|
|
105
138
|
state.rejecter = reject;
|
|
106
139
|
});
|
|
107
140
|
|
|
108
|
-
state.
|
|
141
|
+
state.promise = promise;
|
|
142
|
+
|
|
143
|
+
const instance = new When(state);
|
|
109
144
|
|
|
110
|
-
return
|
|
145
|
+
return instance;
|
|
111
146
|
}
|
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/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 _(and stops it,if it was running)_
|
|
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 _(and stops it,if it was running)_
|
|
15
|
+
*/
|
|
16
|
+
destroy(): void;
|
|
11
17
|
/**
|
|
12
18
|
* Pauses the timer _(if it was running)_
|
|
13
19
|
*/
|