Stream does not get updates #237
Replies: 1 comment 2 replies
-
For an unknown reason, (at least based on my knowledge), this one works fine, both writing changes on Firestore and also getting change streams. extension SideEffectRegistrarExtensions on SideEffectRegistrar {
CapsuleHandle get use => this as CapsuleHandle;
DocumentReference<Json> document({
required String path,
required String id,
}) {
final firebaseFirestore = use(firebaseFirestoreCapsule);
return use.memo(() {
return firebaseFirestore.collection(path).doc(id);
}, [path, id]);
}
(Option<T>, AsyncSetCallback<T>) readUpdate<T>({
required DocumentReference<Json> document,
required String fieldName,
}) {
final getterStream = use.memo(() {
return document.snapshots().map((snapshot) {
if (snapshot.exists) {
if (snapshot.data()?.containsKey(fieldName) == true) {
return Maybe.of(snapshot.get(fieldName) as T?);
}
}
return None<T>();
});
}, [document, fieldName]);
final getterState = use.stream(getterStream);
final setterState = use.memo(() {
return (T newValue) async {
await document.set(
{fieldName: newValue},
SetOptions(merge: true),
);
};
}, [document, fieldName]);
return (getterState.data.unwrapOr(None<T>()), setterState);
}
} DocumentReference<Json> _profileDocumentCapsule(
CapsuleHandle use,
) {
final userId = use(userIdCapsule);
if (userId case Some(:final value)) {
return use.document(
path: "profiles",
id: value.value,
);
}
throw StateError("No user id found.");
}
(
Option<Nickname>,
AsyncSetCallback<Nickname>,
) _nicknameCapsule(
CapsuleHandle use,
) {
final profileDocument = use(_profileDocumentCapsule);
final (value, setValue) = use.readUpdate<String>(
document: profileDocument,
fieldName: 'nickname',
);
return (
Nickname.fromString(value),
(Nickname newNickname) => setValue(newNickname.value),
);
}
(
Option<AgeBracket>,
AsyncSetCallback<AgeBracket>,
) _ageBracketCapsule(
CapsuleHandle use,
) {
final profileDocument = use(_profileDocumentCapsule);
final (value, setValue) = use.readUpdate<String>(
document: profileDocument,
fieldName: 'ageBracket',
);
return (
AgeBracket.fromString(value),
(AgeBracket newAgeBracket) async {
await setValue(newAgeBracket.value);
},
);
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I can't get the following stream working. The update command works fine and I can see the changes in the Firestore console, but read is only happening in the first run, and the streaming values does not happen.
Consider the following custom side effect.
The
document
should return a reference to a Firestore DocumentReference at a specific path (e.g. /profiles/userid1). Then I will pass that reference to the next one, readUpdate, plus a field name, so I can get/set a specific field of the document.Can you help me with this, please?
Beta Was this translation helpful? Give feedback.
All reactions