@oscarpalmer/timer 0.25.0 → 0.26.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.cjs +18 -0
- package/dist/constants.js +11 -12
- package/dist/{functions.mjs → functions.cjs} +35 -34
- package/dist/functions.js +25 -29
- package/dist/global.cjs +9 -0
- package/dist/global.js +1 -13
- package/dist/index.cjs +47 -0
- package/dist/index.js +30 -324
- package/dist/{is.mjs → is.cjs} +7 -8
- package/dist/is.js +4 -5
- package/dist/models.cjs +9 -0
- package/dist/{models.mjs → models.js} +0 -1
- package/dist/timer.cjs +111 -0
- package/dist/timer.js +64 -145
- package/dist/when.cjs +122 -0
- package/dist/when.js +86 -234
- package/package.json +19 -20
- package/src/constants.ts +2 -2
- package/src/functions.ts +3 -3
- package/src/global.ts +2 -2
- package/src/index.ts +4 -3
- package/src/is.ts +2 -2
- package/src/models.ts +1 -1
- package/src/timer.ts +3 -3
- package/src/when.ts +2 -2
- package/types/constants.d.cts +138 -0
- package/types/constants.d.ts +2 -2
- package/types/functions.d.cts +117 -0
- package/types/functions.d.ts +2 -2
- package/types/global.d.cts +3 -0
- package/types/global.d.ts +1 -1
- package/types/index.d.cts +78 -82
- package/types/index.d.ts +1 -1
- package/types/is.d.cts +163 -0
- package/types/is.d.ts +2 -2
- package/types/models.d.cts +123 -0
- package/types/models.d.ts +1 -1
- package/types/timer.d.cts +142 -0
- package/types/timer.d.ts +1 -1
- package/types/when.d.cts +153 -0
- package/types/when.d.ts +3 -3
- package/dist/constants.mjs +0 -19
- package/dist/global.mjs +0 -9
- package/dist/index.mjs +0 -46
- package/dist/timer.mjs +0 -89
- package/dist/when.mjs +0 -91
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
+
|
|
3
|
+
export declare abstract class BasicTimer<State> {
|
|
4
|
+
protected readonly $timer: string;
|
|
5
|
+
protected readonly state: State;
|
|
6
|
+
constructor(type: "repeat" | "wait" | "when", state: State);
|
|
7
|
+
/**
|
|
8
|
+
* Is the timer running?
|
|
9
|
+
*/
|
|
10
|
+
abstract readonly active: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Is the timer destroyed?
|
|
13
|
+
*/
|
|
14
|
+
abstract readonly destroyed: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Is the timer paused?
|
|
17
|
+
*/
|
|
18
|
+
abstract readonly paused: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Gets the traced location of the timer
|
|
21
|
+
*/
|
|
22
|
+
abstract readonly trace: TimerTrace | undefined;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A timer that can be started, stopped, and restarted as neeeded
|
|
26
|
+
*/
|
|
27
|
+
export declare class Timer extends BasicTimer<TimerState> {
|
|
28
|
+
private readonly options;
|
|
29
|
+
get active(): boolean;
|
|
30
|
+
get destroyed(): boolean;
|
|
31
|
+
get paused(): boolean;
|
|
32
|
+
get trace(): TimerTrace | undefined;
|
|
33
|
+
constructor(type: "repeat" | "wait", state: TimerState, options: TimerOptions);
|
|
34
|
+
/**
|
|
35
|
+
* Continues the timer _(if it was paused)_
|
|
36
|
+
*/
|
|
37
|
+
continue(): Timer;
|
|
38
|
+
/**
|
|
39
|
+
* Destroys the timer _(after stopping it, if it was running)_
|
|
40
|
+
*/
|
|
41
|
+
destroy(): void;
|
|
42
|
+
/**
|
|
43
|
+
* Pauses the timer _(if it was running)_
|
|
44
|
+
*/
|
|
45
|
+
pause(): Timer;
|
|
46
|
+
/**
|
|
47
|
+
* Restarts the timer _(if it was running)_
|
|
48
|
+
*/
|
|
49
|
+
restart(): Timer;
|
|
50
|
+
/**
|
|
51
|
+
* Starts the timer _(if it was stopped)_
|
|
52
|
+
*/
|
|
53
|
+
start(): Timer;
|
|
54
|
+
/**
|
|
55
|
+
* Stops the timer _(if it was running)_
|
|
56
|
+
*/
|
|
57
|
+
stop(): Timer;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates a timer which:
|
|
61
|
+
* - calls a callback after a certain amount of time...
|
|
62
|
+
* - ... and repeats it a certain amount of times
|
|
63
|
+
* ---
|
|
64
|
+
* - `options.count` defaults to `Infinity`
|
|
65
|
+
* - `options.interval` defaults to `1000/60` _(1 frame)_
|
|
66
|
+
* - `options.timeout` defaults to `Infinity`
|
|
67
|
+
*/
|
|
68
|
+
export declare function repeat(callback: IndexedCallback, options?: Partial<RepeatOptions>): Timer;
|
|
69
|
+
export declare function timer(type: "repeat" | "wait", callback: AnyCallback, partial: Partial<TimerOptions>, start: boolean): Timer;
|
|
70
|
+
/**
|
|
71
|
+
* Creates a timer which calls a callback after a certain amount of time
|
|
72
|
+
*/
|
|
73
|
+
export declare function wait(callback: () => void): Timer;
|
|
74
|
+
/**
|
|
75
|
+
* Creates a timer which calls a callback after a certain amount of time
|
|
76
|
+
*/
|
|
77
|
+
export declare function wait(callback: () => void, time: number): Timer;
|
|
78
|
+
/**
|
|
79
|
+
* Creates a timer which calls a callback after a certain amount of time
|
|
80
|
+
* - `options.interval` defaults to `1000/60` _(1 frame)_
|
|
81
|
+
* - `options.timeout` defaults to `30_000` _(30 seconds)_
|
|
82
|
+
*/
|
|
83
|
+
export declare function wait(callback: () => void, options: Partial<WaitOptions>): Timer;
|
|
84
|
+
/**
|
|
85
|
+
* Callback that runs after the timer has finished (or is stopped)
|
|
86
|
+
* - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
|
|
87
|
+
*/
|
|
88
|
+
export type AfterCallback = (finished: boolean) => void;
|
|
89
|
+
export type AnyCallback = (() => void) | IndexedCallback;
|
|
90
|
+
/**
|
|
91
|
+
* Callback that runs for each iteration of the timer
|
|
92
|
+
*/
|
|
93
|
+
export type IndexedCallback = (index: number) => void;
|
|
94
|
+
export type BaseOptions = {
|
|
95
|
+
/**
|
|
96
|
+
* Interval between each callback
|
|
97
|
+
*/
|
|
98
|
+
interval: number;
|
|
99
|
+
/**
|
|
100
|
+
* Maximum amount of time the timer may run for
|
|
101
|
+
*/
|
|
102
|
+
timeout: number;
|
|
103
|
+
};
|
|
104
|
+
export type OptionsWithCount = {
|
|
105
|
+
/**
|
|
106
|
+
* How many times the timer should repeat
|
|
107
|
+
*/
|
|
108
|
+
count: number;
|
|
109
|
+
} & BaseOptions;
|
|
110
|
+
export type OptionsWithError = {
|
|
111
|
+
/**
|
|
112
|
+
* Callback to run when an error occurs _(usually a timeout)_
|
|
113
|
+
*/
|
|
114
|
+
errorCallback?: () => void;
|
|
115
|
+
};
|
|
116
|
+
export type RepeatOptions = {
|
|
117
|
+
/**
|
|
118
|
+
* Callback to run after the timer has finished (or is stopped)
|
|
119
|
+
* - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
|
|
120
|
+
*/
|
|
121
|
+
afterCallback?: AfterCallback;
|
|
122
|
+
} & OptionsWithCount & OptionsWithError;
|
|
123
|
+
export type TimerOptions = {} & RepeatOptions;
|
|
124
|
+
export type TimerState = {
|
|
125
|
+
active: boolean;
|
|
126
|
+
callback: AnyCallback;
|
|
127
|
+
destroyed: boolean;
|
|
128
|
+
count?: number;
|
|
129
|
+
elapsed?: number;
|
|
130
|
+
frame?: number;
|
|
131
|
+
index?: number;
|
|
132
|
+
isRepeated: boolean;
|
|
133
|
+
minimum: number;
|
|
134
|
+
paused: boolean;
|
|
135
|
+
trace: TimerTrace;
|
|
136
|
+
};
|
|
137
|
+
declare class TimerTrace extends Error {
|
|
138
|
+
constructor();
|
|
139
|
+
}
|
|
140
|
+
export type WaitOptions = {} & BaseOptions & OptionsWithError;
|
|
141
|
+
|
|
142
|
+
export {};
|
package/types/timer.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TimerTrace, type AnyCallback, type IndexedCallback, type RepeatOptions, type TimerOptions, type TimerState, type WaitOptions } from '
|
|
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;
|
package/types/when.d.cts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
+
|
|
3
|
+
declare abstract class BasicTimer<State> {
|
|
4
|
+
protected readonly $timer: string;
|
|
5
|
+
protected readonly state: State;
|
|
6
|
+
constructor(type: "repeat" | "wait" | "when", state: State);
|
|
7
|
+
/**
|
|
8
|
+
* Is the timer running?
|
|
9
|
+
*/
|
|
10
|
+
abstract readonly active: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Is the timer destroyed?
|
|
13
|
+
*/
|
|
14
|
+
abstract readonly destroyed: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Is the timer paused?
|
|
17
|
+
*/
|
|
18
|
+
abstract readonly paused: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Gets the traced location of the timer
|
|
21
|
+
*/
|
|
22
|
+
abstract readonly trace: TimerTrace | undefined;
|
|
23
|
+
}
|
|
24
|
+
declare class Timer extends BasicTimer<TimerState> {
|
|
25
|
+
private readonly options;
|
|
26
|
+
get active(): boolean;
|
|
27
|
+
get destroyed(): boolean;
|
|
28
|
+
get paused(): boolean;
|
|
29
|
+
get trace(): TimerTrace | undefined;
|
|
30
|
+
constructor(type: "repeat" | "wait", state: TimerState, options: TimerOptions);
|
|
31
|
+
/**
|
|
32
|
+
* Continues the timer _(if it was paused)_
|
|
33
|
+
*/
|
|
34
|
+
continue(): Timer;
|
|
35
|
+
/**
|
|
36
|
+
* Destroys the timer _(after stopping it, if it was running)_
|
|
37
|
+
*/
|
|
38
|
+
destroy(): void;
|
|
39
|
+
/**
|
|
40
|
+
* Pauses the timer _(if it was running)_
|
|
41
|
+
*/
|
|
42
|
+
pause(): Timer;
|
|
43
|
+
/**
|
|
44
|
+
* Restarts the timer _(if it was running)_
|
|
45
|
+
*/
|
|
46
|
+
restart(): Timer;
|
|
47
|
+
/**
|
|
48
|
+
* Starts the timer _(if it was stopped)_
|
|
49
|
+
*/
|
|
50
|
+
start(): Timer;
|
|
51
|
+
/**
|
|
52
|
+
* Stops the timer _(if it was running)_
|
|
53
|
+
*/
|
|
54
|
+
stop(): Timer;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Callback that runs after the timer has finished (or is stopped)
|
|
58
|
+
* - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
|
|
59
|
+
*/
|
|
60
|
+
export type AfterCallback = (finished: boolean) => void;
|
|
61
|
+
export type AnyCallback = (() => void) | IndexedCallback;
|
|
62
|
+
/**
|
|
63
|
+
* Callback that runs for each iteration of the timer
|
|
64
|
+
*/
|
|
65
|
+
export type IndexedCallback = (index: number) => void;
|
|
66
|
+
export type BaseOptions = {
|
|
67
|
+
/**
|
|
68
|
+
* Interval between each callback
|
|
69
|
+
*/
|
|
70
|
+
interval: number;
|
|
71
|
+
/**
|
|
72
|
+
* Maximum amount of time the timer may run for
|
|
73
|
+
*/
|
|
74
|
+
timeout: number;
|
|
75
|
+
};
|
|
76
|
+
export type OptionsWithCount = {
|
|
77
|
+
/**
|
|
78
|
+
* How many times the timer should repeat
|
|
79
|
+
*/
|
|
80
|
+
count: number;
|
|
81
|
+
} & BaseOptions;
|
|
82
|
+
export type OptionsWithError = {
|
|
83
|
+
/**
|
|
84
|
+
* Callback to run when an error occurs _(usually a timeout)_
|
|
85
|
+
*/
|
|
86
|
+
errorCallback?: () => void;
|
|
87
|
+
};
|
|
88
|
+
export type RepeatOptions = {
|
|
89
|
+
/**
|
|
90
|
+
* Callback to run after the timer has finished (or is stopped)
|
|
91
|
+
* - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
|
|
92
|
+
*/
|
|
93
|
+
afterCallback?: AfterCallback;
|
|
94
|
+
} & OptionsWithCount & OptionsWithError;
|
|
95
|
+
export type TimerOptions = {} & RepeatOptions;
|
|
96
|
+
export type TimerState = {
|
|
97
|
+
active: boolean;
|
|
98
|
+
callback: AnyCallback;
|
|
99
|
+
destroyed: boolean;
|
|
100
|
+
count?: number;
|
|
101
|
+
elapsed?: number;
|
|
102
|
+
frame?: number;
|
|
103
|
+
index?: number;
|
|
104
|
+
isRepeated: boolean;
|
|
105
|
+
minimum: number;
|
|
106
|
+
paused: boolean;
|
|
107
|
+
trace: TimerTrace;
|
|
108
|
+
};
|
|
109
|
+
declare class TimerTrace extends Error {
|
|
110
|
+
constructor();
|
|
111
|
+
}
|
|
112
|
+
export type WhenOptions = {} & OptionsWithCount;
|
|
113
|
+
export type WhenState = {
|
|
114
|
+
promise: Promise<void>;
|
|
115
|
+
rejecter?: () => void;
|
|
116
|
+
resolver?: () => void;
|
|
117
|
+
started: boolean;
|
|
118
|
+
timer: Timer;
|
|
119
|
+
};
|
|
120
|
+
export declare class When extends BasicTimer<WhenState> {
|
|
121
|
+
get active(): boolean;
|
|
122
|
+
get destroyed(): boolean;
|
|
123
|
+
get paused(): boolean;
|
|
124
|
+
get trace(): TimerTrace | undefined;
|
|
125
|
+
constructor(state: WhenState);
|
|
126
|
+
/**
|
|
127
|
+
* Continues the timer _(if it was paused)_
|
|
128
|
+
*/
|
|
129
|
+
continue(): When;
|
|
130
|
+
/**
|
|
131
|
+
* Destroys the timer _(and stops it,if it was running)_
|
|
132
|
+
*/
|
|
133
|
+
destroy(): void;
|
|
134
|
+
/**
|
|
135
|
+
* Pauses the timer _(if it was running)_
|
|
136
|
+
*/
|
|
137
|
+
pause(): When;
|
|
138
|
+
/**
|
|
139
|
+
* Stops the timer _(if it was running)_
|
|
140
|
+
*/
|
|
141
|
+
stop(): When;
|
|
142
|
+
/**
|
|
143
|
+
* Starts the timer and returns a promise that resolves when the condition is met
|
|
144
|
+
*/
|
|
145
|
+
then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* - Creates a promise that resolves when a condition is met
|
|
149
|
+
* - If the condition is never met in a timely manner, the promise will reject
|
|
150
|
+
*/
|
|
151
|
+
export declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
|
|
152
|
+
|
|
153
|
+
export {};
|
package/types/when.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { WhenOptions, WhenState } from '
|
|
2
|
-
import { BasicTimer } from '
|
|
1
|
+
import type { WhenOptions, WhenState } from '~/models';
|
|
2
|
+
import { BasicTimer } from '~/timer';
|
|
3
3
|
export declare class When extends BasicTimer<WhenState> {
|
|
4
4
|
get active(): boolean;
|
|
5
5
|
get destroyed(): boolean;
|
|
6
6
|
get paused(): boolean;
|
|
7
|
-
get trace(): import("
|
|
7
|
+
get trace(): import("~/models").TimerTrace | undefined;
|
|
8
8
|
constructor(state: WhenState);
|
|
9
9
|
/**
|
|
10
10
|
* Continues the timer _(if it was paused)_
|
package/dist/constants.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// src/constants.ts
|
|
2
|
-
var activeTimers = new Set;
|
|
3
|
-
var beginTypes = new Set(["continue", "start"]);
|
|
4
|
-
var endTypes = new Set(["pause", "stop"]);
|
|
5
|
-
var endOrRestartTypes = new Set([
|
|
6
|
-
"pause",
|
|
7
|
-
"restart",
|
|
8
|
-
"stop"
|
|
9
|
-
]);
|
|
10
|
-
var hiddenTimers = new Set;
|
|
11
|
-
var milliseconds = 1000 / 60;
|
|
12
|
-
export {
|
|
13
|
-
milliseconds,
|
|
14
|
-
hiddenTimers,
|
|
15
|
-
endTypes,
|
|
16
|
-
endOrRestartTypes,
|
|
17
|
-
beginTypes,
|
|
18
|
-
activeTimers
|
|
19
|
-
};
|
package/dist/global.mjs
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// src/global.ts
|
|
2
|
-
import { activeTimers } from "./constants";
|
|
3
|
-
if (globalThis._oscarpalmer_timers == null) {
|
|
4
|
-
Object.defineProperty(globalThis, "_oscarpalmer_timers", {
|
|
5
|
-
get() {
|
|
6
|
-
return globalThis._oscarpalmer_timer_debug ? [...activeTimers] : [];
|
|
7
|
-
}
|
|
8
|
-
});
|
|
9
|
-
}
|
package/dist/index.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import { noop } from "@oscarpalmer/atoms/function";
|
|
3
|
-
import { activeTimers, hiddenTimers } from "./constants";
|
|
4
|
-
import"./global";
|
|
5
|
-
import { wait } from "./timer";
|
|
6
|
-
function delay(time, timeout) {
|
|
7
|
-
return new Promise((resolve, reject) => {
|
|
8
|
-
const delayed = wait(() => {
|
|
9
|
-
delayed.destroy();
|
|
10
|
-
(resolve ?? noop)();
|
|
11
|
-
}, {
|
|
12
|
-
timeout,
|
|
13
|
-
errorCallback: () => {
|
|
14
|
-
delayed.destroy();
|
|
15
|
-
(reject ?? noop)();
|
|
16
|
-
},
|
|
17
|
-
interval: time
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
import { isRepeated, isTimer, isWaited, isWhen } from "./is";
|
|
22
|
-
import { repeat, wait as wait2 } from "./timer";
|
|
23
|
-
import { when } from "./when";
|
|
24
|
-
document.addEventListener("visibilitychange", () => {
|
|
25
|
-
if (document.hidden) {
|
|
26
|
-
for (const timer of activeTimers) {
|
|
27
|
-
hiddenTimers.add(timer);
|
|
28
|
-
timer.pause();
|
|
29
|
-
}
|
|
30
|
-
} else {
|
|
31
|
-
for (const timer of hiddenTimers) {
|
|
32
|
-
timer.continue();
|
|
33
|
-
}
|
|
34
|
-
hiddenTimers.clear();
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
export {
|
|
38
|
-
when,
|
|
39
|
-
wait2 as wait,
|
|
40
|
-
repeat,
|
|
41
|
-
isWhen,
|
|
42
|
-
isWaited,
|
|
43
|
-
isTimer,
|
|
44
|
-
isRepeated,
|
|
45
|
-
delay
|
|
46
|
-
};
|
package/dist/timer.mjs
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
// src/timer.ts
|
|
2
|
-
import { milliseconds } from "./constants";
|
|
3
|
-
import { getOptions, work } from "./functions";
|
|
4
|
-
import {
|
|
5
|
-
TimerTrace
|
|
6
|
-
} from "./models";
|
|
7
|
-
function repeat(callback, options) {
|
|
8
|
-
return timer("repeat", callback, options ?? {}, true);
|
|
9
|
-
}
|
|
10
|
-
function timer(type, callback, partial, start) {
|
|
11
|
-
const isRepeated = type === "repeat";
|
|
12
|
-
const options = getOptions(partial, isRepeated);
|
|
13
|
-
const instance = new Timer(type, {
|
|
14
|
-
callback,
|
|
15
|
-
isRepeated,
|
|
16
|
-
active: false,
|
|
17
|
-
destroyed: false,
|
|
18
|
-
minimum: options.interval - options.interval % milliseconds / 2,
|
|
19
|
-
paused: false,
|
|
20
|
-
trace: new TimerTrace
|
|
21
|
-
}, options);
|
|
22
|
-
if (start) {
|
|
23
|
-
instance.start();
|
|
24
|
-
}
|
|
25
|
-
return instance;
|
|
26
|
-
}
|
|
27
|
-
function wait(callback, options) {
|
|
28
|
-
return timer("wait", callback, options == null || typeof options === "number" ? {
|
|
29
|
-
interval: options
|
|
30
|
-
} : options, true);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
class BasicTimer {
|
|
34
|
-
constructor(type, state) {
|
|
35
|
-
this.$timer = type;
|
|
36
|
-
this.state = state;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
class Timer extends BasicTimer {
|
|
41
|
-
get active() {
|
|
42
|
-
return this.state.active;
|
|
43
|
-
}
|
|
44
|
-
get destroyed() {
|
|
45
|
-
return this.state.destroyed;
|
|
46
|
-
}
|
|
47
|
-
get paused() {
|
|
48
|
-
return this.state.paused;
|
|
49
|
-
}
|
|
50
|
-
get trace() {
|
|
51
|
-
return globalThis._oscarpalmer_timer_debug ? this.state.trace : undefined;
|
|
52
|
-
}
|
|
53
|
-
constructor(type, state, options) {
|
|
54
|
-
super(type, state);
|
|
55
|
-
this.options = options;
|
|
56
|
-
}
|
|
57
|
-
continue() {
|
|
58
|
-
return work("continue", this, this.state, this.options);
|
|
59
|
-
}
|
|
60
|
-
destroy() {
|
|
61
|
-
if (!this.state.destroyed) {
|
|
62
|
-
this.state.destroyed = true;
|
|
63
|
-
this.stop();
|
|
64
|
-
this.options.afterCallback = undefined;
|
|
65
|
-
this.options.errorCallback = undefined;
|
|
66
|
-
this.state.callback = undefined;
|
|
67
|
-
this.state.trace = undefined;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
pause() {
|
|
71
|
-
return work("pause", this, this.state, this.options);
|
|
72
|
-
}
|
|
73
|
-
restart() {
|
|
74
|
-
return work("restart", this, this.state, this.options);
|
|
75
|
-
}
|
|
76
|
-
start() {
|
|
77
|
-
return work("start", this, this.state, this.options);
|
|
78
|
-
}
|
|
79
|
-
stop() {
|
|
80
|
-
return work("stop", this, this.state, this.options);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
export {
|
|
84
|
-
wait,
|
|
85
|
-
timer,
|
|
86
|
-
repeat,
|
|
87
|
-
Timer,
|
|
88
|
-
BasicTimer
|
|
89
|
-
};
|
package/dist/when.mjs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
// src/when.ts
|
|
2
|
-
import { noop } from "@oscarpalmer/atoms/function";
|
|
3
|
-
import { BasicTimer, timer } from "./timer";
|
|
4
|
-
function when(condition, options) {
|
|
5
|
-
let result = false;
|
|
6
|
-
const state = {
|
|
7
|
-
started: false,
|
|
8
|
-
timer: timer("repeat", () => {
|
|
9
|
-
if (condition()) {
|
|
10
|
-
result = true;
|
|
11
|
-
state.timer.stop();
|
|
12
|
-
}
|
|
13
|
-
}, {
|
|
14
|
-
afterCallback() {
|
|
15
|
-
if (!state.timer.paused) {
|
|
16
|
-
if (result) {
|
|
17
|
-
state.resolver?.();
|
|
18
|
-
} else {
|
|
19
|
-
state.rejecter?.();
|
|
20
|
-
}
|
|
21
|
-
instance.destroy();
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
errorCallback() {
|
|
25
|
-
state.rejecter?.();
|
|
26
|
-
instance.destroy();
|
|
27
|
-
},
|
|
28
|
-
count: options?.count,
|
|
29
|
-
interval: options?.interval,
|
|
30
|
-
timeout: options?.timeout
|
|
31
|
-
}, false)
|
|
32
|
-
};
|
|
33
|
-
const promise = new Promise((resolve, reject) => {
|
|
34
|
-
state.resolver = resolve;
|
|
35
|
-
state.rejecter = reject;
|
|
36
|
-
});
|
|
37
|
-
state.promise = promise;
|
|
38
|
-
const instance = new When(state);
|
|
39
|
-
return instance;
|
|
40
|
-
}
|
|
41
|
-
var destroyedMessage = "Timer has already been destroyed";
|
|
42
|
-
var startedMessage = "Timer has already been started";
|
|
43
|
-
|
|
44
|
-
class When extends BasicTimer {
|
|
45
|
-
get active() {
|
|
46
|
-
return this.state.timer?.active ?? false;
|
|
47
|
-
}
|
|
48
|
-
get destroyed() {
|
|
49
|
-
return this.state.timer == null;
|
|
50
|
-
}
|
|
51
|
-
get paused() {
|
|
52
|
-
return this.state.timer?.paused ?? false;
|
|
53
|
-
}
|
|
54
|
-
get trace() {
|
|
55
|
-
return this.state.timer?.trace;
|
|
56
|
-
}
|
|
57
|
-
constructor(state) {
|
|
58
|
-
super("when", state);
|
|
59
|
-
}
|
|
60
|
-
continue() {
|
|
61
|
-
this.state.timer?.continue();
|
|
62
|
-
return this;
|
|
63
|
-
}
|
|
64
|
-
destroy() {
|
|
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;
|
|
70
|
-
}
|
|
71
|
-
pause() {
|
|
72
|
-
this.state.timer?.pause();
|
|
73
|
-
return this;
|
|
74
|
-
}
|
|
75
|
-
stop() {
|
|
76
|
-
this.state.timer?.stop();
|
|
77
|
-
return this;
|
|
78
|
-
}
|
|
79
|
-
then(resolve, reject) {
|
|
80
|
-
if (this.state.timer == null || this.state?.started) {
|
|
81
|
-
throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
|
|
82
|
-
}
|
|
83
|
-
this.state.started = true;
|
|
84
|
-
this.state.timer.start();
|
|
85
|
-
return this.state.promise.then(resolve ?? noop, reject ?? noop);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
export {
|
|
89
|
-
when,
|
|
90
|
-
When
|
|
91
|
-
};
|