feat: use declarative callbacks for data changes

This commit is contained in:
talksik
2026-03-21 15:31:05 -07:00
parent e96f5cbe8a
commit 3eea235ab1
3 changed files with 53 additions and 64 deletions
+11 -5
View File
@@ -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,
);