@oscarpalmer/timer 0.25.0 → 0.27.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,123 @@
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: 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
+ get trace(): string | 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?: string;
108
+ };
109
+ export declare class TimerTrace extends Error {
110
+ constructor();
111
+ }
112
+ export type WaitOptions = {} & BaseOptions & OptionsWithError;
113
+ export type WhenOptions = {} & OptionsWithCount;
114
+ export type WhenState = {
115
+ promise: Promise<void>;
116
+ rejecter?: () => void;
117
+ resolver?: () => void;
118
+ started: boolean;
119
+ timer: Timer;
120
+ };
121
+ export type WorkType = "continue" | "pause" | "restart" | "start" | "stop";
122
+
123
+ export {};
package/types/models.d.ts CHANGED
@@ -50,7 +50,7 @@ export type TimerState = {
50
50
  isRepeated: boolean;
51
51
  minimum: number;
52
52
  paused: boolean;
53
- trace: TimerTrace;
53
+ trace?: string;
54
54
  };
55
55
  export declare class TimerTrace extends Error {
56
56
  constructor();
@@ -0,0 +1,139 @@
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: string | 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(): string | 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?: string;
136
+ };
137
+ export type WaitOptions = {} & BaseOptions & OptionsWithError;
138
+
139
+ 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 './models';
1
+ import { 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;
@@ -18,7 +18,7 @@ export declare abstract class BasicTimer<State> {
18
18
  /**
19
19
  * Gets the traced location of the timer
20
20
  */
21
- abstract readonly trace: TimerTrace | undefined;
21
+ abstract readonly trace: string | undefined;
22
22
  }
23
23
  /**
24
24
  * A timer that can be started, stopped, and restarted as neeeded
@@ -28,7 +28,7 @@ export declare class Timer extends BasicTimer<TimerState> {
28
28
  get active(): boolean;
29
29
  get destroyed(): boolean;
30
30
  get paused(): boolean;
31
- get trace(): TimerTrace | undefined;
31
+ get trace(): string | undefined;
32
32
  constructor(type: 'repeat' | 'wait', state: TimerState, options: TimerOptions);
33
33
  /**
34
34
  * Continues the timer _(if it was paused)_
@@ -0,0 +1,150 @@
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: 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
+ get trace(): string | 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?: string;
108
+ };
109
+ export type WhenOptions = {} & OptionsWithCount;
110
+ export type WhenState = {
111
+ promise: Promise<void>;
112
+ rejecter?: () => void;
113
+ resolver?: () => void;
114
+ started: boolean;
115
+ timer: Timer;
116
+ };
117
+ export declare class When extends BasicTimer<WhenState> {
118
+ get active(): boolean;
119
+ get destroyed(): boolean;
120
+ get paused(): boolean;
121
+ get trace(): string | undefined;
122
+ constructor(state: WhenState);
123
+ /**
124
+ * Continues the timer _(if it was paused)_
125
+ */
126
+ continue(): When;
127
+ /**
128
+ * Destroys the timer _(and stops it,if it was running)_
129
+ */
130
+ destroy(): void;
131
+ /**
132
+ * Pauses the timer _(if it was running)_
133
+ */
134
+ pause(): When;
135
+ /**
136
+ * Stops the timer _(if it was running)_
137
+ */
138
+ stop(): When;
139
+ /**
140
+ * Starts the timer and returns a promise that resolves when the condition is met
141
+ */
142
+ then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
143
+ }
144
+ /**
145
+ * - Creates a promise that resolves when a condition is met
146
+ * - If the condition is never met in a timely manner, the promise will reject
147
+ */
148
+ export declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
149
+
150
+ export {};
package/types/when.d.ts CHANGED
@@ -4,7 +4,7 @@ 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("./models").TimerTrace | undefined;
7
+ get trace(): string | undefined;
8
8
  constructor(state: WhenState);
9
9
  /**
10
10
  * Continues the timer _(if it was paused)_
@@ -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
- };