UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

107 lines (106 loc) • 3.58 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const ir = require("../ir.cjs"); const refProxyIdentity = require("./ref-proxy-identity.cjs"); function cloneQueryForPlacement(query, resolveCollection) { return cloneQuery(query, { clones: /* @__PURE__ */ new WeakMap(), resolveDescriptor: resolveCollection }); } function cloneQuery(query, context) { const { clones } = context; const existing = clones.get(query); if (existing) return existing; const cloned = { ...query }; clones.set(query, cloned); cloned.from = cloneFromForPlacement(query.from, context); cloned.join = query.join?.map((join) => ({ ...join, from: cloneSourceForPlacement(join.from, context) })); cloned.select = query.select === void 0 || ir.isExpressionLike(query.select) ? query.select : cloneSelectForPlacement(query.select, context); return cloned; } function cloneFromForPlacement(from, context) { if (from.type === `unionFrom`) { return new ir.UnionFrom( from.sources.map((source) => cloneSourceForPlacement(source, context)) ); } if (from.type === `unionAll`) { return new ir.UnionAll(from.queries.map((query) => cloneQuery(query, context))); } return cloneSourceForPlacement(from, context); } function cloneSourceForPlacement(source, context) { if (source.type === `collectionRef`) { return new ir.CollectionRef(source.collection, source.alias, source.bindingId); } if (source.type === `descriptorRef`) { return context.resolveDescriptor ? new ir.CollectionRef( context.resolveDescriptor(source.descriptor), source.alias, source.bindingId ) : new ir.DescriptorRef(source.descriptor, source.alias, source.bindingId); } return new ir.QueryRef( cloneQuery(source.query, context), source.alias, source.bindingId ); } function cloneSelectForPlacement(select, context) { const { clones } = context; const existing = clones.get(select); if (existing) return existing; const cloned = {}; clones.set(select, cloned); for (const [field, value] of Object.entries(select)) { cloned[field] = cloneSelectValueForPlacement(value, context); } return cloned; } function cloneSelectValueForPlacement(value, context) { const { clones } = context; if (value instanceof ir.IncludesSubquery) { const existing = clones.get(value); if (existing) return existing; const cloned = new ir.IncludesSubquery( cloneQuery(value.query, context), value.correlationField, value.childCorrelationField, value.fieldName, value.parentFilters, value.parentProjection, value.materialization, value.scalarField ); clones.set(value, cloned); return cloned; } if (value instanceof ir.ConditionalSelect) { const existing = clones.get(value); if (existing) return existing; const cloned = new ir.ConditionalSelect( value.branches.map((branch) => ({ ...branch, value: cloneSelectValueForPlacement(branch.value, context) })), value.defaultValue !== void 0 ? cloneSelectValueForPlacement(value.defaultValue, context) : void 0 ); clones.set(value, cloned); return cloned; } if (value === null || typeof value !== `object` || Array.isArray(value)) { return value; } if (refProxyIdentity.isRefProxy(value)) { return value; } return ir.isExpressionLike(value) ? value : cloneSelectForPlacement(value, context); } exports.cloneQueryForPlacement = cloneQueryForPlacement; //# sourceMappingURL=clone-query.cjs.map