@tanstack/db
Version:
A reactive client store for building super fast apps on sync
46 lines (45 loc) • 2.95 kB
JavaScript
import { SourceCollectionIdReuseError } from "./errors.js";
import { getLiveQueryStatusFlags, getPublicCollection, isCollection, isFailedStatus, isSingleResultCollection } from "./live-query-primitives.js";
import { createLiveQueryObserver, toBoundaryError } from "./live-query-observer.js";
import { configPool, getLiveQueryHash, getPreparedLiveQueryIdentity, getPreparedLiveQuerySources, prepareLiveQueryValue, rejectLiveQueryInput, releaseDeferredOnReclaim, resolveLiveQueryValue, resumeDeferredLiveQueryCollections } from "./live-query-options.js";
import { createLiveQueryWindowController, fetchNextLiveQueryWindowPage } from "./live-query-window-controller.js";
import { bindLiveQueryWindow, readLiveQueryWindowPageShape } from "./live-query-window-binding.js";
function getLiveQueryKey(input) {
const queryKey = input && typeof input === `object` && !isCollection(input) ? input.queryKey : void 0;
return Array.isArray(queryKey) && queryKey.length > 0 ? queryKey : void 0;
}
var sourceObjectTokens = /* @__PURE__ */ new WeakMap();
var nextSourceObjectToken = 0;
function getSourceObjectToken(source) {
let token = sourceObjectTokens.get(source);
if (token === void 0) {
token = ++nextSourceObjectToken;
sourceObjectTokens.set(source, token);
}
return token;
}
var unscopedClaims = {};
function createLiveQuerySourceIdClaims() {
return /* @__PURE__ */ new WeakMap();
}
function claimLiveQuerySourceIds(claims, sources, client, bindingName) {
const scope = client ?? unscopedClaims;
const prior = claims.get(scope);
const seen = /* @__PURE__ */ new Map();
for (const source of sources) {
const token = getSourceObjectToken(source);
const previous = seen.get(source.id) ?? prior?.get(source.id);
if (previous !== void 0 && previous !== token) throw new SourceCollectionIdReuseError(bindingName, source.id);
seen.set(source.id, token);
}
const claimed = prior ?? /* @__PURE__ */ new Map();
for (const [id, token] of seen) claimed.set(id, token);
claims.set(scope, claimed);
}
function identifyLiveQuery(preparedValue, scope, identify = getPreparedLiveQueryIdentity) {
const identity = identify(preparedValue);
claimLiveQuerySourceIds(scope.claims, getPreparedLiveQuerySources(preparedValue, scope.onUnboundDescriptor), scope.client, scope.bindingName);
return identity;
}
export { bindLiveQueryWindow, claimLiveQuerySourceIds, configPool, createLiveQueryObserver, createLiveQuerySourceIdClaims, createLiveQueryWindowController, fetchNextLiveQueryWindowPage, getLiveQueryHash, getLiveQueryKey, getLiveQueryStatusFlags, getPreparedLiveQueryIdentity, getPreparedLiveQuerySources, getPublicCollection, getSourceObjectToken, identifyLiveQuery, isCollection, isFailedStatus, isSingleResultCollection, prepareLiveQueryValue, readLiveQueryWindowPageShape, rejectLiveQueryInput, releaseDeferredOnReclaim, resolveLiveQueryValue, resumeDeferredLiveQueryCollections, toBoundaryError };
//# sourceMappingURL=live-query-adapter.js.map