@oscarpalmer/timer 0.28.0 → 0.30.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 +32 -4
- package/dist/constants.js +26 -9
- package/dist/delay.cjs +24 -0
- package/dist/delay.js +20 -0
- package/dist/get.cjs +15 -0
- package/dist/get.js +10 -0
- package/dist/global.cjs +17 -2
- package/dist/global.js +15 -1
- package/dist/index.cjs +16 -44
- package/dist/index.js +6 -50
- package/dist/is.cjs +11 -8
- package/dist/is.js +8 -12
- package/dist/models.cjs +5 -2
- package/dist/models.js +2 -3
- package/dist/node_modules/@oscarpalmer/atoms/dist/function.cjs +8 -0
- package/dist/node_modules/@oscarpalmer/atoms/dist/function.js +4 -0
- package/dist/repeat.cjs +30 -0
- package/dist/repeat.js +26 -0
- package/dist/timer.cjs +59 -71
- package/dist/timer.full.js +472 -0
- package/dist/timer.js +56 -72
- package/dist/wait.cjs +30 -0
- package/dist/wait.js +26 -0
- package/dist/when.cjs +59 -44
- package/dist/when.js +55 -44
- package/dist/work.cjs +72 -0
- package/dist/work.js +68 -0
- package/package.json +49 -9
- package/src/constants.ts +51 -6
- package/src/delay.ts +25 -0
- package/src/get.ts +12 -0
- package/src/global.ts +16 -1
- package/src/index.ts +5 -48
- package/src/is.ts +6 -6
- package/src/models.ts +55 -47
- package/src/repeat.ts +32 -0
- package/src/timer.ts +67 -151
- package/src/wait.ts +31 -0
- package/src/when.ts +49 -24
- package/src/work.ts +129 -0
- package/types/constants.d.cts +49 -74
- package/types/constants.d.ts +15 -6
- package/types/delay.d.cts +8 -0
- package/types/delay.d.ts +4 -0
- package/types/get.d.cts +7 -0
- package/types/get.d.ts +3 -0
- package/types/index.d.cts +89 -94
- package/types/index.d.ts +5 -6
- package/types/is.d.cts +50 -69
- package/types/models.d.cts +61 -63
- package/types/models.d.ts +43 -40
- package/types/repeat.d.cts +98 -0
- package/types/repeat.d.ts +7 -0
- package/types/timer.d.cts +35 -97
- package/types/timer.d.ts +19 -52
- package/types/wait.d.cts +83 -0
- package/types/wait.d.ts +8 -0
- package/types/when.d.cts +65 -69
- package/types/when.d.ts +18 -5
- package/types/work.d.cts +78 -0
- package/types/work.d.ts +3 -0
- package/dist/functions.cjs +0 -99
- package/dist/functions.js +0 -99
- package/dist/timer.iife.js +0 -431
- package/src/functions.ts +0 -158
- package/types/functions.d.cts +0 -113
- package/types/functions.d.ts +0 -4
package/dist/constants.cjs
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
function stepper(now) {
|
|
6
|
+
if (values.length === 7) {
|
|
7
|
+
exports.milliseconds = Math.floor(
|
|
8
|
+
values.slice(2).reduce((first, second) => first + second) / 5 * 2
|
|
9
|
+
) / 2;
|
|
10
|
+
last = void 0;
|
|
11
|
+
values.length = 0;
|
|
12
|
+
} else {
|
|
13
|
+
last ??= now;
|
|
14
|
+
const difference = now - last;
|
|
15
|
+
if (difference > 0) {
|
|
16
|
+
values.push(difference);
|
|
17
|
+
}
|
|
18
|
+
last = now;
|
|
19
|
+
requestAnimationFrame(stepper);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
3
22
|
const activeTimers = /* @__PURE__ */ new Set();
|
|
4
23
|
const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
|
|
24
|
+
const destroyedMessage = "Timer has already been destroyed";
|
|
5
25
|
const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
|
|
6
26
|
const endOrRestartTypes = /* @__PURE__ */ new Set([
|
|
7
27
|
"pause",
|
|
@@ -9,10 +29,18 @@ const endOrRestartTypes = /* @__PURE__ */ new Set([
|
|
|
9
29
|
"stop"
|
|
10
30
|
]);
|
|
11
31
|
const hiddenTimers = /* @__PURE__ */ new Set();
|
|
12
|
-
const
|
|
32
|
+
const pauseTypes = /* @__PURE__ */ new Set(["continue", "pause"]);
|
|
33
|
+
const startedMessage = "Timer has already been started";
|
|
34
|
+
const values = [];
|
|
35
|
+
let last;
|
|
36
|
+
exports.milliseconds = Math.floor(1e3 / 60 * 2) / 2;
|
|
37
|
+
requestAnimationFrame(stepper);
|
|
38
|
+
|
|
13
39
|
exports.activeTimers = activeTimers;
|
|
14
40
|
exports.beginTypes = beginTypes;
|
|
41
|
+
exports.destroyedMessage = destroyedMessage;
|
|
15
42
|
exports.endOrRestartTypes = endOrRestartTypes;
|
|
16
43
|
exports.endTypes = endTypes;
|
|
17
44
|
exports.hiddenTimers = hiddenTimers;
|
|
18
|
-
exports.
|
|
45
|
+
exports.pauseTypes = pauseTypes;
|
|
46
|
+
exports.startedMessage = startedMessage;
|
package/dist/constants.js
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
|
+
function stepper(now) {
|
|
2
|
+
if (values.length === 7) {
|
|
3
|
+
milliseconds = Math.floor(
|
|
4
|
+
values.slice(2).reduce((first, second) => first + second) / 5 * 2
|
|
5
|
+
) / 2;
|
|
6
|
+
last = void 0;
|
|
7
|
+
values.length = 0;
|
|
8
|
+
} else {
|
|
9
|
+
last ??= now;
|
|
10
|
+
const difference = now - last;
|
|
11
|
+
if (difference > 0) {
|
|
12
|
+
values.push(difference);
|
|
13
|
+
}
|
|
14
|
+
last = now;
|
|
15
|
+
requestAnimationFrame(stepper);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
1
18
|
const activeTimers = /* @__PURE__ */ new Set();
|
|
2
19
|
const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
|
|
20
|
+
const destroyedMessage = "Timer has already been destroyed";
|
|
3
21
|
const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
|
|
4
22
|
const endOrRestartTypes = /* @__PURE__ */ new Set([
|
|
5
23
|
"pause",
|
|
@@ -7,12 +25,11 @@ const endOrRestartTypes = /* @__PURE__ */ new Set([
|
|
|
7
25
|
"stop"
|
|
8
26
|
]);
|
|
9
27
|
const hiddenTimers = /* @__PURE__ */ new Set();
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
28
|
+
const pauseTypes = /* @__PURE__ */ new Set(["continue", "pause"]);
|
|
29
|
+
const startedMessage = "Timer has already been started";
|
|
30
|
+
const values = [];
|
|
31
|
+
let last;
|
|
32
|
+
let milliseconds = Math.floor(1e3 / 60 * 2) / 2;
|
|
33
|
+
requestAnimationFrame(stepper);
|
|
34
|
+
|
|
35
|
+
export { activeTimers, beginTypes, destroyedMessage, endOrRestartTypes, endTypes, hiddenTimers, milliseconds, pauseTypes, startedMessage };
|
package/dist/delay.cjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const constants = require('./constants.cjs');
|
|
6
|
+
const get = require('./get.cjs');
|
|
7
|
+
|
|
8
|
+
function delay(time) {
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
const interval = get.getValidNumber(time, constants.milliseconds);
|
|
11
|
+
let start;
|
|
12
|
+
function step(now) {
|
|
13
|
+
start ??= now;
|
|
14
|
+
if (interval === constants.milliseconds || now - start >= interval - 5) {
|
|
15
|
+
resolve();
|
|
16
|
+
} else {
|
|
17
|
+
requestAnimationFrame(step);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
requestAnimationFrame(step);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exports.delay = delay;
|
package/dist/delay.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { milliseconds } from './constants.js';
|
|
2
|
+
import { getValidNumber } from './get.js';
|
|
3
|
+
|
|
4
|
+
function delay(time) {
|
|
5
|
+
return new Promise((resolve) => {
|
|
6
|
+
const interval = getValidNumber(time, milliseconds);
|
|
7
|
+
let start;
|
|
8
|
+
function step(now) {
|
|
9
|
+
start ??= now;
|
|
10
|
+
if (interval === milliseconds || now - start >= interval - 5) {
|
|
11
|
+
resolve();
|
|
12
|
+
} else {
|
|
13
|
+
requestAnimationFrame(step);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
requestAnimationFrame(step);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { delay };
|
package/dist/get.cjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const _function = require('./node_modules/@oscarpalmer/atoms/dist/function.cjs');
|
|
6
|
+
|
|
7
|
+
function getCallback(value) {
|
|
8
|
+
return typeof value === "function" ? value : _function.noop;
|
|
9
|
+
}
|
|
10
|
+
function getValidNumber(value, defaultValue) {
|
|
11
|
+
return typeof value === "number" && value > (defaultValue ?? 0) ? value : defaultValue ?? 0;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports.getCallback = getCallback;
|
|
15
|
+
exports.getValidNumber = getValidNumber;
|
package/dist/get.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { noop } from './node_modules/@oscarpalmer/atoms/dist/function.js';
|
|
2
|
+
|
|
3
|
+
function getCallback(value) {
|
|
4
|
+
return typeof value === "function" ? value : noop;
|
|
5
|
+
}
|
|
6
|
+
function getValidNumber(value, defaultValue) {
|
|
7
|
+
return typeof value === "number" && value > (defaultValue ?? 0) ? value : defaultValue ?? 0;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { getCallback, getValidNumber };
|
package/dist/global.cjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const constants = require('./constants.cjs');
|
|
4
|
+
|
|
3
5
|
if (globalThis._oscarpalmer_timers == null) {
|
|
4
6
|
Object.defineProperty(globalThis, "_oscarpalmer_timers", {
|
|
5
7
|
get() {
|
|
@@ -7,3 +9,16 @@ if (globalThis._oscarpalmer_timers == null) {
|
|
|
7
9
|
}
|
|
8
10
|
});
|
|
9
11
|
}
|
|
12
|
+
document.addEventListener("visibilitychange", () => {
|
|
13
|
+
if (document.hidden) {
|
|
14
|
+
for (const timer of constants.activeTimers) {
|
|
15
|
+
constants.hiddenTimers.add(timer);
|
|
16
|
+
timer.pause();
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
for (const timer of constants.hiddenTimers) {
|
|
20
|
+
timer.continue();
|
|
21
|
+
}
|
|
22
|
+
constants.hiddenTimers.clear();
|
|
23
|
+
}
|
|
24
|
+
});
|
package/dist/global.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { activeTimers } from
|
|
1
|
+
import { activeTimers, hiddenTimers } from './constants.js';
|
|
2
|
+
|
|
2
3
|
if (globalThis._oscarpalmer_timers == null) {
|
|
3
4
|
Object.defineProperty(globalThis, "_oscarpalmer_timers", {
|
|
4
5
|
get() {
|
|
@@ -6,3 +7,16 @@ if (globalThis._oscarpalmer_timers == null) {
|
|
|
6
7
|
}
|
|
7
8
|
});
|
|
8
9
|
}
|
|
10
|
+
document.addEventListener("visibilitychange", () => {
|
|
11
|
+
if (document.hidden) {
|
|
12
|
+
for (const timer of activeTimers) {
|
|
13
|
+
hiddenTimers.add(timer);
|
|
14
|
+
timer.pause();
|
|
15
|
+
}
|
|
16
|
+
} else {
|
|
17
|
+
for (const timer of hiddenTimers) {
|
|
18
|
+
timer.continue();
|
|
19
|
+
}
|
|
20
|
+
hiddenTimers.clear();
|
|
21
|
+
}
|
|
22
|
+
});
|
package/dist/index.cjs
CHANGED
|
@@ -1,49 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
require(
|
|
6
|
-
const
|
|
7
|
-
const is = require(
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
(resolve ?? _function.noop)();
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
timeout,
|
|
19
|
-
errorCallback: () => {
|
|
20
|
-
delayed == null ? void 0 : delayed.destroy();
|
|
21
|
-
delayed = null;
|
|
22
|
-
(reject ?? _function.noop)();
|
|
23
|
-
},
|
|
24
|
-
interval: time
|
|
25
|
-
}
|
|
26
|
-
);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
document.addEventListener("visibilitychange", () => {
|
|
30
|
-
if (document.hidden) {
|
|
31
|
-
for (const timer2 of constants.activeTimers) {
|
|
32
|
-
constants.hiddenTimers.add(timer2);
|
|
33
|
-
timer2.pause();
|
|
34
|
-
}
|
|
35
|
-
} else {
|
|
36
|
-
for (const timer2 of constants.hiddenTimers) {
|
|
37
|
-
timer2.continue();
|
|
38
|
-
}
|
|
39
|
-
constants.hiddenTimers.clear();
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
exports.repeat = timer.repeat;
|
|
43
|
-
exports.wait = timer.wait;
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
require('./global.cjs');
|
|
6
|
+
const delay = require('./delay.cjs');
|
|
7
|
+
const is = require('./is.cjs');
|
|
8
|
+
const repeat = require('./repeat.cjs');
|
|
9
|
+
const wait = require('./wait.cjs');
|
|
10
|
+
const when = require('./when.cjs');
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
exports.delay = delay.delay;
|
|
44
15
|
exports.isRepeated = is.isRepeated;
|
|
45
16
|
exports.isTimer = is.isTimer;
|
|
46
17
|
exports.isWaited = is.isWaited;
|
|
47
18
|
exports.isWhen = is.isWhen;
|
|
19
|
+
exports.repeat = repeat.repeat;
|
|
20
|
+
exports.wait = wait.wait;
|
|
48
21
|
exports.when = when.when;
|
|
49
|
-
exports.delay = delay;
|
package/dist/index.js
CHANGED
|
@@ -1,50 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { when } from "./when.js";
|
|
8
|
-
function delay(time, timeout) {
|
|
9
|
-
return new Promise((resolve, reject) => {
|
|
10
|
-
let delayed = wait(
|
|
11
|
-
() => {
|
|
12
|
-
delayed == null ? void 0 : delayed.destroy();
|
|
13
|
-
delayed = null;
|
|
14
|
-
(resolve ?? noop)();
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
timeout,
|
|
18
|
-
errorCallback: () => {
|
|
19
|
-
delayed == null ? void 0 : delayed.destroy();
|
|
20
|
-
delayed = null;
|
|
21
|
-
(reject ?? noop)();
|
|
22
|
-
},
|
|
23
|
-
interval: time
|
|
24
|
-
}
|
|
25
|
-
);
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
document.addEventListener("visibilitychange", () => {
|
|
29
|
-
if (document.hidden) {
|
|
30
|
-
for (const timer of activeTimers) {
|
|
31
|
-
hiddenTimers.add(timer);
|
|
32
|
-
timer.pause();
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
for (const timer of hiddenTimers) {
|
|
36
|
-
timer.continue();
|
|
37
|
-
}
|
|
38
|
-
hiddenTimers.clear();
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
export {
|
|
42
|
-
delay,
|
|
43
|
-
isRepeated,
|
|
44
|
-
isTimer,
|
|
45
|
-
isWaited,
|
|
46
|
-
isWhen,
|
|
47
|
-
repeat,
|
|
48
|
-
wait,
|
|
49
|
-
when
|
|
50
|
-
};
|
|
1
|
+
import './global.js';
|
|
2
|
+
export { delay } from './delay.js';
|
|
3
|
+
export { isRepeated, isTimer, isWaited, isWhen } from './is.js';
|
|
4
|
+
export { repeat } from './repeat.js';
|
|
5
|
+
export { wait } from './wait.js';
|
|
6
|
+
export { when } from './when.js';
|
package/dist/is.cjs
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
function is(names, value) {
|
|
6
|
+
return names.includes(value?.$timer);
|
|
5
7
|
}
|
|
6
8
|
function isRepeated(value) {
|
|
7
|
-
return is(
|
|
9
|
+
return is(["repeat"], value);
|
|
8
10
|
}
|
|
9
11
|
function isTimer(value) {
|
|
10
|
-
return is(
|
|
12
|
+
return is(["repeat", "wait"], value);
|
|
11
13
|
}
|
|
12
14
|
function isWaited(value) {
|
|
13
|
-
return is(
|
|
15
|
+
return is(["wait"], value);
|
|
14
16
|
}
|
|
15
17
|
function isWhen(value) {
|
|
16
|
-
return is(
|
|
18
|
+
return is(["when"], value) && typeof value.then === "function";
|
|
17
19
|
}
|
|
20
|
+
|
|
18
21
|
exports.isRepeated = isRepeated;
|
|
19
22
|
exports.isTimer = isTimer;
|
|
20
23
|
exports.isWaited = isWaited;
|
package/dist/is.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
|
-
function is(
|
|
2
|
-
return
|
|
1
|
+
function is(names, value) {
|
|
2
|
+
return names.includes(value?.$timer);
|
|
3
3
|
}
|
|
4
4
|
function isRepeated(value) {
|
|
5
|
-
return is(
|
|
5
|
+
return is(["repeat"], value);
|
|
6
6
|
}
|
|
7
7
|
function isTimer(value) {
|
|
8
|
-
return is(
|
|
8
|
+
return is(["repeat", "wait"], value);
|
|
9
9
|
}
|
|
10
10
|
function isWaited(value) {
|
|
11
|
-
return is(
|
|
11
|
+
return is(["wait"], value);
|
|
12
12
|
}
|
|
13
13
|
function isWhen(value) {
|
|
14
|
-
return is(
|
|
14
|
+
return is(["when"], value) && typeof value.then === "function";
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
isTimer,
|
|
19
|
-
isWaited,
|
|
20
|
-
isWhen
|
|
21
|
-
};
|
|
16
|
+
|
|
17
|
+
export { isRepeated, isTimer, isWaited, isWhen };
|
package/dist/models.cjs
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
3
5
|
class TimerTrace extends Error {
|
|
4
6
|
constructor() {
|
|
5
7
|
super();
|
|
6
8
|
this.name = "TimerTrace";
|
|
7
9
|
}
|
|
8
10
|
}
|
|
11
|
+
|
|
9
12
|
exports.TimerTrace = TimerTrace;
|
package/dist/models.js
CHANGED
package/dist/repeat.cjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const constants = require('./constants.cjs');
|
|
6
|
+
const get = require('./get.cjs');
|
|
7
|
+
const models = require('./models.cjs');
|
|
8
|
+
const timer = require('./timer.cjs');
|
|
9
|
+
const work = require('./work.cjs');
|
|
10
|
+
|
|
11
|
+
function repeat(callback, options) {
|
|
12
|
+
return new timer.Timer(
|
|
13
|
+
"repeat",
|
|
14
|
+
work.work,
|
|
15
|
+
{
|
|
16
|
+
callback: get.getCallback(callback),
|
|
17
|
+
trace: new models.TimerTrace().stack
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
onAfter: get.getCallback(options?.onAfter),
|
|
21
|
+
onError: void 0,
|
|
22
|
+
count: get.getValidNumber(options?.count),
|
|
23
|
+
interval: get.getValidNumber(options?.interval, constants.milliseconds),
|
|
24
|
+
timeout: 0
|
|
25
|
+
},
|
|
26
|
+
true
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
exports.repeat = repeat;
|
package/dist/repeat.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { milliseconds } from './constants.js';
|
|
2
|
+
import { getCallback, getValidNumber } from './get.js';
|
|
3
|
+
import { TimerTrace } from './models.js';
|
|
4
|
+
import { Timer } from './timer.js';
|
|
5
|
+
import { work } from './work.js';
|
|
6
|
+
|
|
7
|
+
function repeat(callback, options) {
|
|
8
|
+
return new Timer(
|
|
9
|
+
"repeat",
|
|
10
|
+
work,
|
|
11
|
+
{
|
|
12
|
+
callback: getCallback(callback),
|
|
13
|
+
trace: new TimerTrace().stack
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
onAfter: getCallback(options?.onAfter),
|
|
17
|
+
onError: void 0,
|
|
18
|
+
count: getValidNumber(options?.count),
|
|
19
|
+
interval: getValidNumber(options?.interval, milliseconds),
|
|
20
|
+
timeout: 0
|
|
21
|
+
},
|
|
22
|
+
true
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { repeat };
|
package/dist/timer.cjs
CHANGED
|
@@ -1,111 +1,99 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
class Timer {
|
|
6
|
+
constructor(type, worker, state, options, start) {
|
|
7
|
+
this.worker = worker;
|
|
8
|
+
this.options = options;
|
|
8
9
|
this.$timer = type;
|
|
9
|
-
this.state =
|
|
10
|
+
this.state = {
|
|
11
|
+
...state,
|
|
12
|
+
active: false,
|
|
13
|
+
destroyed: false,
|
|
14
|
+
elapsed: 0,
|
|
15
|
+
frame: void 0,
|
|
16
|
+
index: 0,
|
|
17
|
+
paused: false,
|
|
18
|
+
total: 0
|
|
19
|
+
};
|
|
20
|
+
if (start) {
|
|
21
|
+
this.start();
|
|
22
|
+
}
|
|
10
23
|
}
|
|
11
|
-
|
|
12
|
-
|
|
24
|
+
state;
|
|
25
|
+
/**
|
|
26
|
+
* Is the timer active?
|
|
27
|
+
*/
|
|
13
28
|
get active() {
|
|
14
29
|
return this.state.active;
|
|
15
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Is the timer destroyed?
|
|
33
|
+
*/
|
|
16
34
|
get destroyed() {
|
|
17
35
|
return this.state.destroyed;
|
|
18
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Is the timer paused?
|
|
39
|
+
*/
|
|
19
40
|
get paused() {
|
|
20
41
|
return this.state.paused;
|
|
21
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Get the timer's origin _(if debugging is enabled)_
|
|
45
|
+
*/
|
|
22
46
|
get trace() {
|
|
23
|
-
return globalThis._oscarpalmer_timer_debug ? this.state.trace : void 0;
|
|
24
|
-
}
|
|
25
|
-
constructor(type, state, options) {
|
|
26
|
-
super(type, state);
|
|
27
|
-
this.options = options;
|
|
47
|
+
return globalThis._oscarpalmer_timer_debug ?? false ? this.state.trace : void 0;
|
|
28
48
|
}
|
|
29
49
|
/**
|
|
30
|
-
*
|
|
50
|
+
* Continue running the timer _(if it's paused)_
|
|
31
51
|
*/
|
|
32
52
|
continue() {
|
|
33
|
-
return
|
|
53
|
+
return this.#work("continue");
|
|
34
54
|
}
|
|
35
55
|
/**
|
|
36
|
-
*
|
|
56
|
+
* Destroy the timer
|
|
37
57
|
*/
|
|
38
58
|
destroy() {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.stop();
|
|
42
|
-
this.options.afterCallback = void 0;
|
|
43
|
-
this.options.errorCallback = void 0;
|
|
44
|
-
this.state.callback = void 0;
|
|
45
|
-
this.state.trace = void 0;
|
|
46
|
-
}
|
|
59
|
+
this.state.destroyed = true;
|
|
60
|
+
this.#work("stop");
|
|
47
61
|
}
|
|
48
62
|
/**
|
|
49
|
-
*
|
|
63
|
+
* Pause the timer _(if it's running)_
|
|
50
64
|
*/
|
|
51
65
|
pause() {
|
|
52
|
-
return
|
|
66
|
+
return this.#work("pause");
|
|
53
67
|
}
|
|
54
68
|
/**
|
|
55
|
-
*
|
|
69
|
+
* Restart the timer _(or start it, if it's not running)_
|
|
56
70
|
*/
|
|
57
71
|
restart() {
|
|
58
|
-
return
|
|
72
|
+
return this.#work("restart");
|
|
59
73
|
}
|
|
60
74
|
/**
|
|
61
|
-
*
|
|
75
|
+
* Start the timer _(if it's not running)_
|
|
62
76
|
*/
|
|
63
77
|
start() {
|
|
64
|
-
return
|
|
78
|
+
return this.#work("start");
|
|
65
79
|
}
|
|
66
80
|
/**
|
|
67
|
-
*
|
|
81
|
+
* Stop the timer _(if it's running)_
|
|
68
82
|
*/
|
|
69
83
|
stop() {
|
|
70
|
-
return
|
|
84
|
+
return this.#work("stop");
|
|
71
85
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
callback,
|
|
83
|
-
isRepeated,
|
|
84
|
-
active: false,
|
|
85
|
-
destroyed: false,
|
|
86
|
-
minimum: options.interval - options.interval % constants.milliseconds / 2,
|
|
87
|
-
paused: false,
|
|
88
|
-
trace: new models.TimerTrace().stack
|
|
89
|
-
},
|
|
90
|
-
options
|
|
91
|
-
);
|
|
92
|
-
if (start) {
|
|
93
|
-
instance.start();
|
|
86
|
+
#work(type) {
|
|
87
|
+
return this.worker(
|
|
88
|
+
type,
|
|
89
|
+
{
|
|
90
|
+
instance: this,
|
|
91
|
+
type: this.$timer
|
|
92
|
+
},
|
|
93
|
+
this.state,
|
|
94
|
+
this.options
|
|
95
|
+
);
|
|
94
96
|
}
|
|
95
|
-
return instance;
|
|
96
|
-
}
|
|
97
|
-
function wait(callback, options) {
|
|
98
|
-
return timer(
|
|
99
|
-
"wait",
|
|
100
|
-
callback,
|
|
101
|
-
options == null || typeof options === "number" ? {
|
|
102
|
-
interval: options
|
|
103
|
-
} : options,
|
|
104
|
-
true
|
|
105
|
-
);
|
|
106
97
|
}
|
|
107
|
-
|
|
98
|
+
|
|
108
99
|
exports.Timer = Timer;
|
|
109
|
-
exports.repeat = repeat;
|
|
110
|
-
exports.timer = timer;
|
|
111
|
-
exports.wait = wait;
|