@augmentcode/ag-redux-toolkit
Version:
Redux + Saga state management for Svelte apps — designed for AI agents. Includes runtime TypeScript source, AI skills, and reference documentation for robust, observable, high-performance state management.
137 lines (136 loc) • 4.86 kB
JavaScript
//#region src/utils/selector-core/throttled-selector-options.ts
var DEFAULT_THROTTLED_SELECTOR_FREQUENCY = 64;
var MIN_THROTTLED_SELECTOR_FREQUENCY = 1;
var MAX_THROTTLED_SELECTOR_FREQUENCY = 256;
var validateThrottledSelectorFrequency = (frequency) => {
if (typeof frequency !== "number" || !Number.isFinite(frequency) || frequency < 1 || frequency > 256) throw new RangeError(`Store option "throttledSelectorFrequency" must be a finite number in the inclusive range 1..256 FPS. Received ${String(frequency)}.`);
return frequency;
};
var hasRAF = () => typeof requestAnimationFrame === "function";
var getNextFlushDelay = (lastFlushWallTimeAt, frameIntervalMs, now) => {
return lastFlushWallTimeAt === null ? 0 : Math.max(0, frameIntervalMs - (now - lastFlushWallTimeAt));
};
var createSelectorFlushManager = (throttledSelectorFrequency = 64, options = {}) => {
const frequency = validateThrottledSelectorFrequency(throttledSelectorFrequency);
const frameIntervalMs = 1e3 / frequency;
const traceSelectors = options.traceSelectors === true;
let queuedCallbacks = /* @__PURE__ */ new Set();
let frameId = null;
let timerId = null;
let timerDueAt = null;
let lastFlushWallTimeAt = null;
let flushingCallbacks = null;
let disposed = false;
const hasScheduledFlush = () => frameId !== null || timerId !== null;
const canRunScheduledFlush = () => {
if (disposed || queuedCallbacks.size === 0) return false;
return true;
};
const canScheduleFlush = () => canRunScheduledFlush() && !hasScheduledFlush();
const clearScheduledFlush = () => {
if (frameId !== null) {
if (typeof cancelAnimationFrame === "function") cancelAnimationFrame(frameId);
frameId = null;
}
if (timerId !== null) {
clearTimeout(timerId);
timerId = null;
}
timerDueAt = null;
};
const runFrame = (timestamp) => {
frameId = null;
if (timerDueAt !== null) {
if (Math.max(Date.now(), timestamp) < timerDueAt) {
scheduleFrame();
return;
}
timerDueAt = null;
}
runScheduledFlush(timestamp);
};
const runTimer = (timestamp) => {
timerId = null;
if (timerDueAt !== null && Date.now() < timerDueAt) {
const remainingDelayMs = timerDueAt - Date.now();
if (hasRAF() && remainingDelayMs < 1) {
scheduleFrame();
return;
}
timerId = setTimeout(() => {
runTimer(Date.now());
}, remainingDelayMs);
return;
}
timerDueAt = null;
if (hasRAF()) {
scheduleFrame();
return;
}
runScheduledFlush(timestamp);
};
const scheduleFrame = () => {
if (canScheduleFlush()) frameId = requestAnimationFrame(runFrame);
};
const scheduleTimer = (delayMs) => {
timerDueAt = Date.now() + delayMs;
const timerDelayMs = hasRAF() ? Math.floor(delayMs) : delayMs;
timerId = setTimeout(() => {
runTimer(Date.now());
}, timerDelayMs);
};
const runScheduledFlush = (timestamp) => {
if (!canRunScheduledFlush()) return;
flush(timestamp);
};
const scheduleFlush = () => {
if (!canScheduleFlush()) return;
const delay = getNextFlushDelay(lastFlushWallTimeAt, frameIntervalMs, Date.now());
if (delay > 0) {
scheduleTimer(delay);
return;
}
if (hasRAF()) scheduleFrame();
else scheduleTimer(0);
};
const flush = (timestamp) => {
clearScheduledFlush();
if (disposed || queuedCallbacks.size === 0) return;
if (traceSelectors) console.info("START FLUSHING", Math.max(Date.now(), timestamp));
const callbacksToFlush = queuedCallbacks;
queuedCallbacks = /* @__PURE__ */ new Set();
flushingCallbacks = callbacksToFlush;
lastFlushWallTimeAt = Date.now();
for (const callback of callbacksToFlush) callback(timestamp);
flushingCallbacks = null;
if (traceSelectors) console.info("FLUSHED", lastFlushWallTimeAt, callbacksToFlush.size);
};
return {
frequency,
frameIntervalMs,
requestFlush(callback) {
if (traceSelectors) console.info("REQUEST FLUSH", callback);
if (disposed) return;
queuedCallbacks.add(callback);
if (canScheduleFlush()) scheduleFlush();
},
cancelFlush(callback) {
queuedCallbacks.delete(callback);
flushingCallbacks?.delete(callback);
if (queuedCallbacks.size === 0) clearScheduledFlush();
},
dispose() {
disposed = true;
queuedCallbacks.clear();
clearScheduledFlush();
}
};
};
var resolveSelectorFlushManager = (selectorFlushManagerOrFrequency = 64) => {
if (typeof selectorFlushManagerOrFrequency === "number") return createSelectorFlushManager(selectorFlushManagerOrFrequency);
if (typeof selectorFlushManagerOrFrequency === "function") return selectorFlushManagerOrFrequency();
return selectorFlushManagerOrFrequency;
};
//#endregion
export { DEFAULT_THROTTLED_SELECTOR_FREQUENCY, MAX_THROTTLED_SELECTOR_FREQUENCY, MIN_THROTTLED_SELECTOR_FREQUENCY, createSelectorFlushManager, resolveSelectorFlushManager, validateThrottledSelectorFrequency };
//# sourceMappingURL=throttled-selector-options.js.map