@tanstack/db
Version:
A reactive client store for building super fast apps on sync
115 lines (114 loc) • 5.97 kB
JavaScript
const require_error_message = require("./error-message.cjs");
const require_errors = require("./errors.cjs");
const require_live_query_options = require("./live-query-options.cjs");
const require_live_query_primitives = require("./live-query-primitives.cjs");
const require_live_query_collection = require("./query/live-query-collection.cjs");
const require_live_query_window_controller = require("./live-query-window-controller.cjs");
var WINDOW_COLLECTION_GC_TIME_MS = 1;
function readLiveQueryWindowPageShape(config) {
if (`getNextPageParam` in config) throw new Error(/* @__PURE__ */ require_error_message.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.` : require_error_message.codedMessage(246));
return {
pageSize: require_live_query_window_controller.normalizeLiveQueryWindowPageSize(config.pageSize),
initialPageParam: config.initialPageParam ?? 0
};
}
function bindLiveQueryWindow(previous, update, uncommitted) {
const resolved = update.recompile === `always` ? require_live_query_window_controller.resolveLiveQueryWindowInput(update.input) : void 0;
const supplied = resolved === void 0 ? require_live_query_window_controller.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 = require_live_query_window_controller.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` && !require_live_query_primitives.isFailedStatus(uncommitted.collection.status) && require_live_query_window_controller.liveQueryWindowMatches(uncommitted.collection, requiredLimit)) {
collection = uncommitted.collection;
queryHash = uncommitted.queryHash;
ownsCollection = true;
} else {
const window = openWindowCollection(resolved ?? require_live_query_window_controller.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: require_live_query_window_controller.createLiveQueryWindowController(collection, {
pageSize: update.pageSize,
initialPageParam: update.initialPageParam,
initialPageCount,
client: update.client,
queryHash
}),
warning
};
}
function compareUpdate(binding, update, dependencies) {
const comparison = require_live_query_window_controller.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,
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 = require_live_query_options.getLiveQueryHash(prepared, update.queryKey);
} catch (error) {
if (!(error instanceof require_errors.UnhashableQueryIRError) || update.queryKey?.length) throw error;
}
if (input.kind === `collection`) {
const warning = require_live_query_window_controller.getLiveQueryWindowCollectionWarning(input.collection, update.pageSize + 1);
if (require_live_query_window_controller.liveQueryWindowMatches(input.collection, requiredLimit)) input.collection.startSyncImmediate();
return {
collection: input.collection,
queryHash,
ownsCollection: false,
warning
};
}
const collection = require_live_query_collection.createLiveQueryCollection({
query: prepared.query,
gcTime: WINDOW_COLLECTION_GC_TIME_MS
});
require_live_query_window_controller.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