@tldraw/utils
Version:
tldraw infinite canvas SDK (private utilities).
109 lines (108 loc) • 2.86 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var throttle_exports = {};
__export(throttle_exports, {
fpsThrottle: () => fpsThrottle,
throttleToNextFrame: () => throttleToNextFrame
});
module.exports = __toCommonJS(throttle_exports);
const isTest = () => typeof process !== "undefined" && process.env.NODE_ENV === "test" && // @ts-expect-error
!globalThis.__FORCE_RAF_IN_TESTS__;
const fpsQueue = [];
const targetFps = 60;
const targetTimePerFrame = Math.floor(1e3 / targetFps) * 0.9;
let frameRaf;
let flushRaf;
let lastFlushTime = -targetTimePerFrame;
const flush = () => {
const queue = fpsQueue.splice(0, fpsQueue.length);
for (const fn of queue) {
fn();
}
};
function tick(isOnNextFrame = false) {
if (frameRaf) return;
const now = Date.now();
const elapsed = now - lastFlushTime;
if (elapsed < targetTimePerFrame) {
frameRaf = requestAnimationFrame(() => {
frameRaf = void 0;
tick(true);
});
return;
}
if (isOnNextFrame) {
if (flushRaf) return;
lastFlushTime = now;
flush();
} else {
if (flushRaf) return;
flushRaf = requestAnimationFrame(() => {
flushRaf = void 0;
lastFlushTime = now;
flush();
});
}
}
function fpsThrottle(fn) {
if (isTest()) {
fn.cancel = () => {
if (frameRaf) {
cancelAnimationFrame(frameRaf);
frameRaf = void 0;
}
if (flushRaf) {
cancelAnimationFrame(flushRaf);
flushRaf = void 0;
}
};
return fn;
}
const throttledFn = () => {
if (fpsQueue.includes(fn)) {
return;
}
fpsQueue.push(fn);
tick();
};
throttledFn.cancel = () => {
const index = fpsQueue.indexOf(fn);
if (index > -1) {
fpsQueue.splice(index, 1);
}
};
return throttledFn;
}
function throttleToNextFrame(fn) {
if (isTest()) {
fn();
return () => void 0;
}
if (!fpsQueue.includes(fn)) {
fpsQueue.push(fn);
tick();
}
return () => {
const index = fpsQueue.indexOf(fn);
if (index > -1) {
fpsQueue.splice(index, 1);
}
};
}
//# sourceMappingURL=throttle.js.map