feat: use declarative callbacks for data changes
This commit is contained in:
@@ -132,6 +132,8 @@ export function subscribeToParticleChildren(
|
||||
visibilityScopes: string[] = [],
|
||||
orderByField: string = "created_at",
|
||||
orderDirection: "asc" | "desc" = "desc",
|
||||
onAdded?: (child: Particle) => void,
|
||||
onRemoved?: (child: Particle, updatedChildren: Particle[]) => void,
|
||||
): Unsubscribe {
|
||||
let q = query(typedCollection(collectionPath), orderBy(orderByField, orderDirection));
|
||||
if (visibilityScopes.length > 0) {
|
||||
@@ -143,11 +145,15 @@ export function subscribeToParticleChildren(
|
||||
return onSnapshot(
|
||||
q,
|
||||
(snap) => {
|
||||
// onNext(snap.docChanges().map((change) => {
|
||||
// const data = change.doc.data();
|
||||
// return { type: change.type, data };
|
||||
// }));
|
||||
onData(snap.docs.map((d) => d.data()));
|
||||
const updatedChildren = snap.docs.map((d) => d.data());
|
||||
onData(updatedChildren);
|
||||
|
||||
if (onAdded || onRemoved) {
|
||||
for (const change of snap.docChanges()) {
|
||||
if (change.type === "added" && onAdded) onAdded(change.doc.data());
|
||||
if (change.type === "removed" && onRemoved) onRemoved(change.doc.data(), updatedChildren);
|
||||
}
|
||||
}
|
||||
},
|
||||
onError,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user