@oscarpalmer/timer 0.41.3 → 0.42.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,82 @@
1
+ import { Timer } from "./timer.mjs";
2
+
3
+ //#region src/models.d.ts
4
+ /**
5
+ * Options for a repeating timer
6
+ */
7
+ type RepeatOptions = {
8
+ /**
9
+ * Callback to be called when the timer has stopped, either manually or by completing its work
10
+ */
11
+ onAfter: (finished: boolean) => void;
12
+ /**
13
+ * Callback to be called after the timer has timed out
14
+ */
15
+ onTimeout: () => void;
16
+ /**
17
+ * How many times the timer should repeat
18
+ */
19
+ count: number;
20
+ /**
21
+ * The interval between each repeat
22
+ */
23
+ interval: number;
24
+ /**
25
+ * The timeout for the timer _(any value above `0` will enable the timeout)_
26
+ */
27
+ timeout: number;
28
+ };
29
+ type TimerOptions = {
30
+ onAfter: ((finished: boolean) => void) | undefined;
31
+ onError: (() => void) | undefined;
32
+ count: number;
33
+ interval: number;
34
+ timeout: number;
35
+ };
36
+ type TimerState = {
37
+ active: boolean;
38
+ callback: () => void;
39
+ destroyed: boolean;
40
+ elapsed: number;
41
+ frame: number | undefined;
42
+ index: number;
43
+ paused: boolean;
44
+ total: number;
45
+ trace: string | undefined;
46
+ };
47
+ declare class TimerTrace extends Error {
48
+ constructor();
49
+ }
50
+ type TimerType = 'repeat' | 'wait' | 'when';
51
+ /**
52
+ * Options for a conditional timer
53
+ */
54
+ type WhenOptions = {
55
+ /**
56
+ * How many times the timer should check the condition
57
+ */
58
+ count: number;
59
+ /**
60
+ * Then interval between each condtional check
61
+ */
62
+ interval: number;
63
+ /**
64
+ * The timeout for the timer _(any value above `0` will enable the timeout)_
65
+ */
66
+ timeout: number;
67
+ };
68
+ type WhenState = {
69
+ promise: Promise<void>;
70
+ rejecter?: () => void;
71
+ resolver?: () => void;
72
+ started: boolean;
73
+ timer: Timer;
74
+ };
75
+ type WorkHandler = (type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions) => Timer;
76
+ type WorkHandlerTimer = {
77
+ instance: Timer;
78
+ type: TimerType;
79
+ };
80
+ type WorkHandlerType = 'continue' | 'pause' | 'restart' | 'start' | 'stop';
81
+ //#endregion
82
+ export { RepeatOptions, TimerOptions, TimerState, TimerTrace, TimerType, WhenOptions, WhenState, WorkHandler, WorkHandlerTimer, WorkHandlerType };
@@ -1,7 +1,9 @@
1
+ //#region src/models.ts
1
2
  var TimerTrace = class extends Error {
2
3
  constructor() {
3
4
  super();
4
5
  this.name = "TimerTrace";
5
6
  }
6
7
  };
8
+ //#endregion
7
9
  export { TimerTrace };
@@ -0,0 +1,12 @@
1
+ import { Timer } from "./timer.mjs";
2
+ import { RepeatOptions } from "./models.mjs";
3
+ //#region src/repeat.d.ts
4
+ /**
5
+ * Create a repeating timer
6
+ * @param callback Callback to run on each interval
7
+ * @param options Timer options
8
+ * @returns Timer instance
9
+ */
10
+ declare function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer;
11
+ //#endregion
12
+ export { type RepeatOptions, type Timer, repeat };
@@ -1,8 +1,9 @@
1
- import { TYPE_REPEAT } from "./constants.js";
2
- import { getCallback, getValidNumber } from "./get.js";
3
- import "./global.js";
4
- import { TimerTrace } from "./models.js";
5
- import { Timer } from "./timer.js";
1
+ import { TYPE_REPEAT } from "./constants.mjs";
2
+ import { getCallback, getValidNumber } from "./get.mjs";
3
+ import "./global.mjs";
4
+ import { TimerTrace } from "./models.mjs";
5
+ import { Timer } from "./timer.mjs";
6
+ //#region src/repeat.ts
6
7
  /**
7
8
  * Create a repeating timer
8
9
  * @param callback Callback to run on each interval
@@ -21,4 +22,5 @@ function repeat(callback, options) {
21
22
  timeout: getValidNumber(options?.timeout)
22
23
  }, true);
23
24
  }
25
+ //#endregion
24
26
  export { repeat };
@@ -0,0 +1,52 @@
1
+ import { TimerOptions, TimerState, TimerType } from "./models.mjs";
2
+
3
+ //#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
+ }
51
+ //#endregion
52
+ export { Timer };
@@ -1,6 +1,7 @@
1
- import { WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP } from "./constants.js";
2
- import { stop, work } from "./work.js";
1
+ import { WORK_CONTINUE, WORK_PAUSE, WORK_RESTART, WORK_START, WORK_STOP } from "./constants.mjs";
2
+ import { stop, work } from "./work.mjs";
3
3
  import { noop } from "@oscarpalmer/atoms/function";
4
+ //#region src/timer.ts
4
5
  var Timer = class {
5
6
  state;
6
7
  /**
@@ -93,4 +94,5 @@ var Timer = class {
93
94
  }, this.state, this.options);
94
95
  }
95
96
  };
97
+ //#endregion
96
98
  export { Timer };
@@ -1,9 +1,10 @@
1
- import './global';
2
- import { Timer } from './timer';
1
+ import { Timer } from "./timer.mjs";
2
+ //#region src/wait.d.ts
3
3
  /**
4
4
  * Create a waiting timer
5
5
  * @param callback Callback to run when the timer has finished
6
6
  * @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
7
7
  */
8
- export declare function wait(callback: () => void, time?: number): Timer;
9
- export type { Timer };
8
+ declare function wait(callback: () => void, time?: number): Timer;
9
+ //#endregion
10
+ export { type Timer, wait };
@@ -1,8 +1,9 @@
1
- import { TYPE_WAIT } from "./constants.js";
2
- import { getCallback, getValidNumber } from "./get.js";
3
- import "./global.js";
4
- import { TimerTrace } from "./models.js";
5
- import { Timer } from "./timer.js";
1
+ import { TYPE_WAIT } from "./constants.mjs";
2
+ import { getCallback, getValidNumber } from "./get.mjs";
3
+ import "./global.mjs";
4
+ import { TimerTrace } from "./models.mjs";
5
+ import { Timer } from "./timer.mjs";
6
+ //#region src/wait.ts
6
7
  /**
7
8
  * Create a waiting timer
8
9
  * @param callback Callback to run when the timer has finished
@@ -20,4 +21,5 @@ function wait(callback, time) {
20
21
  timeout: 0
21
22
  }, true);
22
23
  }
24
+ //#endregion
23
25
  export { wait };
@@ -0,0 +1,63 @@
1
+ import { WhenOptions } from "./models.mjs";
2
+ //#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
+ * @param resolve Optional resolve callback
38
+ * @param reject Optional reject callback
39
+ * @returns Promise that resolves when the condition is met
40
+ */
41
+ start(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
42
+ /**
43
+ * Stops the timer _(if it was running)_
44
+ */
45
+ stop(): When;
46
+ /**
47
+ * Start the timer
48
+ * @deprecated Use `start()` instead
49
+ * @param resolve Optional resolve callback
50
+ * @param reject Optional reject callback
51
+ * @returns Promise that resolves when the condition is met
52
+ */
53
+ then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
54
+ }
55
+ /**
56
+ * Create a conditional timer
57
+ * @param condition Condition to check
58
+ * @param options Timer options
59
+ * @returns Timer instance
60
+ */
61
+ declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
62
+ //#endregion
63
+ export { type When, when };
@@ -1,9 +1,10 @@
1
- import { MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN } from "./constants.js";
2
- import { getValidNumber, getValidTimeout } from "./get.js";
3
- import "./global.js";
4
- import { TimerTrace } from "./models.js";
5
- import { Timer } from "./timer.js";
1
+ import { MESSAGE_DESTROYED, MESSAGE_STARTED, TYPE_WHEN } from "./constants.mjs";
2
+ import { getValidNumber, getValidTimeout } from "./get.mjs";
3
+ import "./global.mjs";
4
+ import { TimerTrace } from "./models.mjs";
5
+ import { Timer } from "./timer.mjs";
6
6
  import { noop } from "@oscarpalmer/atoms/function";
7
+ //#region src/when.ts
7
8
  var When = class {
8
9
  state = {
9
10
  promise: void 0,
@@ -137,4 +138,5 @@ var When = class {
137
138
  function when(condition, options) {
138
139
  return new When(condition, options);
139
140
  }
141
+ //#endregion
140
142
  export { when };
@@ -0,0 +1,8 @@
1
+ import { Timer } from "./timer.mjs";
2
+ import { TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType } from "./models.mjs";
3
+
4
+ //#region src/work.d.ts
5
+ declare function stop(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
6
+ declare function work(type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
7
+ //#endregion
8
+ export { stop, work };
@@ -1,4 +1,5 @@
1
- import { TIMERS_ACTIVE, WORK_PAUSE, WORK_STOP } from "./constants.js";
1
+ import { TIMERS_ACTIVE, WORK_PAUSE, WORK_STOP } from "./constants.mjs";
2
+ //#region src/work.ts
2
3
  function finish(timer, state, options, success) {
3
4
  cancelAnimationFrame(state.frame);
4
5
  TIMERS_ACTIVE.delete(timer.instance);
@@ -78,4 +79,5 @@ function work(type, timer, state, options) {
78
79
  state.frame = requestAnimationFrame(runner);
79
80
  return timer.instance;
80
81
  }
82
+ //#endregion
81
83
  export { stop, work };
package/package.json CHANGED
@@ -1,75 +1,72 @@
1
1
  {
2
+ "name": "@oscarpalmer/timer",
3
+ "version": "0.42.0",
4
+ "description": "A better solution for timeout- and interval-based timers.",
5
+ "keywords": [
6
+ "requestAnimationFrame",
7
+ "setInterval",
8
+ "setTimeout",
9
+ "timer"
10
+ ],
11
+ "license": "MIT",
2
12
  "author": {
3
13
  "name": "Oscar Palmér",
4
14
  "url": "https://oscarpalmer.se"
5
15
  },
6
- "dependencies": {
7
- "@oscarpalmer/atoms": "^0.152"
8
- },
9
- "description": "A better solution for timeout- and interval-based timers.",
10
- "devDependencies": {
11
- "@types/node": "^25.3",
12
- "@vitest/coverage-istanbul": "^4",
13
- "dts-bundle-generator": "^9.5",
14
- "jsdom": "^28.1",
15
- "oxfmt": "^0.36",
16
- "oxlint": "^1.51",
17
- "rolldown": "1.0.0-rc.6",
18
- "tslib": "^2.8",
19
- "typescript": "^5.9",
20
- "vite": "8.0.0-beta.16",
21
- "vitest": "^4"
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/oscarpalmer/timer.git"
22
19
  },
20
+ "files": [
21
+ "dist",
22
+ "src"
23
+ ],
24
+ "type": "module",
25
+ "module": "dist/index.mjs",
26
+ "types": "dist/index.d.mts",
23
27
  "exports": {
24
28
  "./package.json": "./package.json",
25
29
  ".": {
26
- "types": "./types/index.d.ts",
27
- "default": "./dist/index.js"
30
+ "types": "./dist/index.d.mts",
31
+ "default": "./dist/index.mjs"
28
32
  },
29
33
  "./delay": {
30
- "types": "./types/delay.d.ts",
31
- "default": "./dist/delay.js"
34
+ "types": "./dist/delay.d.mts",
35
+ "default": "./dist/delay.mjs"
32
36
  },
33
37
  "./repeat": {
34
- "types": "./types/repeat.d.ts",
35
- "default": "./dist/repeat.js"
38
+ "types": "./dist/repeat.d.mts",
39
+ "default": "./dist/repeat.mjs"
36
40
  },
37
41
  "./wait": {
38
- "types": "./types/wait.d.ts",
39
- "default": "./dist/wait.js"
42
+ "types": "./dist/wait.d.mts",
43
+ "default": "./dist/wait.mjs"
40
44
  },
41
45
  "./when": {
42
- "types": "./types/when.d.ts",
43
- "default": "./dist/when.js"
46
+ "types": "./dist/when.d.mts",
47
+ "default": "./dist/when.mjs"
44
48
  }
45
49
  },
46
- "files": [
47
- "dist",
48
- "src",
49
- "types"
50
- ],
51
- "keywords": [
52
- "requestAnimationFrame",
53
- "setInterval",
54
- "setTimeout",
55
- "timer"
56
- ],
57
- "license": "MIT",
58
- "module": "dist/index.js",
59
- "name": "@oscarpalmer/timer",
60
- "repository": {
61
- "type": "git",
62
- "url": "git+https://github.com/oscarpalmer/timer.git"
63
- },
64
50
  "scripts": {
65
- "build": "npm run clean && npx vite build && npm run rolldown:build && npx tsc && npx dts-bundle-generator --config ./dts.config.ts --silent",
66
- "clean": "rm -rf ./dist && rm -rf ./types && rm -f ./tsconfig.tsbuildinfo",
67
- "rolldown:build": "npx rolldown -c",
68
- "rolldown:watch": "npx rolldown -c ./rolldown.config.js --watch",
69
- "test": "npx vitest --coverage",
51
+ "build": "npx vp pack && npm run tsdown:build",
52
+ "tsdown:build": "npx tsdown -c ./tsdown.config.ts",
53
+ "tsdown:watch": "npx tsdown -c ./tsdown.config.ts --watch",
54
+ "test": "npx vp test run --coverage",
55
+ "test:leak": "npx vp test run --detect-async-leaks --coverage",
70
56
  "watch": "npx vite build --watch"
71
57
  },
72
- "type": "module",
73
- "types": "types/index.d.ts",
74
- "version": "0.41.3"
58
+ "dependencies": {
59
+ "@oscarpalmer/atoms": "^0.165"
60
+ },
61
+ "devDependencies": {
62
+ "@types/node": "^25.5",
63
+ "@vitest/coverage-istanbul": "^4.1",
64
+ "jsdom": "^28.1",
65
+ "tsdown": "^0.21",
66
+ "typescript": "^5.9",
67
+ "vite": "npm:@voidzero-dev/vite-plus-core@latest",
68
+ "vite-plus": "latest",
69
+ "vitest": "npm:@voidzero-dev/vite-plus-test@latest"
70
+ },
71
+ "packageManager": "npm@11.11.1"
75
72
  }
package/src/delay.ts CHANGED
@@ -1 +1,2 @@
1
- export {delay, type PromiseOptions} from '@oscarpalmer/atoms/promise';
1
+ export {delay} from '@oscarpalmer/atoms/promise/delay';
2
+ export type {PromiseOptions} from '@oscarpalmer/atoms/promise/models';
package/src/get.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {GenericCallback} from '@oscarpalmer/atoms';
1
+ import type {GenericCallback} from '@oscarpalmer/atoms/models';
2
2
  import {noop} from '@oscarpalmer/atoms/function';
3
3
  import {DEFAULT_TIMEOUT} from './constants';
4
4
 
package/src/is.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {PlainObject} from '@oscarpalmer/atoms';
1
+ import type {PlainObject} from '@oscarpalmer/atoms/models';
2
2
  import {TYPE_REPEAT, TYPE_WAIT, TYPE_WHEN} from './constants';
3
3
  import type {Timer} from './timer';
4
4
  import type {When} from './when';
package/dist/delay.js DELETED
@@ -1,2 +0,0 @@
1
- import { delay } from "@oscarpalmer/atoms/promise";
2
- export { delay };
package/dist/index.js DELETED
@@ -1,7 +0,0 @@
1
- import { delay } from "./delay.js";
2
- import "./global.js";
3
- import { isRepeated, isTimer, isWaited, isWhen } from "./is.js";
4
- import { repeat } from "./repeat.js";
5
- import { wait } from "./wait.js";
6
- import { when } from "./when.js";
7
- export { delay, isRepeated, isTimer, isWaited, isWhen, repeat, wait, when };
@@ -1,31 +0,0 @@
1
- import type { TimerType, WorkHandlerType } from './models';
2
- import type { Timer } from './timer';
3
- /**
4
- * Buffer value to use when evaluating if a specific time is within a certain range
5
- */
6
- export declare const BUFFER_INTERVAL = 5;
7
- export declare const DEFAULT_TIMEOUT = 30000;
8
- /**
9
- * Message to show when a when-timer is destroyed
10
- */
11
- export declare const MESSAGE_DESTROYED = "Timer has already been destroyed";
12
- /**
13
- * Message to show when a when-timer is started
14
- */
15
- export declare const MESSAGE_STARTED = "Timer has already been started";
16
- /**
17
- * A set of all active timers
18
- */
19
- export declare const TIMERS_ACTIVE: Set<Timer>;
20
- /**
21
- * A set of timers that were paused due to the document being hidden
22
- */
23
- export declare const TIMERS_HIDDEN: Set<Timer>;
24
- export declare const TYPE_REPEAT: TimerType;
25
- export declare const TYPE_WAIT: TimerType;
26
- export declare const TYPE_WHEN: TimerType;
27
- export declare const WORK_CONTINUE: WorkHandlerType;
28
- export declare const WORK_PAUSE: WorkHandlerType;
29
- export declare const WORK_RESTART: WorkHandlerType;
30
- export declare const WORK_START: WorkHandlerType;
31
- export declare const WORK_STOP: WorkHandlerType;
package/types/delay.d.ts DELETED
@@ -1 +0,0 @@
1
- export { delay, type PromiseOptions } from '@oscarpalmer/atoms/promise';
package/types/get.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { GenericCallback } from '@oscarpalmer/atoms';
2
- export declare function getCallback(value: unknown): GenericCallback;
3
- export declare function getValidTimeout(value: unknown): number;
4
- export declare function getValidNumber(value: unknown, defaultValue?: number): number;
package/types/global.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import type { Timer } from './timer';
2
- declare global {
3
- var _oscarpalmer_timer_debug: boolean | undefined;
4
- var _oscarpalmer_timers: Timer[] | undefined;
5
- }