@oscarpalmer/timer 0.25.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 -2
- 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
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const activeTimers = /* @__PURE__ */ new Set();
|
|
4
|
+
const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
|
|
5
|
+
const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
|
|
6
|
+
const endOrRestartTypes = /* @__PURE__ */ new Set([
|
|
7
|
+
"pause",
|
|
8
|
+
"restart",
|
|
9
|
+
"stop"
|
|
10
|
+
]);
|
|
11
|
+
const hiddenTimers = /* @__PURE__ */ new Set();
|
|
12
|
+
const milliseconds = 1e3 / 60;
|
|
13
|
+
exports.activeTimers = activeTimers;
|
|
14
|
+
exports.beginTypes = beginTypes;
|
|
15
|
+
exports.endOrRestartTypes = endOrRestartTypes;
|
|
16
|
+
exports.endTypes = endTypes;
|
|
17
|
+
exports.hiddenTimers = hiddenTimers;
|
|
18
|
+
exports.milliseconds = milliseconds;
|
package/dist/constants.js
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var endOrRestartTypes = new Set([
|
|
1
|
+
const activeTimers = /* @__PURE__ */ new Set();
|
|
2
|
+
const beginTypes = /* @__PURE__ */ new Set(["continue", "start"]);
|
|
3
|
+
const endTypes = /* @__PURE__ */ new Set(["pause", "stop"]);
|
|
4
|
+
const endOrRestartTypes = /* @__PURE__ */ new Set([
|
|
6
5
|
"pause",
|
|
7
6
|
"restart",
|
|
8
7
|
"stop"
|
|
9
8
|
]);
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const hiddenTimers = /* @__PURE__ */ new Set();
|
|
10
|
+
const milliseconds = 1e3 / 60;
|
|
12
11
|
export {
|
|
13
|
-
|
|
14
|
-
hiddenTimers,
|
|
15
|
-
endTypes,
|
|
16
|
-
endOrRestartTypes,
|
|
12
|
+
activeTimers,
|
|
17
13
|
beginTypes,
|
|
18
|
-
|
|
14
|
+
endOrRestartTypes,
|
|
15
|
+
endTypes,
|
|
16
|
+
hiddenTimers,
|
|
17
|
+
milliseconds
|
|
19
18
|
};
|
|
@@ -1,42 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
beginTypes,
|
|
5
|
-
endOrRestartTypes,
|
|
6
|
-
endTypes,
|
|
7
|
-
milliseconds
|
|
8
|
-
} from "./constants";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const constants = require("./constants.cjs");
|
|
9
4
|
function getOptions(options, isRepeated) {
|
|
10
5
|
return {
|
|
11
6
|
afterCallback: options.afterCallback,
|
|
12
|
-
count: getValueOrDefault(
|
|
7
|
+
count: getValueOrDefault(
|
|
8
|
+
options.count,
|
|
9
|
+
isRepeated ? Number.POSITIVE_INFINITY : 1
|
|
10
|
+
),
|
|
13
11
|
errorCallback: options.errorCallback,
|
|
14
|
-
interval: getValueOrDefault(options.interval, milliseconds, milliseconds),
|
|
15
|
-
timeout: getValueOrDefault(
|
|
12
|
+
interval: getValueOrDefault(options.interval, constants.milliseconds, constants.milliseconds),
|
|
13
|
+
timeout: getValueOrDefault(
|
|
14
|
+
options.timeout,
|
|
15
|
+
isRepeated ? Number.POSITIVE_INFINITY : 3e4
|
|
16
|
+
)
|
|
16
17
|
};
|
|
17
18
|
}
|
|
18
19
|
function getValueOrDefault(value, defaultValue, minimum) {
|
|
19
20
|
return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
|
|
20
21
|
}
|
|
21
22
|
function work(type, timer, state, options) {
|
|
22
|
-
|
|
23
|
+
var _a;
|
|
24
|
+
if (state.destroyed && type !== "stop" || constants.beginTypes.has(type) && state.active || constants.endTypes.has(type) && !state.active) {
|
|
23
25
|
return timer;
|
|
24
26
|
}
|
|
25
27
|
const { count, interval, timeout } = options;
|
|
26
28
|
const { isRepeated, minimum } = state;
|
|
27
|
-
if (endOrRestartTypes.has(type)) {
|
|
29
|
+
if (constants.endOrRestartTypes.has(type)) {
|
|
28
30
|
const isStop = type === "stop";
|
|
29
|
-
activeTimers.delete(timer);
|
|
31
|
+
constants.activeTimers.delete(timer);
|
|
30
32
|
cancelAnimationFrame(state.frame);
|
|
31
33
|
if (isStop) {
|
|
32
|
-
options.afterCallback
|
|
34
|
+
(_a = options.afterCallback) == null ? void 0 : _a.call(options, false);
|
|
33
35
|
}
|
|
34
36
|
state.active = false;
|
|
35
|
-
state.frame =
|
|
37
|
+
state.frame = void 0;
|
|
36
38
|
state.paused = !isStop;
|
|
37
39
|
if (isStop) {
|
|
38
|
-
state.elapsed =
|
|
39
|
-
state.index =
|
|
40
|
+
state.elapsed = void 0;
|
|
41
|
+
state.index = void 0;
|
|
40
42
|
}
|
|
41
43
|
return type === "restart" ? work("start", timer, state, options) : timer;
|
|
42
44
|
}
|
|
@@ -46,26 +48,27 @@ function work(type, timer, state, options) {
|
|
|
46
48
|
let index = type === "continue" ? +(state.index ?? 0) : 0;
|
|
47
49
|
state.elapsed = elapsed;
|
|
48
50
|
state.index = index;
|
|
49
|
-
const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
|
|
51
|
+
const total = (count === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count - index) * (interval > 0 ? interval : constants.milliseconds)) - elapsed;
|
|
50
52
|
let current;
|
|
51
53
|
let start;
|
|
52
54
|
function finish(finished, error) {
|
|
53
|
-
|
|
55
|
+
var _a2, _b;
|
|
56
|
+
constants.activeTimers.delete(timer);
|
|
54
57
|
state.active = false;
|
|
55
|
-
state.elapsed =
|
|
56
|
-
state.frame =
|
|
57
|
-
state.index =
|
|
58
|
+
state.elapsed = void 0;
|
|
59
|
+
state.frame = void 0;
|
|
60
|
+
state.index = void 0;
|
|
58
61
|
if (error) {
|
|
59
|
-
options.errorCallback
|
|
62
|
+
(_a2 = options.errorCallback) == null ? void 0 : _a2.call(options);
|
|
60
63
|
}
|
|
61
|
-
options.afterCallback
|
|
64
|
+
(_b = options.afterCallback) == null ? void 0 : _b.call(options, finished);
|
|
62
65
|
}
|
|
63
66
|
function step(timestamp) {
|
|
64
67
|
if (!state.active) {
|
|
65
68
|
return;
|
|
66
69
|
}
|
|
67
|
-
current
|
|
68
|
-
start
|
|
70
|
+
current ?? (current = timestamp);
|
|
71
|
+
start ?? (start = timestamp);
|
|
69
72
|
const time = timestamp - current;
|
|
70
73
|
state.elapsed = elapsed + (current - start);
|
|
71
74
|
const finished = time - elapsed >= total;
|
|
@@ -75,7 +78,7 @@ function work(type, timer, state, options) {
|
|
|
75
78
|
}
|
|
76
79
|
if (finished || time >= minimum) {
|
|
77
80
|
if (state.active) {
|
|
78
|
-
state.callback(isRepeated ? index :
|
|
81
|
+
state.callback(isRepeated ? index : void 0);
|
|
79
82
|
}
|
|
80
83
|
index += 1;
|
|
81
84
|
state.index = index;
|
|
@@ -88,12 +91,10 @@ function work(type, timer, state, options) {
|
|
|
88
91
|
}
|
|
89
92
|
state.frame = requestAnimationFrame(step);
|
|
90
93
|
}
|
|
91
|
-
activeTimers.add(timer);
|
|
94
|
+
constants.activeTimers.add(timer);
|
|
92
95
|
state.frame = requestAnimationFrame(step);
|
|
93
96
|
return timer;
|
|
94
97
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
getOptions
|
|
99
|
-
};
|
|
98
|
+
exports.getOptions = getOptions;
|
|
99
|
+
exports.getValueOrDefault = getValueOrDefault;
|
|
100
|
+
exports.work = work;
|
package/dist/functions.js
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
var activeTimers = new Set;
|
|
3
|
-
var beginTypes = new Set(["continue", "start"]);
|
|
4
|
-
var endTypes = new Set(["pause", "stop"]);
|
|
5
|
-
var endOrRestartTypes = new Set([
|
|
6
|
-
"pause",
|
|
7
|
-
"restart",
|
|
8
|
-
"stop"
|
|
9
|
-
]);
|
|
10
|
-
var hiddenTimers = new Set;
|
|
11
|
-
var milliseconds = 1000 / 60;
|
|
12
|
-
|
|
13
|
-
// src/functions.ts
|
|
1
|
+
import { beginTypes, endTypes, endOrRestartTypes, activeTimers, milliseconds } from "./constants.js";
|
|
14
2
|
function getOptions(options, isRepeated) {
|
|
15
3
|
return {
|
|
16
4
|
afterCallback: options.afterCallback,
|
|
17
|
-
count: getValueOrDefault(
|
|
5
|
+
count: getValueOrDefault(
|
|
6
|
+
options.count,
|
|
7
|
+
isRepeated ? Number.POSITIVE_INFINITY : 1
|
|
8
|
+
),
|
|
18
9
|
errorCallback: options.errorCallback,
|
|
19
10
|
interval: getValueOrDefault(options.interval, milliseconds, milliseconds),
|
|
20
|
-
timeout: getValueOrDefault(
|
|
11
|
+
timeout: getValueOrDefault(
|
|
12
|
+
options.timeout,
|
|
13
|
+
isRepeated ? Number.POSITIVE_INFINITY : 3e4
|
|
14
|
+
)
|
|
21
15
|
};
|
|
22
16
|
}
|
|
23
17
|
function getValueOrDefault(value, defaultValue, minimum) {
|
|
24
18
|
return typeof value === "number" && value > (minimum ?? 0) ? value : defaultValue;
|
|
25
19
|
}
|
|
26
20
|
function work(type, timer, state, options) {
|
|
21
|
+
var _a;
|
|
27
22
|
if (state.destroyed && type !== "stop" || beginTypes.has(type) && state.active || endTypes.has(type) && !state.active) {
|
|
28
23
|
return timer;
|
|
29
24
|
}
|
|
@@ -34,14 +29,14 @@ function work(type, timer, state, options) {
|
|
|
34
29
|
activeTimers.delete(timer);
|
|
35
30
|
cancelAnimationFrame(state.frame);
|
|
36
31
|
if (isStop) {
|
|
37
|
-
options.afterCallback
|
|
32
|
+
(_a = options.afterCallback) == null ? void 0 : _a.call(options, false);
|
|
38
33
|
}
|
|
39
34
|
state.active = false;
|
|
40
|
-
state.frame =
|
|
35
|
+
state.frame = void 0;
|
|
41
36
|
state.paused = !isStop;
|
|
42
37
|
if (isStop) {
|
|
43
|
-
state.elapsed =
|
|
44
|
-
state.index =
|
|
38
|
+
state.elapsed = void 0;
|
|
39
|
+
state.index = void 0;
|
|
45
40
|
}
|
|
46
41
|
return type === "restart" ? work("start", timer, state, options) : timer;
|
|
47
42
|
}
|
|
@@ -55,22 +50,23 @@ function work(type, timer, state, options) {
|
|
|
55
50
|
let current;
|
|
56
51
|
let start;
|
|
57
52
|
function finish(finished, error) {
|
|
53
|
+
var _a2, _b;
|
|
58
54
|
activeTimers.delete(timer);
|
|
59
55
|
state.active = false;
|
|
60
|
-
state.elapsed =
|
|
61
|
-
state.frame =
|
|
62
|
-
state.index =
|
|
56
|
+
state.elapsed = void 0;
|
|
57
|
+
state.frame = void 0;
|
|
58
|
+
state.index = void 0;
|
|
63
59
|
if (error) {
|
|
64
|
-
options.errorCallback
|
|
60
|
+
(_a2 = options.errorCallback) == null ? void 0 : _a2.call(options);
|
|
65
61
|
}
|
|
66
|
-
options.afterCallback
|
|
62
|
+
(_b = options.afterCallback) == null ? void 0 : _b.call(options, finished);
|
|
67
63
|
}
|
|
68
64
|
function step(timestamp) {
|
|
69
65
|
if (!state.active) {
|
|
70
66
|
return;
|
|
71
67
|
}
|
|
72
|
-
current
|
|
73
|
-
start
|
|
68
|
+
current ?? (current = timestamp);
|
|
69
|
+
start ?? (start = timestamp);
|
|
74
70
|
const time = timestamp - current;
|
|
75
71
|
state.elapsed = elapsed + (current - start);
|
|
76
72
|
const finished = time - elapsed >= total;
|
|
@@ -80,7 +76,7 @@ function work(type, timer, state, options) {
|
|
|
80
76
|
}
|
|
81
77
|
if (finished || time >= minimum) {
|
|
82
78
|
if (state.active) {
|
|
83
|
-
state.callback(isRepeated ? index :
|
|
79
|
+
state.callback(isRepeated ? index : void 0);
|
|
84
80
|
}
|
|
85
81
|
index += 1;
|
|
86
82
|
state.index = index;
|
|
@@ -98,7 +94,7 @@ function work(type, timer, state, options) {
|
|
|
98
94
|
return timer;
|
|
99
95
|
}
|
|
100
96
|
export {
|
|
101
|
-
|
|
97
|
+
getOptions,
|
|
102
98
|
getValueOrDefault,
|
|
103
|
-
|
|
99
|
+
work
|
|
104
100
|
};
|
package/dist/global.cjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const constants = require("./constants.cjs");
|
|
3
|
+
if (globalThis._oscarpalmer_timers == null) {
|
|
4
|
+
Object.defineProperty(globalThis, "_oscarpalmer_timers", {
|
|
5
|
+
get() {
|
|
6
|
+
return globalThis._oscarpalmer_timer_debug ? [...constants.activeTimers] : [];
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
}
|
package/dist/global.js
CHANGED
|
@@ -1,16 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
var activeTimers = new Set;
|
|
3
|
-
var beginTypes = new Set(["continue", "start"]);
|
|
4
|
-
var endTypes = new Set(["pause", "stop"]);
|
|
5
|
-
var endOrRestartTypes = new Set([
|
|
6
|
-
"pause",
|
|
7
|
-
"restart",
|
|
8
|
-
"stop"
|
|
9
|
-
]);
|
|
10
|
-
var hiddenTimers = new Set;
|
|
11
|
-
var milliseconds = 1000 / 60;
|
|
12
|
-
|
|
13
|
-
// src/global.ts
|
|
1
|
+
import { activeTimers } from "./constants.js";
|
|
14
2
|
if (globalThis._oscarpalmer_timers == null) {
|
|
15
3
|
Object.defineProperty(globalThis, "_oscarpalmer_timers", {
|
|
16
4
|
get() {
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const constants = require("./constants.cjs");
|
|
4
|
+
require("./global.cjs");
|
|
5
|
+
const _function = require("@oscarpalmer/atoms/function");
|
|
6
|
+
const timer = require("./timer.cjs");
|
|
7
|
+
const is = require("./is.cjs");
|
|
8
|
+
const when = require("./when.cjs");
|
|
9
|
+
function delay(time, timeout) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
const delayed = timer.wait(
|
|
12
|
+
() => {
|
|
13
|
+
delayed.destroy();
|
|
14
|
+
(resolve ?? _function.noop)();
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
timeout,
|
|
18
|
+
errorCallback: () => {
|
|
19
|
+
delayed.destroy();
|
|
20
|
+
(reject ?? _function.noop)();
|
|
21
|
+
},
|
|
22
|
+
interval: time
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
document.addEventListener("visibilitychange", () => {
|
|
28
|
+
if (document.hidden) {
|
|
29
|
+
for (const timer2 of constants.activeTimers) {
|
|
30
|
+
constants.hiddenTimers.add(timer2);
|
|
31
|
+
timer2.pause();
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
for (const timer2 of constants.hiddenTimers) {
|
|
35
|
+
timer2.continue();
|
|
36
|
+
}
|
|
37
|
+
constants.hiddenTimers.clear();
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
exports.repeat = timer.repeat;
|
|
41
|
+
exports.wait = timer.wait;
|
|
42
|
+
exports.isRepeated = is.isRepeated;
|
|
43
|
+
exports.isTimer = is.isTimer;
|
|
44
|
+
exports.isWaited = is.isWaited;
|
|
45
|
+
exports.isWhen = is.isWhen;
|
|
46
|
+
exports.when = when.when;
|
|
47
|
+
exports.delay = delay;
|