@oscarpalmer/timer 0.20.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,184 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ /**
4
+ * Callback that runs after the timer has finished (or is stopped)
5
+ * - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
6
+ */
7
+ export type AfterCallback = (finished: boolean) => void;
8
+ export type AnyCallback = (() => void) | IndexedCallback;
9
+ /**
10
+ * Callback that runs for each iteration of the timer
11
+ */
12
+ export type IndexedCallback = (index: number) => void;
13
+ export type BaseOptions = {
14
+ /**
15
+ * Interval between each callback
16
+ */
17
+ interval: number;
18
+ /**
19
+ * Maximum amount of time the timer may run for
20
+ */
21
+ timeout: number;
22
+ };
23
+ export type OptionsWithCount = {
24
+ /**
25
+ * How many times the timer should repeat
26
+ */
27
+ count: number;
28
+ } & BaseOptions;
29
+ export type OptionsWithError = {
30
+ /**
31
+ * Callback to run when an error occurs _(usually a timeout)_
32
+ */
33
+ errorCallback?: () => void;
34
+ };
35
+ export type RepeatOptions = {
36
+ /**
37
+ * Callback to run after the timer has finished (or is stopped)
38
+ * - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
39
+ */
40
+ afterCallback?: AfterCallback;
41
+ } & OptionsWithCount & OptionsWithError;
42
+ export type TimerOptions = {} & RepeatOptions;
43
+ export type TimerState = {
44
+ active: boolean;
45
+ callback: AnyCallback;
46
+ count?: number;
47
+ elapsed?: number;
48
+ frame?: number;
49
+ index?: number;
50
+ isRepeated: boolean;
51
+ minimum: number;
52
+ paused: boolean;
53
+ trace: unknown;
54
+ };
55
+ export type WaitOptions = {} & BaseOptions & OptionsWithError;
56
+ export type WhenOptions = {} & OptionsWithCount;
57
+ export type WhenState = {
58
+ promise: Promise<void>;
59
+ rejecter?: () => void;
60
+ resolver?: () => void;
61
+ timer: Timer$1;
62
+ };
63
+ declare abstract class BasicTimer<State> {
64
+ protected readonly $timer: string;
65
+ protected readonly state: State;
66
+ constructor(type: "repeat" | "wait" | "when", state: State);
67
+ /**
68
+ * Is the timer running?
69
+ */
70
+ abstract readonly active: boolean;
71
+ /**
72
+ * Is the timer paused?
73
+ */
74
+ abstract readonly paused: boolean;
75
+ }
76
+ /**
77
+ * A timer that can be started, stopped, and restarted as neeeded
78
+ */
79
+ declare class Timer$1 extends BasicTimer<TimerState> {
80
+ private readonly options;
81
+ get active(): boolean;
82
+ get paused(): boolean;
83
+ /**
84
+ * Gets the traced location of the timer
85
+ */
86
+ get trace(): unknown;
87
+ constructor(type: "repeat" | "wait", state: TimerState, options: TimerOptions);
88
+ /**
89
+ * Continues the timer _(if it was paused)_
90
+ */
91
+ continue(): Timer$1;
92
+ /**
93
+ * Pauses the timer _(if it was running)_
94
+ */
95
+ pause(): Timer$1;
96
+ /**
97
+ * Restarts the timer _(if it was running)_
98
+ */
99
+ restart(): Timer$1;
100
+ /**
101
+ * Starts the timer _(if it was stopped)_
102
+ */
103
+ start(): Timer$1;
104
+ /**
105
+ * Stops the timer _(if it was running)_
106
+ */
107
+ stop(): Timer$1;
108
+ }
109
+ /**
110
+ * Creates a timer which:
111
+ * - calls a callback after a certain amount of time...
112
+ * - ... and repeats it a certain amount of times
113
+ * ---
114
+ * - `options.count` defaults to `Infinity`
115
+ * - `options.interval` defaults to `1000/60` _(1 frame)_
116
+ * - `options.timeout` defaults to `Infinity`
117
+ */
118
+ export declare function repeat(callback: IndexedCallback, options?: Partial<RepeatOptions>): Timer$1;
119
+ /**
120
+ * Creates a timer which calls a callback after a certain amount of time
121
+ */
122
+ export declare function wait(callback: () => void): Timer$1;
123
+ /**
124
+ * Creates a timer which calls a callback after a certain amount of time
125
+ */
126
+ export declare function wait(callback: () => void, time: number): Timer$1;
127
+ /**
128
+ * Creates a timer which calls a callback after a certain amount of time
129
+ * - `options.interval` defaults to `1000/60` _(1 frame)_
130
+ * - `options.timeout` defaults to `30_000` _(30 seconds)_
131
+ */
132
+ export declare function wait(callback: () => void, options: Partial<WaitOptions>): Timer$1;
133
+ export declare class When extends BasicTimer<WhenState> {
134
+ get active(): boolean;
135
+ get paused(): boolean;
136
+ constructor(state: WhenState);
137
+ /**
138
+ * Continues the timer _(if it was paused)_
139
+ */
140
+ continue(): When;
141
+ /**
142
+ * Pauses the timer _(if it was running)_
143
+ */
144
+ pause(): When;
145
+ /**
146
+ * Stops the timer _(if it was running)_
147
+ */
148
+ stop(): When;
149
+ /**
150
+ * Starts the timer and returns a promise that resolves when the condition is met
151
+ */
152
+ then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
153
+ }
154
+ /**
155
+ * - Creates a promise that resolves when a condition is met
156
+ * - If the condition is never met in a timely manner, the promise will reject
157
+ */
158
+ export declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
159
+ /**
160
+ * Is the value a repeating timer?
161
+ */
162
+ export declare function isRepeated(value: unknown): value is Timer$1;
163
+ /**
164
+ * Is the value a timer?
165
+ */
166
+ export declare function isTimer(value: unknown): value is Timer$1;
167
+ /**
168
+ * Is the value a waiting timer?
169
+ */
170
+ export declare function isWaited(value: unknown): value is Timer$1;
171
+ /**
172
+ * Is the value a conditional timer?
173
+ */
174
+ export declare function isWhen(value: unknown): value is When;
175
+ /**
176
+ * Creates a delayed promise that resolves after a certain amount of time _(or rejects when timed out)_
177
+ */
178
+ export declare function delay(time: number, timeout?: number): Promise<void>;
179
+
180
+ export {
181
+ Timer$1 as Timer,
182
+ };
183
+
184
+ export {};
package/types/index.d.ts CHANGED
@@ -1,30 +1,8 @@
1
- export type AfterCallback = (finished: boolean) => void;
2
- type IndexedCallback = (index: number) => void;
3
- export declare class Timer {
4
- private readonly _configuration;
5
- private readonly _state;
6
- get active(): boolean;
7
- get finished(): boolean;
8
- /**
9
- * @param {Callback} callback
10
- * @param {number} time
11
- * @param {number} count
12
- * @param {AfterCallback=} afterCallback
13
- */
14
- constructor(callback: IndexedCallback, time?: number, count?: number, afterCallback?: AfterCallback);
15
- restart(): Timer;
16
- start(): Timer;
17
- stop(): Timer;
18
- }
1
+ import './global';
19
2
  /**
20
- * Creates and starts a new repeated timer
3
+ * Creates a delayed promise that resolves after a certain amount of time _(or rejects when timed out)_
21
4
  */
22
- export declare function repeat(callback: IndexedCallback, count: number): Timer;
23
- export declare function repeat(callback: IndexedCallback, count: number, afterCallback: AfterCallback): Timer;
24
- export declare function repeat(callback: IndexedCallback, count: number, time: number): Timer;
25
- export declare function repeat(callback: IndexedCallback, count: number, time: number, afterCallback: AfterCallback): Timer;
26
- /**
27
- * Creates and starts a new waited timer
28
- */
29
- export declare function wait(callback: IndexedCallback, time?: number): Timer;
30
- export {};
5
+ export declare function delay(time: number, timeout?: number): Promise<void>;
6
+ export { isRepeated, isTimer, isWaited, isWhen } from './is';
7
+ export { repeat, wait, type Timer } from './timer';
8
+ export { when, type When } from './when';
package/types/is.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import type { Timer } from './timer';
2
+ import type { When } from './when';
3
+ /**
4
+ * Is the value a repeating timer?
5
+ */
6
+ export declare function isRepeated(value: unknown): value is Timer;
7
+ /**
8
+ * Is the value a timer?
9
+ */
10
+ export declare function isTimer(value: unknown): value is Timer;
11
+ /**
12
+ * Is the value a waiting timer?
13
+ */
14
+ export declare function isWaited(value: unknown): value is Timer;
15
+ /**
16
+ * Is the value a conditional timer?
17
+ */
18
+ export declare function isWhen(value: unknown): value is When;
@@ -0,0 +1,62 @@
1
+ import type { Timer } from './timer';
2
+ /**
3
+ * Callback that runs after the timer has finished (or is stopped)
4
+ * - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
5
+ */
6
+ export type AfterCallback = (finished: boolean) => void;
7
+ export type AnyCallback = (() => void) | IndexedCallback;
8
+ /**
9
+ * Callback that runs for each iteration of the timer
10
+ */
11
+ export type IndexedCallback = (index: number) => void;
12
+ export type BaseOptions = {
13
+ /**
14
+ * Interval between each callback
15
+ */
16
+ interval: number;
17
+ /**
18
+ * Maximum amount of time the timer may run for
19
+ */
20
+ timeout: number;
21
+ };
22
+ export type OptionsWithCount = {
23
+ /**
24
+ * How many times the timer should repeat
25
+ */
26
+ count: number;
27
+ } & BaseOptions;
28
+ export type OptionsWithError = {
29
+ /**
30
+ * Callback to run when an error occurs _(usually a timeout)_
31
+ */
32
+ errorCallback?: () => void;
33
+ };
34
+ export type RepeatOptions = {
35
+ /**
36
+ * Callback to run after the timer has finished (or is stopped)
37
+ * - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
38
+ */
39
+ afterCallback?: AfterCallback;
40
+ } & OptionsWithCount & OptionsWithError;
41
+ export type TimerOptions = {} & RepeatOptions;
42
+ export type TimerState = {
43
+ active: boolean;
44
+ callback: AnyCallback;
45
+ count?: number;
46
+ elapsed?: number;
47
+ frame?: number;
48
+ index?: number;
49
+ isRepeated: boolean;
50
+ minimum: number;
51
+ paused: boolean;
52
+ trace: unknown;
53
+ };
54
+ export type WaitOptions = {} & BaseOptions & OptionsWithError;
55
+ export type WhenOptions = {} & OptionsWithCount;
56
+ export type WhenState = {
57
+ promise: Promise<void>;
58
+ rejecter?: () => void;
59
+ resolver?: () => void;
60
+ timer: Timer;
61
+ };
62
+ export type WorkType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
@@ -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;