-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Refactor Dart multi_counter sample and provide expert code review #1278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
33dff41
3faab1e
0f346ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,7 +9,6 @@ class StorageController { | |||||||||||||||||
| Future<void> increment(String userId) async { | ||||||||||||||||||
| try { | ||||||||||||||||||
| await _increment(userId); | ||||||||||||||||||
| await _updateGlobalCount(); | ||||||||||||||||||
| } catch (e, stack) { | ||||||||||||||||||
| print('Error incrementing counter for user: $userId'); | ||||||||||||||||||
| print(e); | ||||||||||||||||||
|
|
@@ -20,55 +19,38 @@ class StorageController { | |||||||||||||||||
|
|
||||||||||||||||||
| Future<void> _increment(String userId) async { | ||||||||||||||||||
| await _firestore.runTransaction<void>((transaction) async { | ||||||||||||||||||
| final ref = _firestore.collection(usersCollection).doc(userId); | ||||||||||||||||||
| final userRef = _firestore.collection(usersCollection).doc(userId); | ||||||||||||||||||
| final globalRef = _firestore | ||||||||||||||||||
| .collection(globalCollection) | ||||||||||||||||||
| .doc(varsDocument); | ||||||||||||||||||
|
|
||||||||||||||||||
| final snapshot = await transaction.get(ref); | ||||||||||||||||||
| final snapshot = await transaction.get(userRef); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (!snapshot.exists) { | ||||||||||||||||||
| // Document doesn't exist, create it with count = 1 | ||||||||||||||||||
| transaction.set(ref, _saveCount(1)); | ||||||||||||||||||
| transaction.set(userRef, _saveCount(1)); | ||||||||||||||||||
| transaction.update(globalRef, { | ||||||||||||||||||
| totalCountField: const FieldValue.increment(1), | ||||||||||||||||||
| totalUsersField: const FieldValue.increment(1), | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+32
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two issues in this block:
Suggested change
|
||||||||||||||||||
| } else { | ||||||||||||||||||
| final data = snapshot.data(); | ||||||||||||||||||
| if (data != null && data.containsKey(countField)) { | ||||||||||||||||||
| // Field exists, increment it | ||||||||||||||||||
| transaction.update(ref, {countField: const FieldValue.increment(1)}); | ||||||||||||||||||
| transaction.update(userRef, { | ||||||||||||||||||
| countField: const FieldValue.increment(1), | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+40
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
| } else { | ||||||||||||||||||
| // Field doesn't exist, initialize it to 1 | ||||||||||||||||||
| transaction.update(ref, _saveCount(1)); | ||||||||||||||||||
| transaction.update(userRef, _saveCount(1)); | ||||||||||||||||||
| } | ||||||||||||||||||
| transaction.update(globalRef, { | ||||||||||||||||||
| totalCountField: const FieldValue.increment(1), | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+47
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
| } | ||||||||||||||||||
| }); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| Future<void> _updateGlobalCount() async { | ||||||||||||||||||
| final globalCountSnapshot = await _firestore | ||||||||||||||||||
| .collection(usersCollection) | ||||||||||||||||||
| .aggregate(const sum(countField), const count()) | ||||||||||||||||||
| .get(); | ||||||||||||||||||
|
|
||||||||||||||||||
| var globalCountRaw = globalCountSnapshot.getSum(countField); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (globalCountRaw == null || globalCountRaw < 1) { | ||||||||||||||||||
| // TODO: we don't want to crash here, but we should log | ||||||||||||||||||
| print('Very weird value for global count: "$globalCountRaw'); | ||||||||||||||||||
| globalCountRaw = 1; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| final globalCountValue = globalCountRaw.toInt(); | ||||||||||||||||||
| final userCountValue = globalCountSnapshot.count; | ||||||||||||||||||
|
|
||||||||||||||||||
| final globalVars = _firestore | ||||||||||||||||||
| .collection(globalCollection) | ||||||||||||||||||
| .doc(varsDocument); | ||||||||||||||||||
|
|
||||||||||||||||||
| // TODO: Investigate a more efficient way to do this | ||||||||||||||||||
| // Maybe with a trigger? | ||||||||||||||||||
| await globalVars.set({ | ||||||||||||||||||
| totalCountField: globalCountValue, | ||||||||||||||||||
| totalUsersField: userCountValue, | ||||||||||||||||||
| }); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| Future<void> close() async { | ||||||||||||||||||
| await _firestore.terminate(); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
matcherpackage has been downgraded from0.12.19to0.12.18. This appears to be an unintentional regression, likely caused by an environment mismatch during the dependency resolution. It is recommended to revert this to stay on the latest stable version.