@oscarpalmer/timer 0.27.1 → 0.29.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.
Files changed (67) hide show
  1. package/dist/constants.cjs +32 -4
  2. package/dist/constants.js +26 -9
  3. package/dist/delay.cjs +24 -0
  4. package/dist/delay.js +20 -0
  5. package/dist/get.cjs +15 -0
  6. package/dist/get.js +10 -0
  7. package/dist/global.cjs +17 -2
  8. package/dist/global.js +15 -1
  9. package/dist/index.cjs +16 -42
  10. package/dist/index.js +6 -48
  11. package/dist/is.cjs +11 -8
  12. package/dist/is.js +8 -12
  13. package/dist/models.cjs +5 -2
  14. package/dist/models.js +2 -3
  15. package/dist/node_modules/@oscarpalmer/atoms/dist/function.cjs +8 -0
  16. package/dist/node_modules/@oscarpalmer/atoms/dist/function.js +4 -0
  17. package/dist/repeat.cjs +30 -0
  18. package/dist/repeat.js +26 -0
  19. package/dist/timer.cjs +59 -71
  20. package/dist/timer.full.js +469 -0
  21. package/dist/timer.js +56 -72
  22. package/dist/wait.cjs +30 -0
  23. package/dist/wait.js +26 -0
  24. package/dist/when.cjs +59 -44
  25. package/dist/when.js +55 -44
  26. package/dist/work.cjs +72 -0
  27. package/dist/work.js +68 -0
  28. package/package.json +50 -9
  29. package/src/constants.ts +48 -6
  30. package/src/delay.ts +25 -0
  31. package/src/get.ts +12 -0
  32. package/src/global.ts +16 -1
  33. package/src/index.ts +5 -45
  34. package/src/is.ts +6 -6
  35. package/src/models.ts +55 -47
  36. package/src/repeat.ts +32 -0
  37. package/src/timer.ts +67 -151
  38. package/src/wait.ts +31 -0
  39. package/src/when.ts +49 -24
  40. package/src/work.ts +129 -0
  41. package/types/constants.d.cts +49 -74
  42. package/types/constants.d.ts +15 -6
  43. package/types/delay.d.cts +8 -0
  44. package/types/delay.d.ts +4 -0
  45. package/types/get.d.cts +7 -0
  46. package/types/get.d.ts +3 -0
  47. package/types/index.d.cts +89 -94
  48. package/types/index.d.ts +5 -6
  49. package/types/is.d.cts +50 -69
  50. package/types/models.d.cts +61 -63
  51. package/types/models.d.ts +43 -40
  52. package/types/repeat.d.cts +98 -0
  53. package/types/repeat.d.ts +7 -0
  54. package/types/timer.d.cts +35 -97
  55. package/types/timer.d.ts +19 -52
  56. package/types/wait.d.cts +83 -0
  57. package/types/wait.d.ts +8 -0
  58. package/types/when.d.cts +65 -69
  59. package/types/when.d.ts +18 -5
  60. package/types/work.d.cts +78 -0
  61. package/types/work.d.ts +3 -0
  62. package/dist/functions.cjs +0 -100
  63. package/dist/functions.js +0 -100
  64. package/dist/timer.iife.js +0 -429
  65. package/src/functions.ts +0 -158
  66. package/types/functions.d.cts +0 -114
  67. package/types/functions.d.ts +0 -5
package/types/when.d.cts CHANGED
@@ -1,112 +1,89 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
- declare abstract class BasicTimer<State> {
4
- protected readonly $timer: string;
5
- protected readonly state: State;
6
- constructor(type: "repeat" | "wait" | "when", state: State);
3
+ declare class Timer {
4
+ #private;
5
+ protected readonly worker: WorkHandler;
6
+ protected readonly options: TimerOptions;
7
+ private readonly $timer;
8
+ protected readonly state: TimerState;
7
9
  /**
8
- * Is the timer running?
10
+ * Is the timer active?
9
11
  */
10
- abstract readonly active: boolean;
12
+ get active(): boolean;
11
13
  /**
12
14
  * Is the timer destroyed?
13
15
  */
14
- abstract readonly destroyed: boolean;
16
+ get destroyed(): boolean;
15
17
  /**
16
18
  * Is the timer paused?
17
19
  */
18
- abstract readonly paused: boolean;
20
+ get paused(): boolean;
19
21
  /**
20
- * Gets the traced location of the timer
22
+ * Get the timer's origin _(if debugging is enabled)_
21
23
  */
22
- abstract readonly trace: string | undefined;
23
- }
24
- declare class Timer extends BasicTimer<TimerState> {
25
- private readonly options;
26
- get active(): boolean;
27
- get destroyed(): boolean;
28
- get paused(): boolean;
29
24
  get trace(): string | undefined;
30
- constructor(type: "repeat" | "wait", state: TimerState, options: TimerOptions);
25
+ constructor(type: TimerType, worker: WorkHandler, state: Pick<TimerState, "callback" | "trace">, options: TimerOptions, start: boolean);
31
26
  /**
32
- * Continues the timer _(if it was paused)_
27
+ * Continue running the timer _(if it's paused)_
33
28
  */
34
29
  continue(): Timer;
35
30
  /**
36
- * Destroys the timer _(after stopping it, if it was running)_
31
+ * Destroy the timer
37
32
  */
38
33
  destroy(): void;
39
34
  /**
40
- * Pauses the timer _(if it was running)_
35
+ * Pause the timer _(if it's running)_
41
36
  */
42
37
  pause(): Timer;
43
38
  /**
44
- * Restarts the timer _(if it was running)_
39
+ * Restart the timer _(or start it, if it's not running)_
45
40
  */
46
41
  restart(): Timer;
47
42
  /**
48
- * Starts the timer _(if it was stopped)_
43
+ * Start the timer _(if it's not running)_
49
44
  */
50
45
  start(): Timer;
51
46
  /**
52
- * Stops the timer _(if it was running)_
47
+ * Stop the timer _(if it's running)_
53
48
  */
54
49
  stop(): Timer;
55
50
  }
56
- /**
57
- * Callback that runs after the timer has finished (or is stopped)
58
- * - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
59
- */
60
- export type AfterCallback = (finished: boolean) => void;
61
- export type AnyCallback = (() => void) | IndexedCallback;
62
- /**
63
- * Callback that runs for each iteration of the timer
64
- */
65
- export type IndexedCallback = (index: number) => void;
66
- export type BaseOptions = {
67
- /**
68
- * Interval between each callback
69
- */
51
+ export type TimerOptions = {
52
+ onAfter: ((finished: boolean) => void) | undefined;
53
+ onError: (() => void) | undefined;
54
+ count: number;
70
55
  interval: number;
71
- /**
72
- * Maximum amount of time the timer may run for
73
- */
74
56
  timeout: number;
75
57
  };
76
- export type OptionsWithCount = {
58
+ export type TimerState = {
59
+ active: boolean;
60
+ callback: () => void;
61
+ destroyed: boolean;
62
+ elapsed: number;
63
+ frame: number | undefined;
64
+ index: number;
65
+ paused: boolean;
66
+ total: number;
67
+ trace: string | undefined;
68
+ };
69
+ export type TimerType = "repeat" | "wait" | "when";
70
+ /**
71
+ * Options for a conditional timer
72
+ */
73
+ export type WhenOptions = {
77
74
  /**
78
- * How many times the timer should repeat
75
+ * How many times the timer should check the condition
79
76
  */
80
77
  count: number;
81
- } & BaseOptions;
82
- export type OptionsWithError = {
83
78
  /**
84
- * Callback to run when an error occurs _(usually a timeout)_
79
+ * Then interval between each condtional check
85
80
  */
86
- errorCallback?: () => void;
87
- };
88
- export type RepeatOptions = {
81
+ interval: number;
89
82
  /**
90
- * Callback to run after the timer has finished (or is stopped)
91
- * - `finished` is `true` if the timer was allowed to finish, and `false` if it was stopped
83
+ * The timeout for the timer
92
84
  */
93
- afterCallback?: AfterCallback;
94
- } & OptionsWithCount & OptionsWithError;
95
- export type TimerOptions = {} & RepeatOptions;
96
- export type TimerState = {
97
- active: boolean;
98
- callback: AnyCallback;
99
- destroyed: boolean;
100
- count?: number;
101
- elapsed?: number;
102
- frame?: number;
103
- index?: number;
104
- isRepeated: boolean;
105
- minimum: number;
106
- paused: boolean;
107
- trace?: string;
85
+ timeout: number;
108
86
  };
109
- export type WhenOptions = {} & OptionsWithCount;
110
87
  export type WhenState = {
111
88
  promise: Promise<void>;
112
89
  rejecter?: () => void;
@@ -114,10 +91,30 @@ export type WhenState = {
114
91
  started: boolean;
115
92
  timer: Timer;
116
93
  };
117
- export declare class When extends BasicTimer<WhenState> {
94
+ export type WorkHandler = (type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions) => Timer;
95
+ export type WorkHandlerTimer = {
96
+ instance: Timer;
97
+ type: TimerType;
98
+ };
99
+ export type WorkHandlerType = "continue" | "pause" | "restart" | "start" | "stop";
100
+ export declare class When {
101
+ private readonly $timer;
102
+ private readonly state;
103
+ /**
104
+ * Is the timer active?
105
+ */
118
106
  get active(): boolean;
107
+ /**
108
+ * Is the timer destroyed?
109
+ */
119
110
  get destroyed(): boolean;
111
+ /**
112
+ * Is the timer paused?
113
+ */
120
114
  get paused(): boolean;
115
+ /**
116
+ * Get the timer's origin _(if debugging is enabled)_
117
+ */
121
118
  get trace(): string | undefined;
122
119
  constructor(state: WhenState);
123
120
  /**
@@ -142,8 +139,7 @@ export declare class When extends BasicTimer<WhenState> {
142
139
  then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
143
140
  }
144
141
  /**
145
- * - Creates a promise that resolves when a condition is met
146
- * - If the condition is never met in a timely manner, the promise will reject
142
+ * Create a conditional timer
147
143
  */
148
144
  export declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
149
145
 
package/types/when.d.ts CHANGED
@@ -1,9 +1,22 @@
1
- import type { WhenOptions, WhenState } from './models';
2
- import { BasicTimer } from './timer';
3
- export declare class When extends BasicTimer<WhenState> {
1
+ import { type WhenOptions, type WhenState } from './models';
2
+ declare class When {
3
+ private readonly $timer;
4
+ private readonly state;
5
+ /**
6
+ * Is the timer active?
7
+ */
4
8
  get active(): boolean;
9
+ /**
10
+ * Is the timer destroyed?
11
+ */
5
12
  get destroyed(): boolean;
13
+ /**
14
+ * Is the timer paused?
15
+ */
6
16
  get paused(): boolean;
17
+ /**
18
+ * Get the timer's origin _(if debugging is enabled)_
19
+ */
7
20
  get trace(): string | undefined;
8
21
  constructor(state: WhenState);
9
22
  /**
@@ -28,7 +41,7 @@ export declare class When extends BasicTimer<WhenState> {
28
41
  then(resolve?: (() => void) | null, reject?: (() => void) | null): Promise<void>;
29
42
  }
30
43
  /**
31
- * - Creates a promise that resolves when a condition is met
32
- * - If the condition is never met in a timely manner, the promise will reject
44
+ * Create a conditional timer
33
45
  */
34
46
  export declare function when(condition: () => boolean, options?: Partial<WhenOptions>): When;
47
+ export type { When };
@@ -0,0 +1,78 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ declare class Timer {
4
+ #private;
5
+ protected readonly worker: WorkHandler;
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, worker: WorkHandler, 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
+ export type TimerOptions = {
52
+ onAfter: ((finished: boolean) => void) | undefined;
53
+ onError: (() => void) | undefined;
54
+ count: number;
55
+ interval: number;
56
+ timeout: number;
57
+ };
58
+ export type TimerState = {
59
+ active: boolean;
60
+ callback: () => void;
61
+ destroyed: boolean;
62
+ elapsed: number;
63
+ frame: number | undefined;
64
+ index: number;
65
+ paused: boolean;
66
+ total: number;
67
+ trace: string | undefined;
68
+ };
69
+ export type TimerType = "repeat" | "wait" | "when";
70
+ export type WorkHandler = (type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions) => Timer;
71
+ export type WorkHandlerTimer = {
72
+ instance: Timer;
73
+ type: TimerType;
74
+ };
75
+ export type WorkHandlerType = "continue" | "pause" | "restart" | "start" | "stop";
76
+ export declare function work(type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
77
+
78
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { TimerOptions, TimerState, WorkHandlerTimer, WorkHandlerType } from './models';
2
+ import type { Timer } from './timer';
3
+ export declare function work(type: WorkHandlerType, timer: WorkHandlerTimer, state: TimerState, options: TimerOptions): Timer;
@@ -1,100 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const constants = require("./constants.cjs");
4
- function getOptions(options, isRepeated) {
5
- return {
6
- afterCallback: options.afterCallback,
7
- count: getValueOrDefault(
8
- options.count,
9
- isRepeated ? Number.POSITIVE_INFINITY : 1
10
- ),
11
- errorCallback: options.errorCallback,
12
- interval: getValueOrDefault(options.interval, constants.milliseconds, constants.milliseconds),
13
- timeout: getValueOrDefault(
14
- options.timeout,
15
- isRepeated ? Number.POSITIVE_INFINITY : 3e4
16
- )
17
- };
18
- }
19
- function getValueOrDefault(value, defaultValue, minimum) {
20
- return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
21
- }
22
- function work(type, timer, state, options) {
23
- var _a;
24
- if (state.destroyed && type !== "stop" || constants.beginTypes.has(type) && state.active || constants.endTypes.has(type) && !state.active) {
25
- return timer;
26
- }
27
- const { count, interval, timeout } = options;
28
- const { isRepeated, minimum } = state;
29
- if (constants.endOrRestartTypes.has(type)) {
30
- const isStop = type === "stop";
31
- constants.activeTimers.delete(timer);
32
- cancelAnimationFrame(state.frame);
33
- if (isStop) {
34
- (_a = options.afterCallback) == null ? void 0 : _a.call(options, false);
35
- }
36
- state.active = false;
37
- state.frame = void 0;
38
- state.paused = !isStop;
39
- if (isStop) {
40
- state.elapsed = void 0;
41
- state.index = void 0;
42
- }
43
- return type === "restart" ? work("start", timer, state, options) : timer;
44
- }
45
- state.active = true;
46
- state.paused = false;
47
- const elapsed = type === "continue" ? +(state.elapsed ?? 0) : 0;
48
- let index = type === "continue" ? +(state.index ?? 0) : 0;
49
- state.elapsed = elapsed;
50
- state.index = index;
51
- const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : constants.milliseconds)) - elapsed;
52
- let current;
53
- let start;
54
- function finish(finished, error) {
55
- var _a2, _b;
56
- constants.activeTimers.delete(timer);
57
- state.active = false;
58
- state.elapsed = void 0;
59
- state.frame = void 0;
60
- state.index = void 0;
61
- if (error) {
62
- (_a2 = options.errorCallback) == null ? void 0 : _a2.call(options);
63
- }
64
- (_b = options.afterCallback) == null ? void 0 : _b.call(options, finished);
65
- }
66
- function step(timestamp) {
67
- if (!state.active) {
68
- return;
69
- }
70
- current ?? (current = timestamp);
71
- start ?? (start = timestamp);
72
- const time = timestamp - current;
73
- state.elapsed = elapsed + (current - start);
74
- const finished = time - elapsed >= total;
75
- if (timestamp - start >= timeout - elapsed) {
76
- finish(finished, !finished);
77
- return;
78
- }
79
- if (finished || time >= minimum) {
80
- if (state.active) {
81
- state.callback(isRepeated ? index : void 0);
82
- }
83
- index += 1;
84
- state.index = index;
85
- if (!finished && index < count) {
86
- current = null;
87
- } else {
88
- finish(true, false);
89
- return;
90
- }
91
- }
92
- state.frame = requestAnimationFrame(step);
93
- }
94
- constants.activeTimers.add(timer);
95
- state.frame = requestAnimationFrame(step);
96
- return timer;
97
- }
98
- exports.getOptions = getOptions;
99
- exports.getValueOrDefault = getValueOrDefault;
100
- exports.work = work;
package/dist/functions.js DELETED
@@ -1,100 +0,0 @@
1
- import { beginTypes, endTypes, endOrRestartTypes, activeTimers, milliseconds } from "./constants.js";
2
- function getOptions(options, isRepeated) {
3
- return {
4
- afterCallback: options.afterCallback,
5
- count: getValueOrDefault(
6
- options.count,
7
- isRepeated ? Number.POSITIVE_INFINITY : 1
8
- ),
9
- errorCallback: options.errorCallback,
10
- interval: getValueOrDefault(options.interval, milliseconds, milliseconds),
11
- timeout: getValueOrDefault(
12
- options.timeout,
13
- isRepeated ? Number.POSITIVE_INFINITY : 3e4
14
- )
15
- };
16
- }
17
- function getValueOrDefault(value, defaultValue, minimum) {
18
- return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
19
- }
20
- function work(type, timer, state, options) {
21
- var _a;
22
- if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
23
- return timer;
24
- }
25
- const { count, interval, timeout } = options;
26
- const { isRepeated, minimum } = state;
27
- if (endOrRestartTypes.has(type)) {
28
- const isStop = type === "stop";
29
- activeTimers.delete(timer);
30
- cancelAnimationFrame(state.frame);
31
- if (isStop) {
32
- (_a = options.afterCallback) == null ? void 0 : _a.call(options, false);
33
- }
34
- state.active = false;
35
- state.frame = void 0;
36
- state.paused = !isStop;
37
- if (isStop) {
38
- state.elapsed = void 0;
39
- state.index = void 0;
40
- }
41
- return type === "restart" ? work("start", timer, state, options) : timer;
42
- }
43
- state.active = true;
44
- state.paused = false;
45
- const elapsed = type === "continue" ? +(state.elapsed ?? 0) : 0;
46
- let index = type === "continue" ? +(state.index ?? 0) : 0;
47
- state.elapsed = elapsed;
48
- state.index = index;
49
- const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
50
- let current;
51
- let start;
52
- function finish(finished, error) {
53
- var _a2, _b;
54
- activeTimers.delete(timer);
55
- state.active = false;
56
- state.elapsed = void 0;
57
- state.frame = void 0;
58
- state.index = void 0;
59
- if (error) {
60
- (_a2 = options.errorCallback) == null ? void 0 : _a2.call(options);
61
- }
62
- (_b = options.afterCallback) == null ? void 0 : _b.call(options, finished);
63
- }
64
- function step(timestamp) {
65
- if (!state.active) {
66
- return;
67
- }
68
- current ?? (current = timestamp);
69
- start ?? (start = timestamp);
70
- const time = timestamp - current;
71
- state.elapsed = elapsed + (current - start);
72
- const finished = time - elapsed >= total;
73
- if (timestamp - start >= timeout - elapsed) {
74
- finish(finished, !finished);
75
- return;
76
- }
77
- if (finished || time >= minimum) {
78
- if (state.active) {
79
- state.callback(isRepeated ? index : void 0);
80
- }
81
- index += 1;
82
- state.index = index;
83
- if (!finished && index < count) {
84
- current = null;
85
- } else {
86
- finish(true, false);
87
- return;
88
- }
89
- }
90
- state.frame = requestAnimationFrame(step);
91
- }
92
- activeTimers.add(timer);
93
- state.frame = requestAnimationFrame(step);
94
- return timer;
95
- }
96
- export {
97
- getOptions,
98
- getValueOrDefault,
99
- work
100
- };