@tanstack/db
Version:
A reactive client store for building super fast apps on sync
132 lines (131 loc) • 6.16 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const errorMessage = require("./error-message.cjs");
const errors = require("./errors.cjs");
const liveQueryOptions = require("./live-query-options.cjs");
const liveQueryPrimitives = require("./live-query-primitives.cjs");
const liveQueryCollection = require("./query/live-query-collection.cjs");
const liveQueryWindowController = require("./live-query-window-controller.cjs");
const WINDOW_COLLECTION_GC_TIME_MS = 1;
function readLiveQueryWindowPageShape(config) {
if (`getNextPageParam` in config) {
throw new Error(
errorMessage.devBuild() && process.env.NODE_ENV !== `production` ? `getNextPageParam is not supported by useLiveInfiniteQuery. Use an on-demand collection and fulfill meta.loadSubsetOptions in queryFn for server pagination.` : errorMessage.codedMessage(246)
);
}
return {
pageSize: liveQueryWindowController.normalizeLiveQueryWindowPageSize(config.pageSize),
initialPageParam: config.initialPageParam ?? 0
};
}
function bindLiveQueryWindow(previous, update, uncommitted) {
const resolved = update.recompile === `always` ? liveQueryWindowController.resolveLiveQueryWindowInput(update.input) : void 0;
const supplied = resolved === void 0 ? liveQueryWindowController.getLiveQueryWindowInputKind(update.input) === `collection` ? update.input : void 0 : resolved.kind === `collection` ? resolved.collection : void 0;
const inputKind = supplied ? `collection` : `query`;
const previousKind = previous ? previous.supplied ? `collection` : `query` : void 0;
const dependencies = supplied ? void 0 : [...update.dependencies ?? []];
const change = compareUpdate(previous, update, dependencies);
const needsNewCollection = previous === null || previousKind !== inputKind || (supplied ? previous.supplied !== supplied : change.recompile);
const pageShapeChanged = previous === null || previous.pageSize !== update.pageSize || previous.initialPageParam !== update.initialPageParam;
if (!needsNewCollection && !pageShapeChanged) return previous;
const initialPageCount = liveQueryWindowController.shouldPreserveLiveQueryWindowPageCount({
hasPreviousController: previous !== null,
previousInputKind: previousKind,
inputKind,
sameCollection: supplied !== void 0 && previous?.supplied === supplied,
dependenciesChanged: change.dependenciesChanged,
dependenciesStructurallyEqual: change.dependenciesStructurallyEqual,
pageShapeChanged
}) ? Math.max(1, previous.controller.getSnapshot().pages.length) : 1;
const requiredLimit = initialPageCount * update.pageSize + 1;
let collection = previous?.collection;
let queryHash = previous?.queryHash;
let ownsCollection = previous?.ownsCollection ?? false;
let warning;
if (needsNewCollection) {
if (!supplied && uncommitted?.ownsCollection && !compareUpdate(uncommitted, update, dependencies).recompile && uncommitted.collection.status !== `cleaned-up` && !liveQueryPrimitives.isFailedStatus(uncommitted.collection.status) && liveQueryWindowController.liveQueryWindowMatches(uncommitted.collection, requiredLimit)) {
collection = uncommitted.collection;
queryHash = uncommitted.queryHash;
ownsCollection = true;
} else {
const window = openWindowCollection(
resolved ?? liveQueryWindowController.resolveLiveQueryWindowInput(update.input),
requiredLimit,
update
);
collection = window.collection;
queryHash = window.queryHash;
ownsCollection = window.ownsCollection;
warning = window.warning;
}
}
return {
supplied,
dependencies,
scope: update.scope,
pageSize: update.pageSize,
initialPageParam: update.initialPageParam,
collection,
queryHash,
ownsCollection,
controller: liveQueryWindowController.createLiveQueryWindowController(collection, {
pageSize: update.pageSize,
initialPageParam: update.initialPageParam,
initialPageCount,
client: update.client,
queryHash
}),
warning
};
}
function compareUpdate(binding, update, dependencies) {
const comparison = liveQueryWindowController.compareLiveQueryWindowDependencies(
binding?.dependencies,
dependencies ?? []
);
const scopeChanged = binding?.scope !== update.scope;
const dependenciesChanged = scopeChanged || (update.recompile === `identity` ? !comparison.structurallyEqual : comparison.changed);
return {
recompile: update.recompile === `always` || dependenciesChanged,
dependenciesChanged,
// An identity that changed differs by structure by definition.
dependenciesStructurallyEqual: !scopeChanged && update.recompile !== `identity` && comparison.structurallyEqual
};
}
function openWindowCollection(input, requiredLimit, update) {
const prepared = input.kind === `collection` ? input.collection : { query: input.query.limit(requiredLimit).offset(0) };
let queryHash;
if (update.client) {
try {
queryHash = liveQueryOptions.getLiveQueryHash(prepared, update.queryKey);
} catch (error) {
if (!(error instanceof errors.UnhashableQueryIRError) || update.queryKey?.length)
throw error;
}
}
if (input.kind === `collection`) {
const warning = liveQueryWindowController.getLiveQueryWindowCollectionWarning(
input.collection,
update.pageSize + 1
);
if (liveQueryWindowController.liveQueryWindowMatches(input.collection, requiredLimit)) {
input.collection.startSyncImmediate();
}
return {
collection: input.collection,
queryHash,
ownsCollection: false,
warning
};
}
const collection = liveQueryCollection.createLiveQueryCollection({
query: prepared.query,
gcTime: WINDOW_COLLECTION_GC_TIME_MS
});
liveQueryWindowController.assertLiveQueryWindowManyResult(collection);
collection.startSyncImmediate();
return { collection, queryHash, ownsCollection: true, warning: void 0 };
}
exports.bindLiveQueryWindow = bindLiveQueryWindow;
exports.readLiveQueryWindowPageShape = readLiveQueryWindowPageShape;
//# sourceMappingURL=live-query-window-binding.cjs.map