@tanstack/db
Version:
A reactive client store for building super fast apps on sync
130 lines (129 loc) • 4.6 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const ir = require("../ir.cjs");
function createSingleRowRefProxy() {
const cache = /* @__PURE__ */ new Map();
function createProxy(path) {
const pathKey = path.join(`.`);
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 === `__type`) return void 0;
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 === `__type`)
return true;
return Reflect.has(target, prop);
},
ownKeys(target) {
return Reflect.ownKeys(target);
},
getOwnPropertyDescriptor(target, prop) {
if (prop === `__refProxy` || prop === `__path` || prop === `__type`) {
return { enumerable: false, configurable: true };
}
return Reflect.getOwnPropertyDescriptor(target, prop);
}
});
cache.set(pathKey, proxy);
return proxy;
}
return createProxy([]);
}
function createRefProxy(aliases) {
const cache = /* @__PURE__ */ new Map();
const spreadSentinels = /* @__PURE__ */ new Set();
function createProxy(path) {
const pathKey = path.join(`.`);
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 === `__type`) return void 0;
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 === `__type`)
return true;
return Reflect.has(target, prop);
},
ownKeys(target) {
if (path.length === 1) {
const aliasName = path[0];
spreadSentinels.add(aliasName);
}
return Reflect.ownKeys(target);
},
getOwnPropertyDescriptor(target, prop) {
if (prop === `__refProxy` || prop === `__path` || prop === `__type`) {
return { enumerable: false, configurable: true };
}
return Reflect.getOwnPropertyDescriptor(target, prop);
}
});
cache.set(pathKey, proxy);
return proxy;
}
const rootProxy = new Proxy({}, {
get(target, prop, receiver) {
if (prop === `__refProxy`) return true;
if (prop === `__path`) return [];
if (prop === `__type`) return void 0;
if (prop === `__spreadSentinels`) return spreadSentinels;
if (typeof prop === `symbol`) return Reflect.get(target, prop, receiver);
const propStr = String(prop);
if (aliases.includes(propStr)) {
return createProxy([propStr]);
}
return void 0;
},
has(target, prop) {
if (prop === `__refProxy` || prop === `__path` || prop === `__type` || prop === `__spreadSentinels`)
return true;
if (typeof prop === `string` && aliases.includes(prop)) return true;
return Reflect.has(target, prop);
},
ownKeys(_target) {
return [...aliases, `__refProxy`, `__path`, `__type`, `__spreadSentinels`];
},
getOwnPropertyDescriptor(target, prop) {
if (prop === `__refProxy` || prop === `__path` || prop === `__type` || prop === `__spreadSentinels`) {
return { enumerable: false, configurable: true };
}
if (typeof prop === `string` && aliases.includes(prop)) {
return { enumerable: true, configurable: true };
}
return void 0;
}
});
return rootProxy;
}
function toExpression(value) {
if (isRefProxy(value)) {
return new ir.PropRef(value.__path);
}
if (value && typeof value === `object` && `type` in value && (value.type === `func` || value.type === `ref` || value.type === `val` || value.type === `agg`)) {
return value;
}
return new ir.Value(value);
}
function isRefProxy(value) {
return value && typeof value === `object` && value.__refProxy === true;
}
exports.createRefProxy = createRefProxy;
exports.createSingleRowRefProxy = createSingleRowRefProxy;
exports.isRefProxy = isRefProxy;
exports.toExpression = toExpression;
//# sourceMappingURL=ref-proxy.cjs.map