@tldraw/utils 5.5.0-canary.eef775d9b2dd → 5.5.0-canary.efb270e2aaf8
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-cjs/index.d.ts +92 -4
- package/dist-cjs/index.js +4 -5
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/LruCache.js +3 -0
- package/dist-cjs/lib/LruCache.js.map +2 -2
- package/dist-cjs/lib/throttle.js +13 -0
- package/dist-cjs/lib/throttle.js.map +3 -3
- package/dist-cjs/lib/timers.js +101 -11
- package/dist-cjs/lib/timers.js.map +2 -2
- package/dist-esm/index.d.mts +92 -4
- package/dist-esm/index.mjs +10 -6
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/LruCache.mjs +3 -0
- package/dist-esm/lib/LruCache.mjs.map +2 -2
- package/dist-esm/lib/throttle.mjs +3 -0
- package/dist-esm/lib/throttle.mjs.map +2 -2
- package/dist-esm/lib/timers.mjs +101 -11
- package/dist-esm/lib/timers.mjs.map +2 -2
- package/package.json +12 -14
- package/src/index.ts +7 -2
- package/src/lib/LruCache.test.ts +12 -0
- package/src/lib/LruCache.ts +4 -0
- package/src/lib/storage.test.ts +11 -20
- package/src/lib/throttle.ts +46 -0
- package/src/lib/timers.test.ts +156 -1
- package/src/lib/timers.ts +117 -14
package/dist-cjs/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { default as isEqual } from 'lodash.isequal';
|
|
2
2
|
import { default as isEqualWith } from 'lodash.isequalwith';
|
|
3
|
-
import { default as throttle } from 'lodash.throttle';
|
|
4
3
|
import { default as uniq } from 'lodash.uniq';
|
|
5
4
|
|
|
6
5
|
/* Excluded from this release type: annotateError */
|
|
@@ -931,6 +930,7 @@ export declare class LruCache<K, V> {
|
|
|
931
930
|
get(key: K): undefined | V;
|
|
932
931
|
set(key: K, value: V): void;
|
|
933
932
|
has(key: K): boolean;
|
|
933
|
+
delete(key: K): boolean;
|
|
934
934
|
get size(): number;
|
|
935
935
|
}
|
|
936
936
|
|
|
@@ -1790,7 +1790,48 @@ export declare function sortByMaybeIndex<T extends {
|
|
|
1790
1790
|
declare const structuredClone_2: <T>(i: T) => T;
|
|
1791
1791
|
export { structuredClone_2 as structuredClone }
|
|
1792
1792
|
|
|
1793
|
-
|
|
1793
|
+
/**
|
|
1794
|
+
* Creates a throttled function that invokes `fn` at most once per `wait` milliseconds. Wraps
|
|
1795
|
+
* lodash's `throttle`, typed with {@link ThrottledFunction} so the published types don't depend
|
|
1796
|
+
* on lodash's type packages.
|
|
1797
|
+
*
|
|
1798
|
+
* @example
|
|
1799
|
+
* ```ts
|
|
1800
|
+
* const save = throttle(() => persist(), 1000, { leading: false })
|
|
1801
|
+
* save()
|
|
1802
|
+
* save.flush()
|
|
1803
|
+
* ```
|
|
1804
|
+
*
|
|
1805
|
+
* @public
|
|
1806
|
+
*/
|
|
1807
|
+
export declare const throttle: <T extends (...args: any[]) => any>(fn: T, wait?: number, options?: {
|
|
1808
|
+
leading?: boolean;
|
|
1809
|
+
trailing?: boolean;
|
|
1810
|
+
}) => ThrottledFunction<T>;
|
|
1811
|
+
|
|
1812
|
+
/**
|
|
1813
|
+
* The function returned by `throttle`. Call it like the original function; it returns the
|
|
1814
|
+
* result of the last invocation, or `undefined` if the original hasn't run yet.
|
|
1815
|
+
*
|
|
1816
|
+
* Declared here rather than re-exported from `@types/lodash`, so code can name a throttled
|
|
1817
|
+
* function's type without depending on lodash's type packages.
|
|
1818
|
+
*
|
|
1819
|
+
* @example
|
|
1820
|
+
* ```ts
|
|
1821
|
+
* class Saver {
|
|
1822
|
+
* save: ThrottledFunction<() => void> = throttle(() => this.persist(), 1000)
|
|
1823
|
+
* }
|
|
1824
|
+
* ```
|
|
1825
|
+
*
|
|
1826
|
+
* @public
|
|
1827
|
+
*/
|
|
1828
|
+
export declare interface ThrottledFunction<T extends (...args: any[]) => any> {
|
|
1829
|
+
(...args: Parameters<T>): ReturnType<T> | undefined;
|
|
1830
|
+
/** Cancels any pending invocation. */
|
|
1831
|
+
cancel(): void;
|
|
1832
|
+
/** Immediately runs any pending invocation and returns its result. */
|
|
1833
|
+
flush(): ReturnType<T> | undefined;
|
|
1834
|
+
}
|
|
1794
1835
|
|
|
1795
1836
|
/* Excluded from this release type: throttleToNextFrame */
|
|
1796
1837
|
|
|
@@ -1875,6 +1916,47 @@ export declare class Timers {
|
|
|
1875
1916
|
* @public
|
|
1876
1917
|
*/
|
|
1877
1918
|
requestAnimationFrame(contextId: string, callback: FrameRequestCallback): number;
|
|
1919
|
+
/**
|
|
1920
|
+
* Clears a timeout created with {@link Timers.setTimeout} and stops tracking it.
|
|
1921
|
+
* Prefer this over the global `clearTimeout` for ids that came from this instance,
|
|
1922
|
+
* otherwise the id stays tracked until its context is disposed.
|
|
1923
|
+
* @param contextId - The context identifier the timeout was created under.
|
|
1924
|
+
* @param id - The timer ID returned by `setTimeout`. Ignored when undefined.
|
|
1925
|
+
* @example
|
|
1926
|
+
* ```ts
|
|
1927
|
+
* const timers = new Timers()
|
|
1928
|
+
* const id = timers.setTimeout('autosave', () => save(), 5000)
|
|
1929
|
+
* timers.clearTimeout('autosave', id)
|
|
1930
|
+
* ```
|
|
1931
|
+
* @public
|
|
1932
|
+
*/
|
|
1933
|
+
clearTimeout(contextId: string, id: number | undefined): void;
|
|
1934
|
+
/**
|
|
1935
|
+
* Clears an interval created with {@link Timers.setInterval} and stops tracking it.
|
|
1936
|
+
* @param contextId - The context identifier the interval was created under.
|
|
1937
|
+
* @param id - The interval ID returned by `setInterval`. Ignored when undefined.
|
|
1938
|
+
* @example
|
|
1939
|
+
* ```ts
|
|
1940
|
+
* const timers = new Timers()
|
|
1941
|
+
* const id = timers.setInterval('refresh', () => updateData(), 1000)
|
|
1942
|
+
* timers.clearInterval('refresh', id)
|
|
1943
|
+
* ```
|
|
1944
|
+
* @public
|
|
1945
|
+
*/
|
|
1946
|
+
clearInterval(contextId: string, id: number | undefined): void;
|
|
1947
|
+
/**
|
|
1948
|
+
* Cancels an animation frame requested with {@link Timers.requestAnimationFrame} and stops tracking it.
|
|
1949
|
+
* @param contextId - The context identifier the animation frame was requested under.
|
|
1950
|
+
* @param id - The request ID returned by `requestAnimationFrame`. Ignored when undefined.
|
|
1951
|
+
* @example
|
|
1952
|
+
* ```ts
|
|
1953
|
+
* const timers = new Timers()
|
|
1954
|
+
* const id = timers.requestAnimationFrame('render', () => draw())
|
|
1955
|
+
* timers.cancelAnimationFrame('render', id)
|
|
1956
|
+
* ```
|
|
1957
|
+
* @public
|
|
1958
|
+
*/
|
|
1959
|
+
cancelAnimationFrame(contextId: string, id: number | undefined): void;
|
|
1878
1960
|
/**
|
|
1879
1961
|
* Disposes of all timers associated with the specified context.
|
|
1880
1962
|
* Clears all timeouts, intervals, and animation frames for the given context ID.
|
|
@@ -1912,23 +1994,29 @@ export declare class Timers {
|
|
|
1912
1994
|
* Returns an object with timer methods bound to a specific context.
|
|
1913
1995
|
* Convenient for getting context-specific timer functions without repeatedly passing the contextId.
|
|
1914
1996
|
* @param contextId - The context identifier to bind the returned methods to.
|
|
1915
|
-
* @returns An object with setTimeout, setInterval, requestAnimationFrame, and dispose
|
|
1997
|
+
* @returns An object with setTimeout, setInterval, requestAnimationFrame, the matching clear methods, and dispose bound to the context.
|
|
1916
1998
|
* @example
|
|
1917
1999
|
* ```ts
|
|
1918
2000
|
* const timers = new Timers()
|
|
1919
2001
|
* const uiTimers = timers.forContext('ui')
|
|
1920
2002
|
*
|
|
1921
2003
|
* // These are equivalent to calling timers.setTimeout('ui', ...)
|
|
1922
|
-
* uiTimers.setTimeout(() => console.log('timeout'), 1000)
|
|
2004
|
+
* const id = uiTimers.setTimeout(() => console.log('timeout'), 1000)
|
|
1923
2005
|
* uiTimers.setInterval(() => console.log('interval'), 500)
|
|
1924
2006
|
* uiTimers.requestAnimationFrame(() => console.log('frame'))
|
|
1925
2007
|
*
|
|
2008
|
+
* // Clear a single timer
|
|
2009
|
+
* uiTimers.clearTimeout(id)
|
|
2010
|
+
*
|
|
1926
2011
|
* // Dispose only this context
|
|
1927
2012
|
* uiTimers.dispose()
|
|
1928
2013
|
* ```
|
|
1929
2014
|
* @public
|
|
1930
2015
|
*/
|
|
1931
2016
|
forContext(contextId: string): {
|
|
2017
|
+
cancelAnimationFrame: (id: number | undefined) => void;
|
|
2018
|
+
clearInterval: (id: number | undefined) => void;
|
|
2019
|
+
clearTimeout: (id: number | undefined) => void;
|
|
1932
2020
|
dispose: () => void;
|
|
1933
2021
|
requestAnimationFrame: (callback: FrameRequestCallback) => number;
|
|
1934
2022
|
setInterval: (handler: TimerHandler, timeout?: number, ...args: any[]) => number;
|
package/dist-cjs/index.js
CHANGED
|
@@ -126,9 +126,9 @@ __export(index_exports, {
|
|
|
126
126
|
sortByMaybeIndex: () => import_reordering.sortByMaybeIndex,
|
|
127
127
|
stringEnum: () => import_stringEnum.stringEnum,
|
|
128
128
|
structuredClone: () => import_value.structuredClone,
|
|
129
|
-
throttle: () =>
|
|
129
|
+
throttle: () => import_throttle.throttle,
|
|
130
130
|
throttleToNextFrame: () => import_throttle.throttleToNextFrame,
|
|
131
|
-
uniq: () =>
|
|
131
|
+
uniq: () => import_lodash3.default,
|
|
132
132
|
uniqueId: () => import_id.uniqueId,
|
|
133
133
|
validateIndexKey: () => import_reordering.validateIndexKey,
|
|
134
134
|
warnDeprecatedGetter: () => import_warn.warnDeprecatedGetter,
|
|
@@ -138,8 +138,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
138
138
|
var import_version = require("./lib/version");
|
|
139
139
|
var import_lodash = __toESM(require("lodash.isequal"), 1);
|
|
140
140
|
var import_lodash2 = __toESM(require("lodash.isequalwith"), 1);
|
|
141
|
-
var import_lodash3 = __toESM(require("lodash.
|
|
142
|
-
var import_lodash4 = __toESM(require("lodash.uniq"), 1);
|
|
141
|
+
var import_lodash3 = __toESM(require("lodash.uniq"), 1);
|
|
143
142
|
var import_array = require("./lib/array");
|
|
144
143
|
var import_bind = require("./lib/bind");
|
|
145
144
|
var import_cache = require("./lib/cache");
|
|
@@ -174,7 +173,7 @@ var import_version2 = require("./lib/version");
|
|
|
174
173
|
var import_warn = require("./lib/warn");
|
|
175
174
|
(0, import_version.registerTldrawLibraryVersion)(
|
|
176
175
|
"@tldraw/utils",
|
|
177
|
-
"5.5.0-canary.
|
|
176
|
+
"5.5.0-canary.efb270e2aaf8",
|
|
178
177
|
"cjs"
|
|
179
178
|
);
|
|
180
179
|
//# sourceMappingURL=index.js.map
|
package/dist-cjs/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { registerTldrawLibraryVersion } from './lib/version'\n\nexport { default as isEqual } from 'lodash.isequal'\nexport { default as isEqualWith } from 'lodash.isequalwith'\nexport { default as
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAA6C;AAE7C,oBAAmC;AACnC,IAAAA,iBAAuC;AACvC,IAAAA,
|
|
4
|
+
"sourcesContent": ["import { registerTldrawLibraryVersion } from './lib/version'\n\nexport { default as isEqual } from 'lodash.isequal'\nexport { default as isEqualWith } from 'lodash.isequalwith'\nexport { default as uniq } from 'lodash.uniq'\nexport {\n\tareArraysShallowEqual,\n\tcompact,\n\tdedupe,\n\tlast,\n\tmaxBy,\n\tmergeArraysAndReplaceDefaults,\n\tminBy,\n\tpartition,\n\trotateArray,\n} from './lib/array'\nexport { bind } from './lib/bind'\nexport { WeakCache } from './lib/cache'\nexport {\n\tassert,\n\tassertExists,\n\texhaustiveSwitchError,\n\tpromiseWithResolve,\n\tResult,\n\tsleep,\n\ttype ErrorResult,\n\ttype OkResult,\n} from './lib/control'\nexport { debounce } from './lib/debounce'\nexport { annotateError, getErrorAnnotations, type ErrorAnnotations } from './lib/error'\nexport { ExecutionQueue } from './lib/ExecutionQueue'\nexport { FileHelpers } from './lib/file'\nexport { noop, omitFromStackTrace } from './lib/function'\nexport { getHashForBuffer, getHashForObject, getHashForString, lns } from './lib/hash'\nexport { mockUniqueId, restoreUniqueId, uniqueId } from './lib/id'\nexport { getFirstFromIterable } from './lib/iterable'\nexport { LruCache } from './lib/LruCache'\nexport type { JsonArray, JsonObject, JsonPrimitive, JsonValue } from './lib/json-value'\nexport {\n\tDEFAULT_SUPPORT_VIDEO_TYPES,\n\tDEFAULT_SUPPORTED_IMAGE_TYPES,\n\tDEFAULT_SUPPORTED_MEDIA_TYPE_LIST,\n\tDEFAULT_SUPPORTED_MEDIA_TYPES,\n\tMediaHelpers,\n} from './lib/media/media'\nexport { PngHelpers } from './lib/media/png'\nexport { fetch, Image } from './lib/network'\nexport { invLerp, lerp, modulate, rng } from './lib/number'\nexport {\n\tareObjectsShallowEqual,\n\tfilterEntries,\n\tgetChangedKeys,\n\tgetOwnProperty,\n\tgroupBy,\n\thasOwnProperty,\n\tisEqualAllowingForFloatingPointErrors,\n\tmapObjectMapValues,\n\tobjectMapEntries,\n\tobjectMapEntriesIterable,\n\tobjectMapFromEntries,\n\tobjectMapKeys,\n\tobjectMapValues,\n\tomit,\n} from './lib/object'\nexport { measureAverageDuration, measureCbDuration, measureDuration } from './lib/perf'\nexport { PerformanceTracker } from './lib/PerformanceTracker'\nexport {\n\tgetIndexAbove,\n\tgetIndexBelow,\n\tgetIndexBetween,\n\tgetIndices,\n\tgetIndicesAbove,\n\tgetIndicesBelow,\n\tgetIndicesBetween,\n\tsortByIndex,\n\tsortByMaybeIndex,\n\tvalidateIndexKey,\n\tZERO_INDEX_KEY,\n\ttype IndexKey,\n} from './lib/reordering'\nexport { retry } from './lib/retry'\nexport { sortById } from './lib/sort'\nexport {\n\tclearLocalStorage,\n\tclearSessionStorage,\n\tdeleteFromLocalStorage,\n\tdeleteFromSessionStorage,\n\tgetFromLocalStorage,\n\tgetFromSessionStorage,\n\tsetInLocalStorage,\n\tsetInSessionStorage,\n} from './lib/storage'\nexport { getFirstCharacter, iterateGraphemes } from './lib/string'\nexport { stringEnum } from './lib/stringEnum'\nexport {\n\tFpsScheduler,\n\tfpsThrottle,\n\tthrottle,\n\tthrottleToNextFrame,\n\ttype ThrottledFunction,\n} from './lib/throttle'\nexport { Timers } from './lib/timers'\nexport {\n\ttype Awaitable,\n\ttype Expand,\n\ttype MakeUndefinedOptional,\n\ttype RecursivePartial,\n\ttype Required,\n} from './lib/types'\nexport { safeParseUrl } from './lib/url'\nexport {\n\tisDefined,\n\tisNativeStructuredClone,\n\tisNonNull,\n\tisNonNullish,\n\tSTRUCTURED_CLONE_OBJECT_PROTOTYPE,\n\tstructuredClone,\n} from './lib/value'\nexport { registerTldrawLibraryVersion } from './lib/version'\nexport { warnDeprecatedGetter, warnOnce } from './lib/warn'\n\nregisterTldrawLibraryVersion(\n\t(globalThis as any).TLDRAW_LIBRARY_NAME,\n\t(globalThis as any).TLDRAW_LI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AAuB;AAQvB,iBAA6B;AAC7B,mBAOO;AACP,IAAAC,kBAA6C;AAC7C,kBAA+C;AAAA,IAE/C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AACF;",
|
|
6
6
|
"names": ["import_lodash", "import_version"]
|
|
7
7
|
}
|
package/dist-cjs/lib/LruCache.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/lib/LruCache.ts"],
|
|
4
|
-
"sourcesContent": ["/** Simple LRU cache backed by a Map's insertion-order iteration. @public */\nexport class LruCache<K, V> {\n\tprivate map = new Map<K, V>()\n\tconstructor(private maxSize: number) {}\n\n\tget(key: K): V | undefined {\n\t\tif (!this.map.has(key)) return undefined\n\t\tconst value = this.map.get(key)!\n\t\t// Move to most-recent position\n\t\tthis.map.delete(key)\n\t\tthis.map.set(key, value)\n\t\treturn value\n\t}\n\n\tset(key: K, value: V): void {\n\t\tif (this.map.has(key)) this.map.delete(key)\n\t\tthis.map.set(key, value)\n\t\tif (this.map.size > this.maxSize) {\n\t\t\t// Evict oldest entry\n\t\t\tthis.map.delete(this.map.keys().next().value!)\n\t\t}\n\t}\n\n\thas(key: K): boolean {\n\t\treturn this.map.has(key)\n\t}\n\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget size(): number {\n\t\treturn this.map.size\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACO,MAAM,SAAe;AAAA,EAE3B,YAAoB,SAAiB;AAAjB;AAAA,EAAkB;AAAA,EAAlB;AAAA,EADZ,MAAM,oBAAI,IAAU;AAAA,EAG5B,IAAI,KAAuB;AAC1B,QAAI,CAAC,KAAK,IAAI,IAAI,GAAG,EAAG,QAAO;AAC/B,UAAM,QAAQ,KAAK,IAAI,IAAI,GAAG;AAE9B,SAAK,IAAI,OAAO,GAAG;AACnB,SAAK,IAAI,IAAI,KAAK,KAAK;AACvB,WAAO;AAAA,EACR;AAAA,EAEA,IAAI,KAAQ,OAAgB;AAC3B,QAAI,KAAK,IAAI,IAAI,GAAG,EAAG,MAAK,IAAI,OAAO,GAAG;AAC1C,SAAK,IAAI,IAAI,KAAK,KAAK;AACvB,QAAI,KAAK,IAAI,OAAO,KAAK,SAAS;AAEjC,WAAK,IAAI,OAAO,KAAK,IAAI,KAAK,EAAE,KAAK,EAAE,KAAM;AAAA,IAC9C;AAAA,EACD;AAAA,EAEA,IAAI,KAAiB;AACpB,WAAO,KAAK,IAAI,IAAI,GAAG;AAAA,EACxB;AAAA;AAAA,EAGA,IAAI,OAAe;AAClB,WAAO,KAAK,IAAI;AAAA,EACjB;AACD;",
|
|
4
|
+
"sourcesContent": ["/** Simple LRU cache backed by a Map's insertion-order iteration. @public */\nexport class LruCache<K, V> {\n\tprivate map = new Map<K, V>()\n\tconstructor(private maxSize: number) {}\n\n\tget(key: K): V | undefined {\n\t\tif (!this.map.has(key)) return undefined\n\t\tconst value = this.map.get(key)!\n\t\t// Move to most-recent position\n\t\tthis.map.delete(key)\n\t\tthis.map.set(key, value)\n\t\treturn value\n\t}\n\n\tset(key: K, value: V): void {\n\t\tif (this.map.has(key)) this.map.delete(key)\n\t\tthis.map.set(key, value)\n\t\tif (this.map.size > this.maxSize) {\n\t\t\t// Evict oldest entry\n\t\t\tthis.map.delete(this.map.keys().next().value!)\n\t\t}\n\t}\n\n\thas(key: K): boolean {\n\t\treturn this.map.has(key)\n\t}\n\n\tdelete(key: K): boolean {\n\t\treturn this.map.delete(key)\n\t}\n\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget size(): number {\n\t\treturn this.map.size\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACO,MAAM,SAAe;AAAA,EAE3B,YAAoB,SAAiB;AAAjB;AAAA,EAAkB;AAAA,EAAlB;AAAA,EADZ,MAAM,oBAAI,IAAU;AAAA,EAG5B,IAAI,KAAuB;AAC1B,QAAI,CAAC,KAAK,IAAI,IAAI,GAAG,EAAG,QAAO;AAC/B,UAAM,QAAQ,KAAK,IAAI,IAAI,GAAG;AAE9B,SAAK,IAAI,OAAO,GAAG;AACnB,SAAK,IAAI,IAAI,KAAK,KAAK;AACvB,WAAO;AAAA,EACR;AAAA,EAEA,IAAI,KAAQ,OAAgB;AAC3B,QAAI,KAAK,IAAI,IAAI,GAAG,EAAG,MAAK,IAAI,OAAO,GAAG;AAC1C,SAAK,IAAI,IAAI,KAAK,KAAK;AACvB,QAAI,KAAK,IAAI,OAAO,KAAK,SAAS;AAEjC,WAAK,IAAI,OAAO,KAAK,IAAI,KAAK,EAAE,KAAK,EAAE,KAAM;AAAA,IAC9C;AAAA,EACD;AAAA,EAEA,IAAI,KAAiB;AACpB,WAAO,KAAK,IAAI,IAAI,GAAG;AAAA,EACxB;AAAA,EAEA,OAAO,KAAiB;AACvB,WAAO,KAAK,IAAI,OAAO,GAAG;AAAA,EAC3B;AAAA;AAAA,EAGA,IAAI,OAAe;AAClB,WAAO,KAAK,IAAI;AAAA,EACjB;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist-cjs/lib/throttle.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,14 +17,24 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
var throttle_exports = {};
|
|
20
30
|
__export(throttle_exports, {
|
|
21
31
|
FpsScheduler: () => FpsScheduler,
|
|
22
32
|
fpsThrottle: () => fpsThrottle,
|
|
33
|
+
throttle: () => throttle,
|
|
23
34
|
throttleToNextFrame: () => throttleToNextFrame
|
|
24
35
|
});
|
|
25
36
|
module.exports = __toCommonJS(throttle_exports);
|
|
37
|
+
var import_lodash = __toESM(require("lodash.throttle"), 1);
|
|
26
38
|
const isTest = () => typeof process !== "undefined" && process.env.NODE_ENV === "test" && // @ts-expect-error
|
|
27
39
|
!globalThis.__FORCE_RAF_IN_TESTS__;
|
|
28
40
|
const timingVarianceFactor = 0.9;
|
|
@@ -149,4 +161,5 @@ function fpsThrottle(fn) {
|
|
|
149
161
|
function throttleToNextFrame(fn) {
|
|
150
162
|
return defaultScheduler.throttleToNextFrame(fn);
|
|
151
163
|
}
|
|
164
|
+
const throttle = import_lodash.default;
|
|
152
165
|
//# sourceMappingURL=throttle.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/lib/throttle.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["import _throttle from 'lodash.throttle'\n\nconst isTest = () =>\n\ttypeof process !== 'undefined' &&\n\tprocess.env.NODE_ENV === 'test' &&\n\t// @ts-expect-error\n\t!globalThis.__FORCE_RAF_IN_TESTS__\n\n// Browsers aren't precise with frame timing - this factor prevents skipping frames unnecessarily\n// by aiming slightly below the theoretical frame duration (e.g., ~7.5ms instead of 8.33ms for 120fps)\nconst timingVarianceFactor = 0.9\nconst getTargetTimePerFrame = (targetFps: number) =>\n\tMath.floor(1000 / targetFps) * timingVarianceFactor\n\n/**\n * A scheduler class that manages a queue of functions to be executed at a target frame rate.\n * Each instance maintains its own queue and state, allowing for separate throttling contexts\n * (e.g., UI operations vs network sync operations).\n *\n * @public\n */\nexport class FpsScheduler {\n\tprivate targetFps: number\n\tprivate targetTimePerFrame: number\n\tprivate fpsQueue: Array<() => void> = []\n\tprivate frameRaf: undefined | number\n\tprivate flushRaf: undefined | number\n\tprivate lastFlushTime: number\n\n\tconstructor(targetFps: number = 120) {\n\t\tthis.targetFps = targetFps\n\t\tthis.targetTimePerFrame = getTargetTimePerFrame(targetFps)\n\t\tthis.lastFlushTime = -this.targetTimePerFrame\n\t}\n\n\tupdateTargetFps(targetFps: number) {\n\t\tif (targetFps === this.targetFps) return\n\t\tthis.targetFps = targetFps\n\t\tthis.targetTimePerFrame = getTargetTimePerFrame(targetFps)\n\t\tthis.lastFlushTime = -this.targetTimePerFrame\n\t}\n\n\tprivate flush() {\n\t\tconst queue = this.fpsQueue.splice(0, this.fpsQueue.length)\n\t\tfor (const fn of queue) {\n\t\t\tfn()\n\t\t}\n\t}\n\n\tprivate tick(isOnNextFrame = false) {\n\t\tif (this.frameRaf) return\n\n\t\tconst now = Date.now()\n\t\tconst elapsed = now - this.lastFlushTime\n\n\t\tif (elapsed < this.targetTimePerFrame) {\n\t\t\t// If we're too early to flush, we need to wait until the next frame to try and flush again.\n\t\t\t// eslint-disable-next-line no-restricted-globals\n\t\t\tthis.frameRaf = requestAnimationFrame(() => {\n\t\t\t\tthis.frameRaf = undefined\n\t\t\t\tthis.tick(true)\n\t\t\t})\n\t\t\treturn\n\t\t}\n\n\t\tif (isOnNextFrame) {\n\t\t\t// If we've already waited for the next frame to run the tick, then we can flush immediately\n\t\t\tif (this.flushRaf) return // ...though if there's a flush raf, that means we'll be flushing on this frame already, so we can do nothing here.\n\t\t\tthis.lastFlushTime = now\n\t\t\tthis.flush()\n\t\t} else {\n\t\t\t// If we haven't already waited for the next frame to run the tick, we need to wait until the next frame to flush.\n\t\t\tif (this.flushRaf) return // ...though if there's a flush raf, that means we'll be flushing on the next frame already, so we can do nothing here.\n\t\t\t// eslint-disable-next-line no-restricted-globals\n\t\t\tthis.flushRaf = requestAnimationFrame(() => {\n\t\t\t\tthis.flushRaf = undefined\n\t\t\t\tthis.lastFlushTime = Date.now()\n\t\t\t\tthis.flush()\n\t\t\t})\n\t\t}\n\t}\n\n\t/**\n\t * Creates a throttled version of a function that executes at most once per frame.\n\t * The default target frame rate is set by the FpsScheduler instance.\n\t * Subsequent calls within the same frame are ignored, ensuring smooth performance\n\t * for high-frequency events like mouse movements or scroll events.\n\t *\n\t * @param fn - The function to throttle, optionally with a cancel method\n\t * @returns A throttled function with an optional cancel method to remove pending calls\n\t *\n\t * @public\n\t */\n\tfpsThrottle(fn: { (): void; cancel?(): void }): {\n\t\t(): void\n\t\tcancel?(): void\n\t} {\n\t\tif (isTest()) {\n\t\t\tfn.cancel = () => {\n\t\t\t\tif (this.frameRaf) {\n\t\t\t\t\tcancelAnimationFrame(this.frameRaf)\n\t\t\t\t\tthis.frameRaf = undefined\n\t\t\t\t}\n\t\t\t\tif (this.flushRaf) {\n\t\t\t\t\tcancelAnimationFrame(this.flushRaf)\n\t\t\t\t\tthis.flushRaf = undefined\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn fn\n\t\t}\n\n\t\tconst throttledFn = () => {\n\t\t\tif (this.fpsQueue.includes(fn)) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.fpsQueue.push(fn)\n\t\t\tthis.tick()\n\t\t}\n\t\tthrottledFn.cancel = () => {\n\t\t\tconst index = this.fpsQueue.indexOf(fn)\n\t\t\tif (index > -1) {\n\t\t\t\tthis.fpsQueue.splice(index, 1)\n\t\t\t}\n\t\t}\n\t\treturn throttledFn\n\t}\n\n\t/**\n\t * Schedules a function to execute on the next animation frame.\n\t * If the same function is passed multiple times before the frame executes,\n\t * it will only be called once, effectively batching multiple calls.\n\t *\n\t * @param fn - The function to execute on the next frame\n\t * @returns A cancel function that can prevent execution if called before the next frame\n\t *\n\t * @public\n\t */\n\tthrottleToNextFrame(fn: () => void): () => void {\n\t\tif (isTest()) {\n\t\t\tfn()\n\t\t\treturn () => void null // noop\n\t\t}\n\n\t\tif (!this.fpsQueue.includes(fn)) {\n\t\t\tthis.fpsQueue.push(fn)\n\t\t\tthis.tick()\n\t\t}\n\n\t\treturn () => {\n\t\t\tconst index = this.fpsQueue.indexOf(fn)\n\t\t\tif (index > -1) {\n\t\t\t\tthis.fpsQueue.splice(index, 1)\n\t\t\t}\n\t\t}\n\t}\n}\n\n// Default instance for UI operations\nconst defaultScheduler = new FpsScheduler(120)\n\n/**\n * Creates a throttled version of a function that executes at most once per frame.\n * The default target frame rate is 120fps, but can be customized per function.\n * Subsequent calls within the same frame are ignored, ensuring smooth performance\n * for high-frequency events like mouse movements or scroll events.\n *\n * Uses the default throttle instance for UI operations. If you need a separate\n * throttling queue (e.g., for network operations), create your own Throttle instance.\n *\n * @param fn - The function to throttle, optionally with a cancel method\n * @returns A throttled function with an optional cancel method to remove pending calls\n *\n * @example\n * ```ts\n * // Default 120fps throttling\n * const updateCanvas = fpsThrottle(() => {\n * // This will run at most once per frame (~8.33ms)\n * redrawCanvas()\n * })\n *\n * // Call as often as you want - automatically throttled to 120fps\n * document.addEventListener('mousemove', updateCanvas)\n *\n * // Cancel pending calls if needed\n * updateCanvas.cancel?.()\n * ```\n *\n * @internal\n */\nexport function fpsThrottle(fn: { (): void; cancel?(): void }): {\n\t(): void\n\tcancel?(): void\n} {\n\treturn defaultScheduler.fpsThrottle(fn)\n}\n\n/**\n * Schedules a function to execute on the next animation frame, targeting 120fps.\n * If the same function is passed multiple times before the frame executes,\n * it will only be called once, effectively batching multiple calls.\n *\n * Uses the default throttle instance for UI operations.\n *\n * @param fn - The function to execute on the next frame\n * @returns A cancel function that can prevent execution if called before the next frame\n *\n * @example\n * ```ts\n * const updateUI = throttleToNextFrame(() => {\n * // Batches multiple calls into the next animation frame\n * updateStatusBar()\n * refreshToolbar()\n * })\n *\n * // Multiple calls within the same frame are batched\n * updateUI() // Will execute\n * updateUI() // Ignored (same function already queued)\n * updateUI() // Ignored (same function already queued)\n *\n * // Get cancel function to prevent execution\n * const cancel = updateUI()\n * cancel() // Prevents execution if called before next frame\n * ```\n *\n * @internal\n */\nexport function throttleToNextFrame(fn: () => void): () => void {\n\treturn defaultScheduler.throttleToNextFrame(fn)\n}\n\n/**\n * The function returned by `throttle`. Call it like the original function; it returns the\n * result of the last invocation, or `undefined` if the original hasn't run yet.\n *\n * Declared here rather than re-exported from `@types/lodash`, so code can name a throttled\n * function's type without depending on lodash's type packages.\n *\n * @example\n * ```ts\n * class Saver {\n * save: ThrottledFunction<() => void> = throttle(() => this.persist(), 1000)\n * }\n * ```\n *\n * @public\n */\nexport interface ThrottledFunction<T extends (...args: any[]) => any> {\n\t(...args: Parameters<T>): ReturnType<T> | undefined\n\t/** Cancels any pending invocation. */\n\tcancel(): void\n\t/** Immediately runs any pending invocation and returns its result. */\n\tflush(): ReturnType<T> | undefined\n}\n\n/**\n * Creates a throttled function that invokes `fn` at most once per `wait` milliseconds. Wraps\n * lodash's `throttle`, typed with {@link ThrottledFunction} so the published types don't depend\n * on lodash's type packages.\n *\n * @example\n * ```ts\n * const save = throttle(() => persist(), 1000, { leading: false })\n * save()\n * save.flush()\n * ```\n *\n * @public\n */\nexport const throttle: <T extends (...args: any[]) => any>(\n\tfn: T,\n\twait?: number,\n\toptions?: { leading?: boolean; trailing?: boolean }\n) => ThrottledFunction<T> = _throttle\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;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Y,EAAE;AACvC;AAgCO,SAAS,oBAAoB,IAA4B;AAC/D,SAAO,iBAAiB,oBAAoB,EAAE;AAC/C;AAwCO,MAAM,WAIe,cAAAA;",
|
|
6
|
+
"names": ["_throttle"]
|
|
7
7
|
}
|
package/dist-cjs/lib/timers.js
CHANGED
|
@@ -22,6 +22,9 @@ __export(timers_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(timers_exports);
|
|
24
24
|
class Timers {
|
|
25
|
+
// Ids are dropped as soon as a timer fires or is cleared through this class, so a
|
|
26
|
+
// long-lived context (the editor creates two timeouts per pointer down) does not
|
|
27
|
+
// accumulate stale ids that `dispose` would otherwise have to clear one by one.
|
|
25
28
|
timeouts = /* @__PURE__ */ new Map();
|
|
26
29
|
intervals = /* @__PURE__ */ new Map();
|
|
27
30
|
rafs = /* @__PURE__ */ new Map();
|
|
@@ -38,6 +41,9 @@ class Timers {
|
|
|
38
41
|
this.setTimeout = this.setTimeout.bind(this);
|
|
39
42
|
this.setInterval = this.setInterval.bind(this);
|
|
40
43
|
this.requestAnimationFrame = this.requestAnimationFrame.bind(this);
|
|
44
|
+
this.clearTimeout = this.clearTimeout.bind(this);
|
|
45
|
+
this.clearInterval = this.clearInterval.bind(this);
|
|
46
|
+
this.cancelAnimationFrame = this.cancelAnimationFrame.bind(this);
|
|
41
47
|
this.dispose = this.dispose.bind(this);
|
|
42
48
|
}
|
|
43
49
|
/**
|
|
@@ -56,9 +62,16 @@ class Timers {
|
|
|
56
62
|
* @public
|
|
57
63
|
*/
|
|
58
64
|
setTimeout(contextId, handler, timeout, ...args) {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
const ids = getOrCreateSet(this.timeouts, contextId);
|
|
66
|
+
let fired = false;
|
|
67
|
+
let id = -1;
|
|
68
|
+
const wrapped = typeof handler === "function" ? (...handlerArgs) => {
|
|
69
|
+
fired = true;
|
|
70
|
+
ids.delete(id);
|
|
71
|
+
handler(...handlerArgs);
|
|
72
|
+
} : handler;
|
|
73
|
+
id = window.setTimeout(wrapped, timeout, args);
|
|
74
|
+
if (!fired) ids.add(id);
|
|
62
75
|
return id;
|
|
63
76
|
}
|
|
64
77
|
/**
|
|
@@ -78,8 +91,7 @@ class Timers {
|
|
|
78
91
|
*/
|
|
79
92
|
setInterval(contextId, handler, timeout, ...args) {
|
|
80
93
|
const id = window.setInterval(handler, timeout, args);
|
|
81
|
-
|
|
82
|
-
this.intervals.set(contextId, [...current, id]);
|
|
94
|
+
getOrCreateSet(this.intervals, contextId).add(id);
|
|
83
95
|
return id;
|
|
84
96
|
}
|
|
85
97
|
/**
|
|
@@ -96,11 +108,70 @@ class Timers {
|
|
|
96
108
|
* @public
|
|
97
109
|
*/
|
|
98
110
|
requestAnimationFrame(contextId, callback) {
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
111
|
+
const ids = getOrCreateSet(this.rafs, contextId);
|
|
112
|
+
let fired = false;
|
|
113
|
+
let id = -1;
|
|
114
|
+
id = window.requestAnimationFrame((time) => {
|
|
115
|
+
fired = true;
|
|
116
|
+
ids.delete(id);
|
|
117
|
+
callback(time);
|
|
118
|
+
});
|
|
119
|
+
if (!fired) ids.add(id);
|
|
102
120
|
return id;
|
|
103
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Clears a timeout created with {@link Timers.setTimeout} and stops tracking it.
|
|
124
|
+
* Prefer this over the global `clearTimeout` for ids that came from this instance,
|
|
125
|
+
* otherwise the id stays tracked until its context is disposed.
|
|
126
|
+
* @param contextId - The context identifier the timeout was created under.
|
|
127
|
+
* @param id - The timer ID returned by `setTimeout`. Ignored when undefined.
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* const timers = new Timers()
|
|
131
|
+
* const id = timers.setTimeout('autosave', () => save(), 5000)
|
|
132
|
+
* timers.clearTimeout('autosave', id)
|
|
133
|
+
* ```
|
|
134
|
+
* @public
|
|
135
|
+
*/
|
|
136
|
+
clearTimeout(contextId, id) {
|
|
137
|
+
if (id === void 0) return;
|
|
138
|
+
clearTimeout(id);
|
|
139
|
+
this.timeouts.get(contextId)?.delete(id);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Clears an interval created with {@link Timers.setInterval} and stops tracking it.
|
|
143
|
+
* @param contextId - The context identifier the interval was created under.
|
|
144
|
+
* @param id - The interval ID returned by `setInterval`. Ignored when undefined.
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* const timers = new Timers()
|
|
148
|
+
* const id = timers.setInterval('refresh', () => updateData(), 1000)
|
|
149
|
+
* timers.clearInterval('refresh', id)
|
|
150
|
+
* ```
|
|
151
|
+
* @public
|
|
152
|
+
*/
|
|
153
|
+
clearInterval(contextId, id) {
|
|
154
|
+
if (id === void 0) return;
|
|
155
|
+
clearInterval(id);
|
|
156
|
+
this.intervals.get(contextId)?.delete(id);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Cancels an animation frame requested with {@link Timers.requestAnimationFrame} and stops tracking it.
|
|
160
|
+
* @param contextId - The context identifier the animation frame was requested under.
|
|
161
|
+
* @param id - The request ID returned by `requestAnimationFrame`. Ignored when undefined.
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* const timers = new Timers()
|
|
165
|
+
* const id = timers.requestAnimationFrame('render', () => draw())
|
|
166
|
+
* timers.cancelAnimationFrame('render', id)
|
|
167
|
+
* ```
|
|
168
|
+
* @public
|
|
169
|
+
*/
|
|
170
|
+
cancelAnimationFrame(contextId, id) {
|
|
171
|
+
if (id === void 0) return;
|
|
172
|
+
cancelAnimationFrame(id);
|
|
173
|
+
this.rafs.get(contextId)?.delete(id);
|
|
174
|
+
}
|
|
104
175
|
/**
|
|
105
176
|
* Disposes of all timers associated with the specified context.
|
|
106
177
|
* Clears all timeouts, intervals, and animation frames for the given context ID.
|
|
@@ -141,7 +212,12 @@ class Timers {
|
|
|
141
212
|
* @public
|
|
142
213
|
*/
|
|
143
214
|
disposeAll() {
|
|
144
|
-
|
|
215
|
+
const contextIds = /* @__PURE__ */ new Set([
|
|
216
|
+
...this.timeouts.keys(),
|
|
217
|
+
...this.intervals.keys(),
|
|
218
|
+
...this.rafs.keys()
|
|
219
|
+
]);
|
|
220
|
+
for (const contextId of contextIds) {
|
|
145
221
|
this.dispose(contextId);
|
|
146
222
|
}
|
|
147
223
|
}
|
|
@@ -149,17 +225,20 @@ class Timers {
|
|
|
149
225
|
* Returns an object with timer methods bound to a specific context.
|
|
150
226
|
* Convenient for getting context-specific timer functions without repeatedly passing the contextId.
|
|
151
227
|
* @param contextId - The context identifier to bind the returned methods to.
|
|
152
|
-
* @returns An object with setTimeout, setInterval, requestAnimationFrame, and dispose
|
|
228
|
+
* @returns An object with setTimeout, setInterval, requestAnimationFrame, the matching clear methods, and dispose bound to the context.
|
|
153
229
|
* @example
|
|
154
230
|
* ```ts
|
|
155
231
|
* const timers = new Timers()
|
|
156
232
|
* const uiTimers = timers.forContext('ui')
|
|
157
233
|
*
|
|
158
234
|
* // These are equivalent to calling timers.setTimeout('ui', ...)
|
|
159
|
-
* uiTimers.setTimeout(() => console.log('timeout'), 1000)
|
|
235
|
+
* const id = uiTimers.setTimeout(() => console.log('timeout'), 1000)
|
|
160
236
|
* uiTimers.setInterval(() => console.log('interval'), 500)
|
|
161
237
|
* uiTimers.requestAnimationFrame(() => console.log('frame'))
|
|
162
238
|
*
|
|
239
|
+
* // Clear a single timer
|
|
240
|
+
* uiTimers.clearTimeout(id)
|
|
241
|
+
*
|
|
163
242
|
* // Dispose only this context
|
|
164
243
|
* uiTimers.dispose()
|
|
165
244
|
* ```
|
|
@@ -170,8 +249,19 @@ class Timers {
|
|
|
170
249
|
setTimeout: (handler, timeout, ...args) => this.setTimeout(contextId, handler, timeout, args),
|
|
171
250
|
setInterval: (handler, timeout, ...args) => this.setInterval(contextId, handler, timeout, args),
|
|
172
251
|
requestAnimationFrame: (callback) => this.requestAnimationFrame(contextId, callback),
|
|
252
|
+
clearTimeout: (id) => this.clearTimeout(contextId, id),
|
|
253
|
+
clearInterval: (id) => this.clearInterval(contextId, id),
|
|
254
|
+
cancelAnimationFrame: (id) => this.cancelAnimationFrame(contextId, id),
|
|
173
255
|
dispose: () => this.dispose(contextId)
|
|
174
256
|
};
|
|
175
257
|
}
|
|
176
258
|
}
|
|
259
|
+
function getOrCreateSet(map, contextId) {
|
|
260
|
+
let set = map.get(contextId);
|
|
261
|
+
if (!set) {
|
|
262
|
+
set = /* @__PURE__ */ new Set();
|
|
263
|
+
map.set(contextId, set);
|
|
264
|
+
}
|
|
265
|
+
return set;
|
|
266
|
+
}
|
|
177
267
|
//# sourceMappingURL=timers.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/lib/timers.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable tldraw/no-restricted-properties */\n\n/**\n * A utility class for managing timeouts, intervals, and animation frames with context-based organization and automatic cleanup.\n * Helps prevent memory leaks by organizing timers into named contexts that can be cleared together.\n * @example\n * ```ts\n * const timers = new Timers()\n *\n * // Set timers with context organization\n * timers.setTimeout('ui', () => console.log('Auto save'), 5000)\n * timers.setInterval('ui', () => console.log('Refresh'), 1000)\n * timers.requestAnimationFrame('ui', () => console.log('Render'))\n *\n * // Clear all timers for a context\n * timers.dispose('ui')\n *\n * // Or get context-bound functions\n * const uiTimers = timers.forContext('ui')\n * uiTimers.setTimeout(() => console.log('Contextual timeout'), 1000)\n * ```\n * @public\n */\nexport class Timers {\n\tprivate timeouts = new Map<string, number[]>()\n\tprivate intervals = new Map<string, number[]>()\n\tprivate rafs = new Map<string, number[]>()\n\n\t/**\n\t * Creates a new Timers instance with bound methods for safe callback usage.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * // Methods are pre-bound, safe to use as callbacks\n\t * element.addEventListener('click', timers.dispose)\n\t * ```\n\t */\n\tconstructor() {\n\t\tthis.setTimeout = this.setTimeout.bind(this)\n\t\tthis.setInterval = this.setInterval.bind(this)\n\t\tthis.requestAnimationFrame = this.requestAnimationFrame.bind(this)\n\t\tthis.dispose = this.dispose.bind(this)\n\t}\n\n\t/**\n\t * Creates a timeout that will be tracked under the specified context.\n\t * @param contextId - The context identifier to group this timer under.\n\t * @param handler - The function to execute when the timeout expires.\n\t * @param timeout - The delay in milliseconds (default: 0).\n\t * @param args - Additional arguments to pass to the handler.\n\t * @returns The timer ID that can be used with clearTimeout.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const id = timers.setTimeout('autosave', () => save(), 5000)\n\t * // Timer will be automatically cleared when 'autosave' context is disposed\n\t * ```\n\t * @public\n\t */\n\tsetTimeout(contextId: string, handler: TimerHandler, timeout?: number, ...args: any[]): number {\n\t\tconst id = window.setTimeout(handler, timeout, args)\n\t\tconst current = this.timeouts.get(contextId) ?? []\n\t\tthis.timeouts.set(contextId, [...current, id])\n\t\treturn id\n\t}\n\n\t/**\n\t * Creates an interval that will be tracked under the specified context.\n\t * @param contextId - The context identifier to group this timer under.\n\t * @param handler - The function to execute repeatedly.\n\t * @param timeout - The delay in milliseconds between executions (default: 0).\n\t * @param args - Additional arguments to pass to the handler.\n\t * @returns The interval ID that can be used with clearInterval.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const id = timers.setInterval('refresh', () => updateData(), 1000)\n\t * // Interval will be automatically cleared when 'refresh' context is disposed\n\t * ```\n\t * @public\n\t */\n\tsetInterval(contextId: string, handler: TimerHandler, timeout?: number, ...args: any[]): number {\n\t\tconst id = window.setInterval(handler, timeout, args)\n\t\tconst current = this.intervals.get(contextId) ?? []\n\t\tthis.intervals.set(contextId, [...current, id])\n\t\treturn id\n\t}\n\n\t/**\n\t * Requests an animation frame that will be tracked under the specified context.\n\t * @param contextId - The context identifier to group this animation frame under.\n\t * @param callback - The function to execute on the next animation frame.\n\t * @returns The request ID that can be used with cancelAnimationFrame.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const id = timers.requestAnimationFrame('render', () => draw())\n\t * // Animation frame will be automatically cancelled when 'render' context is disposed\n\t * ```\n\t * @public\n\t */\n\trequestAnimationFrame(contextId: string, callback: FrameRequestCallback): number {\n\t\tconst id = window.requestAnimationFrame(callback)\n\t\tconst current = this.rafs.get(contextId) ?? []\n\t\tthis.rafs.set(contextId, [...current, id])\n\t\treturn id\n\t}\n\n\t/**\n\t * Disposes of all timers associated with the specified context.\n\t * Clears all timeouts, intervals, and animation frames for the given context ID.\n\t * @param contextId - The context identifier whose timers should be cleared.\n\t * @returns void\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * timers.setTimeout('ui', () => console.log('timeout'), 1000)\n\t * timers.setInterval('ui', () => console.log('interval'), 500)\n\t *\n\t * // Clear all 'ui' context timers\n\t * timers.dispose('ui')\n\t * ```\n\t * @public\n\t */\n\tdispose(contextId: string) {\n\t\tthis.timeouts.get(contextId)?.forEach((id) => clearTimeout(id))\n\t\tthis.intervals.get(contextId)?.forEach((id) => clearInterval(id))\n\t\tthis.rafs.get(contextId)?.forEach((id) => cancelAnimationFrame(id))\n\n\t\tthis.timeouts.delete(contextId)\n\t\tthis.intervals.delete(contextId)\n\t\tthis.rafs.delete(contextId)\n\t}\n\n\t/**\n\t * Disposes of all timers across all contexts.\n\t * Clears every timeout, interval, and animation frame managed by this instance.\n\t * @returns void\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * timers.setTimeout('ui', () => console.log('ui'), 1000)\n\t * timers.setTimeout('background', () => console.log('bg'), 2000)\n\t *\n\t * // Clear everything\n\t * timers.disposeAll()\n\t * ```\n\t * @public\n\t */\n\tdisposeAll() {\n\t\tfor (const contextId of this.timeouts.keys()) {\n\t\t\tthis.dispose(contextId)\n\t\t}\n\t}\n\n\t/**\n\t * Returns an object with timer methods bound to a specific context.\n\t * Convenient for getting context-specific timer functions without repeatedly passing the contextId.\n\t * @param contextId - The context identifier to bind the returned methods to.\n\t * @returns An object with setTimeout, setInterval, requestAnimationFrame, and dispose methods bound to the context.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const uiTimers = timers.forContext('ui')\n\t *\n\t * // These are equivalent to calling timers.setTimeout('ui', ...)\n\t * uiTimers.setTimeout(() => console.log('timeout'), 1000)\n\t * uiTimers.setInterval(() => console.log('interval'), 500)\n\t * uiTimers.requestAnimationFrame(() => console.log('frame'))\n\t *\n\t * // Dispose only this context\n\t * uiTimers.dispose()\n\t * ```\n\t * @public\n\t */\n\tforContext(contextId: string) {\n\t\treturn {\n\t\t\tsetTimeout: (handler: TimerHandler, timeout?: number, ...args: any[]) =>\n\t\t\t\tthis.setTimeout(contextId, handler, timeout, args),\n\t\t\tsetInterval: (handler: TimerHandler, timeout?: number, ...args: any[]) =>\n\t\t\t\tthis.setInterval(contextId, handler, timeout, args),\n\t\t\trequestAnimationFrame: (callback: FrameRequestCallback) =>\n\t\t\t\tthis.requestAnimationFrame(contextId, callback),\n\t\t\tdispose: () => this.dispose(contextId),\n\t\t}\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAuBO,MAAM,OAAO;AAAA,
|
|
4
|
+
"sourcesContent": ["/* eslint-disable tldraw/no-restricted-properties */\n\n/**\n * A utility class for managing timeouts, intervals, and animation frames with context-based organization and automatic cleanup.\n * Helps prevent memory leaks by organizing timers into named contexts that can be cleared together.\n * @example\n * ```ts\n * const timers = new Timers()\n *\n * // Set timers with context organization\n * timers.setTimeout('ui', () => console.log('Auto save'), 5000)\n * timers.setInterval('ui', () => console.log('Refresh'), 1000)\n * timers.requestAnimationFrame('ui', () => console.log('Render'))\n *\n * // Clear all timers for a context\n * timers.dispose('ui')\n *\n * // Or get context-bound functions\n * const uiTimers = timers.forContext('ui')\n * uiTimers.setTimeout(() => console.log('Contextual timeout'), 1000)\n * ```\n * @public\n */\nexport class Timers {\n\t// Ids are dropped as soon as a timer fires or is cleared through this class, so a\n\t// long-lived context (the editor creates two timeouts per pointer down) does not\n\t// accumulate stale ids that `dispose` would otherwise have to clear one by one.\n\tprivate timeouts = new Map<string, Set<number>>()\n\tprivate intervals = new Map<string, Set<number>>()\n\tprivate rafs = new Map<string, Set<number>>()\n\n\t/**\n\t * Creates a new Timers instance with bound methods for safe callback usage.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * // Methods are pre-bound, safe to use as callbacks\n\t * element.addEventListener('click', timers.dispose)\n\t * ```\n\t */\n\tconstructor() {\n\t\tthis.setTimeout = this.setTimeout.bind(this)\n\t\tthis.setInterval = this.setInterval.bind(this)\n\t\tthis.requestAnimationFrame = this.requestAnimationFrame.bind(this)\n\t\tthis.clearTimeout = this.clearTimeout.bind(this)\n\t\tthis.clearInterval = this.clearInterval.bind(this)\n\t\tthis.cancelAnimationFrame = this.cancelAnimationFrame.bind(this)\n\t\tthis.dispose = this.dispose.bind(this)\n\t}\n\n\t/**\n\t * Creates a timeout that will be tracked under the specified context.\n\t * @param contextId - The context identifier to group this timer under.\n\t * @param handler - The function to execute when the timeout expires.\n\t * @param timeout - The delay in milliseconds (default: 0).\n\t * @param args - Additional arguments to pass to the handler.\n\t * @returns The timer ID that can be used with clearTimeout.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const id = timers.setTimeout('autosave', () => save(), 5000)\n\t * // Timer will be automatically cleared when 'autosave' context is disposed\n\t * ```\n\t * @public\n\t */\n\tsetTimeout(contextId: string, handler: TimerHandler, timeout?: number, ...args: any[]): number {\n\t\tconst ids = getOrCreateSet(this.timeouts, contextId)\n\t\t// A test stub can run the handler synchronously, before `id` is assigned, so the wrapper\n\t\t// records that it fired instead of relying on the id being set when it runs.\n\t\tlet fired = false\n\t\tlet id = -1\n\t\t// A string handler is evaluated by the browser, so it cannot be wrapped and stays\n\t\t// tracked until it is cleared or the context is disposed.\n\t\tconst wrapped =\n\t\t\ttypeof handler === 'function'\n\t\t\t\t? (...handlerArgs: any[]) => {\n\t\t\t\t\t\tfired = true\n\t\t\t\t\t\tids.delete(id)\n\t\t\t\t\t\thandler(...handlerArgs)\n\t\t\t\t\t}\n\t\t\t\t: handler\n\t\tid = window.setTimeout(wrapped, timeout, args)\n\t\tif (!fired) ids.add(id)\n\t\treturn id\n\t}\n\n\t/**\n\t * Creates an interval that will be tracked under the specified context.\n\t * @param contextId - The context identifier to group this timer under.\n\t * @param handler - The function to execute repeatedly.\n\t * @param timeout - The delay in milliseconds between executions (default: 0).\n\t * @param args - Additional arguments to pass to the handler.\n\t * @returns The interval ID that can be used with clearInterval.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const id = timers.setInterval('refresh', () => updateData(), 1000)\n\t * // Interval will be automatically cleared when 'refresh' context is disposed\n\t * ```\n\t * @public\n\t */\n\tsetInterval(contextId: string, handler: TimerHandler, timeout?: number, ...args: any[]): number {\n\t\tconst id = window.setInterval(handler, timeout, args)\n\t\tgetOrCreateSet(this.intervals, contextId).add(id)\n\t\treturn id\n\t}\n\n\t/**\n\t * Requests an animation frame that will be tracked under the specified context.\n\t * @param contextId - The context identifier to group this animation frame under.\n\t * @param callback - The function to execute on the next animation frame.\n\t * @returns The request ID that can be used with cancelAnimationFrame.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const id = timers.requestAnimationFrame('render', () => draw())\n\t * // Animation frame will be automatically cancelled when 'render' context is disposed\n\t * ```\n\t * @public\n\t */\n\trequestAnimationFrame(contextId: string, callback: FrameRequestCallback): number {\n\t\tconst ids = getOrCreateSet(this.rafs, contextId)\n\t\t// Test stubs (the sync fuzz suite, for one) run the frame synchronously, before `id` is\n\t\t// assigned, so the callback records that it fired rather than touching `id` first.\n\t\tlet fired = false\n\t\tlet id = -1\n\t\tid = window.requestAnimationFrame((time) => {\n\t\t\tfired = true\n\t\t\tids.delete(id)\n\t\t\tcallback(time)\n\t\t})\n\t\tif (!fired) ids.add(id)\n\t\treturn id\n\t}\n\n\t/**\n\t * Clears a timeout created with {@link Timers.setTimeout} and stops tracking it.\n\t * Prefer this over the global `clearTimeout` for ids that came from this instance,\n\t * otherwise the id stays tracked until its context is disposed.\n\t * @param contextId - The context identifier the timeout was created under.\n\t * @param id - The timer ID returned by `setTimeout`. Ignored when undefined.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const id = timers.setTimeout('autosave', () => save(), 5000)\n\t * timers.clearTimeout('autosave', id)\n\t * ```\n\t * @public\n\t */\n\tclearTimeout(contextId: string, id: number | undefined): void {\n\t\tif (id === undefined) return\n\t\tclearTimeout(id)\n\t\tthis.timeouts.get(contextId)?.delete(id)\n\t}\n\n\t/**\n\t * Clears an interval created with {@link Timers.setInterval} and stops tracking it.\n\t * @param contextId - The context identifier the interval was created under.\n\t * @param id - The interval ID returned by `setInterval`. Ignored when undefined.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const id = timers.setInterval('refresh', () => updateData(), 1000)\n\t * timers.clearInterval('refresh', id)\n\t * ```\n\t * @public\n\t */\n\tclearInterval(contextId: string, id: number | undefined): void {\n\t\tif (id === undefined) return\n\t\tclearInterval(id)\n\t\tthis.intervals.get(contextId)?.delete(id)\n\t}\n\n\t/**\n\t * Cancels an animation frame requested with {@link Timers.requestAnimationFrame} and stops tracking it.\n\t * @param contextId - The context identifier the animation frame was requested under.\n\t * @param id - The request ID returned by `requestAnimationFrame`. Ignored when undefined.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const id = timers.requestAnimationFrame('render', () => draw())\n\t * timers.cancelAnimationFrame('render', id)\n\t * ```\n\t * @public\n\t */\n\tcancelAnimationFrame(contextId: string, id: number | undefined): void {\n\t\tif (id === undefined) return\n\t\tcancelAnimationFrame(id)\n\t\tthis.rafs.get(contextId)?.delete(id)\n\t}\n\n\t/**\n\t * Disposes of all timers associated with the specified context.\n\t * Clears all timeouts, intervals, and animation frames for the given context ID.\n\t * @param contextId - The context identifier whose timers should be cleared.\n\t * @returns void\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * timers.setTimeout('ui', () => console.log('timeout'), 1000)\n\t * timers.setInterval('ui', () => console.log('interval'), 500)\n\t *\n\t * // Clear all 'ui' context timers\n\t * timers.dispose('ui')\n\t * ```\n\t * @public\n\t */\n\tdispose(contextId: string) {\n\t\tthis.timeouts.get(contextId)?.forEach((id) => clearTimeout(id))\n\t\tthis.intervals.get(contextId)?.forEach((id) => clearInterval(id))\n\t\tthis.rafs.get(contextId)?.forEach((id) => cancelAnimationFrame(id))\n\n\t\tthis.timeouts.delete(contextId)\n\t\tthis.intervals.delete(contextId)\n\t\tthis.rafs.delete(contextId)\n\t}\n\n\t/**\n\t * Disposes of all timers across all contexts.\n\t * Clears every timeout, interval, and animation frame managed by this instance.\n\t * @returns void\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * timers.setTimeout('ui', () => console.log('ui'), 1000)\n\t * timers.setTimeout('background', () => console.log('bg'), 2000)\n\t *\n\t * // Clear everything\n\t * timers.disposeAll()\n\t * ```\n\t * @public\n\t */\n\tdisposeAll() {\n\t\tconst contextIds = new Set([\n\t\t\t...this.timeouts.keys(),\n\t\t\t...this.intervals.keys(),\n\t\t\t...this.rafs.keys(),\n\t\t])\n\t\tfor (const contextId of contextIds) {\n\t\t\tthis.dispose(contextId)\n\t\t}\n\t}\n\n\t/**\n\t * Returns an object with timer methods bound to a specific context.\n\t * Convenient for getting context-specific timer functions without repeatedly passing the contextId.\n\t * @param contextId - The context identifier to bind the returned methods to.\n\t * @returns An object with setTimeout, setInterval, requestAnimationFrame, the matching clear methods, and dispose bound to the context.\n\t * @example\n\t * ```ts\n\t * const timers = new Timers()\n\t * const uiTimers = timers.forContext('ui')\n\t *\n\t * // These are equivalent to calling timers.setTimeout('ui', ...)\n\t * const id = uiTimers.setTimeout(() => console.log('timeout'), 1000)\n\t * uiTimers.setInterval(() => console.log('interval'), 500)\n\t * uiTimers.requestAnimationFrame(() => console.log('frame'))\n\t *\n\t * // Clear a single timer\n\t * uiTimers.clearTimeout(id)\n\t *\n\t * // Dispose only this context\n\t * uiTimers.dispose()\n\t * ```\n\t * @public\n\t */\n\tforContext(contextId: string) {\n\t\treturn {\n\t\t\tsetTimeout: (handler: TimerHandler, timeout?: number, ...args: any[]) =>\n\t\t\t\tthis.setTimeout(contextId, handler, timeout, args),\n\t\t\tsetInterval: (handler: TimerHandler, timeout?: number, ...args: any[]) =>\n\t\t\t\tthis.setInterval(contextId, handler, timeout, args),\n\t\t\trequestAnimationFrame: (callback: FrameRequestCallback) =>\n\t\t\t\tthis.requestAnimationFrame(contextId, callback),\n\t\t\tclearTimeout: (id: number | undefined) => this.clearTimeout(contextId, id),\n\t\t\tclearInterval: (id: number | undefined) => this.clearInterval(contextId, id),\n\t\t\tcancelAnimationFrame: (id: number | undefined) => this.cancelAnimationFrame(contextId, id),\n\t\t\tdispose: () => this.dispose(contextId),\n\t\t}\n\t}\n}\n\nfunction getOrCreateSet(map: Map<string, Set<number>>, contextId: string): Set<number> {\n\tlet set = map.get(contextId)\n\tif (!set) {\n\t\tset = new Set()\n\t\tmap.set(contextId, set)\n\t}\n\treturn set\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAuBO,MAAM,OAAO;AAAA;AAAA;AAAA;AAAA,EAIX,WAAW,oBAAI,IAAyB;AAAA,EACxC,YAAY,oBAAI,IAAyB;AAAA,EACzC,OAAO,oBAAI,IAAyB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAW5C,cAAc;AACb,SAAK,aAAa,KAAK,WAAW,KAAK,IAAI;AAC3C,SAAK,cAAc,KAAK,YAAY,KAAK,IAAI;AAC7C,SAAK,wBAAwB,KAAK,sBAAsB,KAAK,IAAI;AACjE,SAAK,eAAe,KAAK,aAAa,KAAK,IAAI;AAC/C,SAAK,gBAAgB,KAAK,cAAc,KAAK,IAAI;AACjD,SAAK,uBA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