@oscarpalmer/timer 0.28.0 → 0.30.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 +32 -4
- package/dist/constants.js +26 -9
- package/dist/delay.cjs +24 -0
- package/dist/delay.js +20 -0
- package/dist/get.cjs +15 -0
- package/dist/get.js +10 -0
- package/dist/global.cjs +17 -2
- package/dist/global.js +15 -1
- package/dist/index.cjs +16 -44
- package/dist/index.js +6 -50
- package/dist/is.cjs +11 -8
- package/dist/is.js +8 -12
- package/dist/models.cjs +5 -2
- package/dist/models.js +2 -3
- package/dist/node_modules/@oscarpalmer/atoms/dist/function.cjs +8 -0
- package/dist/node_modules/@oscarpalmer/atoms/dist/function.js +4 -0
- package/dist/repeat.cjs +30 -0
- package/dist/repeat.js +26 -0
- package/dist/timer.cjs +59 -71
- package/dist/timer.full.js +472 -0
- package/dist/timer.js +56 -72
- package/dist/wait.cjs +30 -0
- package/dist/wait.js +26 -0
- package/dist/when.cjs +59 -44
- package/dist/when.js +55 -44
- package/dist/work.cjs +72 -0
- package/dist/work.js +68 -0
- package/package.json +49 -9
- package/src/constants.ts +51 -6
- package/src/delay.ts +25 -0
- package/src/get.ts +12 -0
- package/src/global.ts +16 -1
- package/src/index.ts +5 -48
- package/src/is.ts +6 -6
- package/src/models.ts +55 -47
- package/src/repeat.ts +32 -0
- package/src/timer.ts +67 -151
- package/src/wait.ts +31 -0
- package/src/when.ts +49 -24
- package/src/work.ts +129 -0
- package/types/constants.d.cts +49 -74
- package/types/constants.d.ts +15 -6
- package/types/delay.d.cts +8 -0
- package/types/delay.d.ts +4 -0
- package/types/get.d.cts +7 -0
- package/types/get.d.ts +3 -0
- package/types/index.d.cts +89 -94
- package/types/index.d.ts +5 -6
- package/types/is.d.cts +50 -69
- package/types/models.d.cts +61 -63
- package/types/models.d.ts +43 -40
- package/types/repeat.d.cts +98 -0
- package/types/repeat.d.ts +7 -0
- package/types/timer.d.cts +35 -97
- package/types/timer.d.ts +19 -52
- package/types/wait.d.cts +83 -0
- package/types/wait.d.ts +8 -0
- package/types/when.d.cts +65 -69
- package/types/when.d.ts +18 -5
- package/types/work.d.cts +78 -0
- package/types/work.d.ts +3 -0
- package/dist/functions.cjs +0 -99
- package/dist/functions.js +0 -99
- package/dist/timer.iife.js +0 -431
- package/src/functions.ts +0 -158
- package/types/functions.d.cts +0 -113
- package/types/functions.d.ts +0 -4
package/types/when.d.cts
CHANGED
|
@@ -1,112 +1,89 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
|
-
declare
|
|
4
|
-
|
|
5
|
-
protected readonly
|
|
6
|
-
|
|
3
|
+
declare class Timer {
|
|
4
|
+
#private;
|
|
5
|
+
protected readonly worker: WorkHandler;
|
|
6
|
+
protected readonly options: TimerOptions;
|
|
7
|
+
private readonly $timer;
|
|
8
|
+
protected readonly state: TimerState;
|
|
7
9
|
/**
|
|
8
|
-
* Is the timer
|
|
10
|
+
* Is the timer active?
|
|
9
11
|
*/
|
|
10
|
-
|
|
12
|
+
get active(): boolean;
|
|
11
13
|
/**
|
|
12
14
|
* Is the timer destroyed?
|
|
13
15
|
*/
|
|
14
|
-
|
|
16
|
+
get destroyed(): boolean;
|
|
15
17
|
/**
|
|
16
18
|
* Is the timer paused?
|
|
17
19
|
*/
|
|
18
|
-
|
|
20
|
+
get paused(): boolean;
|
|
19
21
|
/**
|
|
20
|
-
*
|
|
22
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
21
23
|
*/
|
|
22
|
-
abstract readonly trace: string | 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
24
|
get trace(): string | undefined;
|
|
30
|
-
constructor(type: "
|
|
25
|
+
constructor(type: TimerType, worker: WorkHandler, state: Pick<TimerState, "callback" | "trace">, options: TimerOptions, start: boolean);
|
|
31
26
|
/**
|
|
32
|
-
*
|
|
27
|
+
* Continue running the timer _(if it's paused)_
|
|
33
28
|
*/
|
|
34
29
|
continue(): Timer;
|
|
35
30
|
/**
|
|
36
|
-
*
|
|
31
|
+
* Destroy the timer
|
|
37
32
|
*/
|
|
38
33
|
destroy(): void;
|
|
39
34
|
/**
|
|
40
|
-
*
|
|
35
|
+
* Pause the timer _(if it's running)_
|
|
41
36
|
*/
|
|
42
37
|
pause(): Timer;
|
|
43
38
|
/**
|
|
44
|
-
*
|
|
39
|
+
* Restart the timer _(or start it, if it's not running)_
|
|
45
40
|
*/
|
|
46
41
|
restart(): Timer;
|
|
47
42
|
/**
|
|
48
|
-
*
|
|
43
|
+
* Start the timer _(if it's not running)_
|
|
49
44
|
*/
|
|
50
45
|
start(): Timer;
|
|
51
46
|
/**
|
|
52
|
-
*
|
|
47
|
+
* Stop the timer _(if it's running)_
|
|
53
48
|
*/
|
|
54
49
|
stop(): Timer;
|
|
55
50
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
*/
|
|
51
|
+
export type TimerOptions = {
|
|
52
|
+
onAfter: ((finished: boolean) => void) | undefined;
|
|
53
|
+
onError: (() => void) | undefined;
|
|
54
|
+
count: number;
|
|
70
55
|
interval: number;
|
|
71
|
-
/**
|
|
72
|
-
* Maximum amount of time the timer may run for
|
|
73
|
-
*/
|
|
74
56
|
timeout: number;
|
|
75
57
|
};
|
|
76
|
-
export type
|
|
58
|
+
export type TimerState = {
|
|
59
|
+
active: boolean;
|
|
60
|
+
callback: () => void;
|
|
61
|
+
destroyed: boolean;
|
|
62
|
+
elapsed: number;
|
|
63
|
+
frame: number | undefined;
|
|
64
|
+
index: number;
|
|
65
|
+
paused: boolean;
|
|
66
|
+
total: number;
|
|
67
|
+
trace: string | undefined;
|
|
68
|
+
};
|
|
69
|
+
export type TimerType = "repeat" | "wait" | "when";
|
|
70
|
+
/**
|
|
71
|
+
* Options for a conditional timer
|
|
72
|
+
*/
|
|
73
|
+
export type WhenOptions = {
|
|
77
74
|
/**
|
|
78
|
-
* How many times the timer should
|
|
75
|
+
* How many times the timer should check the condition
|
|
79
76
|
*/
|
|
80
77
|
count: number;
|
|
81
|
-
} & BaseOptions;
|
|
82
|
-
export type OptionsWithError = {
|
|
83
78
|
/**
|
|
84
|
-
*
|
|
79
|
+
* Then interval between each condtional check
|
|
85
80
|
*/
|
|
86
|
-
|
|
87
|
-
};
|
|
88
|
-
export type RepeatOptions = {
|
|
81
|
+
interval: number;
|
|
89
82
|
/**
|
|
90
|
-
*
|
|
91
|
-
* - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
|
|
83
|
+
* The timeout for the timer
|
|
92
84
|
*/
|
|
93
|
-
|
|
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?: string;
|
|
85
|
+
timeout: number;
|
|
108
86
|
};
|
|
109
|
-
export type WhenOptions = {} & OptionsWithCount;
|
|
110
87
|
export type WhenState = {
|
|
111
88
|
promise: Promise<void>;
|
|
112
89
|
rejecter?: () => void;
|
|
@@ -114,10 +91,30 @@ export type WhenState = {
|
|
|
114
91
|
started: boolean;
|
|
115
92
|
timer: Timer;
|
|
116
93
|
};
|
|
117
|
-
export
|
|
94
|
+
export type WorkHandler = (type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions) => Timer;
|
|
95
|
+
export type WorkHandlerTimer = {
|
|
96
|
+
instance: Timer;
|
|
97
|
+
type: TimerType;
|
|
98
|
+
};
|
|
99
|
+
export type WorkHandlerType = "continue" | "pause" | "restart" | "start" | "stop";
|
|
100
|
+
export declare class When {
|
|
101
|
+
private readonly $timer;
|
|
102
|
+
private readonly state;
|
|
103
|
+
/**
|
|
104
|
+
* Is the timer active?
|
|
105
|
+
*/
|
|
118
106
|
get active(): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Is the timer destroyed?
|
|
109
|
+
*/
|
|
119
110
|
get destroyed(): boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Is the timer paused?
|
|
113
|
+
*/
|
|
120
114
|
get paused(): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
117
|
+
*/
|
|
121
118
|
get trace(): string | undefined;
|
|
122
119
|
constructor(state: WhenState);
|
|
123
120
|
/**
|
|
@@ -142,8 +139,7 @@ export declare class When extends BasicTimer<WhenState> {
|
|
|
142
139
|
then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
|
|
143
140
|
}
|
|
144
141
|
/**
|
|
145
|
-
*
|
|
146
|
-
* - If the condition is never met in a timely manner, the promise will reject
|
|
142
|
+
* Create a conditional timer
|
|
147
143
|
*/
|
|
148
144
|
export declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
|
|
149
145
|
|
package/types/when.d.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { type WhenOptions, type WhenState } from './models';
|
|
2
|
+
declare class When {
|
|
3
|
+
private readonly $timer;
|
|
4
|
+
private readonly state;
|
|
5
|
+
/**
|
|
6
|
+
* Is the timer active?
|
|
7
|
+
*/
|
|
4
8
|
get active(): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Is the timer destroyed?
|
|
11
|
+
*/
|
|
5
12
|
get destroyed(): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Is the timer paused?
|
|
15
|
+
*/
|
|
6
16
|
get paused(): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
19
|
+
*/
|
|
7
20
|
get trace(): string | undefined;
|
|
8
21
|
constructor(state: WhenState);
|
|
9
22
|
/**
|
|
@@ -28,7 +41,7 @@ export declare class When extends BasicTimer<WhenState> {
|
|
|
28
41
|
then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
|
|
29
42
|
}
|
|
30
43
|
/**
|
|
31
|
-
*
|
|
32
|
-
* - If the condition is never met in a timely manner, the promise will reject
|
|
44
|
+
* Create a conditional timer
|
|
33
45
|
*/
|
|
34
46
|
export declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
|
|
47
|
+
export type { When };
|
package/types/work.d.cts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
+
|
|
3
|
+
declare class Timer {
|
|
4
|
+
#private;
|
|
5
|
+
protected readonly worker: WorkHandler;
|
|
6
|
+
protected readonly options: TimerOptions;
|
|
7
|
+
private readonly $timer;
|
|
8
|
+
protected readonly state: TimerState;
|
|
9
|
+
/**
|
|
10
|
+
* Is the timer active?
|
|
11
|
+
*/
|
|
12
|
+
get active(): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Is the timer destroyed?
|
|
15
|
+
*/
|
|
16
|
+
get destroyed(): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Is the timer paused?
|
|
19
|
+
*/
|
|
20
|
+
get paused(): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
23
|
+
*/
|
|
24
|
+
get trace(): string | undefined;
|
|
25
|
+
constructor(type: TimerType, worker: WorkHandler, state: Pick<TimerState, "callback" | "trace">, options: TimerOptions, start: boolean);
|
|
26
|
+
/**
|
|
27
|
+
* Continue running the timer _(if it's paused)_
|
|
28
|
+
*/
|
|
29
|
+
continue(): Timer;
|
|
30
|
+
/**
|
|
31
|
+
* Destroy the timer
|
|
32
|
+
*/
|
|
33
|
+
destroy(): void;
|
|
34
|
+
/**
|
|
35
|
+
* Pause the timer _(if it's running)_
|
|
36
|
+
*/
|
|
37
|
+
pause(): Timer;
|
|
38
|
+
/**
|
|
39
|
+
* Restart the timer _(or start it, if it's not running)_
|
|
40
|
+
*/
|
|
41
|
+
restart(): Timer;
|
|
42
|
+
/**
|
|
43
|
+
* Start the timer _(if it's not running)_
|
|
44
|
+
*/
|
|
45
|
+
start(): Timer;
|
|
46
|
+
/**
|
|
47
|
+
* Stop the timer _(if it's running)_
|
|
48
|
+
*/
|
|
49
|
+
stop(): Timer;
|
|
50
|
+
}
|
|
51
|
+
export type TimerOptions = {
|
|
52
|
+
onAfter: ((finished: boolean) => void) | undefined;
|
|
53
|
+
onError: (() => void) | undefined;
|
|
54
|
+
count: number;
|
|
55
|
+
interval: number;
|
|
56
|
+
timeout: number;
|
|
57
|
+
};
|
|
58
|
+
export type TimerState = {
|
|
59
|
+
active: boolean;
|
|
60
|
+
callback: () => void;
|
|
61
|
+
destroyed: boolean;
|
|
62
|
+
elapsed: number;
|
|
63
|
+
frame: number | undefined;
|
|
64
|
+
index: number;
|
|
65
|
+
paused: boolean;
|
|
66
|
+
total: number;
|
|
67
|
+
trace: string | undefined;
|
|
68
|
+
};
|
|
69
|
+
export type TimerType = "repeat" | "wait" | "when";
|
|
70
|
+
export type WorkHandler = (type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions) => Timer;
|
|
71
|
+
export type WorkHandlerTimer = {
|
|
72
|
+
instance: Timer;
|
|
73
|
+
type: TimerType;
|
|
74
|
+
};
|
|
75
|
+
export type WorkHandlerType = "continue" | "pause" | "restart" | "start" | "stop";
|
|
76
|
+
export declare function work(type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
|
|
77
|
+
|
|
78
|
+
export {};
|
package/types/work.d.ts
ADDED
package/dist/functions.cjs
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const constants = require("./constants.cjs");
|
|
4
|
-
function getOptions(options, isRepeated) {
|
|
5
|
-
return {
|
|
6
|
-
afterCallback: options.afterCallback,
|
|
7
|
-
count: getNumberOrDefault(
|
|
8
|
-
options.count,
|
|
9
|
-
isRepeated ? Number.POSITIVE_INFINITY : 1
|
|
10
|
-
),
|
|
11
|
-
errorCallback: options.errorCallback,
|
|
12
|
-
interval: getNumberOrDefault(options.interval, constants.milliseconds, constants.milliseconds),
|
|
13
|
-
timeout: getNumberOrDefault(
|
|
14
|
-
options.timeout,
|
|
15
|
-
isRepeated ? Number.POSITIVE_INFINITY : 3e4
|
|
16
|
-
)
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
20
|
-
return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
|
|
21
|
-
}
|
|
22
|
-
function work(type, timer, state, options) {
|
|
23
|
-
var _a;
|
|
24
|
-
if (state.destroyed && type !== "stop" || constants.beginTypes.has(type) && state.active || constants.endTypes.has(type) && !state.active) {
|
|
25
|
-
return timer;
|
|
26
|
-
}
|
|
27
|
-
const { count, interval, timeout } = options;
|
|
28
|
-
const { isRepeated, minimum } = state;
|
|
29
|
-
if (constants.endOrRestartTypes.has(type)) {
|
|
30
|
-
const isStop = type === "stop";
|
|
31
|
-
constants.activeTimers.delete(timer);
|
|
32
|
-
cancelAnimationFrame(state.frame);
|
|
33
|
-
if (isStop) {
|
|
34
|
-
(_a = options.afterCallback) == null ? void 0 : _a.call(options, false);
|
|
35
|
-
}
|
|
36
|
-
state.active = false;
|
|
37
|
-
state.frame = void 0;
|
|
38
|
-
state.paused = !isStop;
|
|
39
|
-
if (isStop) {
|
|
40
|
-
state.elapsed = void 0;
|
|
41
|
-
state.index = void 0;
|
|
42
|
-
}
|
|
43
|
-
return type === "restart" ? work("start", timer, state, options) : timer;
|
|
44
|
-
}
|
|
45
|
-
state.active = true;
|
|
46
|
-
state.paused = false;
|
|
47
|
-
const elapsed = type === "continue" ? Number(state.elapsed ?? 0) : 0;
|
|
48
|
-
let index = type === "continue" ? Number(state.index ?? 0) : 0;
|
|
49
|
-
state.elapsed = elapsed;
|
|
50
|
-
state.index = index;
|
|
51
|
-
const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : constants.milliseconds)) - elapsed;
|
|
52
|
-
let current;
|
|
53
|
-
let start;
|
|
54
|
-
function finish(finished, error) {
|
|
55
|
-
var _a2, _b;
|
|
56
|
-
constants.activeTimers.delete(timer);
|
|
57
|
-
state.active = false;
|
|
58
|
-
state.elapsed = void 0;
|
|
59
|
-
state.frame = void 0;
|
|
60
|
-
state.index = void 0;
|
|
61
|
-
if (error) {
|
|
62
|
-
(_a2 = options.errorCallback) == null ? void 0 : _a2.call(options);
|
|
63
|
-
}
|
|
64
|
-
(_b = options.afterCallback) == null ? void 0 : _b.call(options, finished);
|
|
65
|
-
}
|
|
66
|
-
function step(timestamp) {
|
|
67
|
-
if (!state.active) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
current ?? (current = timestamp);
|
|
71
|
-
start ?? (start = timestamp);
|
|
72
|
-
const time = timestamp - current;
|
|
73
|
-
state.elapsed = elapsed + (current - start);
|
|
74
|
-
const finished = time - elapsed >= total;
|
|
75
|
-
if (timestamp - start >= timeout - elapsed) {
|
|
76
|
-
finish(finished, !finished);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
if (finished || time >= minimum) {
|
|
80
|
-
if (state.active) {
|
|
81
|
-
state.callback(isRepeated ? index : void 0);
|
|
82
|
-
}
|
|
83
|
-
index += 1;
|
|
84
|
-
state.index = index;
|
|
85
|
-
if (!finished && index < count) {
|
|
86
|
-
current = null;
|
|
87
|
-
} else {
|
|
88
|
-
finish(true, false);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
state.frame = requestAnimationFrame(step);
|
|
93
|
-
}
|
|
94
|
-
constants.activeTimers.add(timer);
|
|
95
|
-
state.frame = requestAnimationFrame(step);
|
|
96
|
-
return timer;
|
|
97
|
-
}
|
|
98
|
-
exports.getOptions = getOptions;
|
|
99
|
-
exports.work = work;
|
package/dist/functions.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { beginTypes, endTypes, endOrRestartTypes, activeTimers, milliseconds } from "./constants.js";
|
|
2
|
-
function getOptions(options, isRepeated) {
|
|
3
|
-
return {
|
|
4
|
-
afterCallback: options.afterCallback,
|
|
5
|
-
count: getNumberOrDefault(
|
|
6
|
-
options.count,
|
|
7
|
-
isRepeated ? Number.POSITIVE_INFINITY : 1
|
|
8
|
-
),
|
|
9
|
-
errorCallback: options.errorCallback,
|
|
10
|
-
interval: getNumberOrDefault(options.interval, milliseconds, milliseconds),
|
|
11
|
-
timeout: getNumberOrDefault(
|
|
12
|
-
options.timeout,
|
|
13
|
-
isRepeated ? Number.POSITIVE_INFINITY : 3e4
|
|
14
|
-
)
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
function getNumberOrDefault(value, defaultValue, minimum) {
|
|
18
|
-
return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
|
|
19
|
-
}
|
|
20
|
-
function work(type, timer, state, options) {
|
|
21
|
-
var _a;
|
|
22
|
-
if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
|
|
23
|
-
return timer;
|
|
24
|
-
}
|
|
25
|
-
const { count, interval, timeout } = options;
|
|
26
|
-
const { isRepeated, minimum } = state;
|
|
27
|
-
if (endOrRestartTypes.has(type)) {
|
|
28
|
-
const isStop = type === "stop";
|
|
29
|
-
activeTimers.delete(timer);
|
|
30
|
-
cancelAnimationFrame(state.frame);
|
|
31
|
-
if (isStop) {
|
|
32
|
-
(_a = options.afterCallback) == null ? void 0 : _a.call(options, false);
|
|
33
|
-
}
|
|
34
|
-
state.active = false;
|
|
35
|
-
state.frame = void 0;
|
|
36
|
-
state.paused = !isStop;
|
|
37
|
-
if (isStop) {
|
|
38
|
-
state.elapsed = void 0;
|
|
39
|
-
state.index = void 0;
|
|
40
|
-
}
|
|
41
|
-
return type === "restart" ? work("start", timer, state, options) : timer;
|
|
42
|
-
}
|
|
43
|
-
state.active = true;
|
|
44
|
-
state.paused = false;
|
|
45
|
-
const elapsed = type === "continue" ? Number(state.elapsed ?? 0) : 0;
|
|
46
|
-
let index = type === "continue" ? Number(state.index ?? 0) : 0;
|
|
47
|
-
state.elapsed = elapsed;
|
|
48
|
-
state.index = index;
|
|
49
|
-
const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
|
|
50
|
-
let current;
|
|
51
|
-
let start;
|
|
52
|
-
function finish(finished, error) {
|
|
53
|
-
var _a2, _b;
|
|
54
|
-
activeTimers.delete(timer);
|
|
55
|
-
state.active = false;
|
|
56
|
-
state.elapsed = void 0;
|
|
57
|
-
state.frame = void 0;
|
|
58
|
-
state.index = void 0;
|
|
59
|
-
if (error) {
|
|
60
|
-
(_a2 = options.errorCallback) == null ? void 0 : _a2.call(options);
|
|
61
|
-
}
|
|
62
|
-
(_b = options.afterCallback) == null ? void 0 : _b.call(options, finished);
|
|
63
|
-
}
|
|
64
|
-
function step(timestamp) {
|
|
65
|
-
if (!state.active) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
current ?? (current = timestamp);
|
|
69
|
-
start ?? (start = timestamp);
|
|
70
|
-
const time = timestamp - current;
|
|
71
|
-
state.elapsed = elapsed + (current - start);
|
|
72
|
-
const finished = time - elapsed >= total;
|
|
73
|
-
if (timestamp - start >= timeout - elapsed) {
|
|
74
|
-
finish(finished, !finished);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
if (finished || time >= minimum) {
|
|
78
|
-
if (state.active) {
|
|
79
|
-
state.callback(isRepeated ? index : void 0);
|
|
80
|
-
}
|
|
81
|
-
index += 1;
|
|
82
|
-
state.index = index;
|
|
83
|
-
if (!finished && index < count) {
|
|
84
|
-
current = null;
|
|
85
|
-
} else {
|
|
86
|
-
finish(true, false);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
state.frame = requestAnimationFrame(step);
|
|
91
|
-
}
|
|
92
|
-
activeTimers.add(timer);
|
|
93
|
-
state.frame = requestAnimationFrame(step);
|
|
94
|
-
return timer;
|
|
95
|
-
}
|
|
96
|
-
export {
|
|
97
|
-
getOptions,
|
|
98
|
-
work
|
|
99
|
-
};
|