@tanstack/table-devtools
Version:
Devtools for TanStack Table.
54 lines (52 loc) • 1.55 kB
JavaScript
import { batch } from "solid-js";
//#region src/devtoolsUpdateScheduler.ts
const UPDATE_DELAY_MS = 32;
const IDLE_TIMEOUT_MS = 100;
const pendingUpdates = /* @__PURE__ */ new Set();
let delayHandle;
let idleHandle;
function flushUpdates() {
delayHandle = void 0;
idleHandle = void 0;
const updates = Array.from(pendingUpdates);
pendingUpdates.clear();
batch(() => {
for (const update of updates) update();
});
}
function cancelScheduledFlush() {
if (delayHandle !== void 0) {
clearTimeout(delayHandle);
delayHandle = void 0;
}
if (idleHandle !== void 0 && typeof globalThis.cancelIdleCallback === "function") {
globalThis.cancelIdleCallback(idleHandle);
idleHandle = void 0;
}
}
function requestFlush() {
if (delayHandle !== void 0 || idleHandle !== void 0) return;
delayHandle = setTimeout(() => {
delayHandle = void 0;
if (typeof globalThis.requestIdleCallback === "function") {
idleHandle = globalThis.requestIdleCallback(flushUpdates, { timeout: IDLE_TIMEOUT_MS });
return;
}
delayHandle = setTimeout(flushUpdates, 0);
}, UPDATE_DELAY_MS);
}
/**
* Coalesces devtools-only reactive work and yields to the inspected app before
* flushing it. The returned function removes this update if its owner unmounts
* or its source changes before the queue is flushed.
*/
function scheduleDevtoolsUpdate(update) {
pendingUpdates.add(update);
requestFlush();
return () => {
pendingUpdates.delete(update);
if (pendingUpdates.size === 0) cancelScheduledFlush();
};
}
//#endregion
export { scheduleDevtoolsUpdate };