@oscarpalmer/timer 0.24.0 → 0.26.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/constants.cjs +18 -0
- package/dist/constants.js +11 -12
- package/dist/{functions.mjs → functions.cjs} +35 -34
- package/dist/functions.js +25 -29
- package/dist/global.cjs +9 -0
- package/dist/global.js +1 -13
- package/dist/index.cjs +47 -0
- package/dist/index.js +30 -324
- package/dist/{is.mjs → is.cjs} +7 -8
- package/dist/is.js +4 -5
- package/dist/models.cjs +9 -0
- package/dist/{models.mjs → models.js} +0 -1
- package/dist/timer.cjs +111 -0
- package/dist/timer.js +64 -145
- package/dist/when.cjs +122 -0
- package/dist/when.js +86 -234
- package/package.json +19 -20
- package/src/constants.ts +2 -2
- package/src/functions.ts +3 -3
- package/src/global.ts +2 -1
- package/src/index.ts +4 -3
- package/src/is.ts +2 -2
- package/src/models.ts +1 -1
- package/src/timer.ts +3 -3
- package/src/when.ts +2 -2
- package/types/constants.d.cts +138 -0
- package/types/constants.d.ts +2 -2
- package/types/functions.d.cts +117 -0
- package/types/functions.d.ts +2 -2
- package/types/global.d.cts +3 -0
- package/types/global.d.ts +1 -1
- package/types/index.d.cts +78 -82
- package/types/index.d.ts +1 -1
- package/types/is.d.cts +163 -0
- package/types/is.d.ts +2 -2
- package/types/models.d.cts +123 -0
- package/types/models.d.ts +1 -1
- package/types/timer.d.cts +142 -0
- package/types/timer.d.ts +1 -1
- package/types/when.d.cts +153 -0
- package/types/when.d.ts +3 -3
- package/dist/constants.mjs +0 -19
- package/dist/global.mjs +0 -9
- package/dist/index.mjs +0 -46
- package/dist/timer.mjs +0 -89
- package/dist/when.mjs +0 -91
package/dist/when.js
CHANGED
|
@@ -1,213 +1,112 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var activeTimers = new Set;
|
|
7
|
-
var beginTypes = new Set(["continue", "start"]);
|
|
8
|
-
var endTypes = new Set(["pause", "stop"]);
|
|
9
|
-
var endOrRestartTypes = new Set([
|
|
10
|
-
"pause",
|
|
11
|
-
"restart",
|
|
12
|
-
"stop"
|
|
13
|
-
]);
|
|
14
|
-
var hiddenTimers = new Set;
|
|
15
|
-
var milliseconds = 1000 / 60;
|
|
16
|
-
|
|
17
|
-
// src/functions.ts
|
|
18
|
-
function getOptions(options, isRepeated) {
|
|
19
|
-
return {
|
|
20
|
-
afterCallback: options.afterCallback,
|
|
21
|
-
count: getValueOrDefault(options.count, isRepeated ? Number.POSITIVE_INFINITY : 1),
|
|
22
|
-
errorCallback: options.errorCallback,
|
|
23
|
-
interval: getValueOrDefault(options.interval, milliseconds, milliseconds),
|
|
24
|
-
timeout: getValueOrDefault(options.timeout, isRepeated ? Number.POSITIVE_INFINITY : 30000)
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
function getValueOrDefault(value, defaultValue, minimum) {
|
|
28
|
-
return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
|
|
29
|
-
}
|
|
30
|
-
function work(type, timer, state, options) {
|
|
31
|
-
if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
|
|
32
|
-
return timer;
|
|
33
|
-
}
|
|
34
|
-
const { count, interval, timeout } = options;
|
|
35
|
-
const { isRepeated, minimum } = state;
|
|
36
|
-
if (endOrRestartTypes.has(type)) {
|
|
37
|
-
const isStop = type === "stop";
|
|
38
|
-
activeTimers.delete(timer);
|
|
39
|
-
cancelAnimationFrame(state.frame);
|
|
40
|
-
if (isStop) {
|
|
41
|
-
options.afterCallback?.(false);
|
|
42
|
-
}
|
|
43
|
-
state.active = false;
|
|
44
|
-
state.frame = undefined;
|
|
45
|
-
state.paused = !isStop;
|
|
46
|
-
if (isStop) {
|
|
47
|
-
state.elapsed = undefined;
|
|
48
|
-
state.index = undefined;
|
|
49
|
-
}
|
|
50
|
-
return type === "restart" ? work("start", timer, state, options) : timer;
|
|
51
|
-
}
|
|
52
|
-
state.active = true;
|
|
53
|
-
state.paused = false;
|
|
54
|
-
const elapsed = type === "continue" ? +(state.elapsed ?? 0) : 0;
|
|
55
|
-
let index = type === "continue" ? +(state.index ?? 0) : 0;
|
|
56
|
-
state.elapsed = elapsed;
|
|
57
|
-
state.index = index;
|
|
58
|
-
const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
|
|
59
|
-
let current;
|
|
60
|
-
let start;
|
|
61
|
-
function finish(finished, error) {
|
|
62
|
-
activeTimers.delete(timer);
|
|
63
|
-
state.active = false;
|
|
64
|
-
state.elapsed = undefined;
|
|
65
|
-
state.frame = undefined;
|
|
66
|
-
state.index = undefined;
|
|
67
|
-
if (error) {
|
|
68
|
-
options.errorCallback?.();
|
|
69
|
-
}
|
|
70
|
-
options.afterCallback?.(finished);
|
|
71
|
-
}
|
|
72
|
-
function step(timestamp) {
|
|
73
|
-
if (!state.active) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
current ??= timestamp;
|
|
77
|
-
start ??= timestamp;
|
|
78
|
-
const time = timestamp - current;
|
|
79
|
-
state.elapsed = elapsed + (current - start);
|
|
80
|
-
const finished = time - elapsed >= total;
|
|
81
|
-
if (timestamp - start >= timeout - elapsed) {
|
|
82
|
-
finish(finished, !finished);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (finished || time >= minimum) {
|
|
86
|
-
if (state.active) {
|
|
87
|
-
state.callback(isRepeated ? index : undefined);
|
|
88
|
-
}
|
|
89
|
-
index += 1;
|
|
90
|
-
state.index = index;
|
|
91
|
-
if (!finished && index < count) {
|
|
92
|
-
current = null;
|
|
93
|
-
} else {
|
|
94
|
-
finish(true, false);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
state.frame = requestAnimationFrame(step);
|
|
99
|
-
}
|
|
100
|
-
activeTimers.add(timer);
|
|
101
|
-
state.frame = requestAnimationFrame(step);
|
|
102
|
-
return timer;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// src/models.ts
|
|
106
|
-
class TimerTrace extends Error {
|
|
107
|
-
constructor() {
|
|
108
|
-
super();
|
|
109
|
-
this.name = "TimerTrace";
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// src/timer.ts
|
|
114
|
-
function timer(type, callback, partial, start) {
|
|
115
|
-
const isRepeated = type === "repeat";
|
|
116
|
-
const options = getOptions(partial, isRepeated);
|
|
117
|
-
const instance = new Timer(type, {
|
|
118
|
-
callback,
|
|
119
|
-
isRepeated,
|
|
120
|
-
active: false,
|
|
121
|
-
destroyed: false,
|
|
122
|
-
minimum: options.interval - options.interval % milliseconds / 2,
|
|
123
|
-
paused: false,
|
|
124
|
-
trace: new TimerTrace
|
|
125
|
-
}, options);
|
|
126
|
-
if (start) {
|
|
127
|
-
instance.start();
|
|
128
|
-
}
|
|
129
|
-
return instance;
|
|
130
|
-
}
|
|
131
|
-
class BasicTimer {
|
|
132
|
-
constructor(type, state) {
|
|
133
|
-
this.$timer = type;
|
|
134
|
-
this.state = state;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
class Timer extends BasicTimer {
|
|
1
|
+
import { noop } from "@oscarpalmer/atoms/function";
|
|
2
|
+
import { BasicTimer, timer } from "./timer.js";
|
|
3
|
+
const destroyedMessage = "Timer has already been destroyed";
|
|
4
|
+
const startedMessage = "Timer has already been started";
|
|
5
|
+
class When extends BasicTimer {
|
|
139
6
|
get active() {
|
|
140
|
-
|
|
7
|
+
var _a;
|
|
8
|
+
return ((_a = this.state.timer) == null ? void 0 : _a.active) ?? false;
|
|
141
9
|
}
|
|
142
10
|
get destroyed() {
|
|
143
|
-
return this.state.
|
|
11
|
+
return this.state.timer == null;
|
|
144
12
|
}
|
|
145
13
|
get paused() {
|
|
146
|
-
|
|
14
|
+
var _a;
|
|
15
|
+
return ((_a = this.state.timer) == null ? void 0 : _a.paused) ?? false;
|
|
147
16
|
}
|
|
148
17
|
get trace() {
|
|
149
|
-
|
|
18
|
+
var _a;
|
|
19
|
+
return (_a = this.state.timer) == null ? void 0 : _a.trace;
|
|
150
20
|
}
|
|
151
|
-
constructor(
|
|
152
|
-
super(
|
|
153
|
-
this.options = options;
|
|
21
|
+
constructor(state) {
|
|
22
|
+
super("when", state);
|
|
154
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Continues the timer _(if it was paused)_
|
|
26
|
+
*/
|
|
155
27
|
continue() {
|
|
156
|
-
|
|
28
|
+
var _a;
|
|
29
|
+
(_a = this.state.timer) == null ? void 0 : _a.continue();
|
|
30
|
+
return this;
|
|
157
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Destroys the timer _(and stops it,if it was running)_
|
|
34
|
+
*/
|
|
158
35
|
destroy() {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
this.state.trace = undefined;
|
|
166
|
-
}
|
|
36
|
+
var _a;
|
|
37
|
+
(_a = this.state.timer) == null ? void 0 : _a.destroy();
|
|
38
|
+
this.state.promise = void 0;
|
|
39
|
+
this.state.resolver = noop;
|
|
40
|
+
this.state.rejecter = noop;
|
|
41
|
+
this.state.timer = void 0;
|
|
167
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Pauses the timer _(if it was running)_
|
|
45
|
+
*/
|
|
168
46
|
pause() {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return work("restart", this, this.state, this.options);
|
|
173
|
-
}
|
|
174
|
-
start() {
|
|
175
|
-
return work("start", this, this.state, this.options);
|
|
47
|
+
var _a;
|
|
48
|
+
(_a = this.state.timer) == null ? void 0 : _a.pause();
|
|
49
|
+
return this;
|
|
176
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Stops the timer _(if it was running)_
|
|
53
|
+
*/
|
|
177
54
|
stop() {
|
|
178
|
-
|
|
55
|
+
var _a;
|
|
56
|
+
(_a = this.state.timer) == null ? void 0 : _a.stop();
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Starts the timer and returns a promise that resolves when the condition is met
|
|
61
|
+
*/
|
|
62
|
+
// biome-ignore lint/suspicious/noThenProperty: returning a promise-like object, so it's ok ;)
|
|
63
|
+
then(resolve, reject) {
|
|
64
|
+
var _a;
|
|
65
|
+
if (this.state.timer == null || ((_a = this.state) == null ? void 0 : _a.started)) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
this.state.timer == null ? destroyedMessage : startedMessage
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
this.state.started = true;
|
|
71
|
+
this.state.timer.start();
|
|
72
|
+
return this.state.promise.then(resolve ?? noop, reject ?? noop);
|
|
179
73
|
}
|
|
180
74
|
}
|
|
181
|
-
|
|
182
|
-
// src/when.ts
|
|
183
75
|
function when(condition, options) {
|
|
184
76
|
let result = false;
|
|
185
77
|
const state = {
|
|
186
78
|
started: false,
|
|
187
|
-
timer: timer(
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
afterCallback() {
|
|
194
|
-
if (!state.timer.paused) {
|
|
195
|
-
if (result) {
|
|
196
|
-
state.resolver?.();
|
|
197
|
-
} else {
|
|
198
|
-
state.rejecter?.();
|
|
199
|
-
}
|
|
200
|
-
instance.destroy();
|
|
79
|
+
timer: timer(
|
|
80
|
+
"repeat",
|
|
81
|
+
() => {
|
|
82
|
+
if (condition()) {
|
|
83
|
+
result = true;
|
|
84
|
+
state.timer.stop();
|
|
201
85
|
}
|
|
202
86
|
},
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
87
|
+
{
|
|
88
|
+
afterCallback() {
|
|
89
|
+
var _a, _b;
|
|
90
|
+
if (!state.timer.paused) {
|
|
91
|
+
if (result) {
|
|
92
|
+
(_a = state.resolver) == null ? void 0 : _a.call(state);
|
|
93
|
+
} else {
|
|
94
|
+
(_b = state.rejecter) == null ? void 0 : _b.call(state);
|
|
95
|
+
}
|
|
96
|
+
instance.destroy();
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
errorCallback() {
|
|
100
|
+
var _a;
|
|
101
|
+
(_a = state.rejecter) == null ? void 0 : _a.call(state);
|
|
102
|
+
instance.destroy();
|
|
103
|
+
},
|
|
104
|
+
count: options == null ? void 0 : options.count,
|
|
105
|
+
interval: options == null ? void 0 : options.interval,
|
|
106
|
+
timeout: options == null ? void 0 : options.timeout
|
|
206
107
|
},
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
timeout: options?.timeout
|
|
210
|
-
}, false)
|
|
108
|
+
false
|
|
109
|
+
)
|
|
211
110
|
};
|
|
212
111
|
const promise = new Promise((resolve, reject) => {
|
|
213
112
|
state.resolver = resolve;
|
|
@@ -217,54 +116,7 @@ function when(condition, options) {
|
|
|
217
116
|
const instance = new When(state);
|
|
218
117
|
return instance;
|
|
219
118
|
}
|
|
220
|
-
var destroyedMessage = "Timer has already been destroyed";
|
|
221
|
-
var startedMessage = "Timer has already been started";
|
|
222
|
-
|
|
223
|
-
class When extends BasicTimer {
|
|
224
|
-
get active() {
|
|
225
|
-
return this.state.timer?.active ?? false;
|
|
226
|
-
}
|
|
227
|
-
get destroyed() {
|
|
228
|
-
return this.state.timer == null;
|
|
229
|
-
}
|
|
230
|
-
get paused() {
|
|
231
|
-
return this.state.timer?.paused ?? false;
|
|
232
|
-
}
|
|
233
|
-
get trace() {
|
|
234
|
-
return this.state.timer?.trace;
|
|
235
|
-
}
|
|
236
|
-
constructor(state) {
|
|
237
|
-
super("when", state);
|
|
238
|
-
}
|
|
239
|
-
continue() {
|
|
240
|
-
this.state.timer?.continue();
|
|
241
|
-
return this;
|
|
242
|
-
}
|
|
243
|
-
destroy() {
|
|
244
|
-
this.state.timer?.destroy();
|
|
245
|
-
this.state.promise = undefined;
|
|
246
|
-
this.state.resolver = noop;
|
|
247
|
-
this.state.rejecter = noop;
|
|
248
|
-
this.state.timer = undefined;
|
|
249
|
-
}
|
|
250
|
-
pause() {
|
|
251
|
-
this.state.timer?.pause();
|
|
252
|
-
return this;
|
|
253
|
-
}
|
|
254
|
-
stop() {
|
|
255
|
-
this.state.timer?.stop();
|
|
256
|
-
return this;
|
|
257
|
-
}
|
|
258
|
-
then(resolve, reject) {
|
|
259
|
-
if (this.state.timer == null || this.state?.started) {
|
|
260
|
-
throw new Error(this.state.timer == null ? destroyedMessage : startedMessage);
|
|
261
|
-
}
|
|
262
|
-
this.state.started = true;
|
|
263
|
-
this.state.timer.start();
|
|
264
|
-
return this.state.promise.then(resolve ?? noop, reject ?? noop);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
119
|
export {
|
|
268
|
-
|
|
269
|
-
|
|
120
|
+
When,
|
|
121
|
+
when
|
|
270
122
|
};
|
package/package.json
CHANGED
|
@@ -4,51 +4,50 @@
|
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@oscarpalmer/atoms": "0.
|
|
7
|
+
"@oscarpalmer/atoms": "0.75.0"
|
|
8
8
|
},
|
|
9
9
|
"description": "A better solution for timeout- and interval-based timers.",
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@biomejs/biome": "^1.9
|
|
12
|
-
"@
|
|
13
|
-
"@
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"typescript": "^5.6
|
|
11
|
+
"@biomejs/biome": "^1.9",
|
|
12
|
+
"@types/node": "^22.7",
|
|
13
|
+
"@vitest/coverage-istanbul": "^2.1",
|
|
14
|
+
"dts-bundle-generator": "^9.5",
|
|
15
|
+
"happy-dom": "^15.7",
|
|
16
|
+
"typescript": "^5.6",
|
|
17
|
+
"vite": "^5.4",
|
|
18
|
+
"vitest": "^2.1"
|
|
17
19
|
},
|
|
18
20
|
"exports": {
|
|
19
21
|
".": {
|
|
20
|
-
"bun": "./src/index.ts",
|
|
21
22
|
"import": {
|
|
22
23
|
"types": "./types/index.d.ts",
|
|
23
|
-
"default": "./dist/index.
|
|
24
|
+
"default": "./dist/index.js"
|
|
24
25
|
},
|
|
25
26
|
"require": {
|
|
26
27
|
"types": "./types/index.d.cts",
|
|
27
|
-
"default": "./dist/index.
|
|
28
|
+
"default": "./dist/index.cjs"
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
},
|
|
31
32
|
"files": ["dist", "src", "types"],
|
|
32
33
|
"keywords": ["timer", "setTimeout", "setInterval", "requestAnimationFrame"],
|
|
33
34
|
"license": "MIT",
|
|
34
|
-
"main": "dist/index.
|
|
35
|
-
"module": "dist/index.
|
|
35
|
+
"main": "dist/index.cjs",
|
|
36
|
+
"module": "dist/index.js",
|
|
36
37
|
"name": "@oscarpalmer/timer",
|
|
37
38
|
"repository": {
|
|
38
39
|
"type": "git",
|
|
39
40
|
"url": "git+https://github.com/oscarpalmer/timer.git"
|
|
40
41
|
},
|
|
41
42
|
"scripts": {
|
|
42
|
-
"build": "
|
|
43
|
-
"build:js": "
|
|
43
|
+
"build": "npm run clean && npm run build:js && npm run types",
|
|
44
|
+
"build:js": "npx vite build",
|
|
44
45
|
"clean": "rm -rf ./dist && rm -rf ./types && rm -f ./tsconfig.tsbuildinfo",
|
|
45
|
-
"test": "
|
|
46
|
-
"types": "
|
|
47
|
-
"
|
|
48
|
-
"types:esm": "bunx tsc -p ./tsconfig.json",
|
|
49
|
-
"watch": "bun build ./src/index.ts --outfile ./dist/index.js --watch"
|
|
46
|
+
"test": "npx vitest --coverage",
|
|
47
|
+
"types": "npx tsc && npx dts-bundle-generator --config ./dts.config.ts",
|
|
48
|
+
"watch": "npx vite build --watch"
|
|
50
49
|
},
|
|
51
50
|
"type": "module",
|
|
52
51
|
"types": "types/index.d.cts",
|
|
53
|
-
"version": "0.
|
|
52
|
+
"version": "0.26.0"
|
|
54
53
|
}
|
package/src/constants.ts
CHANGED
package/src/functions.ts
CHANGED
|
@@ -4,9 +4,9 @@ import {
|
|
|
4
4
|
endOrRestartTypes,
|
|
5
5
|
endTypes,
|
|
6
6
|
milliseconds,
|
|
7
|
-
} from '
|
|
8
|
-
import type {TimerOptions, TimerState,
|
|
9
|
-
import type {Timer} from '
|
|
7
|
+
} from '~/constants';
|
|
8
|
+
import type {TimerOptions, TimerState, WorkType} from '~/models';
|
|
9
|
+
import type {Timer} from '~/timer';
|
|
10
10
|
|
|
11
11
|
export function getOptions(
|
|
12
12
|
options: Partial<TimerOptions>,
|
package/src/global.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import {activeTimers, hiddenTimers} from '~/constants';
|
|
2
|
+
import '~/global';
|
|
1
3
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
|
-
import {
|
|
3
|
-
import './global';
|
|
4
|
-
import {wait} from './timer';
|
|
4
|
+
import {wait} from '~/timer';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Creates a delayed promise that resolves after a certain amount of time _(or rejects when timed out)_
|
|
@@ -45,3 +45,4 @@ document.addEventListener('visibilitychange', () => {
|
|
|
45
45
|
export {isRepeated, isTimer, isWaited, isWhen} from './is';
|
|
46
46
|
export {repeat, wait, type Timer} from './timer';
|
|
47
47
|
export {when, type When} from './when';
|
|
48
|
+
|
package/src/is.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {PlainObject} from '@oscarpalmer/atoms/models';
|
|
2
|
-
import type {Timer} from '
|
|
3
|
-
import type {When} from '
|
|
2
|
+
import type {Timer} from '~/timer';
|
|
3
|
+
import type {When} from '~/when';
|
|
4
4
|
|
|
5
5
|
function is(pattern: RegExp, value: unknown) {
|
|
6
6
|
return pattern.test((value as PlainObject)?.$timer as string);
|
package/src/models.ts
CHANGED
package/src/timer.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {milliseconds} from '
|
|
2
|
-
import {getOptions, work} from '
|
|
1
|
+
import {milliseconds} from '~/constants';
|
|
2
|
+
import {getOptions, work} from '~/functions';
|
|
3
3
|
import {
|
|
4
4
|
TimerTrace,
|
|
5
5
|
type AnyCallback,
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
type TimerOptions,
|
|
9
9
|
type TimerState,
|
|
10
10
|
type WaitOptions,
|
|
11
|
-
} from '
|
|
11
|
+
} from '~/models';
|
|
12
12
|
|
|
13
13
|
export abstract class BasicTimer<State> {
|
|
14
14
|
protected declare readonly $timer: string;
|
package/src/when.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {noop} from '@oscarpalmer/atoms/function';
|
|
2
|
-
import type {WhenOptions, WhenState} from '
|
|
3
|
-
import {BasicTimer, timer} from '
|
|
2
|
+
import type {WhenOptions, WhenState} from '~/models';
|
|
3
|
+
import {BasicTimer, timer} from '~/timer';
|
|
4
4
|
|
|
5
5
|
const destroyedMessage = 'Timer has already been destroyed';
|
|
6
6
|
const startedMessage = 'Timer has already been started';
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
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);
|
|
7
|
+
/**
|
|
8
|
+
* Is the timer running?
|
|
9
|
+
*/
|
|
10
|
+
abstract readonly active: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Is the timer destroyed?
|
|
13
|
+
*/
|
|
14
|
+
abstract readonly destroyed: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Is the timer paused?
|
|
17
|
+
*/
|
|
18
|
+
abstract readonly paused: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Gets the traced location of the timer
|
|
21
|
+
*/
|
|
22
|
+
abstract readonly trace: TimerTrace | 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
|
+
get trace(): TimerTrace | undefined;
|
|
30
|
+
constructor(type: "repeat" | "wait", state: TimerState, options: TimerOptions);
|
|
31
|
+
/**
|
|
32
|
+
* Continues the timer _(if it was paused)_
|
|
33
|
+
*/
|
|
34
|
+
continue(): Timer;
|
|
35
|
+
/**
|
|
36
|
+
* Destroys the timer _(after stopping it, if it was running)_
|
|
37
|
+
*/
|
|
38
|
+
destroy(): void;
|
|
39
|
+
/**
|
|
40
|
+
* Pauses the timer _(if it was running)_
|
|
41
|
+
*/
|
|
42
|
+
pause(): Timer;
|
|
43
|
+
/**
|
|
44
|
+
* Restarts the timer _(if it was running)_
|
|
45
|
+
*/
|
|
46
|
+
restart(): Timer;
|
|
47
|
+
/**
|
|
48
|
+
* Starts the timer _(if it was stopped)_
|
|
49
|
+
*/
|
|
50
|
+
start(): Timer;
|
|
51
|
+
/**
|
|
52
|
+
* Stops the timer _(if it was running)_
|
|
53
|
+
*/
|
|
54
|
+
stop(): Timer;
|
|
55
|
+
}
|
|
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
|
+
*/
|
|
70
|
+
interval: number;
|
|
71
|
+
/**
|
|
72
|
+
* Maximum amount of time the timer may run for
|
|
73
|
+
*/
|
|
74
|
+
timeout: number;
|
|
75
|
+
};
|
|
76
|
+
export type OptionsWithCount = {
|
|
77
|
+
/**
|
|
78
|
+
* How many times the timer should repeat
|
|
79
|
+
*/
|
|
80
|
+
count: number;
|
|
81
|
+
} & BaseOptions;
|
|
82
|
+
export type OptionsWithError = {
|
|
83
|
+
/**
|
|
84
|
+
* Callback to run when an error occurs _(usually a timeout)_
|
|
85
|
+
*/
|
|
86
|
+
errorCallback?: () => void;
|
|
87
|
+
};
|
|
88
|
+
export type RepeatOptions = {
|
|
89
|
+
/**
|
|
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
|
|
92
|
+
*/
|
|
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: TimerTrace;
|
|
108
|
+
};
|
|
109
|
+
declare class TimerTrace extends Error {
|
|
110
|
+
constructor();
|
|
111
|
+
}
|
|
112
|
+
export type WorkType = "continue" | "pause" | "restart" | "start" | "stop";
|
|
113
|
+
/**
|
|
114
|
+
* A set of all active timers
|
|
115
|
+
*/
|
|
116
|
+
export declare const activeTimers: Set<Timer>;
|
|
117
|
+
/**
|
|
118
|
+
* A set of types that allow work to begin
|
|
119
|
+
*/
|
|
120
|
+
export declare const beginTypes: Set<WorkType>;
|
|
121
|
+
/**
|
|
122
|
+
* A set of types that allow work to end
|
|
123
|
+
*/
|
|
124
|
+
export declare const endTypes: Set<WorkType>;
|
|
125
|
+
/**
|
|
126
|
+
* A set of types that allow work to end or restart
|
|
127
|
+
*/
|
|
128
|
+
export declare const endOrRestartTypes: Set<WorkType>;
|
|
129
|
+
/**
|
|
130
|
+
* A set of timers that were paused due to the document being hidden
|
|
131
|
+
*/
|
|
132
|
+
export declare const hiddenTimers: Set<Timer>;
|
|
133
|
+
/**
|
|
134
|
+
* Milliseconds in a frame, probably ;-)
|
|
135
|
+
*/
|
|
136
|
+
export declare const milliseconds: number;
|
|
137
|
+
|
|
138
|
+
export {};
|
package/types/constants.d.ts
CHANGED