@tanstack/vue-db
Version:
Vue integration for @tanstack/db
117 lines (116 loc) • 4.71 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const vue = require("vue");
const db = require("@tanstack/db");
const DEFAULT_GC_TIME_MS = 1;
function useLiveInfiniteQuery(queryFnOrCollection, config, deps = []) {
let validatedCollection = null;
let previousController = null;
let previousInput = null;
let previousDependencies = null;
let previousPageSize = null;
let previousInitialPageParam = null;
const controller = vue.computed(() => {
const dependencies = deps.map((dependency) => vue.toValue(dependency));
const pageSize = db.normalizeLiveQueryWindowPageSize(config.pageSize);
const initialPageParam = config.initialPageParam ?? 0;
const unwrappedInput = typeof queryFnOrCollection === `function` ? queryFnOrCollection : vue.toValue(queryFnOrCollection);
const input = db.resolveLiveQueryWindowInput(unwrappedInput);
const dependencyComparison = db.compareLiveQueryWindowDependencies(
previousDependencies,
dependencies
);
const dependenciesChanged = dependencyComparison.changed;
const dependenciesStructurallyEqual = dependencyComparison.structurallyEqual;
const pageShapeChanged = previousPageSize !== pageSize || previousInitialPageParam !== initialPageParam;
const sameCollection = input.kind === `collection` && previousInput?.kind === `collection` && previousInput.collection === input.collection;
const canPreservePageCount = db.shouldPreserveLiveQueryWindowPageCount({
hasPreviousController: previousController !== null,
previousInputKind: previousInput?.kind,
inputKind: input.kind,
sameCollection,
dependenciesChanged,
dependenciesStructurallyEqual,
pageShapeChanged
});
const previousPageCount = previousController ? Math.max(1, previousController.getSnapshot().pages.length) : 1;
const initialPageCount = canPreservePageCount ? previousPageCount : 1;
previousInput = input;
previousDependencies = [...dependencies];
previousPageSize = pageSize;
previousInitialPageParam = initialPageParam;
if (input.kind === `collection`) {
const collection2 = input.collection;
const warning = db.getLiveQueryWindowCollectionWarning(
collection2,
pageSize + 1
);
if (validatedCollection !== collection2) {
validatedCollection = collection2;
if (warning) console.warn(warning);
}
const currentController2 = db.createLiveQueryWindowController(collection2, {
pageSize,
initialPageParam,
initialPageCount
});
previousController = currentController2;
return currentController2;
}
const collection = db.createLiveQueryCollection({
query: input.query.limit(pageSize + 1).offset(0),
startSync: false,
gcTime: DEFAULT_GC_TIME_MS
});
db.assertLiveQueryWindowManyResult(collection);
const currentController = db.createLiveQueryWindowController(collection, {
pageSize,
initialPageParam,
initialPageCount
});
previousController = currentController;
return currentController;
});
const snapshot = vue.shallowRef(controller.value.getSnapshot());
vue.watchEffect(
(onInvalidate) => {
const currentController = controller.value;
const updateSnapshot = () => {
snapshot.value = currentController.getSnapshot();
};
updateSnapshot();
const unsubscribe = currentController.subscribe(updateSnapshot);
updateSnapshot();
onInvalidate(() => {
unsubscribe();
currentController.dispose();
});
},
{ flush: `sync` }
);
return {
state: vue.computed(
() => snapshot.value.state
),
data: vue.computed(() => snapshot.value.data),
collection: vue.computed(
() => snapshot.value.collection
),
status: vue.computed(() => snapshot.value.status),
isLoading: vue.computed(() => snapshot.value.isLoading),
isReady: vue.computed(() => snapshot.value.isReady),
isIdle: vue.computed(() => snapshot.value.isIdle),
isError: vue.computed(() => snapshot.value.isError),
isCleanedUp: vue.computed(() => snapshot.value.isCleanedUp),
pages: vue.computed(
() => snapshot.value.pages
),
pageParams: vue.computed(() => snapshot.value.pageParams),
fetchNextPage: () => db.fetchNextLiveQueryWindowPage(controller.value),
hasNextPage: vue.computed(() => snapshot.value.hasNextPage),
isFetchingNextPage: vue.computed(() => snapshot.value.isFetchingNextPage),
error: vue.computed(() => snapshot.value.error)
};
}
exports.useLiveInfiniteQuery = useLiveInfiniteQuery;
//# sourceMappingURL=useLiveInfiniteQuery.cjs.map