@oscarpalmer/timer 0.19.0 → 0.21.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.
@@ -0,0 +1,72 @@
1
+ import type { AnyCallback, IndexedCallback, RepeatOptions, TimerOptions, TimerState, WaitOptions } from './models';
2
+ export declare abstract class BasicTimer<State> {
3
+ protected readonly $timer: string;
4
+ protected readonly state: State;
5
+ constructor(type: 'repeat' | 'wait' | 'when', state: State);
6
+ /**
7
+ * Is the timer running?
8
+ */
9
+ abstract readonly active: boolean;
10
+ /**
11
+ * Is the timer paused?
12
+ */
13
+ abstract readonly paused: boolean;
14
+ }
15
+ /**
16
+ * A timer that can be started, stopped, and restarted as neeeded
17
+ */
18
+ export declare class Timer extends BasicTimer<TimerState> {
19
+ private readonly options;
20
+ get active(): boolean;
21
+ get paused(): boolean;
22
+ /**
23
+ * Gets the traced location of the timer
24
+ */
25
+ get trace(): unknown;
26
+ constructor(type: 'repeat' | 'wait', state: TimerState, options: TimerOptions);
27
+ /**
28
+ * Continues the timer _(if it was paused)_
29
+ */
30
+ continue(): Timer;
31
+ /**
32
+ * Pauses the timer _(if it was running)_
33
+ */
34
+ pause(): Timer;
35
+ /**
36
+ * Restarts the timer _(if it was running)_
37
+ */
38
+ restart(): Timer;
39
+ /**
40
+ * Starts the timer _(if it was stopped)_
41
+ */
42
+ start(): Timer;
43
+ /**
44
+ * Stops the timer _(if it was running)_
45
+ */
46
+ stop(): Timer;
47
+ }
48
+ /**
49
+ * Creates a timer which:
50
+ * - calls a callback after a certain amount of time...
51
+ * - ... and repeats it a certain amount of times
52
+ * ---
53
+ * - `options.count` defaults to `Infinity`
54
+ * - `options.interval` defaults to `1000/60` _(1 frame)_
55
+ * - `options.timeout` defaults to `Infinity`
56
+ */
57
+ export declare function repeat(callback: IndexedCallback, options?: Partial<RepeatOptions>): Timer;
58
+ export declare function timer(type: 'repeat' | 'wait', callback: AnyCallback, partial: Partial<TimerOptions>, start: boolean): Timer;
59
+ /**
60
+ * Creates a timer which calls a callback after a certain amount of time
61
+ */
62
+ export declare function wait(callback: () => void): Timer;
63
+ /**
64
+ * Creates a timer which calls a callback after a certain amount of time
65
+ */
66
+ export declare function wait(callback: () => void, time: number): Timer;
67
+ /**
68
+ * Creates a timer which calls a callback after a certain amount of time
69
+ * - `options.interval` defaults to `1000/60` _(1 frame)_
70
+ * - `options.timeout` defaults to `30_000` _(30 seconds)_
71
+ */
72
+ export declare function wait(callback: () => void, options: Partial<WaitOptions>): Timer;
@@ -0,0 +1,28 @@
1
+ import type { WhenOptions, WhenState } from './models';
2
+ import { BasicTimer } from './timer';
3
+ export declare class When extends BasicTimer<WhenState> {
4
+ get active(): boolean;
5
+ get paused(): boolean;
6
+ constructor(state: WhenState);
7
+ /**
8
+ * Continues the timer _(if it was paused)_
9
+ */
10
+ continue(): When;
11
+ /**
12
+ * Pauses the timer _(if it was running)_
13
+ */
14
+ pause(): When;
15
+ /**
16
+ * Stops the timer _(if it was running)_
17
+ */
18
+ stop(): When;
19
+ /**
20
+ * Starts the timer and returns a promise that resolves when the condition is met
21
+ */
22
+ then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
23
+ }
24
+ /**
25
+ * - Creates a promise that resolves when a condition is met
26
+ * - If the condition is never met in a timely manner, the promise will reject
27
+ */
28
+ export declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
@@ -1,162 +0,0 @@
1
- var Timer = (function (exports) {
2
- 'use strict';
3
-
4
- const callbacks = new WeakMap();
5
- const configuration = new WeakMap();
6
- const state = new WeakMap();
7
- const milliseconds = Math.round(1000 / 60);
8
- function run(timed) {
9
- const timedConfiguration = configuration.get(timed);
10
- const timedCallbacks = callbacks.get(timed);
11
- const timedState = state.get(timed);
12
- timedState.active = true;
13
- timedState.finished = false;
14
- const isRepeated = timed instanceof Repeated;
15
- let index = 0;
16
- let start;
17
- function step(timestamp) {
18
- if (!timedState.active) {
19
- return;
20
- }
21
- start ?? (start = timestamp);
22
- const elapsed = timestamp - start;
23
- const elapsedMinimum = elapsed - milliseconds;
24
- const elapsedMaximum = elapsed + milliseconds;
25
- if (
26
- elapsedMinimum < timedConfiguration.time &&
27
- timedConfiguration.time < elapsedMaximum
28
- ) {
29
- if (timedState.active) {
30
- timedCallbacks.default(isRepeated ? index : undefined);
31
- }
32
- index += 1;
33
- if (isRepeated && index < timedConfiguration.count) {
34
- start = undefined;
35
- } else {
36
- timedState.finished = true;
37
- timed.stop();
38
- return;
39
- }
40
- }
41
- timedState.frame = globalThis.requestAnimationFrame(step);
42
- }
43
- timedState.frame = globalThis.requestAnimationFrame(step);
44
- }
45
- class Timed {
46
- get active() {
47
- return state.get(this)?.active ?? false;
48
- }
49
- get finished() {
50
- return !this.active && (state.get(this)?.finished ?? false);
51
- }
52
- /**
53
- * @param {Callback} callback
54
- * @param {number} time
55
- * @param {number} count
56
- * @param {AfterCallback=} afterCallback
57
- */
58
- constructor(callback, time, count, afterCallback) {
59
- const isRepeated = this instanceof Repeated;
60
- const type = isRepeated ? 'repeated' : 'waited';
61
- if (typeof callback !== 'function') {
62
- throw new TypeError(`A ${type} timer must have a callback function`);
63
- }
64
- if (typeof time !== 'number' || time < 0) {
65
- throw new TypeError(
66
- `A ${type} timer must have a non-negative number as its time`,
67
- );
68
- }
69
- if (isRepeated && (typeof count !== 'number' || count < 2)) {
70
- throw new TypeError(
71
- 'A repeated timer must have a number above 1 as its repeat count',
72
- );
73
- }
74
- if (
75
- isRepeated &&
76
- afterCallback !== undefined &&
77
- typeof afterCallback !== 'function'
78
- ) {
79
- throw new TypeError(
80
- "A repeated timer's after-callback must be a function",
81
- );
82
- }
83
- callbacks.set(this, {
84
- after: afterCallback,
85
- default: callback,
86
- });
87
- configuration.set(this, {count, time});
88
- state.set(this, {
89
- active: false,
90
- finished: false,
91
- });
92
- }
93
- restart() {
94
- this.stop();
95
- run(this);
96
- return this;
97
- }
98
- start() {
99
- if (!this.active) {
100
- run(this);
101
- }
102
- return this;
103
- }
104
- stop() {
105
- const timedCallbacks = callbacks.get(this);
106
- const timedState = state.get(this);
107
- timedState.active = false;
108
- if (timedState.frame === undefined) {
109
- return this;
110
- }
111
- globalThis.cancelAnimationFrame(timedState.frame);
112
- timedCallbacks.after?.(this.finished);
113
- timedState.frame = undefined;
114
- return this;
115
- }
116
- }
117
- /**
118
- * A timer that waits and runs repeatedly
119
- */
120
- class Repeated extends Timed {}
121
- /**
122
- * A timer that waits and runs once
123
- */
124
- class Waited extends Timed {
125
- /**
126
- * Creates a new waited timer
127
- * @param {() => void} callback
128
- * @param {number} time
129
- */
130
- constructor(callback, time) {
131
- super(callback, time, 1);
132
- }
133
- }
134
- /**
135
- * Creates and starts a new repeated timer
136
- * @param {RepeatedCallback} callback
137
- * @param {number} time
138
- * @param {number} count
139
- * @param {AfterCallback=} afterCallback
140
- * @return {Repeated}
141
- */
142
- function repeat(callback, time, count, afterCallback) {
143
- return new Repeated(callback, time, count, afterCallback).start();
144
- }
145
- /**
146
- * Creates and starts a new waited timer
147
- * @param {() => void} callback
148
- * @param {number} time
149
- * @return {Waited}
150
- */
151
- function wait(callback, time) {
152
- return new Waited(callback, time).start();
153
- }
154
-
155
- exports.Repeated = Repeated;
156
- exports.Waited = Waited;
157
- exports.repeat = repeat;
158
- exports.wait = wait;
159
-
160
- return exports;
161
-
162
- })({});