@oscarpalmer/timer 0.35.0 → 0.37.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/delay.cjs CHANGED
@@ -6,7 +6,7 @@ function delay(time) {
6
6
  let start;
7
7
  function step(now) {
8
8
  start ??= now;
9
- if (interval === require_constants.milliseconds || now - start >= interval - require_constants.intervalBuffer) resolve();
9
+ if (interval === require_constants.milliseconds || now - start >= interval - 5) resolve();
10
10
  else requestAnimationFrame(step);
11
11
  }
12
12
  requestAnimationFrame(step);
package/dist/delay.js CHANGED
@@ -6,7 +6,7 @@ function delay(time) {
6
6
  let start;
7
7
  function step(now) {
8
8
  start ??= now;
9
- if (interval === milliseconds || now - start >= interval - intervalBuffer) resolve();
9
+ if (interval === milliseconds || now - start >= interval - 5) resolve();
10
10
  else requestAnimationFrame(step);
11
11
  }
12
12
  requestAnimationFrame(step);
package/dist/get.cjs CHANGED
@@ -1,10 +1,17 @@
1
1
  const require_rolldown_runtime = require("./rolldown_runtime.cjs");
2
- const __oscarpalmer_atoms_function = require_rolldown_runtime.__toESM(require("@oscarpalmer/atoms/function"));
2
+ const require_constants = require("./constants.cjs");
3
+ let __oscarpalmer_atoms_function = require("@oscarpalmer/atoms/function");
4
+ __oscarpalmer_atoms_function = require_rolldown_runtime.__toESM(__oscarpalmer_atoms_function);
3
5
  function getCallback(value) {
4
6
  return typeof value === "function" ? value : __oscarpalmer_atoms_function.noop;
5
7
  }
8
+ function getValidTimeout(value) {
9
+ return typeof value === "number" && value > 0 ? value : require_constants.defaultTimeout;
10
+ }
6
11
  function getValidNumber(value, defaultValue) {
7
- return typeof value === "number" ? value > 0 ? value : 0 : defaultValue ?? 0;
12
+ const actualDefault = defaultValue ?? 0;
13
+ return typeof value === "number" && value > actualDefault ? value : actualDefault;
8
14
  }
9
15
  exports.getCallback = getCallback;
10
16
  exports.getValidNumber = getValidNumber;
17
+ exports.getValidTimeout = getValidTimeout;
package/dist/get.js CHANGED
@@ -1,8 +1,13 @@
1
+ import { defaultTimeout } from "./constants.js";
1
2
  import { noop } from "@oscarpalmer/atoms/function";
2
3
  function getCallback(value) {
3
4
  return typeof value === "function" ? value : noop;
4
5
  }
6
+ function getValidTimeout(value) {
7
+ return typeof value === "number" && value > 0 ? value : defaultTimeout;
8
+ }
5
9
  function getValidNumber(value, defaultValue) {
6
- return typeof value === "number" ? value > 0 ? value : 0 : defaultValue ?? 0;
10
+ const actualDefault = defaultValue ?? 0;
11
+ return typeof value === "number" && value > actualDefault ? value : actualDefault;
7
12
  }
8
- export { getCallback, getValidNumber };
13
+ export { getCallback, getValidNumber, getValidTimeout };
package/dist/global.cjs CHANGED
@@ -2,6 +2,7 @@ const require_constants = require("./constants.cjs");
2
2
  if (globalThis._oscarpalmer_timers == null) Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
3
3
  return globalThis._oscarpalmer_timer_debug ? [...require_constants.activeTimers] : [];
4
4
  } });
5
+ /* istanbul ignore next */
5
6
  document.addEventListener("visibilitychange", () => {
6
7
  const from = document.hidden ? require_constants.activeTimers : require_constants.hiddenTimers;
7
8
  const method = document.hidden ? require_constants.WORK_PAUSE : require_constants.WORK_CONTINUE;
package/dist/global.js CHANGED
@@ -2,6 +2,7 @@ import { WORK_CONTINUE, WORK_PAUSE, activeTimers, hiddenTimers } from "./constan
2
2
  if (globalThis._oscarpalmer_timers == null) Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
3
3
  return globalThis._oscarpalmer_timer_debug ? [...activeTimers] : [];
4
4
  } });
5
+ /* istanbul ignore next */
5
6
  document.addEventListener("visibilitychange", () => {
6
7
  const from = document.hidden ? activeTimers : hiddenTimers;
7
8
  const method = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
package/dist/is.cjs CHANGED
@@ -12,7 +12,7 @@ function isWaited(value) {
12
12
  return is([require_constants.TYPE_WAIT], value);
13
13
  }
14
14
  function isWhen(value) {
15
- return is([require_constants.TYPE_WHEN], value) && typeof value.then === "function";
15
+ return is(["when"], value) && typeof value.then === "function";
16
16
  }
17
17
  exports.isRepeated = isRepeated;
18
18
  exports.isTimer = isTimer;
package/dist/is.js CHANGED
@@ -12,6 +12,6 @@ function isWaited(value) {
12
12
  return is([TYPE_WAIT], value);
13
13
  }
14
14
  function isWhen(value) {
15
- return is([TYPE_WHEN], value) && typeof value.then === "function";
15
+ return is(["when"], value) && typeof value.then === "function";
16
16
  }
17
17
  export { isRepeated, isTimer, isWaited, isWhen };
package/dist/repeat.cjs CHANGED
@@ -9,10 +9,10 @@ function repeat(callback, options) {
9
9
  trace: new require_models.TimerTrace().stack
10
10
  }, {
11
11
  onAfter: require_get.getCallback(options?.onAfter),
12
- onError: void 0,
12
+ onError: require_get.getCallback(options?.onTimeout),
13
13
  count: require_get.getValidNumber(options?.count),
14
14
  interval: require_get.getValidNumber(options?.interval, require_constants.milliseconds),
15
- timeout: 0
15
+ timeout: require_get.getValidNumber(options?.timeout)
16
16
  }, true);
17
17
  }
18
18
  exports.repeat = repeat;
package/dist/repeat.js CHANGED
@@ -9,10 +9,10 @@ function repeat(callback, options) {
9
9
  trace: new TimerTrace().stack
10
10
  }, {
11
11
  onAfter: getCallback(options?.onAfter),
12
- onError: void 0,
12
+ onError: getCallback(options?.onTimeout),
13
13
  count: getValidNumber(options?.count),
14
14
  interval: getValidNumber(options?.interval, milliseconds),
15
- timeout: 0
15
+ timeout: getValidNumber(options?.timeout)
16
16
  }, true);
17
17
  }
18
18
  export { repeat };
package/dist/timer.cjs CHANGED
@@ -1,7 +1,8 @@
1
1
  const require_rolldown_runtime = require("./rolldown_runtime.cjs");
2
2
  const require_constants = require("./constants.cjs");
3
3
  const require_work = require("./work.cjs");
4
- const __oscarpalmer_atoms_function = require_rolldown_runtime.__toESM(require("@oscarpalmer/atoms/function"));
4
+ let __oscarpalmer_atoms_function = require("@oscarpalmer/atoms/function");
5
+ __oscarpalmer_atoms_function = require_rolldown_runtime.__toESM(__oscarpalmer_atoms_function);
5
6
  var Timer = class {
6
7
  state;
7
8
  get active() {
@@ -68,6 +68,7 @@ if (globalThis._oscarpalmer_timers == null) {
68
68
  },
69
69
  });
70
70
  }
71
+ /* istanbul ignore next */
71
72
  document.addEventListener('visibilitychange', () => {
72
73
  const from = document.hidden ? activeTimers : hiddenTimers;
73
74
  const method = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
@@ -85,7 +86,8 @@ function calculate() {
85
86
  let last;
86
87
  function step(now) {
87
88
  if (last != null) values.push(now - last);
88
- if (last = now, values.length >= 10) {
89
+ last = now;
90
+ if (values.length >= 10) {
89
91
  const median = values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - 4);
90
92
  resolve(median);
91
93
  } else requestAnimationFrame(step);
@@ -100,12 +102,14 @@ calculate().then((value) => {
100
102
  function getCallback(value) {
101
103
  return typeof value === 'function' ? value : noop;
102
104
  }
105
+ function getValidTimeout(value) {
106
+ return typeof value === 'number' && value > 0 ? value : defaultTimeout;
107
+ }
103
108
  function getValidNumber(value, defaultValue) {
104
- return typeof value === 'number'
105
- ? value > 0
106
- ? value
107
- : 0
108
- : (defaultValue ?? 0);
109
+ const actualDefault = defaultValue ?? 0;
110
+ return typeof value === 'number' && value > actualDefault
111
+ ? value
112
+ : actualDefault;
109
113
  }
110
114
 
111
115
  /**
@@ -370,10 +374,10 @@ function repeat(callback, options) {
370
374
  trace: new TimerTrace().stack,
371
375
  }, {
372
376
  onAfter: getCallback(options?.onAfter),
373
- onError: undefined,
377
+ onError: getCallback(options?.onTimeout),
374
378
  count: getValidNumber(options?.count),
375
379
  interval: getValidNumber(options?.interval, milliseconds),
376
- timeout: 0,
380
+ timeout: getValidNumber(options?.timeout),
377
381
  }, true);
378
382
  }
379
383
 
@@ -466,7 +470,7 @@ class When {
466
470
  },
467
471
  count: getValidNumber(options?.count),
468
472
  interval: getValidNumber(options?.interval, milliseconds),
469
- timeout: getValidNumber(options?.timeout, defaultTimeout),
473
+ timeout: getValidTimeout(options?.timeout),
470
474
  }, false);
471
475
  }
472
476
  /**
package/dist/when.cjs CHANGED
@@ -4,7 +4,8 @@ const require_get = require("./get.cjs");
4
4
  require("./global.cjs");
5
5
  const require_models = require("./models.cjs");
6
6
  const require_timer = require("./timer.cjs");
7
- const __oscarpalmer_atoms_function = require_rolldown_runtime.__toESM(require("@oscarpalmer/atoms/function"));
7
+ let __oscarpalmer_atoms_function = require("@oscarpalmer/atoms/function");
8
+ __oscarpalmer_atoms_function = require_rolldown_runtime.__toESM(__oscarpalmer_atoms_function);
8
9
  var When = class {
9
10
  $timer = require_constants.TYPE_WHEN;
10
11
  state = {
@@ -57,7 +58,7 @@ var When = class {
57
58
  },
58
59
  count: require_get.getValidNumber(options?.count),
59
60
  interval: require_get.getValidNumber(options?.interval, require_constants.milliseconds),
60
- timeout: require_get.getValidNumber(options?.timeout, require_constants.defaultTimeout)
61
+ timeout: require_get.getValidTimeout(options?.timeout)
61
62
  }, false);
62
63
  }
63
64
  continue() {
package/dist/when.js CHANGED
@@ -1,5 +1,5 @@
1
- import { TYPE_WHEN, defaultTimeout, destroyedMessage, milliseconds, startedMessage } from "./constants.js";
2
- import { getValidNumber } from "./get.js";
1
+ import { TYPE_WHEN, destroyedMessage, milliseconds, startedMessage } from "./constants.js";
2
+ import { getValidNumber, getValidTimeout } from "./get.js";
3
3
  import "./global.js";
4
4
  import { TimerTrace } from "./models.js";
5
5
  import { Timer } from "./timer.js";
@@ -56,7 +56,7 @@ var When = class {
56
56
  },
57
57
  count: getValidNumber(options?.count),
58
58
  interval: getValidNumber(options?.interval, milliseconds),
59
- timeout: getValidNumber(options?.timeout, defaultTimeout)
59
+ timeout: getValidTimeout(options?.timeout)
60
60
  }, false);
61
61
  }
62
62
  continue() {
package/dist/work.cjs CHANGED
@@ -5,13 +5,13 @@ function finish(timer, state, options, success) {
5
5
  state.active = false;
6
6
  state.elapsed = 0;
7
7
  state.frame = void 0;
8
- if (timer.type === require_constants.TYPE_WAIT) state.callback();
8
+ if (timer.type === "wait") state.callback();
9
9
  else options.onAfter?.(success);
10
10
  }
11
11
  function ignore(type, state) {
12
12
  if (state.destroyed) return type !== require_constants.WORK_STOP;
13
- if (state.paused) return type === require_constants.WORK_PAUSE || type === require_constants.WORK_START;
14
- return state.active && type === require_constants.WORK_START;
13
+ if (state.paused) return type === "pause" || type === "start";
14
+ return state.active && type === "start";
15
15
  }
16
16
  function pause(timer, state) {
17
17
  cancelAnimationFrame(state.frame);
@@ -34,7 +34,7 @@ function run(timer, state, options) {
34
34
  finish(timer, state, options, false);
35
35
  return;
36
36
  }
37
- if (options.interval === require_constants.milliseconds || state.elapsed >= options.interval - require_constants.intervalBuffer) {
37
+ if (options.interval === require_constants.milliseconds || state.elapsed >= options.interval - 5) {
38
38
  if (options.count > -1) state.callback(state.index);
39
39
  start = now;
40
40
  state.elapsed = 0;
@@ -48,7 +48,7 @@ function run(timer, state, options) {
48
48
  };
49
49
  }
50
50
  function setState(type, state) {
51
- const pausable = type === require_constants.WORK_CONTINUE || type === require_constants.WORK_PAUSE;
51
+ const pausable = type === "continue" || type === "pause";
52
52
  state.elapsed = pausable ? state.elapsed : 0;
53
53
  state.index = pausable ? state.index : 0;
54
54
  state.total = pausable ? state.total : 0;
@@ -65,8 +65,8 @@ function stop(timer, state, options) {
65
65
  function work(type, timer, state, options) {
66
66
  if (ignore(type, state)) return timer.instance;
67
67
  setState(type, state);
68
- if (type === require_constants.WORK_STOP) return stop(timer, state, options);
69
- if (type === require_constants.WORK_PAUSE || type === require_constants.WORK_RESTART) {
68
+ if (type === "stop") return stop(timer, state, options);
69
+ if (type === "pause" || type === "restart") {
70
70
  cancelAnimationFrame(state.frame);
71
71
  state.frame = void 0;
72
72
  }
package/dist/work.js CHANGED
@@ -5,13 +5,13 @@ function finish(timer, state, options, success) {
5
5
  state.active = false;
6
6
  state.elapsed = 0;
7
7
  state.frame = void 0;
8
- if (timer.type === TYPE_WAIT) state.callback();
8
+ if (timer.type === "wait") state.callback();
9
9
  else options.onAfter?.(success);
10
10
  }
11
11
  function ignore(type, state) {
12
12
  if (state.destroyed) return type !== WORK_STOP;
13
- if (state.paused) return type === WORK_PAUSE || type === WORK_START;
14
- return state.active && type === WORK_START;
13
+ if (state.paused) return type === "pause" || type === "start";
14
+ return state.active && type === "start";
15
15
  }
16
16
  function pause(timer, state) {
17
17
  cancelAnimationFrame(state.frame);
@@ -34,7 +34,7 @@ function run(timer, state, options) {
34
34
  finish(timer, state, options, false);
35
35
  return;
36
36
  }
37
- if (options.interval === milliseconds || state.elapsed >= options.interval - intervalBuffer) {
37
+ if (options.interval === milliseconds || state.elapsed >= options.interval - 5) {
38
38
  if (options.count > -1) state.callback(state.index);
39
39
  start = now;
40
40
  state.elapsed = 0;
@@ -48,7 +48,7 @@ function run(timer, state, options) {
48
48
  };
49
49
  }
50
50
  function setState(type, state) {
51
- const pausable = type === WORK_CONTINUE || type === WORK_PAUSE;
51
+ const pausable = type === "continue" || type === "pause";
52
52
  state.elapsed = pausable ? state.elapsed : 0;
53
53
  state.index = pausable ? state.index : 0;
54
54
  state.total = pausable ? state.total : 0;
@@ -65,8 +65,8 @@ function stop(timer, state, options) {
65
65
  function work(type, timer, state, options) {
66
66
  if (ignore(type, state)) return timer.instance;
67
67
  setState(type, state);
68
- if (type === WORK_STOP) return stop(timer, state, options);
69
- if (type === WORK_PAUSE || type === WORK_RESTART) {
68
+ if (type === "stop") return stop(timer, state, options);
69
+ if (type === "pause" || type === "restart") {
70
70
  cancelAnimationFrame(state.frame);
71
71
  state.frame = void 0;
72
72
  }
package/package.json CHANGED
@@ -4,80 +4,57 @@
4
4
  "url": "https://oscarpalmer.se"
5
5
  },
6
6
  "dependencies": {
7
- "@oscarpalmer/atoms": "^0.106"
7
+ "@oscarpalmer/atoms": "^0.109"
8
8
  },
9
9
  "description": "A better solution for timeout- and interval-based timers.",
10
10
  "devDependencies": {
11
11
  "@biomejs/biome": "^2.2",
12
12
  "@rollup/plugin-node-resolve": "^16",
13
13
  "@rollup/plugin-typescript": "^12.1",
14
- "@types/node": "^24.3",
14
+ "@types/node": "^24.5",
15
15
  "@vitest/coverage-istanbul": "^3.2",
16
- "dts-bundle-generator": "^9.5",
17
- "glob": "^11",
18
- "jsdom": "^26.1",
19
- "rollup": "^4.49",
16
+ "jsdom": "^27",
17
+ "rollup": "^4.50",
20
18
  "tslib": "^2.8",
21
19
  "typescript": "^5.9",
22
20
  "vite": "npm:rolldown-vite@latest",
23
21
  "vitest": "^3.2"
24
22
  },
25
23
  "exports": {
24
+ "./package.json": "./package.json",
26
25
  ".": {
27
- "import": {
28
- "types": "./types/index.d.ts",
29
- "default": "./dist/index.js"
30
- },
31
- "require": {
32
- "types": "./types/index.d.cts",
33
- "default": "./dist/index.cjs"
34
- }
26
+ "types": "./types/index.d.ts",
27
+ "default": "./dist/index.js"
35
28
  },
36
29
  "./delay": {
37
- "import": {
38
- "types": "./types/delay.d.ts",
39
- "default": "./dist/delay.js"
40
- },
41
- "require": {
42
- "types": "./types/delay.d.cts",
43
- "default": "./dist/delay.cjs"
44
- }
30
+ "types": "./types/delay.d.ts",
31
+ "default": "./dist/delay.js"
45
32
  },
46
33
  "./repeat": {
47
- "import": {
48
- "types": "./types/repeat.d.ts",
49
- "default": "./dist/repeat.js"
50
- },
51
- "require": {
52
- "types": "./types/repeat.d.cts",
53
- "default": "./dist/repeat.cjs"
54
- }
34
+ "types": "./types/repeat.d.ts",
35
+ "default": "./dist/repeat.js"
55
36
  },
56
37
  "./wait": {
57
- "import": {
58
- "types": "./types/wait.d.ts",
59
- "default": "./dist/wait.js"
60
- },
61
- "require": {
62
- "types": "./types/wait.d.cts",
63
- "default": "./dist/wait.cjs"
64
- }
38
+ "types": "./types/wait.d.ts",
39
+ "default": "./dist/wait.js"
65
40
  },
66
41
  "./when": {
67
- "import": {
68
- "types": "./types/when.d.ts",
69
- "default": "./dist/when.js"
70
- },
71
- "require": {
72
- "types": "./types/when.d.cts",
73
- "default": "./dist/when.cjs"
74
- }
42
+ "types": "./types/when.d.ts",
43
+ "default": "./dist/when.js"
75
44
  }
76
45
  },
77
- "files": ["dist", "src", "types"],
78
- "keywords": ["timer", "setTimeout", "setInterval", "requestAnimationFrame"],
46
+ "files": [
47
+ "dist",
48
+ "src",
49
+ "types"
50
+ ],
51
+ "keywords": [
52
+ "timer",
53
+ "setTimeout",
54
+ "setInterval",
55
+ "requestAnimationFrame"
56
+ ],
79
57
  "license": "MIT",
80
- "main": "dist/index.cjs",
81
58
  "module": "dist/index.js",
82
59
  "name": "@oscarpalmer/timer",
83
60
  "repository": {
@@ -85,15 +62,16 @@
85
62
  "url": "git+https://github.com/oscarpalmer/timer.git"
86
63
  },
87
64
  "scripts": {
88
- "build": "npm run clean && npm run build:js && npm run rollup && npm run types",
65
+ "build": "npm run clean && npm run build:js && npm run rollup:build && npx tsc",
89
66
  "build:js": "npx vite build",
90
67
  "clean": "rm -rf ./dist && rm -rf ./types && rm -f ./tsconfig.tsbuildinfo",
91
- "rollup": "npx rollup -c",
68
+ "rollup:build": "npx rollup -c",
69
+ "rollup:watch": "npx rollup -c --watch",
92
70
  "test": "npx vitest --coverage",
93
71
  "types": "npx tsc && npx dts-bundle-generator --config ./dts.config.cts --silent",
94
72
  "watch": "npx vite build --watch"
95
73
  },
96
74
  "type": "module",
97
- "types": "types/index.d.cts",
98
- "version": "0.35.0"
99
- }
75
+ "types": "types/index.d.ts",
76
+ "version": "0.37.0"
77
+ }
package/src/get.ts CHANGED
@@ -1,14 +1,19 @@
1
+ import type {GenericCallback} from '@oscarpalmer/atoms';
1
2
  import {noop} from '@oscarpalmer/atoms/function';
2
- import type {GenericCallback} from '@oscarpalmer/atoms/models';
3
+ import {defaultTimeout} from './constants';
3
4
 
4
5
  export function getCallback(value: unknown): GenericCallback {
5
6
  return typeof value === 'function' ? (value as GenericCallback) : noop;
6
7
  }
7
8
 
9
+ export function getValidTimeout(value: unknown): number {
10
+ return typeof value === 'number' && value > 0 ? value : defaultTimeout;
11
+ }
12
+
8
13
  export function getValidNumber(value: unknown, defaultValue?: number): number {
9
- return typeof value === 'number'
10
- ? value > 0
11
- ? value
12
- : 0
13
- : (defaultValue ?? 0);
14
+ const actualDefault = defaultValue ?? 0;
15
+
16
+ return typeof value === 'number' && value > actualDefault
17
+ ? value
18
+ : actualDefault;
14
19
  }
package/src/global.ts CHANGED
@@ -19,6 +19,7 @@ if (globalThis._oscarpalmer_timers == null) {
19
19
  });
20
20
  }
21
21
 
22
+ /* istanbul ignore next */
22
23
  document.addEventListener('visibilitychange', () => {
23
24
  const from = document.hidden ? activeTimers : hiddenTimers;
24
25
  const method = document.hidden ? WORK_PAUSE : WORK_CONTINUE;
package/src/is.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {PlainObject} from '@oscarpalmer/atoms/models';
1
+ import type {PlainObject} from '@oscarpalmer/atoms';
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/src/models.ts CHANGED
@@ -4,19 +4,27 @@ import type {Timer} from './timer';
4
4
  * Options for a repeating timer
5
5
  */
6
6
  export type RepeatOptions = {
7
- /**
8
- * Callback to be called when the timer has stopped, either manually or by completing its work
9
- */
10
- onAfter: ((finished: boolean) => void) | undefined;
11
- /**
12
- * How many times the timer should repeat
13
- */
14
- count: number;
15
- /**
16
- * The interval between each repeat
17
- */
18
- interval: number;
19
- };
7
+ /**
8
+ * Callback to be called when the timer has stopped, either manually or by completing its work
9
+ */
10
+ onAfter: (finished: boolean) => void;
11
+ /**
12
+ * Callback to be called after the timer has timed out
13
+ */
14
+ onTimeout: () => void;
15
+ /**
16
+ * How many times the timer should repeat
17
+ */
18
+ count: number;
19
+ /**
20
+ * The interval between each repeat
21
+ */
22
+ interval: number;
23
+ /**
24
+ * The timeout for the timer _(any value above `0` will enable the timeout)_
25
+ */
26
+ timeout: number;
27
+ };
20
28
 
21
29
  export type TimerOptions = {
22
30
  onAfter: ((finished: boolean) => void) | undefined;
@@ -52,19 +60,19 @@ export type TimerType = 'repeat' | 'wait' | 'when';
52
60
  * Options for a conditional timer
53
61
  */
54
62
  export 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
65
- */
66
- timeout: number;
67
- };
63
+ /**
64
+ * How many times the timer should check the condition
65
+ */
66
+ count: number;
67
+ /**
68
+ * Then interval between each condtional check
69
+ */
70
+ interval: number;
71
+ /**
72
+ * The timeout for the timer _(any value above `0` will enable the timeout)_
73
+ */
74
+ timeout: number;
75
+ };
68
76
 
69
77
  export type WhenState = {
70
78
  promise: Promise<void>;
package/src/repeat.ts CHANGED
@@ -19,10 +19,10 @@ export function repeat(
19
19
  },
20
20
  {
21
21
  onAfter: getCallback(options?.onAfter),
22
- onError: undefined,
22
+ onError: getCallback(options?.onTimeout),
23
23
  count: getValidNumber(options?.count),
24
24
  interval: getValidNumber(options?.interval, milliseconds),
25
- timeout: 0,
25
+ timeout: getValidNumber(options?.timeout),
26
26
  },
27
27
  true,
28
28
  );
package/src/when.ts CHANGED
@@ -1,12 +1,11 @@
1
1
  import {noop} from '@oscarpalmer/atoms/function';
2
2
  import {
3
- defaultTimeout,
4
3
  destroyedMessage,
5
4
  milliseconds,
6
5
  startedMessage,
7
6
  TYPE_WHEN,
8
7
  } from './constants';
9
- import {getValidNumber} from './get';
8
+ import {getValidNumber, getValidTimeout} from './get';
10
9
  import './global';
11
10
  import {TimerTrace, type WhenOptions, type WhenState} from './models';
12
11
  import {Timer} from './timer';
@@ -95,7 +94,7 @@ class When {
95
94
  },
96
95
  count: getValidNumber(options?.count),
97
96
  interval: getValidNumber(options?.interval, milliseconds),
98
- timeout: getValidNumber(options?.timeout, defaultTimeout),
97
+ timeout: getValidTimeout(options?.timeout),
99
98
  },
100
99
  false,
101
100
  );
package/types/get.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- import type { GenericCallback } from '@oscarpalmer/atoms/models';
1
+ import type { GenericCallback } from '@oscarpalmer/atoms';
2
2
  export declare function getCallback(value: unknown): GenericCallback;
3
+ export declare function getValidTimeout(value: unknown): number;
3
4
  export declare function getValidNumber(value: unknown, defaultValue?: number): number;
package/types/models.d.ts CHANGED
@@ -6,7 +6,11 @@ export type RepeatOptions = {
6
6
  /**
7
7
  * Callback to be called when the timer has stopped, either manually or by completing its work
8
8
  */
9
- onAfter: ((finished: boolean) => void) | undefined;
9
+ onAfter: (finished: boolean) => void;
10
+ /**
11
+ * Callback to be called after the timer has timed out
12
+ */
13
+ onTimeout: () => void;
10
14
  /**
11
15
  * How many times the timer should repeat
12
16
  */
@@ -15,6 +19,10 @@ export type RepeatOptions = {
15
19
  * The interval between each repeat
16
20
  */
17
21
  interval: number;
22
+ /**
23
+ * The timeout for the timer _(any value above `0` will enable the timeout)_
24
+ */
25
+ timeout: number;
18
26
  };
19
27
  export type TimerOptions = {
20
28
  onAfter: ((finished: boolean) => void) | undefined;
@@ -51,7 +59,7 @@ export type WhenOptions = {
51
59
  */
52
60
  interval: number;
53
61
  /**
54
- * The timeout for the timer
62
+ * The timeout for the timer _(any value above `0` will enable the timeout)_
55
63
  */
56
64
  timeout: number;
57
65
  };