@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.
104 lines (103 loc) • 4.72 kB
JavaScript
import { shallowEqual } from "fast-equals";
//#region src/utils/selector-core/create-cached-selector.ts
var proxyValuesWeakMap = /* @__PURE__ */ new WeakMap();
var collectionFieldsSet = new Set([
"idField",
"ids",
"map",
"refsCount"
]);
var safePropertyNameRegex = /^[A-Za-z_$][0-9A-Za-z_$]*$/;
var renderAccessedPathSegment = (segment, index) => {
if (typeof segment === "symbol") return `[${String(segment)}]`;
if (index === 0 && safePropertyNameRegex.test(segment)) return segment;
if (safePropertyNameRegex.test(segment)) return `.${segment}`;
return `[${JSON.stringify(segment)}]`;
};
var renderAccessedPath = (path) => path.map(renderAccessedPathSegment).join("");
var renderAccessedPaths = (accessedPaths, parsedPaths) => Array.from(accessedPaths).map((pathString) => parsedPaths.get(pathString)).filter((path) => Boolean(path)).map(renderAccessedPath).sort();
var getRawValue = (maybeProxy) => {
if (maybeProxy === null || typeof maybeProxy !== "object" && typeof maybeProxy !== "function") return maybeProxy;
return proxyValuesWeakMap.get(maybeProxy) ?? maybeProxy;
};
var isCollectionLike = (item) => {
const record = item;
if (typeof record.idField !== "string") return false;
if (!Array.isArray(record.ids)) return false;
if (typeof record.map !== "object" || record.map === null || Array.isArray(record.map)) return false;
if (typeof record.refsCount !== "object" || record.refsCount === null || Array.isArray(record.refsCount)) return false;
for (const itemKey of Object.keys(item)) if (!collectionFieldsSet.has(itemKey)) return false;
return true;
};
var createTrackingProxy = (target, accessedPaths, parsedPaths, currentPath = []) => {
if (target === null || target === void 0 || typeof target !== "object") return target;
const rawTarget = getRawValue(target);
const proxy = new Proxy(rawTarget, { get(obj, prop) {
const value = Reflect.get(obj, prop);
if (!prop) return value;
if (typeof prop === "string" && collectionFieldsSet.has(prop) && isCollectionLike(obj)) return value;
const newPath = [...currentPath, prop];
const pathString = JSON.stringify(newPath);
parsedPaths.set(pathString, newPath);
accessedPaths.add(pathString);
if (value !== null && typeof value === "object" && !Array.isArray(value) && !isCollectionLike(value)) return createTrackingProxy(value, accessedPaths, parsedPaths, newPath);
return value;
} });
proxyValuesWeakMap.set(proxy, rawTarget);
return proxy;
};
var isValueChangedAtPath = (prevState, nextState, path) => {
let currentPrev = prevState;
let currentNext = nextState;
for (const key of path) {
currentPrev = currentPrev ? currentPrev[key] : void 0;
currentNext = currentNext ? currentNext[key] : void 0;
if (currentPrev === currentNext) return false;
}
return true;
};
var hasStateChanged = (oldState, newState, accessedPaths, parsedPaths) => {
for (const pathStr of accessedPaths) {
const path = parsedPaths.get(pathStr);
if (!path) continue;
if (isValueChangedAtPath(oldState, newState, path)) return true;
}
return false;
};
var createCachedSelector = (selectorFunc, options) => {
let previousSelectResult = void 0;
let previousArgs = void 0;
let previousState = void 0;
let accessedPaths = /* @__PURE__ */ new Set();
const parsedPaths = /* @__PURE__ */ new Map();
return (state, ...args) => {
const rawValue = getRawValue(state);
if (options?.lockUpdatesPredicate?.(rawValue) && previousSelectResult !== void 0) return previousSelectResult;
const argsChanged = !previousArgs || !shallowEqual(args, previousArgs);
const stateChanged = !argsChanged && previousState ? hasStateChanged(previousState, rawValue, accessedPaths, parsedPaths) : true;
if (!argsChanged && !stateChanged && previousSelectResult !== void 0) return previousSelectResult;
if (rawValue !== state) {
const result = selectorFunc(state, ...args);
previousArgs = args;
previousSelectResult = result;
return result;
}
const newAccessedPaths = /* @__PURE__ */ new Set();
const result = getRawValue(selectorFunc(createTrackingProxy(state, newAccessedPaths, parsedPaths), ...args));
options?.traceReporter?.({
selectorFunc,
accessedPathCount: newAccessedPaths.size,
accessedPaths: newAccessedPaths,
parsedPaths
});
const finalResult = previousSelectResult !== void 0 && shallowEqual(previousSelectResult, result) ? previousSelectResult : result;
previousSelectResult = finalResult;
previousArgs = args;
previousState = rawValue;
accessedPaths = newAccessedPaths;
return finalResult;
};
};
//#endregion
export { createCachedSelector, createTrackingProxy, getRawValue, hasStateChanged, renderAccessedPath, renderAccessedPaths };
//# sourceMappingURL=create-cached-selector.js.map