@tanstack/react-db
Version:
React integration for @tanstack/db
52 lines (51 loc) • 2.08 kB
JavaScript
import { getPreparedLiveQuerySources } from "@tanstack/db";
import { resumeDeferredLiveQueryCollections } from "@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 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);
}
export {
claimSourceIds,
createSourceIdBindings,
getPreparedSources,
getSourceObjectToken,
resumeDeferredLiveQueryCollections as resumeSyncStarts
};
//# sourceMappingURL=source-id-bindings.js.map