@tanstack/react-db
Version:
React integration for @tanstack/db
54 lines (53 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const db = require("@tanstack/db");
const sourceObjectTokens = /* @__PURE__ */ new WeakMap();
let nextSourceObjectToken = 0;
function getSourceObjectToken(source) {
let id = sourceObjectTokens.get(source);
if (id === void 0) {
id = ++nextSourceObjectToken;
sourceObjectTokens.set(source, id);
}
return id;
}
function getPreparedSources(preparedValue, client, hookName) {
return db.getPreparedLiveQuerySources(
preparedValue,
!client && hookName ? (alias) => {
throw new Error(
`[${hookName}] Collection descriptor "${alias}" requires a DbClient when the query is consumed. Wrap this component in <DbProvider client={client}> or use a concrete Collection.`
);
} : void 0
);
}
function createSourceIdBindings() {
return { unscoped: /* @__PURE__ */ new Map(), byClient: /* @__PURE__ */ new WeakMap() };
}
function claimSourceIds(bindings, preparedValue, client, hookName, release) {
const prior = client ? bindings.byClient.get(client) : bindings.unscoped;
const seen = /* @__PURE__ */ new Map();
for (const source of getPreparedSources(preparedValue, client, hookName)) {
const token = getSourceObjectToken(source);
const previous = seen.get(source.id) ?? prior?.get(source.id);
if (previous !== void 0 && previous !== token) {
release();
throw new Error(
`[${hookName}] Source Collection "${source.id}" was replaced by a different Collection with the same ID while this hook is mounted. Unmount the hook and clean up its previous source and client scope before reusing the ID.`
);
}
seen.set(source.id, token);
}
const claimed = prior ?? /* @__PURE__ */ new Map();
for (const [id, token] of seen) claimed.set(id, token);
if (client) bindings.byClient.set(client, claimed);
}
Object.defineProperty(exports, "resumeSyncStarts", {
enumerable: true,
get: () => db.resumeDeferredLiveQueryCollections
});
exports.claimSourceIds = claimSourceIds;
exports.createSourceIdBindings = createSourceIdBindings;
exports.getPreparedSources = getPreparedSources;
exports.getSourceObjectToken = getSourceObjectToken;
//# sourceMappingURL=source-id-bindings.cjs.map