@oscarpalmer/timer 0.43.0 → 0.44.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/models.d.mts CHANGED
@@ -1,5 +1,3 @@
1
- import { Timer } from "./timer.mjs";
2
-
3
1
  //#region src/models.d.ts
4
2
  /**
5
3
  * Options for a repeating timer
@@ -26,6 +24,49 @@ type RepeatOptions = {
26
24
  */
27
25
  timeout: number;
28
26
  };
27
+ type Timer = {
28
+ /**
29
+ * Is the timer active?
30
+ */
31
+ get active(): boolean;
32
+ /**
33
+ * Is the timer destroyed?
34
+ */
35
+ get destroyed(): boolean;
36
+ /**
37
+ * Is the timer paused?
38
+ */
39
+ get paused(): boolean;
40
+ /**
41
+ * Get the timer's origin _(if debugging is enabled)_
42
+ */
43
+ get trace(): string | undefined;
44
+ /**
45
+ * Continue running the timer _(if it's paused)_
46
+ */
47
+ continue(): Timer;
48
+ /**
49
+ * Destroy the timer
50
+ */
51
+ destroy(): void;
52
+ /**
53
+ * Pause the timer _(if it's running)_
54
+ */
55
+ pause(): Timer;
56
+ /**
57
+ * Restart the timer _(or start it, if it's not running)_
58
+ */
59
+ restart(): Timer;
60
+ /**
61
+ * Start the timer _(if it's not running)_
62
+ */
63
+ start(): Timer;
64
+ /**
65
+ * Stop the timer _(if it's running)_
66
+ */
67
+ stop(): Timer;
68
+ };
69
+ type TimerName = 'repeat' | 'wait' | 'when';
29
70
  type TimerOptions = {
30
71
  onAfter: ((finished: boolean) => void) | undefined;
31
72
  onError: (() => void) | undefined;
@@ -47,7 +88,47 @@ type TimerState = {
47
88
  declare class TimerTrace extends Error {
48
89
  constructor();
49
90
  }
50
- type TimerType = 'repeat' | 'wait' | 'when';
91
+ type When = {
92
+ /**
93
+ * Is the timer active?
94
+ */
95
+ get active(): boolean;
96
+ /**
97
+ * Is the timer destroyed?
98
+ */
99
+ get destroyed(): boolean;
100
+ /**
101
+ * Is the timer paused?
102
+ */
103
+ get paused(): boolean;
104
+ /**
105
+ * Get the timer's origin _(if debugging is enabled)_
106
+ */
107
+ get trace(): string | undefined;
108
+ /**
109
+ * Continues the timer _(if it was paused)_
110
+ */
111
+ continue(): When;
112
+ /**
113
+ * Destroys the timer _(and stops it,if it was running)_
114
+ */
115
+ destroy(): void;
116
+ /**
117
+ * Pauses the timer _(if it was running)_
118
+ */
119
+ pause(): When;
120
+ /**
121
+ * Start the timer
122
+ *
123
+ * @param resolve Optional resolve callback
124
+ * @returns Promise that resolves when the condition is met
125
+ */
126
+ start(resolve?: (() => void) | null): Promise<void>;
127
+ /**
128
+ * Stops the timer _(if it was running)_
129
+ */
130
+ stop(): When;
131
+ };
51
132
  /**
52
133
  * Options for a conditional timer
53
134
  */
@@ -69,14 +150,15 @@ type WhenState = {
69
150
  promise: Promise<void>;
70
151
  rejecter?: () => void;
71
152
  resolver?: () => void;
153
+ result: boolean;
72
154
  started: boolean;
73
155
  timer: Timer;
74
156
  };
75
157
  type WorkHandler = (type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions) => Timer;
76
158
  type WorkHandlerTimer = {
77
159
  instance: Timer;
78
- type: TimerType;
160
+ name: TimerName;
79
161
  };
80
162
  type WorkHandlerType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
81
163
  //#endregion
82
- export { RepeatOptions, TimerOptions, TimerState, TimerTrace, TimerType, WhenOptions, WhenState, WorkHandler, WorkHandlerTimer, WorkHandlerType };
164
+ export { RepeatOptions, Timer, TimerName, TimerOptions, TimerState, TimerTrace, When, WhenOptions, WhenState, WorkHandler, WorkHandlerTimer, WorkHandlerType };
package/dist/repeat.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { Timer } from "./timer.mjs";
2
- import { RepeatOptions } from "./models.mjs";
1
+ import { RepeatOptions, Timer } from "./models.mjs";
2
+ import "./global.mjs";
3
3
  //#region src/repeat.d.ts
4
4
  /**
5
5
  * Create a repeating timer
@@ -10,4 +10,4 @@ import { RepeatOptions } from "./models.mjs";
10
10
  */
11
11
  declare function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer;
12
12
  //#endregion
13
- export { type RepeatOptions, type Timer, repeat };
13
+ export { repeat };
package/dist/repeat.mjs CHANGED
@@ -2,7 +2,7 @@ import { TYPE_REPEAT } from "./constants.mjs";
2
2
  import { getCallback, getValidNumber } from "./get.mjs";
3
3
  import "./global.mjs";
4
4
  import { TimerTrace } from "./models.mjs";
5
- import { Timer } from "./timer.mjs";
5
+ import { createTimer } from "./timer.mjs";
6
6
  //#region src/repeat.ts
7
7
  /**
8
8
  * Create a repeating timer
@@ -12,7 +12,7 @@ import { Timer } from "./timer.mjs";
12
12
  * @returns Timer instance
13
13
  */
14
14
  function repeat(callback, options) {
15
- return new Timer(TYPE_REPEAT, {
15
+ return createTimer(TYPE_REPEAT, {
16
16
  callback: getCallback(callback),
17
17
  trace: new TimerTrace().stack
18
18
  }, {
package/dist/timer.d.mts CHANGED
@@ -1,52 +1,5 @@
1
- import { TimerOptions, TimerState, TimerType } from "./models.mjs";
2
-
1
+ import { Timer, TimerName, TimerOptions, TimerState } from "./models.mjs";
3
2
  //#region src/timer.d.ts
4
- declare class Timer {
5
- #private;
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, 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
- }
3
+ declare function createTimer(name: TimerName, pick: Pick<TimerState, 'callback' | 'trace'>, options: TimerOptions, start: boolean): Timer;
51
4
  //#endregion
52
- export { Timer };
5
+ export { createTimer };
package/dist/timer.mjs CHANGED
@@ -2,97 +2,66 @@ import { WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP } from "
2
2
  import { stop, work } from "./work.mjs";
3
3
  import { noop } from "@oscarpalmer/atoms/function";
4
4
  //#region src/timer.ts
5
- var Timer = class {
6
- state;
7
- /**
8
- * Is the timer active?
9
- */
10
- get active() {
11
- return this.state.active;
12
- }
13
- /**
14
- * Is the timer destroyed?
15
- */
16
- get destroyed() {
17
- return this.state.destroyed;
18
- }
19
- /**
20
- * Is the timer paused?
21
- */
22
- get paused() {
23
- return this.state.paused;
24
- }
25
- /**
26
- * Get the timer's origin _(if debugging is enabled)_
27
- */
28
- get trace() {
29
- return globalThis._oscarpalmer_timer_debug ?? false ? this.state.trace : void 0;
30
- }
31
- constructor(type, state, options, start) {
32
- this.options = options;
33
- Object.defineProperty(this, "$timer", { value: type });
34
- this.state = {
35
- ...state,
36
- active: false,
37
- destroyed: false,
38
- elapsed: 0,
39
- frame: void 0,
40
- index: 0,
41
- paused: false,
42
- total: 0
43
- };
44
- if (start) this.start();
45
- }
46
- /**
47
- * Continue running the timer _(if it's paused)_
48
- */
49
- continue() {
50
- return this.#work(WORK_CONTINUE);
51
- }
52
- /**
53
- * Destroy the timer
54
- */
55
- destroy() {
56
- this.state.destroyed = true;
57
- this.options.onAfter = noop;
58
- this.options.onError = noop;
59
- this.state.callback = noop;
60
- if (!globalThis._oscarpalmer_timer_debug) this.state.trace = void 0;
61
- stop({
62
- instance: this,
63
- type: this.$timer
64
- }, this.state, this.options);
65
- }
66
- /**
67
- * Pause the timer _(if it's running)_
68
- */
69
- pause() {
70
- return this.#work(WORK_PAUSE);
71
- }
72
- /**
73
- * Restart the timer _(or start it, if it's not running)_
74
- */
75
- restart() {
76
- return this.#work(WORK_RESTART);
77
- }
78
- /**
79
- * Start the timer _(if it's not running)_
80
- */
81
- start() {
82
- return this.#work(WORK_START);
83
- }
84
- /**
85
- * Stop the timer _(if it's running)_
86
- */
87
- stop() {
88
- return this.#work(WORK_STOP);
89
- }
90
- #work(type) {
5
+ function createTimer(name, pick, options, start) {
6
+ function worker(type) {
91
7
  return work(type, {
92
- instance: this,
93
- type: this.$timer
94
- }, this.state, this.options);
8
+ name,
9
+ instance
10
+ }, state, options);
95
11
  }
96
- };
12
+ const state = {
13
+ ...pick,
14
+ active: false,
15
+ destroyed: false,
16
+ elapsed: 0,
17
+ frame: void 0,
18
+ index: 0,
19
+ paused: false,
20
+ total: 0
21
+ };
22
+ const instance = {
23
+ continue: () => worker(WORK_CONTINUE),
24
+ destroy: () => destroyTimer(name, instance, state, options),
25
+ pause: () => worker(WORK_PAUSE),
26
+ restart: () => worker(WORK_RESTART),
27
+ start: () => worker(WORK_START),
28
+ stop: () => worker(WORK_STOP)
29
+ };
30
+ Object.defineProperties(instance, {
31
+ $timer: {
32
+ enumerable: false,
33
+ value: name
34
+ },
35
+ active: {
36
+ enumerable: true,
37
+ get: () => state.active
38
+ },
39
+ destroyed: {
40
+ enumerable: true,
41
+ get: () => state.destroyed
42
+ },
43
+ paused: {
44
+ enumerable: true,
45
+ get: () => state.paused
46
+ },
47
+ trace: {
48
+ enumerable: true,
49
+ get: () => globalThis._oscarpalmer_timer_debug ?? false ? state.trace : void 0
50
+ }
51
+ });
52
+ if (start) instance.start();
53
+ return Object.freeze(instance);
54
+ }
55
+ function destroyTimer(name, instance, state, options) {
56
+ state.destroyed = true;
57
+ options.onAfter = noop;
58
+ options.onError = noop;
59
+ state.callback = noop;
60
+ if (!globalThis._oscarpalmer_timer_debug) state.trace = void 0;
61
+ stop({
62
+ instance,
63
+ name
64
+ }, state, options);
65
+ }
97
66
  //#endregion
98
- export { Timer };
67
+ export { createTimer };
package/dist/wait.d.mts CHANGED
@@ -1,4 +1,5 @@
1
- import { Timer } from "./timer.mjs";
1
+ import { Timer } from "./models.mjs";
2
+ import "./global.mjs";
2
3
  //#region src/wait.d.ts
3
4
  /**
4
5
  * Create a waiting timer
@@ -8,4 +9,4 @@ import { Timer } from "./timer.mjs";
8
9
  */
9
10
  declare function wait(callback: () => void, time?: number): Timer;
10
11
  //#endregion
11
- export { type Timer, wait };
12
+ export { wait };
package/dist/wait.mjs CHANGED
@@ -2,7 +2,7 @@ import { TYPE_WAIT } from "./constants.mjs";
2
2
  import { getCallback, getValidNumber } from "./get.mjs";
3
3
  import "./global.mjs";
4
4
  import { TimerTrace } from "./models.mjs";
5
- import { Timer } from "./timer.mjs";
5
+ import { createTimer } from "./timer.mjs";
6
6
  //#region src/wait.ts
7
7
  /**
8
8
  * Create a waiting timer
@@ -11,7 +11,7 @@ import { Timer } from "./timer.mjs";
11
11
  * @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
12
12
  */
13
13
  function wait(callback, time) {
14
- return new Timer(TYPE_WAIT, {
14
+ return createTimer(TYPE_WAIT, {
15
15
  callback: getCallback(callback),
16
16
  trace: new TimerTrace().stack
17
17
  }, {
package/dist/when.d.mts CHANGED
@@ -1,49 +1,6 @@
1
- import { WhenOptions } from "./models.mjs";
1
+ import { When, WhenOptions } from "./models.mjs";
2
+ import "./global.mjs";
2
3
  //#region src/when.d.ts
3
- declare class When {
4
- private readonly $timer;
5
- private readonly state;
6
- /**
7
- * Is the timer active?
8
- */
9
- get active(): boolean;
10
- /**
11
- * Is the timer destroyed?
12
- */
13
- get destroyed(): boolean;
14
- /**
15
- * Is the timer paused?
16
- */
17
- get paused(): boolean;
18
- /**
19
- * Get the timer's origin _(if debugging is enabled)_
20
- */
21
- get trace(): string | undefined;
22
- constructor(condition: () => boolean, options?: Partial<WhenOptions>);
23
- /**
24
- * Continues the timer _(if it was paused)_
25
- */
26
- continue(): When;
27
- /**
28
- * Destroys the timer _(and stops it,if it was running)_
29
- */
30
- destroy(): void;
31
- /**
32
- * Pauses the timer _(if it was running)_
33
- */
34
- pause(): When;
35
- /**
36
- * Start the timer
37
- *
38
- * @param resolve Optional resolve callback
39
- * @returns Promise that resolves when the condition is met
40
- */
41
- start(resolve?: (() => void) | null): Promise<void>;
42
- /**
43
- * Stops the timer _(if it was running)_
44
- */
45
- stop(): When;
46
- }
47
4
  /**
48
5
  * Create a conditional timer
49
6
  * @param condition Condition to check
@@ -52,4 +9,4 @@ declare class When {
52
9
  */
53
10
  declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
54
11
  //#endregion
55
- export { type When, when };
12
+ export { when };
package/dist/when.mjs CHANGED
@@ -1,124 +1,47 @@
1
- import { MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN } from "./constants.mjs";
1
+ import { MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN, WORK_CONTINUE } from "./constants.mjs";
2
2
  import { getValidNumber, getValidTimeout } from "./get.mjs";
3
3
  import "./global.mjs";
4
4
  import { TimerTrace } from "./models.mjs";
5
- import { Timer } from "./timer.mjs";
5
+ import { createTimer } from "./timer.mjs";
6
6
  import { noop } from "@oscarpalmer/atoms/function";
7
7
  //#region src/when.ts
8
- var When = class {
9
- state = {
10
- promise: void 0,
11
- rejecter: void 0,
12
- resolver: void 0,
13
- started: false,
14
- timer: void 0
15
- };
16
- /**
17
- * Is the timer active?
18
- */
19
- get active() {
20
- return this.state.timer?.active ?? false;
21
- }
22
- /**
23
- * Is the timer destroyed?
24
- */
25
- get destroyed() {
26
- return this.state.timer == null;
27
- }
28
- /**
29
- * Is the timer paused?
30
- */
31
- get paused() {
32
- return this.state.timer?.paused ?? false;
33
- }
34
- /**
35
- * Get the timer's origin _(if debugging is enabled)_
36
- */
37
- get trace() {
38
- return globalThis._oscarpalmer_timer_debug ?? false ? this.state.timer?.trace : void 0;
39
- }
40
- constructor(condition, options) {
41
- Object.defineProperty(this, "$timer", { value: TYPE_WHEN });
42
- const { state } = this;
43
- state.promise = new Promise((resolve, reject) => {
44
- state.resolver = resolve;
45
- state.rejecter = reject;
46
- });
47
- let result = false;
48
- this.state.timer = new Timer(TYPE_WHEN, {
49
- callback() {
50
- try {
51
- if (condition()) {
52
- result = true;
53
- state.timer.stop();
54
- }
55
- } catch {
56
- state.timer.stop();
57
- }
58
- },
59
- trace: new TimerTrace().stack
60
- }, {
61
- onAfter: () => {
62
- if (result) state.resolver?.();
63
- else state.rejecter?.();
64
- this.destroy();
65
- },
66
- onError: () => {
67
- state.rejecter?.();
68
- this.destroy();
69
- },
70
- count: getValidNumber(options?.count),
71
- interval: getValidNumber(options?.interval),
72
- timeout: getValidTimeout(options?.timeout)
73
- }, false);
74
- }
75
- /**
76
- * Continues the timer _(if it was paused)_
77
- */
78
- continue() {
79
- this.state.timer?.continue();
80
- return this;
81
- }
82
- /**
83
- * Destroys the timer _(and stops it,if it was running)_
84
- */
85
- destroy() {
86
- const { state } = this;
87
- state.timer?.destroy();
88
- state.promise = void 0;
89
- state.resolver = noop;
90
- state.rejecter = noop;
91
- state.timer = void 0;
92
- }
93
- /**
94
- * Pauses the timer _(if it was running)_
95
- */
96
- pause() {
97
- this.state.timer?.pause();
98
- return this;
99
- }
100
- /**
101
- * Start the timer
102
- *
103
- * @param resolve Optional resolve callback
104
- * @returns Promise that resolves when the condition is met
105
- */
106
- start(resolve) {
107
- const { state } = this;
108
- if (state.timer == null) throw new Error(MESSAGE_DESTROYED);
109
- if (state.started) throw new Error(MESSAGE_STARTED);
110
- state.started = true;
111
- state.timer.start();
112
- return state.promise.then(resolve);
113
- }
114
- /**
115
- * Stops the timer _(if it was running)_
116
- */
117
- stop() {
118
- this.state.timer?.stop();
119
- return this;
8
+ function destroyWhen(state) {
9
+ state.timer?.destroy();
10
+ state.promise = void 0;
11
+ state.resolver = noop;
12
+ state.rejecter = noop;
13
+ state.timer = void 0;
14
+ }
15
+ function onAfter(instance, state) {
16
+ if (state.result) state.resolver?.();
17
+ else state.rejecter?.();
18
+ instance.destroy();
19
+ }
20
+ function onCallback(condition, state) {
21
+ try {
22
+ if (condition()) {
23
+ state.result = true;
24
+ state.timer.stop();
25
+ }
26
+ } catch {
27
+ state.timer.stop();
120
28
  }
121
- };
29
+ }
30
+ function onError(instance, state) {
31
+ state.rejecter?.();
32
+ instance.destroy();
33
+ }
34
+ function onWhen(type, instance, state) {
35
+ state.timer?.[type]?.();
36
+ return instance;
37
+ }
38
+ function startWhen(state, resolve) {
39
+ if (state.timer == null) throw new Error(MESSAGE_DESTROYED);
40
+ if (state.started) throw new Error(MESSAGE_STARTED);
41
+ state.started = true;
42
+ state.timer.start();
43
+ return state.promise.then(resolve);
44
+ }
122
45
  /**
123
46
  * Create a conditional timer
124
47
  * @param condition Condition to check
@@ -126,7 +49,57 @@ var When = class {
126
49
  * @returns Timer instance
127
50
  */
128
51
  function when(condition, options) {
129
- return new When(condition, options);
52
+ const state = {
53
+ promise: void 0,
54
+ result: false,
55
+ started: false,
56
+ timer: void 0
57
+ };
58
+ let instance;
59
+ state.promise = new Promise((resolve, reject) => {
60
+ state.resolver = resolve;
61
+ state.rejecter = reject;
62
+ });
63
+ state.timer = createTimer(TYPE_WHEN, {
64
+ callback: () => onCallback(condition, state),
65
+ trace: new TimerTrace().stack
66
+ }, {
67
+ onAfter: () => onAfter(instance, state),
68
+ onError: () => onError(instance, state),
69
+ count: getValidNumber(options?.count),
70
+ interval: getValidNumber(options?.interval),
71
+ timeout: getValidTimeout(options?.timeout)
72
+ }, false);
73
+ instance = {
74
+ continue: () => onWhen(WORK_CONTINUE, instance, state),
75
+ destroy: () => destroyWhen(state),
76
+ pause: () => onWhen("pause", instance, state),
77
+ start: (resolve) => startWhen(state, resolve),
78
+ stop: () => onWhen("stop", instance, state)
79
+ };
80
+ Object.defineProperties(instance, {
81
+ $timer: {
82
+ enumerable: false,
83
+ value: TYPE_WHEN
84
+ },
85
+ active: {
86
+ enumerable: true,
87
+ get: () => state.timer?.active ?? false
88
+ },
89
+ destroyed: {
90
+ enumerable: true,
91
+ get: () => state.timer == null
92
+ },
93
+ paused: {
94
+ enumerable: true,
95
+ get: () => state.timer?.paused ?? false
96
+ },
97
+ trace: {
98
+ enumerable: true,
99
+ get: () => globalThis._oscarpalmer_timer_debug ?? false ? state.timer?.trace : void 0
100
+ }
101
+ });
102
+ return Object.freeze(instance);
130
103
  }
131
104
  //#endregion
132
105
  export { when };
package/dist/work.d.mts CHANGED
@@ -1,6 +1,4 @@
1
- import { Timer } from "./timer.mjs";
2
- import { TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType } from "./models.mjs";
3
-
1
+ import { Timer, TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType } from "./models.mjs";
4
2
  //#region src/work.d.ts
5
3
  declare function stop(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
6
4
  declare function work(type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
package/dist/work.mjs CHANGED
@@ -6,7 +6,7 @@ function finish(timer, state, options, success) {
6
6
  state.active = false;
7
7
  state.elapsed = 0;
8
8
  state.frame = void 0;
9
- if (timer.type === "wait") state.callback();
9
+ if (timer.name === "wait") state.callback();
10
10
  else options.onAfter?.(success);
11
11
  }
12
12
  function ignore(type, state) {