Open
Description
The loading slot is triggered when the $store == undefined , But as the writable store is of type T[] , it defaults to [] even when we explicitly pass undefined, so the loading state never gets triggered to over come this we can add type to writable to make it accept undefined value as its initial state, Here is the updated code
export function collectionStore<T>(
firestore: Firestore,
ref: string | Query | CollectionReference,
startWith: T[]|undefined = undefined
) {
let unsubscribe: () => void;
// Fallback for SSR
if (!firestore || !globalThis.window) {
console.warn('Firestore is not initialized or not in browser');
const { subscribe } = writable(startWith);
return {
subscribe,
ref: null,
}
}
const colRef = typeof ref === 'string' ? collection(firestore, ref) : ref;
const { subscribe } = writable<T[] | undefined>(startWith, (set) => {
unsubscribe = onSnapshot(colRef, (snapshot) => {
const data = snapshot.docs.map((s) => {
return { id: s.id, ref: s.ref, ...s.data() } as T;
});
set(data);
});
return () => unsubscribe();
});
return {
subscribe,
ref: colRef,
};
}
Metadata
Metadata
Assignees
Labels
No labels