@oscarpalmer/atoms 0.69.0 → 0.70.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 +0 -284
- package/dist/js/index.mjs +0 -1
- package/package.json +2 -7
- package/src/js/index.ts +0 -1
- package/types/index.d.cts +285 -942
- package/types/index.d.ts +0 -1
- package/dist/js/timer/constants.mjs +0 -9
- package/dist/js/timer/functions.mjs +0 -93
- package/dist/js/timer/index.js +0 -290
- package/dist/js/timer/index.mjs +0 -46
- package/dist/js/timer/is.mjs +0 -22
- package/dist/js/timer/models.mjs +0 -0
- package/dist/js/timer/repeat.mjs +0 -5
- package/dist/js/timer/timer.mjs +0 -79
- package/dist/js/timer/when.mjs +0 -69
- package/src/js/timer/constants.ts +0 -16
- package/src/js/timer/functions.ts +0 -151
- package/src/js/timer/index.ts +0 -49
- package/src/js/timer/is.ts +0 -35
- package/src/js/timer/models.ts +0 -76
- package/src/js/timer/repeat.ts +0 -0
- package/src/js/timer/timer.ts +0 -186
- package/src/js/timer/when.ts +0 -111
- package/types/timer/constants.d.ts +0 -13
- package/types/timer/functions.d.ts +0 -5
- package/types/timer/index.d.ts +0 -11
- package/types/timer/is.d.ts +0 -18
- package/types/timer/models.d.ts +0 -62
- package/types/timer/repeat.d.ts +0 -1
- package/types/timer/timer.d.ts +0 -72
- package/types/timer/when.d.ts +0 -28
package/dist/js/index.js
CHANGED
|
@@ -1943,282 +1943,6 @@ if (globalThis._atomic_queued == null) {
|
|
|
1943
1943
|
}
|
|
1944
1944
|
});
|
|
1945
1945
|
}
|
|
1946
|
-
// src/js/timer/constants.ts
|
|
1947
|
-
var activeTimers = new Set;
|
|
1948
|
-
var hiddenTimers = new Set;
|
|
1949
|
-
var milliseconds = 1000 / 60;
|
|
1950
|
-
|
|
1951
|
-
// src/js/timer/functions.ts
|
|
1952
|
-
function getOptions(options, isRepeated) {
|
|
1953
|
-
return {
|
|
1954
|
-
afterCallback: options.afterCallback,
|
|
1955
|
-
count: getValueOrDefault(options.count, isRepeated ? Number.POSITIVE_INFINITY : 1),
|
|
1956
|
-
errorCallback: options.errorCallback,
|
|
1957
|
-
interval: getValueOrDefault(options.interval, milliseconds, milliseconds),
|
|
1958
|
-
timeout: getValueOrDefault(options.timeout, isRepeated ? Number.POSITIVE_INFINITY : 30000)
|
|
1959
|
-
};
|
|
1960
|
-
}
|
|
1961
|
-
function getValueOrDefault(value3, defaultValue, minimum) {
|
|
1962
|
-
return typeof value3 === "number" && value3 > (minimum ?? 0) ? value3 : defaultValue;
|
|
1963
|
-
}
|
|
1964
|
-
function work(type, timer, state, options) {
|
|
1965
|
-
if (["continue", "start"].includes(type) && state.active || ["pause", "stop"].includes(type) && !state.active) {
|
|
1966
|
-
return timer;
|
|
1967
|
-
}
|
|
1968
|
-
const { count: count3, interval, timeout } = options;
|
|
1969
|
-
const { isRepeated, minimum } = state;
|
|
1970
|
-
if (["pause", "restart", "stop"].includes(type)) {
|
|
1971
|
-
const isStop = type === "stop";
|
|
1972
|
-
activeTimers.delete(timer);
|
|
1973
|
-
cancelAnimationFrame(state.frame);
|
|
1974
|
-
if (isStop) {
|
|
1975
|
-
options.afterCallback?.(false);
|
|
1976
|
-
}
|
|
1977
|
-
state.active = false;
|
|
1978
|
-
state.frame = undefined;
|
|
1979
|
-
state.paused = !isStop;
|
|
1980
|
-
if (isStop) {
|
|
1981
|
-
state.elapsed = undefined;
|
|
1982
|
-
state.index = undefined;
|
|
1983
|
-
}
|
|
1984
|
-
return type === "restart" ? work("start", timer, state, options) : timer;
|
|
1985
|
-
}
|
|
1986
|
-
state.active = true;
|
|
1987
|
-
state.paused = false;
|
|
1988
|
-
const elapsed = type === "continue" ? +(state.elapsed ?? 0) : 0;
|
|
1989
|
-
let index = type === "continue" ? +(state.index ?? 0) : 0;
|
|
1990
|
-
state.elapsed = elapsed;
|
|
1991
|
-
state.index = index;
|
|
1992
|
-
const total = (count3 === Number.POSITIVE_INFINITY ? Number.POSITIVE_INFINITY : (count3 - index) * (interval > 0 ? interval : milliseconds)) - elapsed;
|
|
1993
|
-
let current;
|
|
1994
|
-
let start;
|
|
1995
|
-
function finish(finished, error) {
|
|
1996
|
-
activeTimers.delete(timer);
|
|
1997
|
-
state.active = false;
|
|
1998
|
-
state.elapsed = undefined;
|
|
1999
|
-
state.frame = undefined;
|
|
2000
|
-
state.index = undefined;
|
|
2001
|
-
if (error) {
|
|
2002
|
-
options.errorCallback?.();
|
|
2003
|
-
}
|
|
2004
|
-
options.afterCallback?.(finished);
|
|
2005
|
-
}
|
|
2006
|
-
function step(timestamp) {
|
|
2007
|
-
if (!state.active) {
|
|
2008
|
-
return;
|
|
2009
|
-
}
|
|
2010
|
-
current ??= timestamp;
|
|
2011
|
-
start ??= timestamp;
|
|
2012
|
-
const time = timestamp - current;
|
|
2013
|
-
state.elapsed = elapsed + (current - start);
|
|
2014
|
-
const finished = time - elapsed >= total;
|
|
2015
|
-
if (timestamp - start >= timeout - elapsed) {
|
|
2016
|
-
finish(finished, !finished);
|
|
2017
|
-
return;
|
|
2018
|
-
}
|
|
2019
|
-
if (finished || time >= minimum) {
|
|
2020
|
-
if (state.active) {
|
|
2021
|
-
state.callback(isRepeated ? index : undefined);
|
|
2022
|
-
}
|
|
2023
|
-
index += 1;
|
|
2024
|
-
state.index = index;
|
|
2025
|
-
if (!finished && index < count3) {
|
|
2026
|
-
current = null;
|
|
2027
|
-
} else {
|
|
2028
|
-
finish(true, false);
|
|
2029
|
-
return;
|
|
2030
|
-
}
|
|
2031
|
-
}
|
|
2032
|
-
state.frame = requestAnimationFrame(step);
|
|
2033
|
-
}
|
|
2034
|
-
activeTimers.add(timer);
|
|
2035
|
-
state.frame = requestAnimationFrame(step);
|
|
2036
|
-
return timer;
|
|
2037
|
-
}
|
|
2038
|
-
|
|
2039
|
-
// src/js/timer/timer.ts
|
|
2040
|
-
function repeat(callback, options) {
|
|
2041
|
-
return timer("repeat", callback, options ?? {}, true);
|
|
2042
|
-
}
|
|
2043
|
-
function timer(type, callback, partial2, start) {
|
|
2044
|
-
const isRepeated = type === "repeat";
|
|
2045
|
-
const options = getOptions(partial2, isRepeated);
|
|
2046
|
-
const instance = new Timer(type, {
|
|
2047
|
-
callback,
|
|
2048
|
-
isRepeated,
|
|
2049
|
-
active: false,
|
|
2050
|
-
minimum: options.interval - options.interval % milliseconds / 2,
|
|
2051
|
-
paused: false,
|
|
2052
|
-
trace: new TimerTrace
|
|
2053
|
-
}, options);
|
|
2054
|
-
if (start) {
|
|
2055
|
-
instance.start();
|
|
2056
|
-
}
|
|
2057
|
-
return instance;
|
|
2058
|
-
}
|
|
2059
|
-
function wait(callback, options) {
|
|
2060
|
-
return timer("wait", callback, options == null || typeof options === "number" ? {
|
|
2061
|
-
interval: options
|
|
2062
|
-
} : options, true);
|
|
2063
|
-
}
|
|
2064
|
-
|
|
2065
|
-
class BasicTimer {
|
|
2066
|
-
constructor(type, state) {
|
|
2067
|
-
this.$timer = type;
|
|
2068
|
-
this.state = state;
|
|
2069
|
-
}
|
|
2070
|
-
}
|
|
2071
|
-
|
|
2072
|
-
class Timer extends BasicTimer {
|
|
2073
|
-
get active() {
|
|
2074
|
-
return this.state.active;
|
|
2075
|
-
}
|
|
2076
|
-
get paused() {
|
|
2077
|
-
return this.state.paused;
|
|
2078
|
-
}
|
|
2079
|
-
get trace() {
|
|
2080
|
-
return globalThis._atomic_timer_debug ? this.state.trace : undefined;
|
|
2081
|
-
}
|
|
2082
|
-
constructor(type, state, options) {
|
|
2083
|
-
super(type, state);
|
|
2084
|
-
this.options = options;
|
|
2085
|
-
}
|
|
2086
|
-
continue() {
|
|
2087
|
-
return work("continue", this, this.state, this.options);
|
|
2088
|
-
}
|
|
2089
|
-
pause() {
|
|
2090
|
-
return work("pause", this, this.state, this.options);
|
|
2091
|
-
}
|
|
2092
|
-
restart() {
|
|
2093
|
-
return work("restart", this, this.state, this.options);
|
|
2094
|
-
}
|
|
2095
|
-
start() {
|
|
2096
|
-
return work("start", this, this.state, this.options);
|
|
2097
|
-
}
|
|
2098
|
-
stop() {
|
|
2099
|
-
return work("stop", this, this.state, this.options);
|
|
2100
|
-
}
|
|
2101
|
-
}
|
|
2102
|
-
|
|
2103
|
-
class TimerTrace extends Error {
|
|
2104
|
-
constructor() {
|
|
2105
|
-
super();
|
|
2106
|
-
this.name = "TimerTrace";
|
|
2107
|
-
}
|
|
2108
|
-
}
|
|
2109
|
-
|
|
2110
|
-
// src/js/timer/index.ts
|
|
2111
|
-
function delay(time, timeout) {
|
|
2112
|
-
return new Promise((resolve, reject) => {
|
|
2113
|
-
wait(resolve ?? noop, {
|
|
2114
|
-
timeout,
|
|
2115
|
-
errorCallback: reject ?? noop,
|
|
2116
|
-
interval: time
|
|
2117
|
-
});
|
|
2118
|
-
});
|
|
2119
|
-
}
|
|
2120
|
-
|
|
2121
|
-
// src/js/timer/is.ts
|
|
2122
|
-
function is12(pattern, value3) {
|
|
2123
|
-
return pattern.test(value3?.$timer);
|
|
2124
|
-
}
|
|
2125
|
-
function isRepeated(value3) {
|
|
2126
|
-
return is12(/^repeat$/, value3);
|
|
2127
|
-
}
|
|
2128
|
-
function isTimer(value3) {
|
|
2129
|
-
return is12(/^repeat|wait$/, value3);
|
|
2130
|
-
}
|
|
2131
|
-
function isWaited(value3) {
|
|
2132
|
-
return is12(/^wait$/, value3);
|
|
2133
|
-
}
|
|
2134
|
-
function isWhen(value3) {
|
|
2135
|
-
return is12(/^when$/, value3) && typeof value3.then === "function";
|
|
2136
|
-
}
|
|
2137
|
-
// src/js/timer/when.ts
|
|
2138
|
-
function when(condition, options) {
|
|
2139
|
-
const repeated = timer("repeat", () => {
|
|
2140
|
-
if (condition()) {
|
|
2141
|
-
repeated.stop();
|
|
2142
|
-
state.resolver?.();
|
|
2143
|
-
}
|
|
2144
|
-
}, {
|
|
2145
|
-
afterCallback() {
|
|
2146
|
-
if (!repeated.paused) {
|
|
2147
|
-
if (condition()) {
|
|
2148
|
-
state.resolver?.();
|
|
2149
|
-
} else {
|
|
2150
|
-
state.rejecter?.();
|
|
2151
|
-
}
|
|
2152
|
-
}
|
|
2153
|
-
},
|
|
2154
|
-
errorCallback() {
|
|
2155
|
-
state.rejecter?.();
|
|
2156
|
-
},
|
|
2157
|
-
count: options?.count,
|
|
2158
|
-
interval: options?.interval,
|
|
2159
|
-
timeout: options?.timeout
|
|
2160
|
-
}, false);
|
|
2161
|
-
const state = {};
|
|
2162
|
-
state.promise = new Promise((resolve, reject) => {
|
|
2163
|
-
state.resolver = resolve;
|
|
2164
|
-
state.rejecter = reject;
|
|
2165
|
-
});
|
|
2166
|
-
state.timer = repeated;
|
|
2167
|
-
return new When(state);
|
|
2168
|
-
}
|
|
2169
|
-
|
|
2170
|
-
class When extends BasicTimer {
|
|
2171
|
-
get active() {
|
|
2172
|
-
return this.state.timer.active;
|
|
2173
|
-
}
|
|
2174
|
-
get paused() {
|
|
2175
|
-
return this.state.timer.paused;
|
|
2176
|
-
}
|
|
2177
|
-
constructor(state) {
|
|
2178
|
-
super("when", state);
|
|
2179
|
-
}
|
|
2180
|
-
continue() {
|
|
2181
|
-
this.state.timer.continue();
|
|
2182
|
-
return this;
|
|
2183
|
-
}
|
|
2184
|
-
pause() {
|
|
2185
|
-
this.state.timer.pause();
|
|
2186
|
-
return this;
|
|
2187
|
-
}
|
|
2188
|
-
stop() {
|
|
2189
|
-
if (this.state.timer.active) {
|
|
2190
|
-
this.state.timer.stop();
|
|
2191
|
-
this.state.rejecter?.();
|
|
2192
|
-
}
|
|
2193
|
-
return this;
|
|
2194
|
-
}
|
|
2195
|
-
then(resolve, reject) {
|
|
2196
|
-
this.state.timer.start();
|
|
2197
|
-
return this.state.promise.then(resolve ?? noop, reject ?? noop);
|
|
2198
|
-
}
|
|
2199
|
-
}
|
|
2200
|
-
|
|
2201
|
-
// src/js/timer/index.ts
|
|
2202
|
-
if (globalThis._atomic_timers == null) {
|
|
2203
|
-
Object.defineProperty(globalThis, "_atomic_timers", {
|
|
2204
|
-
get() {
|
|
2205
|
-
return globalThis._atomic_timer_debug ? [...activeTimers] : [];
|
|
2206
|
-
}
|
|
2207
|
-
});
|
|
2208
|
-
}
|
|
2209
|
-
document.addEventListener("visibilitychange", () => {
|
|
2210
|
-
if (document.hidden) {
|
|
2211
|
-
for (const timer4 of activeTimers) {
|
|
2212
|
-
hiddenTimers.add(timer4);
|
|
2213
|
-
timer4.pause();
|
|
2214
|
-
}
|
|
2215
|
-
} else {
|
|
2216
|
-
for (const timer4 of hiddenTimers) {
|
|
2217
|
-
timer4.continue();
|
|
2218
|
-
}
|
|
2219
|
-
hiddenTimers.clear();
|
|
2220
|
-
}
|
|
2221
|
-
});
|
|
2222
1946
|
// src/js/touch.ts
|
|
2223
1947
|
var supportsTouch = (() => {
|
|
2224
1948
|
let value3 = false;
|
|
@@ -2239,8 +1963,6 @@ var supportsTouch = (() => {
|
|
|
2239
1963
|
})();
|
|
2240
1964
|
export {
|
|
2241
1965
|
words,
|
|
2242
|
-
when,
|
|
2243
|
-
wait,
|
|
2244
1966
|
unsmush,
|
|
2245
1967
|
unique,
|
|
2246
1968
|
truncate,
|
|
@@ -2262,7 +1984,6 @@ export {
|
|
|
2262
1984
|
setAttribute,
|
|
2263
1985
|
sanitise,
|
|
2264
1986
|
round,
|
|
2265
|
-
repeat,
|
|
2266
1987
|
queue,
|
|
2267
1988
|
push,
|
|
2268
1989
|
pascalCase,
|
|
@@ -2276,11 +1997,7 @@ export {
|
|
|
2276
1997
|
logger,
|
|
2277
1998
|
kebabCase,
|
|
2278
1999
|
join,
|
|
2279
|
-
isWhen,
|
|
2280
|
-
isWaited,
|
|
2281
|
-
isTimer,
|
|
2282
2000
|
isTabbableElement,
|
|
2283
|
-
isRepeated,
|
|
2284
2001
|
isRGBColour,
|
|
2285
2002
|
isPrimitive,
|
|
2286
2003
|
isPlainObject,
|
|
@@ -2338,7 +2055,6 @@ export {
|
|
|
2338
2055
|
equal,
|
|
2339
2056
|
emitter,
|
|
2340
2057
|
diff,
|
|
2341
|
-
delay,
|
|
2342
2058
|
debounce,
|
|
2343
2059
|
createUuid,
|
|
2344
2060
|
count,
|
package/dist/js/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
+
"@oscarpalmer/timer": "^0.21.0",
|
|
7
8
|
"type-fest": "^4.23.0"
|
|
8
9
|
},
|
|
9
10
|
"description": "Sweet little atomic goodies…",
|
|
@@ -146,12 +147,6 @@
|
|
|
146
147
|
"import": "./dist/js/touch.mjs",
|
|
147
148
|
"require": "./dist/js/touch.js"
|
|
148
149
|
},
|
|
149
|
-
"./timer": {
|
|
150
|
-
"types": "./types/timer/index.d.ts",
|
|
151
|
-
"bun": "./src/js/timer/index.ts",
|
|
152
|
-
"import": "./dist/js/timer/index.mjs",
|
|
153
|
-
"require": "./dist/js/timer/index.js"
|
|
154
|
-
},
|
|
155
150
|
"./value": {
|
|
156
151
|
"types": "./types/value/index.d.ts",
|
|
157
152
|
"bun": "./src/js/value/index.ts",
|
|
@@ -192,5 +187,5 @@
|
|
|
192
187
|
},
|
|
193
188
|
"type": "module",
|
|
194
189
|
"types": "./types/index.d.cts",
|
|
195
|
-
"version": "0.
|
|
190
|
+
"version": "0.70.0"
|
|
196
191
|
}
|