@tldraw/utils 5.5.0 → 5.6.0-canary.2675be7f44e8
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 +42 -2
- package/dist-cjs/index.js +4 -5
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/throttle.js +13 -0
- package/dist-cjs/lib/throttle.js.map +3 -3
- package/dist-esm/index.d.mts +42 -2
- package/dist-esm/index.mjs +10 -6
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/throttle.mjs +3 -0
- package/dist-esm/lib/throttle.mjs.map +2 -2
- package/package.json +11 -13
- package/src/index.ts +7 -2
- package/src/lib/throttle.ts +46 -0
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 */
|
|
@@ -1791,7 +1790,48 @@ export declare function sortByMaybeIndex<T extends {
|
|
|
1791
1790
|
declare const structuredClone_2: <T>(i: T) => T;
|
|
1792
1791
|
export { structuredClone_2 as structuredClone }
|
|
1793
1792
|
|
|
1794
|
-
|
|
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
|
+
}
|
|
1795
1835
|
|
|
1796
1836
|
/* Excluded from this release type: throttleToNextFrame */
|
|
1797
1837
|
|
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.
|
|
176
|
+
"5.6.0-canary.2675be7f44e8",
|
|
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_LIBRARY_VERSION,\n\t(globalThis as any).TLDRAW_LIBRARY_MODULES\n)\n"],
|
|
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,iBAAgC;AAChC,mBAUO;AACP,kBAAqB;AACrB,mBAA0B;AAC1B,qBASO;AACP,sBAAyB;AACzB,mBAA0E;AAC1E,4BAA+B;AAC/B,kBAA4B;AAC5B,sBAAyC;AACzC,kBAA0E;AAC1E,gBAAwD;AACxD,sBAAqC;AACrC,sBAAyB;AAEzB,mBAMO;AACP,iBAA2B;AAC3B,qBAA6B;AAC7B,oBAA6C;AAC7C,oBAeO;AACP,kBAA2E;AAC3E,gCAAmC;AACnC,wBAaO;AACP,mBAAsB;AACtB,kBAAyB;AACzB,qBASO;AACP,oBAAoD;AACpD,wBAA2B;AAC3B,sBAMO;AACP,oBAAuB;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/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": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAsB;AAEtB,MAAM,SAAS,MACd,OAAO,YAAY,eACnB,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
"names": ["_throttle"]
|
|
7
7
|
}
|
package/dist-esm/index.d.mts
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 */
|
|
@@ -1791,7 +1790,48 @@ export declare function sortByMaybeIndex<T extends {
|
|
|
1791
1790
|
declare const structuredClone_2: <T>(i: T) => T;
|
|
1792
1791
|
export { structuredClone_2 as structuredClone }
|
|
1793
1792
|
|
|
1794
|
-
|
|
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
|
+
}
|
|
1795
1835
|
|
|
1796
1836
|
/* Excluded from this release type: throttleToNextFrame */
|
|
1797
1837
|
|
package/dist-esm/index.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { registerTldrawLibraryVersion } from "./lib/version.mjs";
|
|
2
2
|
import { default as default2 } from "lodash.isequal";
|
|
3
3
|
import { default as default3 } from "lodash.isequalwith";
|
|
4
|
-
import { default as default4 } from "lodash.
|
|
5
|
-
import { default as default5 } from "lodash.uniq";
|
|
4
|
+
import { default as default4 } from "lodash.uniq";
|
|
6
5
|
import {
|
|
7
6
|
areArraysShallowEqual,
|
|
8
7
|
compact,
|
|
@@ -88,7 +87,12 @@ import {
|
|
|
88
87
|
} from "./lib/storage.mjs";
|
|
89
88
|
import { getFirstCharacter, iterateGraphemes } from "./lib/string.mjs";
|
|
90
89
|
import { stringEnum } from "./lib/stringEnum.mjs";
|
|
91
|
-
import {
|
|
90
|
+
import {
|
|
91
|
+
FpsScheduler,
|
|
92
|
+
fpsThrottle,
|
|
93
|
+
throttle,
|
|
94
|
+
throttleToNextFrame
|
|
95
|
+
} from "./lib/throttle.mjs";
|
|
92
96
|
import { Timers } from "./lib/timers.mjs";
|
|
93
97
|
import { safeParseUrl } from "./lib/url.mjs";
|
|
94
98
|
import {
|
|
@@ -103,7 +107,7 @@ import { registerTldrawLibraryVersion as registerTldrawLibraryVersion2 } from ".
|
|
|
103
107
|
import { warnDeprecatedGetter, warnOnce } from "./lib/warn.mjs";
|
|
104
108
|
registerTldrawLibraryVersion(
|
|
105
109
|
"@tldraw/utils",
|
|
106
|
-
"5.
|
|
110
|
+
"5.6.0-canary.2675be7f44e8",
|
|
107
111
|
"esm"
|
|
108
112
|
);
|
|
109
113
|
export {
|
|
@@ -205,9 +209,9 @@ export {
|
|
|
205
209
|
sortByMaybeIndex,
|
|
206
210
|
stringEnum,
|
|
207
211
|
structuredClone,
|
|
208
|
-
|
|
212
|
+
throttle,
|
|
209
213
|
throttleToNextFrame,
|
|
210
|
-
|
|
214
|
+
default4 as uniq,
|
|
211
215
|
uniqueId,
|
|
212
216
|
validateIndexKey,
|
|
213
217
|
warnDeprecatedGetter,
|
package/dist-esm/index.mjs.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,SAAS,oCAAoC;AAE7C,SAAoB,WAAXA,gBAA0B;AACnC,SAAoB,WAAXA,gBAA8B;AACvC,SAAoB,WAAXA,
|
|
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_LIBRARY_VERSION,\n\t(globalThis as any).TLDRAW_LIBRARY_MODULES\n)\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,oCAAoC;AAE7C,SAAoB,WAAXA,gBAA0B;AACnC,SAAoB,WAAXA,gBAA8B;AACvC,SAAoB,WAAXA,gBAAuB;AAChC;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAC1B;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGM;AACP,SAAS,gBAAgB;AACzB,SAAS,eAAe,2BAAkD;AAC1E,SAAS,sBAAsB;AAC/B,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4 @@
|
|
|
1
|
+
import _throttle from "lodash.throttle";
|
|
1
2
|
const isTest = () => typeof process !== "undefined" && process.env.NODE_ENV === "test" && // @ts-expect-error
|
|
2
3
|
!globalThis.__FORCE_RAF_IN_TESTS__;
|
|
3
4
|
const timingVarianceFactor = 0.9;
|
|
@@ -124,9 +125,11 @@ function fpsThrottle(fn) {
|
|
|
124
125
|
function throttleToNextFrame(fn) {
|
|
125
126
|
return defaultScheduler.throttleToNextFrame(fn);
|
|
126
127
|
}
|
|
128
|
+
const throttle = _throttle;
|
|
127
129
|
export {
|
|
128
130
|
FpsScheduler,
|
|
129
131
|
fpsThrottle,
|
|
132
|
+
throttle,
|
|
130
133
|
throttleToNextFrame
|
|
131
134
|
};
|
|
132
135
|
//# sourceMappingURL=throttle.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/lib/throttle.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": "AAAA,MAAM,SAAS,MACd,OAAO,YAAY,eACnB,QAAQ,IAAI,aAAa;AAEzB,CAAC,WAAW;AAIb,MAAM,uBAAuB;AAC7B,MAAM,wBAAwB,CAAC,cAC9B,KAAK,MAAM,MAAO,SAAS,IAAI;AASzB,MAAM,aAAa;AAAA,EACjB;AAAA,EACA;AAAA,EACA,WAA8B,CAAC;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,YAAoB,KAAK;AACpC,SAAK,YAAY;AACjB,SAAK,qBAAqB,sBAAsB,SAAS;AACzD,SAAK,gBAAgB,CAAC,KAAK;AAAA,EAC5B;AAAA,EAEA,gBAAgB,WAAmB;AAClC,QAAI,cAAc,KAAK,UAAW;AAClC,SAAK,YAAY;AACjB,SAAK,qBAAqB,sBAAsB,SAAS;AACzD,SAAK,gBAAgB,CAAC,KAAK;AAAA,EAC5B;AAAA,EAEQ,QAAQ;AACf,UAAM,QAAQ,KAAK,SA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MAC9B;AAAA,IACD;AACA,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,oBAAoB,IAA4B;AAC/C,QAAI,OAAO,GAAG;AACb,SAAG;AACH,aAAO,MAAM;AAAA,IACd;AAEA,QAAI,CAAC,KAAK,SAAS,SAAS,EAAE,GAAG;AAChC,WAAK,SAAS,KAAK,EAAE;AACrB,WAAK,KAAK;AAAA,IACX;AAEA,WAAO,MAAM;AACZ,YAAM,QAAQ,KAAK,SAAS,QAAQ,EAAE;AACtC,UAAI,QAAQ,IAAI;AACf,aAAK,SAAS,OAAO,OAAO,CAAC;AAAA,MAC9B;AAAA,IACD;AAAA,EACD;AACD;AAGA,MAAM,mBAAmB,IAAI,aAAa,GAAG;AA+BtC,SAAS,YAAY,IAG1B;AACD,SAAO,iBAAiB,YAAY,EAAE;AACvC;AAgCO,SAAS,oBAAoB,IAA4B;AAC/D,SAAO,iBAAiB,oBAAoB,EAAE;AAC/C;",
|
|
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": "AAAA,OAAO,eAAe;AAEtB,MAAM,SAAS,MACd,OAAO,YAAY,eACnB,QAAQ,IAAI,aAAa;AAEzB,CAAC,WAAW;AAIb,MAAM,uBAAuB;AAC7B,MAAM,wBAAwB,CAAC,cAC9B,KAAK,MAAM,MAAO,SAAS,IAAI;AASzB,MAAM,aAAa;AAAA,EACjB;AAAA,EACA;AAAA,EACA,WAA8B,CAAC;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,YAAoB,KAAK;AACpC,SAAK,YAAY;AACjB,SAAK,qBAAqB,sBAAsB,SAAS;AACzD,SAAK,gBAAgB,CAAC,KAAK;AAAA,EAC5B;AAAA,EAEA,gBAAgB,WAAmB;AAClC,QAAI,cAAc,KAAK,UAAW;AAClC,SAAK,YAAY;AACjB,SAAK,qBAAqB,sBAAsB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,QAAQ,KAAK,SAAS,QAAQ,EAAE;AACtC,UAAI,QAAQ,IAAI;AACf,aAAK,SAAS,OAAO,OAAO,CAAC;AAAA,MAC9B;AAAA,IACD;AACA,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,oBAAoB,IAA4B;AAC/C,QAAI,OAAO,GAAG;AACb,SAAG;AACH,aAAO,MAAM;AAAA,IACd;AAEA,QAAI,CAAC,KAAK,SAAS,SAAS,EAAE,GAAG;AAChC,WAAK,SAAS,KAAK,EAAE;AACrB,WAAK,KAAK;AAAA,IACX;AAEA,WAAO,MAAM;AACZ,YAAM,QAAQ,KAAK,SAAS,QAAQ,EAAE;AACtC,UAAI,QAAQ,IAAI;AACf,aAAK,SAAS,OAAO,OAAO,CAAC;AAAA,MAC9B;AAAA,IACD;AAAA,EACD;AACD;AAGA,MAAM,mBAAmB,IAAI,aAAa,GAAG;AA+BtC,SAAS,YAAY,IAG1B;AACD,SAAO,iBAAiB,YAAY,EAAE;AACvC;AAgCO,SAAS,oBAAoB,IAA4B;AAC/D,SAAO,iBAAiB,oBAAoB,EAAE;AAC/C;AAwCO,MAAM,WAIe;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tldraw/utils",
|
|
3
3
|
"description": "tldraw infinite canvas SDK (private utilities).",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.6.0-canary.2675be7f44e8",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "tldraw Inc.",
|
|
7
7
|
"email": "hello@tldraw.com"
|
|
@@ -38,17 +38,6 @@
|
|
|
38
38
|
"src",
|
|
39
39
|
"DOCS.md"
|
|
40
40
|
],
|
|
41
|
-
"scripts": {
|
|
42
|
-
"test-ci": "yarn run -T vitest run --passWithNoTests",
|
|
43
|
-
"test": "yarn run -T vitest --passWithNoTests",
|
|
44
|
-
"test-coverage": "yarn run -T vitest run --coverage --passWithNoTests",
|
|
45
|
-
"build": "yarn run -T tsx ../../internal/scripts/build-package.ts",
|
|
46
|
-
"build-api": "yarn run -T tsx ../../internal/scripts/build-api.ts",
|
|
47
|
-
"prepack": "yarn run -T tsx ../../internal/scripts/prepack.ts",
|
|
48
|
-
"postpack": "../../internal/scripts/postpack.sh",
|
|
49
|
-
"pack-tarball": "yarn pack",
|
|
50
|
-
"lint": "yarn run -T tsx ../../internal/scripts/lint.ts"
|
|
51
|
-
},
|
|
52
41
|
"dependencies": {
|
|
53
42
|
"lodash.isequal": "^4.5.0",
|
|
54
43
|
"lodash.isequalwith": "^4.4.0",
|
|
@@ -60,7 +49,6 @@
|
|
|
60
49
|
"@types/lodash.isequalwith": "^4.4.9",
|
|
61
50
|
"@types/lodash.throttle": "^4.1.9",
|
|
62
51
|
"@types/lodash.uniq": "^4.5.9",
|
|
63
|
-
"lazyrepo": "0.0.0-alpha.27",
|
|
64
52
|
"vitest": "^5.0.0"
|
|
65
53
|
},
|
|
66
54
|
"engines": {
|
|
@@ -73,5 +61,15 @@
|
|
|
73
61
|
"import": "./dist-esm/index.mjs",
|
|
74
62
|
"require": "./dist-cjs/index.js"
|
|
75
63
|
}
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"test-ci": "pnpm exec vitest run --passWithNoTests",
|
|
67
|
+
"test": "pnpm exec vitest --passWithNoTests",
|
|
68
|
+
"test-coverage": "pnpm exec vitest run --coverage --passWithNoTests",
|
|
69
|
+
"build": "pnpm exec tsx ../../internal/scripts/build-package.ts",
|
|
70
|
+
"build-api": "pnpm exec tsx ../../internal/scripts/build-api.ts",
|
|
71
|
+
"pack-tarball": "pnpm pack",
|
|
72
|
+
"lint": "pnpm exec tsx ../../internal/scripts/lint.ts",
|
|
73
|
+
"build-types": "tsc --build"
|
|
76
74
|
}
|
|
77
75
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { registerTldrawLibraryVersion } from './lib/version'
|
|
|
2
2
|
|
|
3
3
|
export { default as isEqual } from 'lodash.isequal'
|
|
4
4
|
export { default as isEqualWith } from 'lodash.isequalwith'
|
|
5
|
-
export { default as throttle } from 'lodash.throttle'
|
|
6
5
|
export { default as uniq } from 'lodash.uniq'
|
|
7
6
|
export {
|
|
8
7
|
areArraysShallowEqual,
|
|
@@ -93,7 +92,13 @@ export {
|
|
|
93
92
|
} from './lib/storage'
|
|
94
93
|
export { getFirstCharacter, iterateGraphemes } from './lib/string'
|
|
95
94
|
export { stringEnum } from './lib/stringEnum'
|
|
96
|
-
export {
|
|
95
|
+
export {
|
|
96
|
+
FpsScheduler,
|
|
97
|
+
fpsThrottle,
|
|
98
|
+
throttle,
|
|
99
|
+
throttleToNextFrame,
|
|
100
|
+
type ThrottledFunction,
|
|
101
|
+
} from './lib/throttle'
|
|
97
102
|
export { Timers } from './lib/timers'
|
|
98
103
|
export {
|
|
99
104
|
type Awaitable,
|
package/src/lib/throttle.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import _throttle from 'lodash.throttle'
|
|
2
|
+
|
|
1
3
|
const isTest = () =>
|
|
2
4
|
typeof process !== 'undefined' &&
|
|
3
5
|
process.env.NODE_ENV === 'test' &&
|
|
@@ -225,3 +227,47 @@ export function fpsThrottle(fn: { (): void; cancel?(): void }): {
|
|
|
225
227
|
export function throttleToNextFrame(fn: () => void): () => void {
|
|
226
228
|
return defaultScheduler.throttleToNextFrame(fn)
|
|
227
229
|
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* The function returned by `throttle`. Call it like the original function; it returns the
|
|
233
|
+
* result of the last invocation, or `undefined` if the original hasn't run yet.
|
|
234
|
+
*
|
|
235
|
+
* Declared here rather than re-exported from `@types/lodash`, so code can name a throttled
|
|
236
|
+
* function's type without depending on lodash's type packages.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* class Saver {
|
|
241
|
+
* save: ThrottledFunction<() => void> = throttle(() => this.persist(), 1000)
|
|
242
|
+
* }
|
|
243
|
+
* ```
|
|
244
|
+
*
|
|
245
|
+
* @public
|
|
246
|
+
*/
|
|
247
|
+
export interface ThrottledFunction<T extends (...args: any[]) => any> {
|
|
248
|
+
(...args: Parameters<T>): ReturnType<T> | undefined
|
|
249
|
+
/** Cancels any pending invocation. */
|
|
250
|
+
cancel(): void
|
|
251
|
+
/** Immediately runs any pending invocation and returns its result. */
|
|
252
|
+
flush(): ReturnType<T> | undefined
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Creates a throttled function that invokes `fn` at most once per `wait` milliseconds. Wraps
|
|
257
|
+
* lodash's `throttle`, typed with {@link ThrottledFunction} so the published types don't depend
|
|
258
|
+
* on lodash's type packages.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```ts
|
|
262
|
+
* const save = throttle(() => persist(), 1000, { leading: false })
|
|
263
|
+
* save()
|
|
264
|
+
* save.flush()
|
|
265
|
+
* ```
|
|
266
|
+
*
|
|
267
|
+
* @public
|
|
268
|
+
*/
|
|
269
|
+
export const throttle: <T extends (...args: any[]) => any>(
|
|
270
|
+
fn: T,
|
|
271
|
+
wait?: number,
|
|
272
|
+
options?: { leading?: boolean; trailing?: boolean }
|
|
273
|
+
) => ThrottledFunction<T> = _throttle
|