@ibodr/utils 0.2.5 → 0.2.7
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/index.d.ts +104 -20
- package/dist/index.mjs +346 -24
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -2
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,6 @@ import isEqualWith from 'lodash.isequalwith';
|
|
|
3
3
|
export { default as isEqualWith } from 'lodash.isequalwith';
|
|
4
4
|
export { default as throttle } from 'lodash.throttle';
|
|
5
5
|
export { default as uniq } from 'lodash.uniq';
|
|
6
|
-
import { generateKeyBetween, generateNKeysBetween } from 'jittered-fractional-indexing';
|
|
7
6
|
|
|
8
7
|
// src/lib/version.ts
|
|
9
8
|
function registerDrawLibraryVersion(name, version, modules) {
|
|
@@ -111,8 +110,8 @@ function omitFromStackTrace(fn) {
|
|
|
111
110
|
};
|
|
112
111
|
return wrappedFn;
|
|
113
112
|
}
|
|
114
|
-
|
|
115
|
-
}
|
|
113
|
+
function noop() {
|
|
114
|
+
}
|
|
116
115
|
|
|
117
116
|
// src/lib/control.ts
|
|
118
117
|
var Result = {
|
|
@@ -283,7 +282,10 @@ function debounce(callback, wait) {
|
|
|
283
282
|
fn.cancel = () => {
|
|
284
283
|
if (!state) return;
|
|
285
284
|
clearTimeout(state.timeout);
|
|
285
|
+
const s = state;
|
|
286
286
|
state = void 0;
|
|
287
|
+
s.promise.catch(noop);
|
|
288
|
+
s.reject(new Error("Debounced function was cancelled"));
|
|
287
289
|
};
|
|
288
290
|
return fn;
|
|
289
291
|
}
|
|
@@ -438,11 +440,11 @@ async function fetch(input, init) {
|
|
|
438
440
|
...init
|
|
439
441
|
});
|
|
440
442
|
}
|
|
441
|
-
|
|
443
|
+
function Image(width, height) {
|
|
442
444
|
const img = new window.Image(width, height);
|
|
443
445
|
img.referrerPolicy = "strict-origin-when-cross-origin";
|
|
444
446
|
return img;
|
|
445
|
-
}
|
|
447
|
+
}
|
|
446
448
|
|
|
447
449
|
// src/lib/file.ts
|
|
448
450
|
var FileHelpers = class _FileHelpers {
|
|
@@ -675,6 +677,9 @@ var LruCache = class {
|
|
|
675
677
|
has(key) {
|
|
676
678
|
return this.map.has(key);
|
|
677
679
|
}
|
|
680
|
+
delete(key) {
|
|
681
|
+
return this.map.delete(key);
|
|
682
|
+
}
|
|
678
683
|
// eslint-disable-next-line tldraw/no-setter-getter
|
|
679
684
|
get size() {
|
|
680
685
|
return this.map.size;
|
|
@@ -741,10 +746,10 @@ function isApngAnimated(buffer) {
|
|
|
741
746
|
}
|
|
742
747
|
|
|
743
748
|
// src/lib/media/avif.ts
|
|
744
|
-
|
|
749
|
+
function isAvifAnimated(buffer) {
|
|
745
750
|
const view = new Uint8Array(buffer);
|
|
746
751
|
return view[3] === 44;
|
|
747
|
-
}
|
|
752
|
+
}
|
|
748
753
|
|
|
749
754
|
// src/lib/media/gif.ts
|
|
750
755
|
function getDataBlocksLength(buffer, offset) {
|
|
@@ -1938,11 +1943,210 @@ var PerformanceTracker = class {
|
|
|
1938
1943
|
return this.started;
|
|
1939
1944
|
}
|
|
1940
1945
|
};
|
|
1941
|
-
|
|
1946
|
+
|
|
1947
|
+
// src/lib/fractionalIndexing.ts
|
|
1948
|
+
var DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
1949
|
+
var ZERO = DIGITS[0];
|
|
1950
|
+
var LARGEST_DIGIT = DIGITS[DIGITS.length - 1];
|
|
1951
|
+
var SMALLEST_INTEGER = "A" + ZERO.repeat(26);
|
|
1952
|
+
var JITTER_BITS = 16;
|
|
1953
|
+
function getIntegerLength(head) {
|
|
1954
|
+
if (head >= "a" && head <= "z") {
|
|
1955
|
+
return head.charCodeAt(0) - "a".charCodeAt(0) + 2;
|
|
1956
|
+
} else if (head >= "A" && head <= "Z") {
|
|
1957
|
+
return "Z".charCodeAt(0) - head.charCodeAt(0) + 2;
|
|
1958
|
+
}
|
|
1959
|
+
throw new Error("invalid order key head: " + head);
|
|
1960
|
+
}
|
|
1961
|
+
function getIntegerPart(key) {
|
|
1962
|
+
const integerPartLength = getIntegerLength(key[0]);
|
|
1963
|
+
if (integerPartLength > key.length) {
|
|
1964
|
+
throw new Error("invalid order key: " + key);
|
|
1965
|
+
}
|
|
1966
|
+
return key.slice(0, integerPartLength);
|
|
1967
|
+
}
|
|
1968
|
+
function digitIndex(char) {
|
|
1969
|
+
const code = char.charCodeAt(0);
|
|
1970
|
+
if (code <= 57) return code - 48;
|
|
1971
|
+
if (code <= 90) return code - 55;
|
|
1972
|
+
return code - 61;
|
|
1973
|
+
}
|
|
1974
|
+
function validateOrderKey(key) {
|
|
1975
|
+
if (key === SMALLEST_INTEGER) {
|
|
1976
|
+
throw new Error("invalid order key: " + key);
|
|
1977
|
+
}
|
|
1978
|
+
const i = getIntegerPart(key);
|
|
1979
|
+
if (key.length > i.length && key[key.length - 1] === ZERO) {
|
|
1980
|
+
throw new Error("invalid order key: " + key);
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
function midpoint(a, b) {
|
|
1984
|
+
if (b != null && a >= b) {
|
|
1985
|
+
throw new Error(a + " >= " + b);
|
|
1986
|
+
}
|
|
1987
|
+
if (a.slice(-1) === ZERO || b && b.slice(-1) === ZERO) {
|
|
1988
|
+
throw new Error("trailing zero");
|
|
1989
|
+
}
|
|
1990
|
+
if (b) {
|
|
1991
|
+
let n = 0;
|
|
1992
|
+
while ((a[n] || ZERO) === b[n]) {
|
|
1993
|
+
n++;
|
|
1994
|
+
}
|
|
1995
|
+
if (n > 0) {
|
|
1996
|
+
return b.slice(0, n) + midpoint(a.slice(n), b.slice(n));
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
const digitA = a ? digitIndex(a[0]) : 0;
|
|
2000
|
+
const digitB = b != null ? digitIndex(b[0]) : DIGITS.length;
|
|
2001
|
+
if (digitB - digitA > 1) {
|
|
2002
|
+
const midDigit = Math.round(0.5 * (digitA + digitB));
|
|
2003
|
+
return DIGITS[midDigit];
|
|
2004
|
+
}
|
|
2005
|
+
if (b && b.length > 1) {
|
|
2006
|
+
return b.slice(0, 1);
|
|
2007
|
+
}
|
|
2008
|
+
return DIGITS[digitA] + midpoint(a.slice(1), null);
|
|
2009
|
+
}
|
|
2010
|
+
function incrementInteger(x) {
|
|
2011
|
+
const [head, ...digs] = x.split("");
|
|
2012
|
+
let carry = true;
|
|
2013
|
+
for (let i = digs.length - 1; carry && i >= 0; i--) {
|
|
2014
|
+
const d = digitIndex(digs[i]) + 1;
|
|
2015
|
+
if (d === DIGITS.length) {
|
|
2016
|
+
digs[i] = ZERO;
|
|
2017
|
+
} else {
|
|
2018
|
+
digs[i] = DIGITS[d];
|
|
2019
|
+
carry = false;
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
if (carry) {
|
|
2023
|
+
if (head === "Z") return "a" + ZERO;
|
|
2024
|
+
if (head === "z") return null;
|
|
2025
|
+
const h = String.fromCharCode(head.charCodeAt(0) + 1);
|
|
2026
|
+
if (h > "a") {
|
|
2027
|
+
digs.push(ZERO);
|
|
2028
|
+
} else {
|
|
2029
|
+
digs.pop();
|
|
2030
|
+
}
|
|
2031
|
+
return h + digs.join("");
|
|
2032
|
+
}
|
|
2033
|
+
return head + digs.join("");
|
|
2034
|
+
}
|
|
2035
|
+
function decrementInteger(x) {
|
|
2036
|
+
const [head, ...digs] = x.split("");
|
|
2037
|
+
let borrow = true;
|
|
2038
|
+
for (let i = digs.length - 1; borrow && i >= 0; i--) {
|
|
2039
|
+
const d = digitIndex(digs[i]) - 1;
|
|
2040
|
+
if (d === -1) {
|
|
2041
|
+
digs[i] = LARGEST_DIGIT;
|
|
2042
|
+
} else {
|
|
2043
|
+
digs[i] = DIGITS[d];
|
|
2044
|
+
borrow = false;
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
if (borrow) {
|
|
2048
|
+
if (head === "a") return "Z" + LARGEST_DIGIT;
|
|
2049
|
+
if (head === "A") return null;
|
|
2050
|
+
const h = String.fromCharCode(head.charCodeAt(0) - 1);
|
|
2051
|
+
if (h < "Z") {
|
|
2052
|
+
digs.push(LARGEST_DIGIT);
|
|
2053
|
+
} else {
|
|
2054
|
+
digs.pop();
|
|
2055
|
+
}
|
|
2056
|
+
return h + digs.join("");
|
|
2057
|
+
}
|
|
2058
|
+
return head + digs.join("");
|
|
2059
|
+
}
|
|
2060
|
+
function keyBetweenUnchecked(a, b) {
|
|
2061
|
+
if (a != null && b != null && a >= b) {
|
|
2062
|
+
throw new Error(a + " >= " + b);
|
|
2063
|
+
}
|
|
2064
|
+
if (a == null) {
|
|
2065
|
+
if (b == null) return "a" + ZERO;
|
|
2066
|
+
const ib2 = getIntegerPart(b);
|
|
2067
|
+
const fb2 = b.slice(ib2.length);
|
|
2068
|
+
if (ib2 === SMALLEST_INTEGER) {
|
|
2069
|
+
return ib2 + midpoint("", fb2);
|
|
2070
|
+
}
|
|
2071
|
+
if (ib2 < b) return ib2;
|
|
2072
|
+
const res = decrementInteger(ib2);
|
|
2073
|
+
if (res == null) throw new Error("cannot decrement any more");
|
|
2074
|
+
return res;
|
|
2075
|
+
}
|
|
2076
|
+
if (b == null) {
|
|
2077
|
+
const ia2 = getIntegerPart(a);
|
|
2078
|
+
const fa2 = a.slice(ia2.length);
|
|
2079
|
+
const i2 = incrementInteger(ia2);
|
|
2080
|
+
return i2 == null ? ia2 + midpoint(fa2, null) : i2;
|
|
2081
|
+
}
|
|
2082
|
+
const ia = getIntegerPart(a);
|
|
2083
|
+
const fa = a.slice(ia.length);
|
|
2084
|
+
const ib = getIntegerPart(b);
|
|
2085
|
+
const fb = b.slice(ib.length);
|
|
2086
|
+
if (ia === ib) {
|
|
2087
|
+
return ia + midpoint(fa, fb);
|
|
2088
|
+
}
|
|
2089
|
+
const i = incrementInteger(ia);
|
|
2090
|
+
if (i == null) throw new Error("cannot increment any more");
|
|
2091
|
+
if (i < b) return i;
|
|
2092
|
+
return ia + midpoint(fa, null);
|
|
2093
|
+
}
|
|
2094
|
+
function nKeysBetweenUnchecked(a, b, n) {
|
|
2095
|
+
if (n === 0) return [];
|
|
2096
|
+
if (n === 1) return [keyBetweenUnchecked(a, b)];
|
|
2097
|
+
if (b == null) {
|
|
2098
|
+
let c2 = keyBetweenUnchecked(a, b);
|
|
2099
|
+
const result = [c2];
|
|
2100
|
+
for (let i = 0; i < n - 1; i++) {
|
|
2101
|
+
c2 = keyBetweenUnchecked(c2, b);
|
|
2102
|
+
result.push(c2);
|
|
2103
|
+
}
|
|
2104
|
+
return result;
|
|
2105
|
+
}
|
|
2106
|
+
if (a == null) {
|
|
2107
|
+
let c2 = keyBetweenUnchecked(a, b);
|
|
2108
|
+
const result = [c2];
|
|
2109
|
+
for (let i = 0; i < n - 1; i++) {
|
|
2110
|
+
c2 = keyBetweenUnchecked(a, c2);
|
|
2111
|
+
result.push(c2);
|
|
2112
|
+
}
|
|
2113
|
+
result.reverse();
|
|
2114
|
+
return result;
|
|
2115
|
+
}
|
|
2116
|
+
const mid = Math.floor(n / 2);
|
|
2117
|
+
const c = keyBetweenUnchecked(a, b);
|
|
2118
|
+
return [...nKeysBetweenUnchecked(a, c, mid), c, ...nKeysBetweenUnchecked(c, b, n - mid - 1)];
|
|
2119
|
+
}
|
|
2120
|
+
function jitterBetween(low, high) {
|
|
2121
|
+
let mid = keyBetweenUnchecked(low, high);
|
|
2122
|
+
for (let i = 0; i < JITTER_BITS; i++) {
|
|
2123
|
+
if (Math.random() < 0.5) {
|
|
2124
|
+
low = mid;
|
|
2125
|
+
} else {
|
|
2126
|
+
high = mid;
|
|
2127
|
+
}
|
|
2128
|
+
mid = keyBetweenUnchecked(low, high);
|
|
2129
|
+
}
|
|
2130
|
+
return mid;
|
|
2131
|
+
}
|
|
2132
|
+
function generateNJitteredKeysBetween(a, b, n) {
|
|
2133
|
+
if (n === 0) return [];
|
|
2134
|
+
if (a != null) validateOrderKey(a);
|
|
2135
|
+
if (b != null) validateOrderKey(b);
|
|
2136
|
+
const keys = nKeysBetweenUnchecked(a, b, n + 1);
|
|
2137
|
+
const result = new Array(n);
|
|
2138
|
+
for (let i = 0; i < n; i++) {
|
|
2139
|
+
result[i] = jitterBetween(keys[i], keys[i + 1]);
|
|
2140
|
+
}
|
|
2141
|
+
return result;
|
|
2142
|
+
}
|
|
2143
|
+
|
|
2144
|
+
// src/lib/reordering.ts
|
|
2145
|
+
var generateKeysFn = generateNJitteredKeysBetween;
|
|
1942
2146
|
var ZERO_INDEX_KEY = "a0";
|
|
1943
2147
|
function validateIndexKey(index) {
|
|
1944
2148
|
try {
|
|
1945
|
-
|
|
2149
|
+
validateOrderKey(index);
|
|
1946
2150
|
} catch {
|
|
1947
2151
|
throw new Error("invalid index: " + index);
|
|
1948
2152
|
}
|
|
@@ -2067,6 +2271,34 @@ function clearSessionStorage() {
|
|
|
2067
2271
|
}
|
|
2068
2272
|
}
|
|
2069
2273
|
|
|
2274
|
+
// src/lib/string.ts
|
|
2275
|
+
var graphemeSegmenter;
|
|
2276
|
+
var checkedForSegmenter = false;
|
|
2277
|
+
function getGraphemeSegmenter() {
|
|
2278
|
+
if (!checkedForSegmenter) {
|
|
2279
|
+
checkedForSegmenter = true;
|
|
2280
|
+
if (typeof Intl !== "undefined" && "Segmenter" in Intl) {
|
|
2281
|
+
graphemeSegmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
return graphemeSegmenter;
|
|
2285
|
+
}
|
|
2286
|
+
function* iterateGraphemes(str) {
|
|
2287
|
+
const segmenter = getGraphemeSegmenter();
|
|
2288
|
+
if (segmenter) {
|
|
2289
|
+
for (const { segment } of segmenter.segment(str)) yield segment;
|
|
2290
|
+
} else {
|
|
2291
|
+
yield* str;
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
function getFirstCharacter(str) {
|
|
2295
|
+
if (!str) return "";
|
|
2296
|
+
for (const segment of iterateGraphemes(str)) {
|
|
2297
|
+
return segment;
|
|
2298
|
+
}
|
|
2299
|
+
return "";
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2070
2302
|
// src/lib/stringEnum.ts
|
|
2071
2303
|
function stringEnum(...values) {
|
|
2072
2304
|
const obj = {};
|
|
@@ -2185,6 +2417,9 @@ function throttleToNextFrame(fn) {
|
|
|
2185
2417
|
|
|
2186
2418
|
// src/lib/timers.ts
|
|
2187
2419
|
var Timers = class {
|
|
2420
|
+
// Ids are dropped as soon as a timer fires or is cleared through this class, so a
|
|
2421
|
+
// long-lived context (the editor creates two timeouts per pointer down) does not
|
|
2422
|
+
// accumulate stale ids that `dispose` would otherwise have to clear one by one.
|
|
2188
2423
|
timeouts = /* @__PURE__ */ new Map();
|
|
2189
2424
|
intervals = /* @__PURE__ */ new Map();
|
|
2190
2425
|
rafs = /* @__PURE__ */ new Map();
|
|
@@ -2201,6 +2436,9 @@ var Timers = class {
|
|
|
2201
2436
|
this.setTimeout = this.setTimeout.bind(this);
|
|
2202
2437
|
this.setInterval = this.setInterval.bind(this);
|
|
2203
2438
|
this.requestAnimationFrame = this.requestAnimationFrame.bind(this);
|
|
2439
|
+
this.clearTimeout = this.clearTimeout.bind(this);
|
|
2440
|
+
this.clearInterval = this.clearInterval.bind(this);
|
|
2441
|
+
this.cancelAnimationFrame = this.cancelAnimationFrame.bind(this);
|
|
2204
2442
|
this.dispose = this.dispose.bind(this);
|
|
2205
2443
|
}
|
|
2206
2444
|
/**
|
|
@@ -2219,9 +2457,16 @@ var Timers = class {
|
|
|
2219
2457
|
* @public
|
|
2220
2458
|
*/
|
|
2221
2459
|
setTimeout(contextId, handler, timeout, ...args) {
|
|
2222
|
-
const
|
|
2223
|
-
|
|
2224
|
-
|
|
2460
|
+
const ids = getOrCreateSet(this.timeouts, contextId);
|
|
2461
|
+
let fired = false;
|
|
2462
|
+
let id = -1;
|
|
2463
|
+
const wrapped = typeof handler === "function" ? (...handlerArgs) => {
|
|
2464
|
+
fired = true;
|
|
2465
|
+
ids.delete(id);
|
|
2466
|
+
handler(...handlerArgs);
|
|
2467
|
+
} : handler;
|
|
2468
|
+
id = window.setTimeout(wrapped, timeout, args);
|
|
2469
|
+
if (!fired) ids.add(id);
|
|
2225
2470
|
return id;
|
|
2226
2471
|
}
|
|
2227
2472
|
/**
|
|
@@ -2241,8 +2486,7 @@ var Timers = class {
|
|
|
2241
2486
|
*/
|
|
2242
2487
|
setInterval(contextId, handler, timeout, ...args) {
|
|
2243
2488
|
const id = window.setInterval(handler, timeout, args);
|
|
2244
|
-
|
|
2245
|
-
this.intervals.set(contextId, [...current, id]);
|
|
2489
|
+
getOrCreateSet(this.intervals, contextId).add(id);
|
|
2246
2490
|
return id;
|
|
2247
2491
|
}
|
|
2248
2492
|
/**
|
|
@@ -2259,11 +2503,70 @@ var Timers = class {
|
|
|
2259
2503
|
* @public
|
|
2260
2504
|
*/
|
|
2261
2505
|
requestAnimationFrame(contextId, callback) {
|
|
2262
|
-
const
|
|
2263
|
-
|
|
2264
|
-
|
|
2506
|
+
const ids = getOrCreateSet(this.rafs, contextId);
|
|
2507
|
+
let fired = false;
|
|
2508
|
+
let id = -1;
|
|
2509
|
+
id = window.requestAnimationFrame((time) => {
|
|
2510
|
+
fired = true;
|
|
2511
|
+
ids.delete(id);
|
|
2512
|
+
callback(time);
|
|
2513
|
+
});
|
|
2514
|
+
if (!fired) ids.add(id);
|
|
2265
2515
|
return id;
|
|
2266
2516
|
}
|
|
2517
|
+
/**
|
|
2518
|
+
* Clears a timeout created with {@link Timers.setTimeout} and stops tracking it.
|
|
2519
|
+
* Prefer this over the global `clearTimeout` for ids that came from this instance,
|
|
2520
|
+
* otherwise the id stays tracked until its context is disposed.
|
|
2521
|
+
* @param contextId - The context identifier the timeout was created under.
|
|
2522
|
+
* @param id - The timer ID returned by `setTimeout`. Ignored when undefined.
|
|
2523
|
+
* @example
|
|
2524
|
+
* ```ts
|
|
2525
|
+
* const timers = new Timers()
|
|
2526
|
+
* const id = timers.setTimeout('autosave', () => save(), 5000)
|
|
2527
|
+
* timers.clearTimeout('autosave', id)
|
|
2528
|
+
* ```
|
|
2529
|
+
* @public
|
|
2530
|
+
*/
|
|
2531
|
+
clearTimeout(contextId, id) {
|
|
2532
|
+
if (id === void 0) return;
|
|
2533
|
+
clearTimeout(id);
|
|
2534
|
+
this.timeouts.get(contextId)?.delete(id);
|
|
2535
|
+
}
|
|
2536
|
+
/**
|
|
2537
|
+
* Clears an interval created with {@link Timers.setInterval} and stops tracking it.
|
|
2538
|
+
* @param contextId - The context identifier the interval was created under.
|
|
2539
|
+
* @param id - The interval ID returned by `setInterval`. Ignored when undefined.
|
|
2540
|
+
* @example
|
|
2541
|
+
* ```ts
|
|
2542
|
+
* const timers = new Timers()
|
|
2543
|
+
* const id = timers.setInterval('refresh', () => updateData(), 1000)
|
|
2544
|
+
* timers.clearInterval('refresh', id)
|
|
2545
|
+
* ```
|
|
2546
|
+
* @public
|
|
2547
|
+
*/
|
|
2548
|
+
clearInterval(contextId, id) {
|
|
2549
|
+
if (id === void 0) return;
|
|
2550
|
+
clearInterval(id);
|
|
2551
|
+
this.intervals.get(contextId)?.delete(id);
|
|
2552
|
+
}
|
|
2553
|
+
/**
|
|
2554
|
+
* Cancels an animation frame requested with {@link Timers.requestAnimationFrame} and stops tracking it.
|
|
2555
|
+
* @param contextId - The context identifier the animation frame was requested under.
|
|
2556
|
+
* @param id - The request ID returned by `requestAnimationFrame`. Ignored when undefined.
|
|
2557
|
+
* @example
|
|
2558
|
+
* ```ts
|
|
2559
|
+
* const timers = new Timers()
|
|
2560
|
+
* const id = timers.requestAnimationFrame('render', () => draw())
|
|
2561
|
+
* timers.cancelAnimationFrame('render', id)
|
|
2562
|
+
* ```
|
|
2563
|
+
* @public
|
|
2564
|
+
*/
|
|
2565
|
+
cancelAnimationFrame(contextId, id) {
|
|
2566
|
+
if (id === void 0) return;
|
|
2567
|
+
cancelAnimationFrame(id);
|
|
2568
|
+
this.rafs.get(contextId)?.delete(id);
|
|
2569
|
+
}
|
|
2267
2570
|
/**
|
|
2268
2571
|
* Disposes of all timers associated with the specified context.
|
|
2269
2572
|
* Clears all timeouts, intervals, and animation frames for the given context ID.
|
|
@@ -2304,7 +2607,12 @@ var Timers = class {
|
|
|
2304
2607
|
* @public
|
|
2305
2608
|
*/
|
|
2306
2609
|
disposeAll() {
|
|
2307
|
-
|
|
2610
|
+
const contextIds = /* @__PURE__ */ new Set([
|
|
2611
|
+
...this.timeouts.keys(),
|
|
2612
|
+
...this.intervals.keys(),
|
|
2613
|
+
...this.rafs.keys()
|
|
2614
|
+
]);
|
|
2615
|
+
for (const contextId of contextIds) {
|
|
2308
2616
|
this.dispose(contextId);
|
|
2309
2617
|
}
|
|
2310
2618
|
}
|
|
@@ -2312,17 +2620,20 @@ var Timers = class {
|
|
|
2312
2620
|
* Returns an object with timer methods bound to a specific context.
|
|
2313
2621
|
* Convenient for getting context-specific timer functions without repeatedly passing the contextId.
|
|
2314
2622
|
* @param contextId - The context identifier to bind the returned methods to.
|
|
2315
|
-
* @returns An object with setTimeout, setInterval, requestAnimationFrame, and dispose
|
|
2623
|
+
* @returns An object with setTimeout, setInterval, requestAnimationFrame, the matching clear methods, and dispose bound to the context.
|
|
2316
2624
|
* @example
|
|
2317
2625
|
* ```ts
|
|
2318
2626
|
* const timers = new Timers()
|
|
2319
2627
|
* const uiTimers = timers.forContext('ui')
|
|
2320
2628
|
*
|
|
2321
2629
|
* // These are equivalent to calling timers.setTimeout('ui', ...)
|
|
2322
|
-
* uiTimers.setTimeout(() => console.log('timeout'), 1000)
|
|
2630
|
+
* const id = uiTimers.setTimeout(() => console.log('timeout'), 1000)
|
|
2323
2631
|
* uiTimers.setInterval(() => console.log('interval'), 500)
|
|
2324
2632
|
* uiTimers.requestAnimationFrame(() => console.log('frame'))
|
|
2325
2633
|
*
|
|
2634
|
+
* // Clear a single timer
|
|
2635
|
+
* uiTimers.clearTimeout(id)
|
|
2636
|
+
*
|
|
2326
2637
|
* // Dispose only this context
|
|
2327
2638
|
* uiTimers.dispose()
|
|
2328
2639
|
* ```
|
|
@@ -2333,19 +2644,30 @@ var Timers = class {
|
|
|
2333
2644
|
setTimeout: (handler, timeout, ...args) => this.setTimeout(contextId, handler, timeout, args),
|
|
2334
2645
|
setInterval: (handler, timeout, ...args) => this.setInterval(contextId, handler, timeout, args),
|
|
2335
2646
|
requestAnimationFrame: (callback) => this.requestAnimationFrame(contextId, callback),
|
|
2647
|
+
clearTimeout: (id) => this.clearTimeout(contextId, id),
|
|
2648
|
+
clearInterval: (id) => this.clearInterval(contextId, id),
|
|
2649
|
+
cancelAnimationFrame: (id) => this.cancelAnimationFrame(contextId, id),
|
|
2336
2650
|
dispose: () => this.dispose(contextId)
|
|
2337
2651
|
};
|
|
2338
2652
|
}
|
|
2339
2653
|
};
|
|
2654
|
+
function getOrCreateSet(map, contextId) {
|
|
2655
|
+
let set = map.get(contextId);
|
|
2656
|
+
if (!set) {
|
|
2657
|
+
set = /* @__PURE__ */ new Set();
|
|
2658
|
+
map.set(contextId, set);
|
|
2659
|
+
}
|
|
2660
|
+
return set;
|
|
2661
|
+
}
|
|
2340
2662
|
|
|
2341
2663
|
// src/lib/url.ts
|
|
2342
|
-
|
|
2664
|
+
function safeParseUrl(url, baseUrl) {
|
|
2343
2665
|
try {
|
|
2344
2666
|
return new URL(url, baseUrl);
|
|
2345
2667
|
} catch {
|
|
2346
2668
|
return;
|
|
2347
2669
|
}
|
|
2348
|
-
}
|
|
2670
|
+
}
|
|
2349
2671
|
|
|
2350
2672
|
// src/lib/value.ts
|
|
2351
2673
|
function isDefined(value) {
|
|
@@ -2392,7 +2714,7 @@ function warnOnce(message) {
|
|
|
2392
2714
|
// src/index.ts
|
|
2393
2715
|
registerDrawLibraryVersion(
|
|
2394
2716
|
"@ibodr/utils",
|
|
2395
|
-
"0.2.
|
|
2717
|
+
"0.2.7",
|
|
2396
2718
|
"esm"
|
|
2397
2719
|
);
|
|
2398
2720
|
/*!
|
|
@@ -2428,6 +2750,6 @@ registerDrawLibraryVersion(
|
|
|
2428
2750
|
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
2429
2751
|
*/
|
|
2430
2752
|
|
|
2431
|
-
export { DEFAULT_SUPPORTED_IMAGE_TYPES, DEFAULT_SUPPORTED_MEDIA_TYPES, DEFAULT_SUPPORTED_MEDIA_TYPE_LIST, DEFAULT_SUPPORT_VIDEO_TYPES, ExecutionQueue, FileHelpers, FpsScheduler, Image, LruCache, MediaHelpers, PerformanceTracker, PngHelpers, Result, STRUCTURED_CLONE_OBJECT_PROTOTYPE, Timers, WeakCache, ZERO_INDEX_KEY, annotateError, areArraysShallowEqual, areObjectsShallowEqual, assert, assertExists, bind, clearLocalStorage, clearSessionStorage, compact, debounce, dedupe, deleteFromLocalStorage, deleteFromSessionStorage, exhaustiveSwitchError, fetch, filterEntries, fpsThrottle, getChangedKeys, getErrorAnnotations, getFirstFromIterable, getFromLocalStorage, getFromSessionStorage, getHashForBuffer, getHashForObject, getHashForString, getIndexAbove, getIndexBelow, getIndexBetween, getIndices, getIndicesAbove, getIndicesBelow, getIndicesBetween, getOwnProperty, groupBy, hasOwnProperty, invLerp, isDefined, isEqualAllowingForFloatingPointErrors, isNativeStructuredClone, isNonNull, isNonNullish, last, lerp, lns, mapObjectMapValues, maxBy, measureAverageDuration, measureCbDuration, measureDuration, mergeArraysAndReplaceDefaults, minBy, mockUniqueId, modulate, noop, objectMapEntries, objectMapEntriesIterable, objectMapFromEntries, objectMapKeys, objectMapValues, omit, omitFromStackTrace, partition, promiseWithResolve, registerDrawLibraryVersion, restoreUniqueId, retry, rng, rotateArray, safeParseUrl, setInLocalStorage, setInSessionStorage, sleep, sortById, sortByIndex, sortByMaybeIndex, stringEnum, structuredClone, throttleToNextFrame, uniqueId, validateIndexKey, warnDeprecatedGetter, warnOnce };
|
|
2753
|
+
export { DEFAULT_SUPPORTED_IMAGE_TYPES, DEFAULT_SUPPORTED_MEDIA_TYPES, DEFAULT_SUPPORTED_MEDIA_TYPE_LIST, DEFAULT_SUPPORT_VIDEO_TYPES, ExecutionQueue, FileHelpers, FpsScheduler, Image, LruCache, MediaHelpers, PerformanceTracker, PngHelpers, Result, STRUCTURED_CLONE_OBJECT_PROTOTYPE, Timers, WeakCache, ZERO_INDEX_KEY, annotateError, areArraysShallowEqual, areObjectsShallowEqual, assert, assertExists, bind, clearLocalStorage, clearSessionStorage, compact, debounce, dedupe, deleteFromLocalStorage, deleteFromSessionStorage, exhaustiveSwitchError, fetch, filterEntries, fpsThrottle, getChangedKeys, getErrorAnnotations, getFirstCharacter, getFirstFromIterable, getFromLocalStorage, getFromSessionStorage, getHashForBuffer, getHashForObject, getHashForString, getIndexAbove, getIndexBelow, getIndexBetween, getIndices, getIndicesAbove, getIndicesBelow, getIndicesBetween, getOwnProperty, groupBy, hasOwnProperty, invLerp, isDefined, isEqualAllowingForFloatingPointErrors, isNativeStructuredClone, isNonNull, isNonNullish, iterateGraphemes, last, lerp, lns, mapObjectMapValues, maxBy, measureAverageDuration, measureCbDuration, measureDuration, mergeArraysAndReplaceDefaults, minBy, mockUniqueId, modulate, noop, objectMapEntries, objectMapEntriesIterable, objectMapFromEntries, objectMapKeys, objectMapValues, omit, omitFromStackTrace, partition, promiseWithResolve, registerDrawLibraryVersion, restoreUniqueId, retry, rng, rotateArray, safeParseUrl, setInLocalStorage, setInSessionStorage, sleep, sortById, sortByIndex, sortByMaybeIndex, stringEnum, structuredClone, throttleToNextFrame, uniqueId, validateIndexKey, warnDeprecatedGetter, warnOnce };
|
|
2432
2754
|
//# sourceMappingURL=index.mjs.map
|
|
2433
2755
|
//# sourceMappingURL=index.mjs.map
|