@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/timer",
3
- "version": "0.43.0",
3
+ "version": "0.44.0",
4
4
  "description": "A better solution for timeout- and interval-based timers.",
5
5
  "keywords": [
6
6
  "requestAnimationFrame",
@@ -34,6 +34,10 @@
34
34
  "types": "./dist/delay.d.mts",
35
35
  "default": "./dist/delay.mjs"
36
36
  },
37
+ "./models": {
38
+ "types": "./dist/models.d.mts",
39
+ "default": "./dist/models.mjs"
40
+ },
37
41
  "./repeat": {
38
42
  "types": "./dist/repeat.d.mts",
39
43
  "default": "./dist/repeat.mjs"
@@ -56,13 +60,13 @@
56
60
  "watch": "npx vite build --watch"
57
61
  },
58
62
  "dependencies": {
59
- "@oscarpalmer/atoms": "^0.187"
63
+ "@oscarpalmer/atoms": "^0.193"
60
64
  },
61
65
  "devDependencies": {
62
- "@oxlint/plugins": "^1.66",
63
- "@types/node": "^25.9",
66
+ "@oxlint/plugins": "^1.80",
67
+ "@types/node": "^26.3",
64
68
  "@vitest/coverage-istanbul": "^4.1",
65
- "jsdom": "^29.1",
69
+ "jsdom": "^30",
66
70
  "tsdown": "^0.22",
67
71
  "typescript": "^6",
68
72
  "vite": "npm:@voidzero-dev/vite-plus-core@latest",
package/src/constants.ts CHANGED
@@ -1,5 +1,4 @@
1
- import type {TimerType, WorkHandlerType} from './models';
2
- import type {Timer} from './timer';
1
+ import type {Timer, TimerName, WorkHandlerType} from './models';
3
2
 
4
3
  /**
5
4
  * Buffer value to use when evaluating if a specific time is within a certain range
@@ -28,11 +27,11 @@ export const TIMERS_ACTIVE = new Set<Timer>();
28
27
  */
29
28
  export const TIMERS_HIDDEN = new Set<Timer>();
30
29
 
31
- export const TYPE_REPEAT: TimerType = 'repeat';
30
+ export const TYPE_REPEAT: TimerName = 'repeat';
32
31
 
33
- export const TYPE_WAIT: TimerType = 'wait';
32
+ export const TYPE_WAIT: TimerName = 'wait';
34
33
 
35
- export const TYPE_WHEN: TimerType = 'when';
34
+ export const TYPE_WHEN: TimerName = 'when';
36
35
 
37
36
  export const WORK_CONTINUE: WorkHandlerType = 'continue';
38
37
 
package/src/delay.ts CHANGED
@@ -1,2 +1 @@
1
1
  export {delay} from '@oscarpalmer/atoms/promise/delay';
2
- export type {PromiseOptions} from '@oscarpalmer/atoms/promise/models';
package/src/get.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type {GenericCallback} from '@oscarpalmer/atoms/models';
2
1
  import {noop} from '@oscarpalmer/atoms/function';
2
+ import type {GenericCallback} from '@oscarpalmer/atoms/models';
3
3
  import {DEFAULT_TIMEOUT} from './constants';
4
4
 
5
5
  export function getCallback(value: unknown): GenericCallback {
package/src/global.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {TIMERS_ACTIVE, TIMERS_HIDDEN, WORK_CONTINUE, WORK_PAUSE} from './constants';
2
- import type {Timer} from './timer';
2
+ import type {Timer} from './models';
3
3
 
4
4
  declare global {
5
5
  var _oscarpalmer_timer_debug: boolean | undefined;
package/src/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import './global';
2
2
 
3
- export * from './delay';
3
+ export {delay} from './delay';
4
4
  export {isRepeated, isTimer, isWaited, isWhen} from './is';
5
- export * from './repeat';
6
- export type {Timer} from './timer';
7
- export * from './wait';
8
- export * from './when';
5
+ export type {RepeatOptions, Timer, TimerOptions, When, WhenOptions} from './models';
6
+ export {repeat} from './repeat';
7
+ export {wait} from './wait';
8
+ export {when} from './when';
package/src/is.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type {PlainObject} from '@oscarpalmer/atoms/models';
2
2
  import {TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN} from './constants';
3
- import type {Timer} from './timer';
4
- import type {When} from './when';
3
+ import type {Timer, When} from './models';
5
4
 
6
5
  function is(names: string[], value: unknown) {
7
6
  return names.includes((value as PlainObject)?.$timer as string);
package/src/models.ts CHANGED
@@ -1,5 +1,3 @@
1
- import type {Timer} from './timer';
2
-
3
1
  /**
4
2
  * Options for a repeating timer
5
3
  */
@@ -26,6 +24,60 @@ export type RepeatOptions = {
26
24
  timeout: number;
27
25
  };
28
26
 
27
+ export type Timer = {
28
+ /**
29
+ * Is the timer active?
30
+ */
31
+ get active(): boolean;
32
+
33
+ /**
34
+ * Is the timer destroyed?
35
+ */
36
+ get destroyed(): boolean;
37
+
38
+ /**
39
+ * Is the timer paused?
40
+ */
41
+ get paused(): boolean;
42
+
43
+ /**
44
+ * Get the timer's origin _(if debugging is enabled)_
45
+ */
46
+ get trace(): string | undefined;
47
+
48
+ /**
49
+ * Continue running the timer _(if it's paused)_
50
+ */
51
+ continue(): Timer;
52
+
53
+ /**
54
+ * Destroy the timer
55
+ */
56
+ destroy(): void;
57
+
58
+ /**
59
+ * Pause the timer _(if it's running)_
60
+ */
61
+ pause(): Timer;
62
+
63
+ /**
64
+ * Restart the timer _(or start it, if it's not running)_
65
+ */
66
+ restart(): Timer;
67
+
68
+ /**
69
+ * Start the timer _(if it's not running)_
70
+ */
71
+ start(): Timer;
72
+
73
+ /**
74
+ * Stop the timer _(if it's running)_
75
+ */
76
+ stop(): Timer;
77
+ };
78
+
79
+ export type TimerName = 'repeat' | 'wait' | 'when';
80
+
29
81
  export type TimerOptions = {
30
82
  onAfter: ((finished: boolean) => void) | undefined;
31
83
  onError: (() => void) | undefined;
@@ -54,7 +106,55 @@ export class TimerTrace extends Error {
54
106
  }
55
107
  }
56
108
 
57
- export type TimerType = 'repeat' | 'wait' | 'when';
109
+ export type When = {
110
+ /**
111
+ * Is the timer active?
112
+ */
113
+ get active(): boolean;
114
+
115
+ /**
116
+ * Is the timer destroyed?
117
+ */
118
+ get destroyed(): boolean;
119
+
120
+ /**
121
+ * Is the timer paused?
122
+ */
123
+ get paused(): boolean;
124
+
125
+ /**
126
+ * Get the timer's origin _(if debugging is enabled)_
127
+ */
128
+ get trace(): string | undefined;
129
+
130
+ /**
131
+ * Continues the timer _(if it was paused)_
132
+ */
133
+ continue(): When;
134
+
135
+ /**
136
+ * Destroys the timer _(and stops it,if it was running)_
137
+ */
138
+ destroy(): void;
139
+
140
+ /**
141
+ * Pauses the timer _(if it was running)_
142
+ */
143
+ pause(): When;
144
+
145
+ /**
146
+ * Start the timer
147
+ *
148
+ * @param resolve Optional resolve callback
149
+ * @returns Promise that resolves when the condition is met
150
+ */
151
+ start(resolve?: (() => void) | null): Promise<void>;
152
+
153
+ /**
154
+ * Stops the timer _(if it was running)_
155
+ */
156
+ stop(): When;
157
+ };
58
158
 
59
159
  /**
60
160
  * Options for a conditional timer
@@ -78,6 +178,7 @@ export type WhenState = {
78
178
  promise: Promise<void>;
79
179
  rejecter?: () => void;
80
180
  resolver?: () => void;
181
+ result: boolean;
81
182
  started: boolean;
82
183
  timer: Timer;
83
184
  };
@@ -91,7 +192,7 @@ export type WorkHandler = (
91
192
 
92
193
  export type WorkHandlerTimer = {
93
194
  instance: Timer;
94
- type: TimerType;
195
+ name: TimerName;
95
196
  };
96
197
 
97
198
  export type WorkHandlerType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
package/src/repeat.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import {TYPE_REPEAT} from './constants';
2
2
  import {getCallback, getValidNumber} from './get';
3
3
  import './global';
4
- import {type RepeatOptions, TimerTrace} from './models';
5
- import {Timer} from './timer';
4
+ import {type RepeatOptions, type Timer, TimerTrace} from './models';
5
+ import {createTimer} from './timer';
6
6
 
7
7
  /**
8
8
  * Create a repeating timer
@@ -12,7 +12,7 @@ import {Timer} from './timer';
12
12
  * @returns Timer instance
13
13
  */
14
14
  export function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer {
15
- return new Timer(
15
+ return createTimer(
16
16
  TYPE_REPEAT,
17
17
  {
18
18
  callback: getCallback(callback),
@@ -28,5 +28,3 @@ export function repeat(callback: (index: number) => void, options?: Partial<Repe
28
28
  true,
29
29
  );
30
30
  }
31
-
32
- export type {RepeatOptions, Timer};
package/src/timer.ts CHANGED
@@ -1,135 +1,91 @@
1
1
  import {noop} from '@oscarpalmer/atoms/function';
2
2
  import {WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP} from './constants';
3
- import type {TimerOptions, TimerState, TimerType, WorkHandlerType} from './models';
3
+ import type {Timer, TimerName, TimerOptions, TimerState, WorkHandlerType} from './models';
4
4
  import {stop, work} from './work';
5
5
 
6
- export class Timer {
7
- declare private readonly $timer: TimerType;
8
-
9
- protected readonly state: TimerState;
10
-
11
- /**
12
- * Is the timer active?
13
- */
14
- get active(): boolean {
15
- return this.state.active;
16
- }
17
-
18
- /**
19
- * Is the timer destroyed?
20
- */
21
- get destroyed(): boolean {
22
- return this.state.destroyed;
23
- }
24
-
25
- /**
26
- * Is the timer paused?
27
- */
28
- get paused(): boolean {
29
- return this.state.paused;
30
- }
31
-
32
- /**
33
- * Get the timer's origin _(if debugging is enabled)_
34
- */
35
- get trace(): string | undefined {
36
- return (globalThis._oscarpalmer_timer_debug ?? false) ? this.state.trace : undefined;
37
- }
38
-
39
- constructor(
40
- type: TimerType,
41
- state: Pick<TimerState, 'callback' | 'trace'>,
42
- protected readonly options: TimerOptions,
43
- start: boolean,
44
- ) {
45
- Object.defineProperty(this, '$timer', {
46
- value: type,
47
- });
48
-
49
- this.state = {
50
- ...state,
51
- active: false,
52
- destroyed: false,
53
- elapsed: 0,
54
- frame: undefined,
55
- index: 0,
56
- paused: false,
57
- total: 0,
58
- };
59
-
60
- if (start) {
61
- this.start();
62
- }
63
- }
64
-
65
- /**
66
- * Continue running the timer _(if it's paused)_
67
- */
68
- continue(): Timer {
69
- return this.#work(WORK_CONTINUE);
70
- }
71
-
72
- /**
73
- * Destroy the timer
74
- */
75
- destroy(): void {
76
- this.state.destroyed = true;
77
-
78
- this.options.onAfter = noop;
79
- this.options.onError = noop;
80
- this.state.callback = noop;
81
-
82
- if (!globalThis._oscarpalmer_timer_debug) {
83
- this.state.trace = undefined;
84
- }
85
-
86
- stop(
6
+ export function createTimer(
7
+ name: TimerName,
8
+ pick: Pick<TimerState, 'callback' | 'trace'>,
9
+ options: TimerOptions,
10
+ start: boolean,
11
+ ): Timer {
12
+ function worker(type: WorkHandlerType): Timer {
13
+ return work(
14
+ type,
87
15
  {
88
- instance: this,
89
- type: this.$timer,
16
+ name,
17
+ instance: instance as Timer,
90
18
  },
91
- this.state,
92
- this.options,
19
+ state,
20
+ options,
93
21
  );
94
22
  }
95
23
 
96
- /**
97
- * Pause the timer _(if it's running)_
98
- */
99
- pause(): Timer {
100
- return this.#work(WORK_PAUSE);
24
+ const state: TimerState = {
25
+ ...pick,
26
+ active: false,
27
+ destroyed: false,
28
+ elapsed: 0,
29
+ frame: undefined,
30
+ index: 0,
31
+ paused: false,
32
+ total: 0,
33
+ };
34
+
35
+ const instance = {
36
+ continue: () => worker(WORK_CONTINUE),
37
+ destroy: () => destroyTimer(name, instance as Timer, state, options),
38
+ pause: () => worker(WORK_PAUSE),
39
+ restart: () => worker(WORK_RESTART),
40
+ start: () => worker(WORK_START),
41
+ stop: () => worker(WORK_STOP),
42
+ };
43
+
44
+ Object.defineProperties(instance, {
45
+ $timer: {
46
+ enumerable: false,
47
+ value: name,
48
+ },
49
+ active: {
50
+ enumerable: true,
51
+ get: () => state.active,
52
+ },
53
+ destroyed: {
54
+ enumerable: true,
55
+ get: () => state.destroyed,
56
+ },
57
+ paused: {
58
+ enumerable: true,
59
+ get: () => state.paused,
60
+ },
61
+ trace: {
62
+ enumerable: true,
63
+ get: () => ((globalThis._oscarpalmer_timer_debug ?? false) ? state.trace : undefined),
64
+ },
65
+ });
66
+
67
+ if (start) {
68
+ instance.start();
101
69
  }
102
70
 
103
- /**
104
- * Restart the timer _(or start it, if it's not running)_
105
- */
106
- restart(): Timer {
107
- return this.#work(WORK_RESTART);
108
- }
71
+ return Object.freeze(instance) as Timer;
72
+ }
109
73
 
110
- /**
111
- * Start the timer _(if it's not running)_
112
- */
113
- start(): Timer {
114
- return this.#work(WORK_START);
115
- }
74
+ function destroyTimer(
75
+ name: TimerName,
76
+ instance: Timer,
77
+ state: TimerState,
78
+ options: TimerOptions,
79
+ ): void {
80
+ state.destroyed = true;
116
81
 
117
- /**
118
- * Stop the timer _(if it's running)_
119
- */
120
- stop(): Timer {
121
- return this.#work(WORK_STOP);
122
- }
82
+ options.onAfter = noop;
83
+ options.onError = noop;
84
+ state.callback = noop;
123
85
 
124
- #work(type: WorkHandlerType): Timer {
125
- return work(
126
- type,
127
- {
128
- instance: this,
129
- type: this.$timer,
130
- },
131
- this.state,
132
- this.options,
133
- );
86
+ if (!globalThis._oscarpalmer_timer_debug) {
87
+ state.trace = undefined;
134
88
  }
89
+
90
+ stop({instance, name}, state, options);
135
91
  }
package/src/wait.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import {TYPE_WAIT} from './constants';
2
2
  import {getCallback, getValidNumber} from './get';
3
3
  import './global';
4
- import {TimerTrace} from './models';
5
- import {Timer} from './timer';
4
+ import {type Timer, TimerTrace} from './models';
5
+ import {createTimer} from './timer';
6
6
 
7
7
  /**
8
8
  * Create a waiting timer
@@ -11,7 +11,7 @@ import {Timer} from './timer';
11
11
  * @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
12
12
  */
13
13
  export function wait(callback: () => void, time?: number): Timer {
14
- return new Timer(
14
+ return createTimer(
15
15
  TYPE_WAIT,
16
16
  {
17
17
  callback: getCallback(callback),
@@ -27,5 +27,3 @@ export function wait(callback: () => void, time?: number): Timer {
27
27
  true,
28
28
  );
29
29
  }
30
-
31
- export type {Timer};