@oscarpalmer/atoms 0.44.0 → 0.46.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/js/index.js +39 -26
- package/dist/js/timer.js +38 -25
- package/dist/js/timer.mjs +38 -25
- package/package.json +3 -2
- package/src/js/timer.ts +75 -57
- package/types/timer.d.ts +2 -2
package/dist/js/index.js
CHANGED
|
@@ -840,6 +840,9 @@ function getRandomHex() {
|
|
|
840
840
|
return "0123456789ABCDEF"[getRandomInteger(0, 16)];
|
|
841
841
|
}
|
|
842
842
|
// src/js/timer.ts
|
|
843
|
+
var getValueOrDefault = function(value, defaultValue) {
|
|
844
|
+
return typeof value === "number" && value > 0 ? value : defaultValue;
|
|
845
|
+
};
|
|
843
846
|
var is3 = function(value, pattern) {
|
|
844
847
|
return pattern.test(value?.$timer);
|
|
845
848
|
};
|
|
@@ -856,20 +859,16 @@ function isWhen(value) {
|
|
|
856
859
|
return is3(value, /^when$/) && typeof value.then === "function";
|
|
857
860
|
}
|
|
858
861
|
function repeat(callback, options) {
|
|
859
|
-
return timer("repeat", callback, {
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
count: typeof options.count === "number" && options.count > 0 ? options.count : 1,
|
|
870
|
-
errorCallback: options.errorCallback,
|
|
871
|
-
interval: typeof options.interval === "number" && options.interval >= 0 ? options.interval : 0,
|
|
872
|
-
timeout: typeof options.timeout === "number" && options.timeout > 0 ? options.timeout : 30000
|
|
862
|
+
return timer("repeat", callback, options ?? {}).start();
|
|
863
|
+
}
|
|
864
|
+
var timer = function(type, callback, partial) {
|
|
865
|
+
const isRepeated2 = type === "repeat";
|
|
866
|
+
const options = {
|
|
867
|
+
afterCallback: partial.afterCallback,
|
|
868
|
+
count: getValueOrDefault(partial.count, isRepeated2 ? Number.POSITIVE_INFINITY : 1),
|
|
869
|
+
errorCallback: partial.errorCallback,
|
|
870
|
+
interval: getValueOrDefault(partial.interval, 0),
|
|
871
|
+
timeout: getValueOrDefault(partial.timeout, isRepeated2 ? Number.POSITIVE_INFINITY : 30000)
|
|
873
872
|
};
|
|
874
873
|
const state = {
|
|
875
874
|
callback,
|
|
@@ -877,13 +876,13 @@ var timer = function(type, callback, options) {
|
|
|
877
876
|
};
|
|
878
877
|
const instance = Object.create({
|
|
879
878
|
restart() {
|
|
880
|
-
return work2("restart", this, state,
|
|
879
|
+
return work2("restart", this, state, options, isRepeated2);
|
|
881
880
|
},
|
|
882
881
|
start() {
|
|
883
|
-
return work2("start", this, state,
|
|
882
|
+
return work2("start", this, state, options, isRepeated2);
|
|
884
883
|
},
|
|
885
884
|
stop() {
|
|
886
|
-
return work2("stop", this, state,
|
|
885
|
+
return work2("stop", this, state, options, isRepeated2);
|
|
887
886
|
}
|
|
888
887
|
});
|
|
889
888
|
Object.defineProperties(instance, {
|
|
@@ -901,12 +900,9 @@ var timer = function(type, callback, options) {
|
|
|
901
900
|
return instance;
|
|
902
901
|
};
|
|
903
902
|
function wait(callback, options) {
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
errorCallback: optionsIsNumber ? undefined : options?.errorCallback,
|
|
908
|
-
interval: optionsIsNumber ? options : options?.interval ?? 0
|
|
909
|
-
}).start();
|
|
903
|
+
return timer("wait", callback, options == null || typeof options === "number" ? {
|
|
904
|
+
interval: options
|
|
905
|
+
} : options).start();
|
|
910
906
|
}
|
|
911
907
|
function when(condition, options) {
|
|
912
908
|
let rejecter;
|
|
@@ -954,7 +950,7 @@ function when(condition, options) {
|
|
|
954
950
|
});
|
|
955
951
|
return instance;
|
|
956
952
|
}
|
|
957
|
-
var work2 = function(type, timer2, state, options) {
|
|
953
|
+
var work2 = function(type, timer2, state, options, isRepeated2) {
|
|
958
954
|
if (type === "start" && state.active || type === "stop" && !state.active) {
|
|
959
955
|
return timer2;
|
|
960
956
|
}
|
|
@@ -964,12 +960,13 @@ var work2 = function(type, timer2, state, options) {
|
|
|
964
960
|
options.afterCallback?.(false);
|
|
965
961
|
}
|
|
966
962
|
if (type === "stop") {
|
|
963
|
+
activeTimers.delete(timer2);
|
|
967
964
|
state.active = false;
|
|
968
965
|
state.frame = undefined;
|
|
969
966
|
return timer2;
|
|
970
967
|
}
|
|
971
968
|
state.active = true;
|
|
972
|
-
const
|
|
969
|
+
const canTimeout = timeout > 0 && timeout < Number.POSITIVE_INFINITY;
|
|
973
970
|
const total = count === Number.POSITIVE_INFINITY ? timeout : count * (interval > 0 ? interval : 17);
|
|
974
971
|
let current;
|
|
975
972
|
let start;
|
|
@@ -996,7 +993,7 @@ var work2 = function(type, timer2, state, options) {
|
|
|
996
993
|
}
|
|
997
994
|
index += 1;
|
|
998
995
|
switch (true) {
|
|
999
|
-
case (!finished && timestamp - start >= timeout):
|
|
996
|
+
case (canTimeout && !finished && timestamp - start >= timeout):
|
|
1000
997
|
finish(false, true);
|
|
1001
998
|
return;
|
|
1002
999
|
case (!finished && index < count):
|
|
@@ -1009,9 +1006,25 @@ var work2 = function(type, timer2, state, options) {
|
|
|
1009
1006
|
}
|
|
1010
1007
|
state.frame = requestAnimationFrame(step);
|
|
1011
1008
|
}
|
|
1009
|
+
activeTimers.add(timer2);
|
|
1012
1010
|
state.frame = requestAnimationFrame(step);
|
|
1013
1011
|
return timer2;
|
|
1014
1012
|
};
|
|
1013
|
+
var activeTimers = new Set;
|
|
1014
|
+
var hiddenTimers = new Set;
|
|
1015
|
+
document.addEventListener("visibilitychange", () => {
|
|
1016
|
+
if (document.hidden) {
|
|
1017
|
+
for (const timer2 of activeTimers) {
|
|
1018
|
+
hiddenTimers.add(timer2);
|
|
1019
|
+
timer2.stop();
|
|
1020
|
+
}
|
|
1021
|
+
} else {
|
|
1022
|
+
for (const timer2 of hiddenTimers) {
|
|
1023
|
+
timer2.start();
|
|
1024
|
+
}
|
|
1025
|
+
hiddenTimers.clear();
|
|
1026
|
+
}
|
|
1027
|
+
});
|
|
1015
1028
|
// src/js/touch.ts
|
|
1016
1029
|
var supportsTouch = (() => {
|
|
1017
1030
|
let value = false;
|
package/dist/js/timer.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// src/js/timer.ts
|
|
2
|
+
var getValueOrDefault = function(value, defaultValue) {
|
|
3
|
+
return typeof value === "number" && value > 0 ? value : defaultValue;
|
|
4
|
+
};
|
|
2
5
|
var is = function(value, pattern) {
|
|
3
6
|
return pattern.test(value?.$timer);
|
|
4
7
|
};
|
|
@@ -15,20 +18,16 @@ function isWhen(value) {
|
|
|
15
18
|
return is(value, /^when$/) && typeof value.then === "function";
|
|
16
19
|
}
|
|
17
20
|
function repeat(callback, options) {
|
|
18
|
-
return timer("repeat", callback, {
|
|
19
|
-
...options ?? {},
|
|
20
|
-
...{
|
|
21
|
-
count: typeof options?.count === "number" ? options.count > 0 ? options.count : 1 : Number.POSITIVE_INFINITY
|
|
22
|
-
}
|
|
23
|
-
}).start();
|
|
21
|
+
return timer("repeat", callback, options ?? {}).start();
|
|
24
22
|
}
|
|
25
|
-
var timer = function(type, callback,
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
var timer = function(type, callback, partial) {
|
|
24
|
+
const isRepeated2 = type === "repeat";
|
|
25
|
+
const options = {
|
|
26
|
+
afterCallback: partial.afterCallback,
|
|
27
|
+
count: getValueOrDefault(partial.count, isRepeated2 ? Number.POSITIVE_INFINITY : 1),
|
|
28
|
+
errorCallback: partial.errorCallback,
|
|
29
|
+
interval: getValueOrDefault(partial.interval, 0),
|
|
30
|
+
timeout: getValueOrDefault(partial.timeout, isRepeated2 ? Number.POSITIVE_INFINITY : 30000)
|
|
32
31
|
};
|
|
33
32
|
const state = {
|
|
34
33
|
callback,
|
|
@@ -36,13 +35,13 @@ var timer = function(type, callback, options) {
|
|
|
36
35
|
};
|
|
37
36
|
const instance = Object.create({
|
|
38
37
|
restart() {
|
|
39
|
-
return work("restart", this, state,
|
|
38
|
+
return work("restart", this, state, options, isRepeated2);
|
|
40
39
|
},
|
|
41
40
|
start() {
|
|
42
|
-
return work("start", this, state,
|
|
41
|
+
return work("start", this, state, options, isRepeated2);
|
|
43
42
|
},
|
|
44
43
|
stop() {
|
|
45
|
-
return work("stop", this, state,
|
|
44
|
+
return work("stop", this, state, options, isRepeated2);
|
|
46
45
|
}
|
|
47
46
|
});
|
|
48
47
|
Object.defineProperties(instance, {
|
|
@@ -60,12 +59,9 @@ var timer = function(type, callback, options) {
|
|
|
60
59
|
return instance;
|
|
61
60
|
};
|
|
62
61
|
function wait(callback, options) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
errorCallback: optionsIsNumber ? undefined : options?.errorCallback,
|
|
67
|
-
interval: optionsIsNumber ? options : options?.interval ?? 0
|
|
68
|
-
}).start();
|
|
62
|
+
return timer("wait", callback, options == null || typeof options === "number" ? {
|
|
63
|
+
interval: options
|
|
64
|
+
} : options).start();
|
|
69
65
|
}
|
|
70
66
|
function when(condition, options) {
|
|
71
67
|
let rejecter;
|
|
@@ -113,7 +109,7 @@ function when(condition, options) {
|
|
|
113
109
|
});
|
|
114
110
|
return instance;
|
|
115
111
|
}
|
|
116
|
-
var work = function(type, timer2, state, options) {
|
|
112
|
+
var work = function(type, timer2, state, options, isRepeated2) {
|
|
117
113
|
if (type === "start" && state.active || type === "stop" && !state.active) {
|
|
118
114
|
return timer2;
|
|
119
115
|
}
|
|
@@ -123,12 +119,13 @@ var work = function(type, timer2, state, options) {
|
|
|
123
119
|
options.afterCallback?.(false);
|
|
124
120
|
}
|
|
125
121
|
if (type === "stop") {
|
|
122
|
+
activeTimers.delete(timer2);
|
|
126
123
|
state.active = false;
|
|
127
124
|
state.frame = undefined;
|
|
128
125
|
return timer2;
|
|
129
126
|
}
|
|
130
127
|
state.active = true;
|
|
131
|
-
const
|
|
128
|
+
const canTimeout = timeout > 0 && timeout < Number.POSITIVE_INFINITY;
|
|
132
129
|
const total = count === Number.POSITIVE_INFINITY ? timeout : count * (interval > 0 ? interval : 17);
|
|
133
130
|
let current;
|
|
134
131
|
let start;
|
|
@@ -155,7 +152,7 @@ var work = function(type, timer2, state, options) {
|
|
|
155
152
|
}
|
|
156
153
|
index += 1;
|
|
157
154
|
switch (true) {
|
|
158
|
-
case (!finished && timestamp - start >= timeout):
|
|
155
|
+
case (canTimeout && !finished && timestamp - start >= timeout):
|
|
159
156
|
finish(false, true);
|
|
160
157
|
return;
|
|
161
158
|
case (!finished && index < count):
|
|
@@ -168,9 +165,25 @@ var work = function(type, timer2, state, options) {
|
|
|
168
165
|
}
|
|
169
166
|
state.frame = requestAnimationFrame(step);
|
|
170
167
|
}
|
|
168
|
+
activeTimers.add(timer2);
|
|
171
169
|
state.frame = requestAnimationFrame(step);
|
|
172
170
|
return timer2;
|
|
173
171
|
};
|
|
172
|
+
var activeTimers = new Set;
|
|
173
|
+
var hiddenTimers = new Set;
|
|
174
|
+
document.addEventListener("visibilitychange", () => {
|
|
175
|
+
if (document.hidden) {
|
|
176
|
+
for (const timer2 of activeTimers) {
|
|
177
|
+
hiddenTimers.add(timer2);
|
|
178
|
+
timer2.stop();
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
for (const timer2 of hiddenTimers) {
|
|
182
|
+
timer2.start();
|
|
183
|
+
}
|
|
184
|
+
hiddenTimers.clear();
|
|
185
|
+
}
|
|
186
|
+
});
|
|
174
187
|
export {
|
|
175
188
|
when,
|
|
176
189
|
wait,
|
package/dist/js/timer.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// src/js/timer.ts
|
|
2
|
+
var getValueOrDefault = function(value, defaultValue) {
|
|
3
|
+
return typeof value === "number" && value > 0 ? value : defaultValue;
|
|
4
|
+
};
|
|
2
5
|
var is = function(value, pattern) {
|
|
3
6
|
return pattern.test(value?.$timer);
|
|
4
7
|
};
|
|
@@ -15,20 +18,16 @@ function isWhen(value) {
|
|
|
15
18
|
return is(value, /^when$/) && typeof value.then === "function";
|
|
16
19
|
}
|
|
17
20
|
function repeat(callback, options) {
|
|
18
|
-
return timer("repeat", callback, {
|
|
19
|
-
...options ?? {},
|
|
20
|
-
...{
|
|
21
|
-
count: typeof options?.count === "number" ? options.count > 0 ? options.count : 1 : Number.POSITIVE_INFINITY
|
|
22
|
-
}
|
|
23
|
-
}).start();
|
|
21
|
+
return timer("repeat", callback, options ?? {}).start();
|
|
24
22
|
}
|
|
25
|
-
var timer = function(type, callback,
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
var timer = function(type, callback, partial) {
|
|
24
|
+
const isRepeated2 = type === "repeat";
|
|
25
|
+
const options = {
|
|
26
|
+
afterCallback: partial.afterCallback,
|
|
27
|
+
count: getValueOrDefault(partial.count, isRepeated2 ? Number.POSITIVE_INFINITY : 1),
|
|
28
|
+
errorCallback: partial.errorCallback,
|
|
29
|
+
interval: getValueOrDefault(partial.interval, 0),
|
|
30
|
+
timeout: getValueOrDefault(partial.timeout, isRepeated2 ? Number.POSITIVE_INFINITY : 30000)
|
|
32
31
|
};
|
|
33
32
|
const state = {
|
|
34
33
|
callback,
|
|
@@ -36,13 +35,13 @@ var timer = function(type, callback, options) {
|
|
|
36
35
|
};
|
|
37
36
|
const instance = Object.create({
|
|
38
37
|
restart() {
|
|
39
|
-
return work("restart", this, state,
|
|
38
|
+
return work("restart", this, state, options, isRepeated2);
|
|
40
39
|
},
|
|
41
40
|
start() {
|
|
42
|
-
return work("start", this, state,
|
|
41
|
+
return work("start", this, state, options, isRepeated2);
|
|
43
42
|
},
|
|
44
43
|
stop() {
|
|
45
|
-
return work("stop", this, state,
|
|
44
|
+
return work("stop", this, state, options, isRepeated2);
|
|
46
45
|
}
|
|
47
46
|
});
|
|
48
47
|
Object.defineProperties(instance, {
|
|
@@ -60,12 +59,9 @@ var timer = function(type, callback, options) {
|
|
|
60
59
|
return instance;
|
|
61
60
|
};
|
|
62
61
|
function wait(callback, options) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
errorCallback: optionsIsNumber ? undefined : options?.errorCallback,
|
|
67
|
-
interval: optionsIsNumber ? options : options?.interval ?? 0
|
|
68
|
-
}).start();
|
|
62
|
+
return timer("wait", callback, options == null || typeof options === "number" ? {
|
|
63
|
+
interval: options
|
|
64
|
+
} : options).start();
|
|
69
65
|
}
|
|
70
66
|
function when(condition, options) {
|
|
71
67
|
let rejecter;
|
|
@@ -113,7 +109,7 @@ function when(condition, options) {
|
|
|
113
109
|
});
|
|
114
110
|
return instance;
|
|
115
111
|
}
|
|
116
|
-
var work = function(type, timer2, state, options) {
|
|
112
|
+
var work = function(type, timer2, state, options, isRepeated2) {
|
|
117
113
|
if (type === "start" && state.active || type === "stop" && !state.active) {
|
|
118
114
|
return timer2;
|
|
119
115
|
}
|
|
@@ -123,12 +119,13 @@ var work = function(type, timer2, state, options) {
|
|
|
123
119
|
options.afterCallback?.(false);
|
|
124
120
|
}
|
|
125
121
|
if (type === "stop") {
|
|
122
|
+
activeTimers.delete(timer2);
|
|
126
123
|
state.active = false;
|
|
127
124
|
state.frame = undefined;
|
|
128
125
|
return timer2;
|
|
129
126
|
}
|
|
130
127
|
state.active = true;
|
|
131
|
-
const
|
|
128
|
+
const canTimeout = timeout > 0 && timeout < Number.POSITIVE_INFINITY;
|
|
132
129
|
const total = count === Number.POSITIVE_INFINITY ? timeout : count * (interval > 0 ? interval : 17);
|
|
133
130
|
let current;
|
|
134
131
|
let start;
|
|
@@ -155,7 +152,7 @@ var work = function(type, timer2, state, options) {
|
|
|
155
152
|
}
|
|
156
153
|
index += 1;
|
|
157
154
|
switch (true) {
|
|
158
|
-
case (!finished && timestamp - start >= timeout):
|
|
155
|
+
case (canTimeout && !finished && timestamp - start >= timeout):
|
|
159
156
|
finish(false, true);
|
|
160
157
|
return;
|
|
161
158
|
case (!finished && index < count):
|
|
@@ -168,9 +165,25 @@ var work = function(type, timer2, state, options) {
|
|
|
168
165
|
}
|
|
169
166
|
state.frame = requestAnimationFrame(step);
|
|
170
167
|
}
|
|
168
|
+
activeTimers.add(timer2);
|
|
171
169
|
state.frame = requestAnimationFrame(step);
|
|
172
170
|
return timer2;
|
|
173
171
|
};
|
|
172
|
+
var activeTimers = new Set;
|
|
173
|
+
var hiddenTimers = new Set;
|
|
174
|
+
document.addEventListener("visibilitychange", () => {
|
|
175
|
+
if (document.hidden) {
|
|
176
|
+
for (const timer2 of activeTimers) {
|
|
177
|
+
hiddenTimers.add(timer2);
|
|
178
|
+
timer2.stop();
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
for (const timer2 of hiddenTimers) {
|
|
182
|
+
timer2.start();
|
|
183
|
+
}
|
|
184
|
+
hiddenTimers.clear();
|
|
185
|
+
}
|
|
186
|
+
});
|
|
174
187
|
export {
|
|
175
188
|
when,
|
|
176
189
|
wait,
|
package/package.json
CHANGED
|
@@ -120,9 +120,10 @@
|
|
|
120
120
|
"url": "git+https://github.com/oscarpalmer/atoms.git"
|
|
121
121
|
},
|
|
122
122
|
"scripts": {
|
|
123
|
-
"build": "bun run build:css && bun run build:js && bun run types",
|
|
123
|
+
"build": "bun run clean && bun run build:css && bun run build:js && bun run types",
|
|
124
124
|
"build:css": "bunx sass ./src/css:./dist/css --no-source-map",
|
|
125
125
|
"build:js": "bunx bun ./.bun.ts && bunx bun ./.bun.ts --mjs",
|
|
126
|
+
"clean": "rm -rf ./dist && rm -rf ./types && rm ./tsconfig.tsbuildinfo",
|
|
126
127
|
"test": "bun test",
|
|
127
128
|
"types": "bunx tsc -p ./tsconfig.json",
|
|
128
129
|
"watch:css": "bunx sass ./src/css:./dist/css --no-source-map --watch",
|
|
@@ -130,5 +131,5 @@
|
|
|
130
131
|
},
|
|
131
132
|
"type": "module",
|
|
132
133
|
"types": "./types/index.d.ts",
|
|
133
|
-
"version": "0.
|
|
134
|
+
"version": "0.46.0"
|
|
134
135
|
}
|
package/src/js/timer.ts
CHANGED
|
@@ -80,27 +80,34 @@ type TimerOptions = {} & RepeatOptions;
|
|
|
80
80
|
type WaitOptions = {} & BaseOptions & OptionsWithError;
|
|
81
81
|
|
|
82
82
|
export type When = {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Is the timer running?
|
|
85
|
+
*/
|
|
86
|
+
get active(): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Stops the timer
|
|
89
|
+
*/
|
|
90
|
+
stop(): void;
|
|
91
|
+
/**
|
|
92
|
+
* Starts the timer and returns a promise that resolves when the condition is met
|
|
93
|
+
*/
|
|
94
|
+
then(
|
|
95
|
+
resolve?: (() => void) | null,
|
|
96
|
+
reject?: (() => void) | null,
|
|
97
|
+
): Promise<void>;
|
|
98
|
+
};
|
|
99
99
|
|
|
100
100
|
type WhenOptions = {} & OptionsWithCount;
|
|
101
101
|
|
|
102
102
|
type WorkType = 'restart' | 'start' | 'stop';
|
|
103
103
|
|
|
104
|
+
const activeTimers = new Set<Timer>();
|
|
105
|
+
const hiddenTimers = new Set<Timer>();
|
|
106
|
+
|
|
107
|
+
function getValueOrDefault(value: unknown, defaultValue: number): number {
|
|
108
|
+
return typeof value === 'number' && value > 0 ? value : defaultValue;
|
|
109
|
+
}
|
|
110
|
+
|
|
104
111
|
function is(value: unknown, pattern: RegExp) {
|
|
105
112
|
return pattern.test((value as PlainObject)?.$timer as string);
|
|
106
113
|
}
|
|
@@ -138,47 +145,36 @@ export function isWhen(value: unknown): value is When {
|
|
|
138
145
|
* - calls a callback after a certain amount of time...
|
|
139
146
|
* - ... and repeats it a certain amount of times
|
|
140
147
|
* ---
|
|
141
|
-
* - `options.count` defaults to `Infinity`
|
|
148
|
+
* - `options.count` defaults to `Infinity`
|
|
142
149
|
* - `options.interval` defaults to `0`
|
|
143
|
-
* - `options.timeout` defaults to `
|
|
150
|
+
* - `options.timeout` defaults to `Infinity`
|
|
144
151
|
*/
|
|
145
152
|
export function repeat(
|
|
146
153
|
callback: IndexedCallback,
|
|
147
154
|
options?: Partial<RepeatOptions>,
|
|
148
155
|
): Timer {
|
|
149
|
-
return timer('repeat', callback, {
|
|
150
|
-
...(options ?? {}),
|
|
151
|
-
...{
|
|
152
|
-
count:
|
|
153
|
-
typeof options?.count === 'number'
|
|
154
|
-
? options.count > 0
|
|
155
|
-
? options.count
|
|
156
|
-
: 1
|
|
157
|
-
: Number.POSITIVE_INFINITY,
|
|
158
|
-
},
|
|
159
|
-
}).start();
|
|
156
|
+
return timer('repeat', callback, options ?? {}).start();
|
|
160
157
|
}
|
|
161
158
|
|
|
162
159
|
function timer(
|
|
163
160
|
type: 'repeat' | 'wait',
|
|
164
161
|
callback: AnyCallback,
|
|
165
|
-
|
|
162
|
+
partial: Partial<TimerOptions>,
|
|
166
163
|
): Timer {
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
: 30_000,
|
|
164
|
+
const isRepeated = type === 'repeat';
|
|
165
|
+
|
|
166
|
+
const options: TimerOptions = {
|
|
167
|
+
afterCallback: partial.afterCallback,
|
|
168
|
+
count: getValueOrDefault(
|
|
169
|
+
partial.count,
|
|
170
|
+
isRepeated ? Number.POSITIVE_INFINITY : 1,
|
|
171
|
+
),
|
|
172
|
+
errorCallback: partial.errorCallback,
|
|
173
|
+
interval: getValueOrDefault(partial.interval, 0),
|
|
174
|
+
timeout: getValueOrDefault(
|
|
175
|
+
partial.timeout,
|
|
176
|
+
isRepeated ? Number.POSITIVE_INFINITY : 30_000,
|
|
177
|
+
),
|
|
182
178
|
};
|
|
183
179
|
|
|
184
180
|
const state: State = {
|
|
@@ -188,13 +184,13 @@ function timer(
|
|
|
188
184
|
|
|
189
185
|
const instance = Object.create({
|
|
190
186
|
restart() {
|
|
191
|
-
return work('restart', this as Timer, state,
|
|
187
|
+
return work('restart', this as Timer, state, options, isRepeated);
|
|
192
188
|
},
|
|
193
189
|
start() {
|
|
194
|
-
return work('start', this as Timer, state,
|
|
190
|
+
return work('start', this as Timer, state, options, isRepeated);
|
|
195
191
|
},
|
|
196
192
|
stop() {
|
|
197
|
-
return work('stop', this as Timer, state,
|
|
193
|
+
return work('stop', this as Timer, state, options, isRepeated);
|
|
198
194
|
},
|
|
199
195
|
});
|
|
200
196
|
|
|
@@ -238,13 +234,15 @@ export function wait(
|
|
|
238
234
|
callback: () => void,
|
|
239
235
|
options?: number | Partial<WaitOptions>,
|
|
240
236
|
): Timer {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
237
|
+
return timer(
|
|
238
|
+
'wait',
|
|
239
|
+
callback,
|
|
240
|
+
options == null || typeof options === 'number'
|
|
241
|
+
? {
|
|
242
|
+
interval: options,
|
|
243
|
+
}
|
|
244
|
+
: options,
|
|
245
|
+
).start();
|
|
248
246
|
}
|
|
249
247
|
|
|
250
248
|
/**
|
|
@@ -318,6 +316,7 @@ function work(
|
|
|
318
316
|
timer: Timer,
|
|
319
317
|
state: State,
|
|
320
318
|
options: RepeatOptions,
|
|
319
|
+
isRepeated: boolean,
|
|
321
320
|
): Timer {
|
|
322
321
|
if (
|
|
323
322
|
(type === 'start' && state.active) ||
|
|
@@ -335,6 +334,8 @@ function work(
|
|
|
335
334
|
}
|
|
336
335
|
|
|
337
336
|
if (type === 'stop') {
|
|
337
|
+
activeTimers.delete(timer);
|
|
338
|
+
|
|
338
339
|
state.active = false;
|
|
339
340
|
state.frame = undefined;
|
|
340
341
|
|
|
@@ -343,7 +344,7 @@ function work(
|
|
|
343
344
|
|
|
344
345
|
state.active = true;
|
|
345
346
|
|
|
346
|
-
const
|
|
347
|
+
const canTimeout = timeout > 0 && timeout < Number.POSITIVE_INFINITY;
|
|
347
348
|
|
|
348
349
|
const total =
|
|
349
350
|
count === Number.POSITIVE_INFINITY
|
|
@@ -385,7 +386,7 @@ function work(
|
|
|
385
386
|
index += 1;
|
|
386
387
|
|
|
387
388
|
switch (true) {
|
|
388
|
-
case !finished && timestamp - start >= timeout:
|
|
389
|
+
case canTimeout && !finished && timestamp - start >= timeout:
|
|
389
390
|
finish(false, true);
|
|
390
391
|
return;
|
|
391
392
|
|
|
@@ -402,7 +403,24 @@ function work(
|
|
|
402
403
|
state.frame = requestAnimationFrame(step);
|
|
403
404
|
}
|
|
404
405
|
|
|
406
|
+
activeTimers.add(timer);
|
|
407
|
+
|
|
405
408
|
state.frame = requestAnimationFrame(step);
|
|
406
409
|
|
|
407
410
|
return timer;
|
|
408
411
|
}
|
|
412
|
+
|
|
413
|
+
document.addEventListener('visibilitychange', () => {
|
|
414
|
+
if (document.hidden) {
|
|
415
|
+
for (const timer of activeTimers) {
|
|
416
|
+
hiddenTimers.add(timer);
|
|
417
|
+
timer.stop();
|
|
418
|
+
}
|
|
419
|
+
} else {
|
|
420
|
+
for (const timer of hiddenTimers) {
|
|
421
|
+
timer.start();
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
hiddenTimers.clear();
|
|
425
|
+
}
|
|
426
|
+
});
|
package/types/timer.d.ts
CHANGED
|
@@ -94,9 +94,9 @@ export declare function isWhen(value: unknown): value is When;
|
|
|
94
94
|
* - calls a callback after a certain amount of time...
|
|
95
95
|
* - ... and repeats it a certain amount of times
|
|
96
96
|
* ---
|
|
97
|
-
* - `options.count` defaults to `Infinity`
|
|
97
|
+
* - `options.count` defaults to `Infinity`
|
|
98
98
|
* - `options.interval` defaults to `0`
|
|
99
|
-
* - `options.timeout` defaults to `
|
|
99
|
+
* - `options.timeout` defaults to `Infinity`
|
|
100
100
|
*/
|
|
101
101
|
export declare function repeat(callback: IndexedCallback, options?: Partial<RepeatOptions>): Timer;
|
|
102
102
|
/**
|