@oscarpalmer/timer 0.37.0 → 0.38.1

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/src/work.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
- activeTimers,
3
- intervalBuffer,
4
- milliseconds,
2
+ TIMERS_ACTIVE,
3
+ BUFFER_INTERVAL,
4
+ MILLISECONDS,
5
5
  TYPE_WAIT,
6
6
  WORK_CONTINUE,
7
7
  WORK_PAUSE,
@@ -9,12 +9,7 @@ import {
9
9
  WORK_START,
10
10
  WORK_STOP,
11
11
  } from './constants';
12
- import type {
13
- TimerOptions,
14
- TimerState,
15
- WorkHandlerTimer,
16
- WorkHandlerType,
17
- } from './models';
12
+ import type {TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType} from './models';
18
13
  import type {Timer} from './timer';
19
14
 
20
15
  function finish(
@@ -25,7 +20,7 @@ function finish(
25
20
  ): void {
26
21
  cancelAnimationFrame(state.frame as never);
27
22
 
28
- activeTimers.delete(timer.instance);
23
+ TIMERS_ACTIVE.delete(timer.instance);
29
24
 
30
25
  state.active = false;
31
26
  state.elapsed = 0;
@@ -89,10 +84,7 @@ function run(
89
84
  return;
90
85
  }
91
86
 
92
- if (
93
- options.interval === milliseconds ||
94
- state.elapsed >= options.interval - intervalBuffer
95
- ) {
87
+ if (options.interval === MILLISECONDS || state.elapsed >= options.interval - BUFFER_INTERVAL) {
96
88
  if (options.count > -1) {
97
89
  (state.callback as (index: number) => void)(state.index);
98
90
  }
@@ -102,10 +94,7 @@ function run(
102
94
  state.elapsed = 0;
103
95
  state.index += 1;
104
96
 
105
- if (
106
- options.count === -1 ||
107
- (options.count > 0 && state.index >= options.count)
108
- ) {
97
+ if (options.count === -1 || (options.count > 0 && state.index >= options.count)) {
109
98
  finish(timer, state, options, true);
110
99
 
111
100
  return;
@@ -124,23 +113,19 @@ function setState(type: WorkHandlerType, state: TimerState): void {
124
113
  state.total = pausable ? state.total : 0;
125
114
  }
126
115
 
127
- export function stop(
128
- timer: WorkHandlerTimer,
129
- state: TimerState,
130
- options: TimerOptions,
131
- ): Timer {
132
- cancelAnimationFrame(state.frame as never);
116
+ export function stop(timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer {
117
+ cancelAnimationFrame(state.frame as never);
133
118
 
134
- activeTimers.delete(timer.instance);
119
+ TIMERS_ACTIVE.delete(timer.instance);
135
120
 
136
- options.onAfter?.(false);
121
+ options.onAfter?.(false);
137
122
 
138
- state.active = !stop;
139
- state.frame = undefined;
140
- state.paused = false;
123
+ state.active = !stop;
124
+ state.frame = undefined;
125
+ state.paused = false;
141
126
 
142
- return timer.instance;
143
- }
127
+ return timer.instance;
128
+ }
144
129
 
145
130
  export function work(
146
131
  type: WorkHandlerType,
@@ -171,7 +156,7 @@ export function work(
171
156
  return pause(timer, state);
172
157
  }
173
158
 
174
- activeTimers.add(timer.instance);
159
+ TIMERS_ACTIVE.add(timer.instance);
175
160
 
176
161
  const runner = run(timer, state, options);
177
162
 
@@ -1,26 +1,26 @@
1
1
  import type { TimerType, WorkHandlerType } from './models';
2
2
  import type { Timer } from './timer';
3
- export declare const defaultTimeout = 30000;
4
- /**
5
- * A set of all active timers
6
- */
7
- export declare const activeTimers: Set<Timer>;
8
3
  /**
9
4
  * Buffer value to use when evaluating if a specific time is within a certain range
10
5
  */
11
- export declare const intervalBuffer = 5;
6
+ export declare const BUFFER_INTERVAL = 5;
7
+ export declare const DEFAULT_TIMEOUT = 30000;
12
8
  /**
13
9
  * Message to show when a when-timer is destroyed
14
10
  */
15
- export declare const destroyedMessage = "Timer has already been destroyed";
11
+ export declare const MESSAGE_DESTROYED = "Timer has already been destroyed";
16
12
  /**
17
- * A set of timers that were paused due to the document being hidden
13
+ * Message to show when a when-timer is started
18
14
  */
19
- export declare const hiddenTimers: Set<Timer>;
15
+ export declare const MESSAGE_STARTED = "Timer has already been started";
20
16
  /**
21
- * Message to show when a when-timer is started
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
22
  */
23
- export declare const startedMessage = "Timer has already been started";
23
+ export declare const TIMERS_HIDDEN: Set<Timer>;
24
24
  export declare const TYPE_REPEAT: TimerType;
25
25
  export declare const TYPE_WAIT: TimerType;
26
26
  export declare const TYPE_WHEN: TimerType;
@@ -32,4 +32,4 @@ export declare const WORK_STOP: WorkHandlerType;
32
32
  /**
33
33
  * A calculated average of the refresh rate of the display _(in milliseconds)_
34
34
  */
35
- export declare let milliseconds: number;
35
+ export declare let MILLISECONDS: number;
package/types/delay.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  /**
2
- * Create a delayed promise that resolves after a certain amount of time _(in milliseconds; defaults to screen refresh rate)_
2
+ * Create a delayed promise that resolves after a certain amount of time
3
+ * @param time How long to wait for _(in milliseconds; defaults to screen refresh rate)_
4
+ * @returns A promise that resolves after the delay
3
5
  */
4
6
  export declare function delay(time?: number): Promise<void>;
package/types/is.d.ts CHANGED
@@ -2,17 +2,25 @@ import type { Timer } from './timer';
2
2
  import type { When } from './when';
3
3
  /**
4
4
  * Is the value a repeating timer?
5
+ * @param value Value to check
6
+ * @returns `true` if the value is a repeating timer
5
7
  */
6
8
  export declare function isRepeated(value: unknown): value is Timer;
7
9
  /**
8
10
  * Is the value a timer?
11
+ * @param value Value to check
12
+ * @returns `true` if the value is a timer
9
13
  */
10
14
  export declare function isTimer(value: unknown): value is Timer;
11
15
  /**
12
16
  * Is the value a waiting timer?
17
+ * @param value Value to check
18
+ * @returns `true` if the value is a waiting timer
13
19
  */
14
20
  export declare function isWaited(value: unknown): value is Timer;
15
21
  /**
16
22
  * Is the value a conditional timer?
23
+ * @param value Value to check
24
+ * @returns `true` if the value is a conditional timer
17
25
  */
18
26
  export declare function isWhen(value: unknown): value is When;
package/types/repeat.d.ts CHANGED
@@ -3,6 +3,9 @@ import { type RepeatOptions } from './models';
3
3
  import { Timer } from './timer';
4
4
  /**
5
5
  * Create a repeating timer
6
+ * @param callback Callback to run on each interval
7
+ * @param options Timer options
8
+ * @returns Timer instance
6
9
  */
7
10
  export declare function repeat(callback: (index: number) => void, options?: Partial<RepeatOptions>): Timer;
8
11
  export type { RepeatOptions, Timer };
package/types/when.d.ts CHANGED
@@ -32,17 +32,31 @@ declare class When {
32
32
  * Pauses the timer _(if it was running)_
33
33
  */
34
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>;
35
42
  /**
36
43
  * Stops the timer _(if it was running)_
37
44
  */
38
45
  stop(): When;
39
46
  /**
40
- * Starts the timer and returns a promise that resolves when the condition is met
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
41
52
  */
42
53
  then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
43
54
  }
44
55
  /**
45
56
  * Create a conditional timer
57
+ * @param condition Condition to check
58
+ * @param options Timer options
59
+ * @returns Timer instance
46
60
  */
47
61
  export declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
48
62
  export type { When };
@@ -1,53 +0,0 @@
1
- function calculate() {
2
- return new Promise((resolve) => {
3
- const values = [];
4
- let last;
5
- function step(now) {
6
- if (last != null) values.push(now - last);
7
- last = now;
8
- if (values.length >= 10) {
9
- const median = values.sort().slice(2, -2).reduce((first, second) => first + second, 0) / (values.length - 4);
10
- resolve(median);
11
- } else requestAnimationFrame(step);
12
- }
13
- requestAnimationFrame(step);
14
- });
15
- }
16
- const defaultTimeout = 3e4;
17
- const activeTimers = /* @__PURE__ */ new Set();
18
- const intervalBuffer = 5;
19
- const destroyedMessage = "Timer has already been destroyed";
20
- const hiddenTimers = /* @__PURE__ */ new Set();
21
- const startedMessage = "Timer has already been started";
22
- const TYPE_REPEAT = "repeat";
23
- const TYPE_WAIT = "wait";
24
- const TYPE_WHEN = "when";
25
- const WORK_CONTINUE = "continue";
26
- const WORK_PAUSE = "pause";
27
- const WORK_RESTART = "restart";
28
- const WORK_START = "start";
29
- const WORK_STOP = "stop";
30
- let milliseconds = 1e3 / 60;
31
- calculate().then((value) => {
32
- milliseconds = value;
33
- });
34
- exports.TYPE_REPEAT = TYPE_REPEAT;
35
- exports.TYPE_WAIT = TYPE_WAIT;
36
- exports.TYPE_WHEN = TYPE_WHEN;
37
- exports.WORK_CONTINUE = WORK_CONTINUE;
38
- exports.WORK_PAUSE = WORK_PAUSE;
39
- exports.WORK_RESTART = WORK_RESTART;
40
- exports.WORK_START = WORK_START;
41
- exports.WORK_STOP = WORK_STOP;
42
- exports.activeTimers = activeTimers;
43
- exports.defaultTimeout = defaultTimeout;
44
- exports.destroyedMessage = destroyedMessage;
45
- exports.hiddenTimers = hiddenTimers;
46
- exports.intervalBuffer = intervalBuffer;
47
- Object.defineProperty(exports, "milliseconds", {
48
- enumerable: true,
49
- get: function() {
50
- return milliseconds;
51
- }
52
- });
53
- exports.startedMessage = startedMessage;
package/dist/delay.cjs DELETED
@@ -1,15 +0,0 @@
1
- const require_constants = require("./constants.cjs");
2
- const require_get = require("./get.cjs");
3
- function delay(time) {
4
- return new Promise((resolve) => {
5
- const interval = require_get.getValidNumber(time, require_constants.milliseconds);
6
- let start;
7
- function step(now) {
8
- start ??= now;
9
- if (interval === require_constants.milliseconds || now - start >= interval - 5) resolve();
10
- else requestAnimationFrame(step);
11
- }
12
- requestAnimationFrame(step);
13
- });
14
- }
15
- exports.delay = delay;
package/dist/get.cjs DELETED
@@ -1,17 +0,0 @@
1
- const require_rolldown_runtime = require("./rolldown_runtime.cjs");
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);
5
- function getCallback(value) {
6
- return typeof value === "function" ? value : __oscarpalmer_atoms_function.noop;
7
- }
8
- function getValidTimeout(value) {
9
- return typeof value === "number" && value > 0 ? value : require_constants.defaultTimeout;
10
- }
11
- function getValidNumber(value, defaultValue) {
12
- const actualDefault = defaultValue ?? 0;
13
- return typeof value === "number" && value > actualDefault ? value : actualDefault;
14
- }
15
- exports.getCallback = getCallback;
16
- exports.getValidNumber = getValidNumber;
17
- exports.getValidTimeout = getValidTimeout;
package/dist/global.cjs DELETED
@@ -1,15 +0,0 @@
1
- const require_constants = require("./constants.cjs");
2
- if (globalThis._oscarpalmer_timers == null) Object.defineProperty(globalThis, "_oscarpalmer_timers", { get() {
3
- return globalThis._oscarpalmer_timer_debug ? [...require_constants.activeTimers] : [];
4
- } });
5
- /* istanbul ignore next */
6
- document.addEventListener("visibilitychange", () => {
7
- const from = document.hidden ? require_constants.activeTimers : require_constants.hiddenTimers;
8
- const method = document.hidden ? require_constants.WORK_PAUSE : require_constants.WORK_CONTINUE;
9
- const to = document.hidden ? require_constants.hiddenTimers : require_constants.activeTimers;
10
- for (const timer of from) {
11
- timer[method]();
12
- to.add(timer);
13
- }
14
- from.clear();
15
- });
package/dist/index.cjs DELETED
@@ -1,14 +0,0 @@
1
- require("./global.cjs");
2
- const require_when = require("./when.cjs");
3
- const require_wait = require("./wait.cjs");
4
- const require_repeat = require("./repeat.cjs");
5
- const require_is = require("./is.cjs");
6
- const require_delay = require("./delay.cjs");
7
- exports.delay = require_delay.delay;
8
- exports.isRepeated = require_is.isRepeated;
9
- exports.isTimer = require_is.isTimer;
10
- exports.isWaited = require_is.isWaited;
11
- exports.isWhen = require_is.isWhen;
12
- exports.repeat = require_repeat.repeat;
13
- exports.wait = require_wait.wait;
14
- exports.when = require_when.when;
package/dist/is.cjs DELETED
@@ -1,20 +0,0 @@
1
- const require_constants = require("./constants.cjs");
2
- function is(names, value) {
3
- return names.includes(value?.$timer);
4
- }
5
- function isRepeated(value) {
6
- return is([require_constants.TYPE_REPEAT], value);
7
- }
8
- function isTimer(value) {
9
- return is([require_constants.TYPE_REPEAT, require_constants.TYPE_WAIT], value);
10
- }
11
- function isWaited(value) {
12
- return is([require_constants.TYPE_WAIT], value);
13
- }
14
- function isWhen(value) {
15
- return is(["when"], value) && typeof value.then === "function";
16
- }
17
- exports.isRepeated = isRepeated;
18
- exports.isTimer = isTimer;
19
- exports.isWaited = isWaited;
20
- exports.isWhen = isWhen;
package/dist/models.cjs DELETED
@@ -1,7 +0,0 @@
1
- var TimerTrace = class extends Error {
2
- constructor() {
3
- super();
4
- this.name = "TimerTrace";
5
- }
6
- };
7
- exports.TimerTrace = TimerTrace;
package/dist/repeat.cjs DELETED
@@ -1,18 +0,0 @@
1
- const require_constants = require("./constants.cjs");
2
- const require_get = require("./get.cjs");
3
- require("./global.cjs");
4
- const require_models = require("./models.cjs");
5
- const require_timer = require("./timer.cjs");
6
- function repeat(callback, options) {
7
- return new require_timer.Timer(require_constants.TYPE_REPEAT, {
8
- callback: require_get.getCallback(callback),
9
- trace: new require_models.TimerTrace().stack
10
- }, {
11
- onAfter: require_get.getCallback(options?.onAfter),
12
- onError: require_get.getCallback(options?.onTimeout),
13
- count: require_get.getValidNumber(options?.count),
14
- interval: require_get.getValidNumber(options?.interval, require_constants.milliseconds),
15
- timeout: require_get.getValidNumber(options?.timeout)
16
- }, true);
17
- }
18
- exports.repeat = repeat;
@@ -1,21 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __copyProps = (to, from, except, desc) => {
8
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
9
- key = keys[i];
10
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
11
- get: ((k) => from[k]).bind(null, key),
12
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
13
- });
14
- }
15
- return to;
16
- };
17
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
18
- value: mod,
19
- enumerable: true
20
- }) : target, mod));
21
- exports.__toESM = __toESM;
package/dist/timer.cjs DELETED
@@ -1,68 +0,0 @@
1
- const require_rolldown_runtime = require("./rolldown_runtime.cjs");
2
- const require_constants = require("./constants.cjs");
3
- const require_work = require("./work.cjs");
4
- let __oscarpalmer_atoms_function = require("@oscarpalmer/atoms/function");
5
- __oscarpalmer_atoms_function = require_rolldown_runtime.__toESM(__oscarpalmer_atoms_function);
6
- var Timer = class {
7
- state;
8
- get active() {
9
- return this.state.active;
10
- }
11
- get destroyed() {
12
- return this.state.destroyed;
13
- }
14
- get paused() {
15
- return this.state.paused;
16
- }
17
- get trace() {
18
- return globalThis._oscarpalmer_timer_debug ?? false ? this.state.trace : void 0;
19
- }
20
- constructor(type, state, options, start) {
21
- this.options = options;
22
- this.$timer = type;
23
- this.state = {
24
- ...state,
25
- active: false,
26
- destroyed: false,
27
- elapsed: 0,
28
- frame: void 0,
29
- index: 0,
30
- paused: false,
31
- total: 0
32
- };
33
- if (start) this.start();
34
- }
35
- continue() {
36
- return this.#work(require_constants.WORK_CONTINUE);
37
- }
38
- destroy() {
39
- this.state.destroyed = true;
40
- this.options.onAfter = __oscarpalmer_atoms_function.noop;
41
- this.options.onError = __oscarpalmer_atoms_function.noop;
42
- this.state.callback = __oscarpalmer_atoms_function.noop;
43
- if (!globalThis._oscarpalmer_timer_debug) this.state.trace = void 0;
44
- require_work.stop({
45
- instance: this,
46
- type: this.$timer
47
- }, this.state, this.options);
48
- }
49
- pause() {
50
- return this.#work(require_constants.WORK_PAUSE);
51
- }
52
- restart() {
53
- return this.#work(require_constants.WORK_RESTART);
54
- }
55
- start() {
56
- return this.#work(require_constants.WORK_START);
57
- }
58
- stop() {
59
- return this.#work(require_constants.WORK_STOP);
60
- }
61
- #work(type) {
62
- return require_work.work(type, {
63
- instance: this,
64
- type: this.$timer
65
- }, this.state, this.options);
66
- }
67
- };
68
- exports.Timer = Timer;
package/dist/wait.cjs DELETED
@@ -1,18 +0,0 @@
1
- const require_constants = require("./constants.cjs");
2
- const require_get = require("./get.cjs");
3
- require("./global.cjs");
4
- const require_models = require("./models.cjs");
5
- const require_timer = require("./timer.cjs");
6
- function wait(callback, time) {
7
- return new require_timer.Timer(require_constants.TYPE_WAIT, {
8
- callback: require_get.getCallback(callback),
9
- trace: new require_models.TimerTrace().stack
10
- }, {
11
- onAfter: void 0,
12
- onError: void 0,
13
- count: -1,
14
- interval: require_get.getValidNumber(time, require_constants.milliseconds),
15
- timeout: 0
16
- }, true);
17
- }
18
- exports.wait = wait;
package/dist/when.cjs DELETED
@@ -1,96 +0,0 @@
1
- const require_rolldown_runtime = require("./rolldown_runtime.cjs");
2
- const require_constants = require("./constants.cjs");
3
- const require_get = require("./get.cjs");
4
- require("./global.cjs");
5
- const require_models = require("./models.cjs");
6
- const require_timer = require("./timer.cjs");
7
- let __oscarpalmer_atoms_function = require("@oscarpalmer/atoms/function");
8
- __oscarpalmer_atoms_function = require_rolldown_runtime.__toESM(__oscarpalmer_atoms_function);
9
- var When = class {
10
- $timer = require_constants.TYPE_WHEN;
11
- state = {
12
- promise: void 0,
13
- rejecter: void 0,
14
- resolver: void 0,
15
- started: false,
16
- timer: void 0
17
- };
18
- get active() {
19
- return this.state.timer?.active ?? false;
20
- }
21
- get destroyed() {
22
- return this.state.timer == null;
23
- }
24
- get paused() {
25
- return this.state.timer?.paused ?? false;
26
- }
27
- get trace() {
28
- return globalThis._oscarpalmer_timer_debug ?? false ? this.state.timer?.trace : void 0;
29
- }
30
- constructor(condition, options) {
31
- const { state } = this;
32
- state.promise = new Promise((resolve, reject) => {
33
- state.resolver = resolve;
34
- state.rejecter = reject;
35
- });
36
- let result = false;
37
- this.state.timer = new require_timer.Timer(require_constants.TYPE_WHEN, {
38
- callback() {
39
- try {
40
- if (condition()) {
41
- result = true;
42
- state.timer.stop();
43
- }
44
- } catch {
45
- state.timer.stop();
46
- }
47
- },
48
- trace: new require_models.TimerTrace().stack
49
- }, {
50
- onAfter: () => {
51
- if (result) state.resolver?.();
52
- else state.rejecter?.();
53
- this.destroy();
54
- },
55
- onError: () => {
56
- state.rejecter?.();
57
- this.destroy();
58
- },
59
- count: require_get.getValidNumber(options?.count),
60
- interval: require_get.getValidNumber(options?.interval, require_constants.milliseconds),
61
- timeout: require_get.getValidTimeout(options?.timeout)
62
- }, false);
63
- }
64
- continue() {
65
- this.state.timer?.continue();
66
- return this;
67
- }
68
- destroy() {
69
- const { state } = this;
70
- state.timer?.destroy();
71
- state.promise = void 0;
72
- state.resolver = __oscarpalmer_atoms_function.noop;
73
- state.rejecter = __oscarpalmer_atoms_function.noop;
74
- state.timer = void 0;
75
- }
76
- pause() {
77
- this.state.timer?.pause();
78
- return this;
79
- }
80
- stop() {
81
- this.state.timer?.stop();
82
- return this;
83
- }
84
- then(resolve, reject) {
85
- const { state } = this;
86
- if (state.timer == null) throw new Error(require_constants.destroyedMessage);
87
- if (state.started) throw new Error(require_constants.startedMessage);
88
- state.started = true;
89
- state.timer.start();
90
- return state.promise.then(resolve ?? __oscarpalmer_atoms_function.noop, reject ?? __oscarpalmer_atoms_function.noop);
91
- }
92
- };
93
- function when(condition, options) {
94
- return new When(condition, options);
95
- }
96
- exports.when = when;
package/dist/work.cjs DELETED
@@ -1,82 +0,0 @@
1
- const require_constants = require("./constants.cjs");
2
- function finish(timer, state, options, success) {
3
- cancelAnimationFrame(state.frame);
4
- require_constants.activeTimers.delete(timer.instance);
5
- state.active = false;
6
- state.elapsed = 0;
7
- state.frame = void 0;
8
- if (timer.type === "wait") state.callback();
9
- else options.onAfter?.(success);
10
- }
11
- function ignore(type, state) {
12
- if (state.destroyed) return type !== require_constants.WORK_STOP;
13
- if (state.paused) return type === "pause" || type === "start";
14
- return state.active && type === "start";
15
- }
16
- function pause(timer, state) {
17
- cancelAnimationFrame(state.frame);
18
- state.frame = void 0;
19
- return timer.instance;
20
- }
21
- function run(timer, state, options) {
22
- let last;
23
- let start;
24
- return function step(now) {
25
- if (!state.active) return;
26
- last ??= now;
27
- start ??= now;
28
- const difference = now - last;
29
- state.elapsed += difference;
30
- state.total += difference;
31
- last = now;
32
- if (options.timeout > 0 && state.total >= options.timeout) {
33
- options.onError?.();
34
- finish(timer, state, options, false);
35
- return;
36
- }
37
- if (options.interval === require_constants.milliseconds || state.elapsed >= options.interval - 5) {
38
- if (options.count > -1) state.callback(state.index);
39
- start = now;
40
- state.elapsed = 0;
41
- state.index += 1;
42
- if (options.count === -1 || options.count > 0 && state.index >= options.count) {
43
- finish(timer, state, options, true);
44
- return;
45
- }
46
- }
47
- state.frame = requestAnimationFrame(step);
48
- };
49
- }
50
- function setState(type, state) {
51
- const pausable = type === "continue" || type === "pause";
52
- state.elapsed = pausable ? state.elapsed : 0;
53
- state.index = pausable ? state.index : 0;
54
- state.total = pausable ? state.total : 0;
55
- }
56
- function stop(timer, state, options) {
57
- cancelAnimationFrame(state.frame);
58
- require_constants.activeTimers.delete(timer.instance);
59
- options.onAfter?.(false);
60
- state.active = !stop;
61
- state.frame = void 0;
62
- state.paused = false;
63
- return timer.instance;
64
- }
65
- function work(type, timer, state, options) {
66
- if (ignore(type, state)) return timer.instance;
67
- setState(type, state);
68
- if (type === "stop") return stop(timer, state, options);
69
- if (type === "pause" || type === "restart") {
70
- cancelAnimationFrame(state.frame);
71
- state.frame = void 0;
72
- }
73
- state.active = true;
74
- state.paused = type === require_constants.WORK_PAUSE;
75
- if (state.paused) return pause(timer, state);
76
- require_constants.activeTimers.add(timer.instance);
77
- const runner = run(timer, state, options);
78
- state.frame = requestAnimationFrame(runner);
79
- return timer.instance;
80
- }
81
- exports.stop = stop;
82
- exports.work = work;