@tanstack/vue-db
Version:
Vue integration for @tanstack/db
117 lines (116 loc) • 4.83 kB
JavaScript
import { computed, toValue, shallowRef, watchEffect } from "vue";
import { normalizeLiveQueryWindowPageSize, resolveLiveQueryWindowInput, compareLiveQueryWindowDependencies, shouldPreserveLiveQueryWindowPageCount, getLiveQueryWindowCollectionWarning, createLiveQueryWindowController, createLiveQueryCollection, assertLiveQueryWindowManyResult, fetchNextLiveQueryWindowPage } from "@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 = computed(() => {
const dependencies = deps.map((dependency) => toValue(dependency));
const pageSize = normalizeLiveQueryWindowPageSize(config.pageSize);
const initialPageParam = config.initialPageParam ?? 0;
const unwrappedInput = typeof queryFnOrCollection === `function` ? queryFnOrCollection : toValue(queryFnOrCollection);
const input = resolveLiveQueryWindowInput(unwrappedInput);
const dependencyComparison = 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 = 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 = getLiveQueryWindowCollectionWarning(
collection2,
pageSize + 1
);
if (validatedCollection !== collection2) {
validatedCollection = collection2;
if (warning) console.warn(warning);
}
const currentController2 = createLiveQueryWindowController(collection2, {
pageSize,
initialPageParam,
initialPageCount
});
previousController = currentController2;
return currentController2;
}
const collection = createLiveQueryCollection({
query: input.query.limit(pageSize + 1).offset(0),
startSync: false,
gcTime: DEFAULT_GC_TIME_MS
});
assertLiveQueryWindowManyResult(collection);
const currentController = createLiveQueryWindowController(collection, {
pageSize,
initialPageParam,
initialPageCount
});
previousController = currentController;
return currentController;
});
const snapshot = shallowRef(controller.value.getSnapshot());
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: computed(
() => snapshot.value.state
),
data: computed(() => snapshot.value.data),
collection: computed(
() => snapshot.value.collection
),
status: computed(() => snapshot.value.status),
isLoading: computed(() => snapshot.value.isLoading),
isReady: computed(() => snapshot.value.isReady),
isIdle: computed(() => snapshot.value.isIdle),
isError: computed(() => snapshot.value.isError),
isCleanedUp: computed(() => snapshot.value.isCleanedUp),
pages: computed(
() => snapshot.value.pages
),
pageParams: computed(() => snapshot.value.pageParams),
fetchNextPage: () => fetchNextLiveQueryWindowPage(controller.value),
hasNextPage: computed(() => snapshot.value.hasNextPage),
isFetchingNextPage: computed(() => snapshot.value.isFetchingNextPage),
error: computed(() => snapshot.value.error)
};
}
export {
useLiveInfiniteQuery
};
//# sourceMappingURL=useLiveInfiniteQuery.js.map