UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

221 lines (220 loc) • 8.15 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const ir = require("../ir.cjs"); const refProxyIdentity = require("./ref-proxy-identity.cjs"); const wrapperIdentity = require("./wrapper-identity.cjs"); function createSingleRowRefProxy() { const cache = /* @__PURE__ */ new Map(); function createProxy(path) { const pathKey = JSON.stringify(path); if (cache.has(pathKey)) { return cache.get(pathKey); } const proxy = new Proxy({}, { get(target, prop, receiver) { if (prop === `__refProxy`) return true; if (prop === `__path`) return path; if (prop === `__sourceAlias`) return void 0; if (prop === `__type`) return void 0; if (prop === refProxyIdentity.REF_PROXY_BRAND) return true; if (typeof prop === `symbol`) return Reflect.get(target, prop, receiver); const newPath = [...path, String(prop)]; return createProxy(newPath); }, has(target, prop) { if (prop === `__refProxy` || prop === `__path` || prop === `__sourceAlias` || prop === `__type`) return true; return Reflect.has(target, prop); }, ownKeys(target) { return Reflect.ownKeys(target); }, getOwnPropertyDescriptor(target, prop) { if (prop === `__refProxy` || prop === `__path` || prop === `__sourceAlias` || prop === `__type`) { return { enumerable: false, configurable: true }; } return Reflect.getOwnPropertyDescriptor(target, prop); } }); cache.set(pathKey, proxy); return proxy; } return createProxy([]); } function createRefProxy(aliases) { const aliasProxies = /* @__PURE__ */ new Map(); let accessId = 0; function createProxy(path) { let children; const proxy = new Proxy({}, { get(target, prop, receiver) { if (prop === `__refProxy`) return true; if (prop === `__path`) return path; if (prop === `__sourceAlias`) return path[0]; if (prop === `__type`) return void 0; if (prop === refProxyIdentity.REF_PROXY_BRAND) return path; if (typeof prop === `symbol`) return Reflect.get(target, prop, receiver); children ??= /* @__PURE__ */ new Map(); let child = children.get(prop); if (child === void 0) { child = createProxy([...path, prop]); children.set(prop, child); } return child; }, has(target, prop) { if (prop === `__refProxy` || prop === `__path` || prop === `__sourceAlias` || prop === `__type`) return true; return Reflect.has(target, prop); }, ownKeys(target) { const id = ++accessId; const sentinelKey = `__SPREAD_SENTINEL__${path.join(`.`)}__${id}`; if (!Object.prototype.hasOwnProperty.call(target, sentinelKey)) { Object.defineProperty(target, sentinelKey, { enumerable: true, configurable: true, value: true }); } return Reflect.ownKeys(target); }, getOwnPropertyDescriptor(target, prop) { if (prop === `__refProxy` || prop === `__path` || prop === `__sourceAlias` || prop === `__type`) { return { enumerable: false, configurable: true }; } return Reflect.getOwnPropertyDescriptor(target, prop); } }); return proxy; } const rootProxy = new Proxy({}, { get(target, prop, receiver) { if (prop === `__refProxy`) return true; if (prop === `__path`) return []; if (prop === `__sourceAlias`) return void 0; if (prop === `__type`) return void 0; if (prop === refProxyIdentity.REF_PROXY_BRAND) return true; if (typeof prop === `symbol`) return Reflect.get(target, prop, receiver); const propStr = String(prop); if (aliases.includes(propStr) || aliases.includes(`*`)) { let proxy = aliasProxies.get(propStr); if (proxy === void 0) { proxy = createProxy([propStr]); aliasProxies.set(propStr, proxy); } return proxy; } return void 0; }, has(target, prop) { if (prop === `__refProxy` || prop === `__path` || prop === `__sourceAlias` || prop === `__type`) return true; if (typeof prop === `string` && aliases.includes(prop)) return true; return Reflect.has(target, prop); }, ownKeys(_target) { return [...aliases, `__refProxy`, `__path`, `__sourceAlias`, `__type`]; }, getOwnPropertyDescriptor(target, prop) { if (prop === `__refProxy` || prop === `__path` || prop === `__sourceAlias` || prop === `__type`) { return { enumerable: false, configurable: true }; } if (typeof prop === `string` && aliases.includes(prop)) { return { enumerable: true, configurable: true }; } return void 0; } }); return rootProxy; } function createRefProxyWithSelected(aliases) { const baseProxy = createRefProxy(aliases); const cache = /* @__PURE__ */ new Map(); function createSelectedProxy(path) { const pathKey = JSON.stringify(path); if (cache.has(pathKey)) { return cache.get(pathKey); } const proxy = new Proxy({}, { get(target, prop, receiver) { if (prop === `__refProxy`) return true; if (prop === `__path`) return [`$selected`, ...path]; if (prop === `__sourceAlias`) return `$selected`; if (prop === `__type`) return void 0; if (prop === refProxyIdentity.REF_PROXY_BRAND) return true; if (typeof prop === `symbol`) return Reflect.get(target, prop, receiver); const newPath = [...path, String(prop)]; return createSelectedProxy(newPath); }, has(target, prop) { if (prop === `__refProxy` || prop === `__path` || prop === `__sourceAlias` || prop === `__type`) return true; return Reflect.has(target, prop); }, ownKeys(target) { return Reflect.ownKeys(target); }, getOwnPropertyDescriptor(target, prop) { if (prop === `__refProxy` || prop === `__path` || prop === `__sourceAlias` || prop === `__type`) { return { enumerable: false, configurable: true }; } return Reflect.getOwnPropertyDescriptor(target, prop); } }); cache.set(pathKey, proxy); return proxy; } const wrappedSelectedProxy = createSelectedProxy([]); const selectedRootProxy = new Proxy(baseProxy, { get(target, prop, receiver) { if (prop === `$selected`) { return wrappedSelectedProxy; } return Reflect.get(target, prop, receiver); }, has(target, prop) { if (prop === `$selected`) return true; return Reflect.has(target, prop); }, ownKeys(target) { return [...Reflect.ownKeys(target), `$selected`]; }, getOwnPropertyDescriptor(target, prop) { if (prop === `$selected`) { return { enumerable: true, configurable: true, value: wrappedSelectedProxy }; } return Reflect.getOwnPropertyDescriptor(target, prop); } }); return selectedRootProxy; } function toExpression(value) { if (value === null || typeof value !== `object` && typeof value !== `function`) { return new ir.Value(value); } const brand = refProxyIdentity.readRefProxyBrand(value); if (brand !== void 0) { return Array.isArray(brand) ? new ir.PropRef(brand, brand[0]) : new ir.PropRef(value.__path, value.__sourceAlias); } const name = wrapperIdentity.getWrapperExpressionName(value); if (name) { throw new Error( `${name} cannot be used inside expressions (e.g., coalesce(), eq(), not()). Use ${name} directly as a select field value instead.` ); } if (ir.isBasicOrAggregateExpression(value)) { return value; } return new ir.Value(value); } exports.isRefProxy = refProxyIdentity.isRefProxy; exports.createRefProxy = createRefProxy; exports.createRefProxyWithSelected = createRefProxyWithSelected; exports.createSingleRowRefProxy = createSingleRowRefProxy; exports.toExpression = toExpression; //# sourceMappingURL=ref-proxy.cjs.map