extensions
extensions copied to clipboard
feat(firestore-counter): Create distributed_counter.dart
Translated from the TypeScript version
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).
:memo: Please visit https://cla.developers.google.com/ to sign.
Once you've signed (or fixed any issues), please reply here with @googlebot I signed it!
and we'll verify it.
What to do if you already signed the CLA
Individual signers
- It's possible we don't have your GitHub username or you're using a different email address on your commit. Check your existing CLA data and verify that your email is set on your git commits.
Corporate signers
- Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to go/cla#troubleshoot (Public version).
- The email used to register you as an authorized contributor must be the email used for the Git commit. Check your existing CLA data and verify that your email is set on your git commits.
- The email used to register you as an authorized contributor must also be attached to your GitHub account.
ℹ️ Googlers: Go here for more info.
@googlebot I signed it!
cc @rrousselGit - would you remind reviewing this one? If you need more context feel free to ping me internally
Thanks for your contribution @Matrinica! Do you have time to update your code to address @rrousselGit's comments? Once those suggestions are addressed, we can merge this PR
Hi @Matrinica.
This PR has gone a little stale. Are you able to address the code suggestions above, we can then hopefully get this reviewed ahead of a release. Thanks!
@Matrinica Thanks for contributing, I was looking for that the whole time. It would be great if you could finish it for release, so people might be able to use it?! I am looking forward to it!
Here is how to use the package, which could be displayed at the firebase How this extension works
under the Extension:
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore/FirestoreCounter.dart'; // *this would be needed to be implemented*
// somewhere in your app code initialize Firestore instance
final db = FirebaseFirestore.instance;
// create reference to the collection and the document you wish to use
DocumentReference doc = db.collection("pages").doc("hello-world");
// initialize FirestoreShardedCounter with the document and the property which will hold the counter value
DistributedCounter visits = new DistributedCounter(doc, "visits");
// to increment counter
await visits.incrementBy(1);
// make one time call to query total amount of visits
int totalVisits = await visits.get();
print("totalVisits: $totalVisits");
// listen for updates
StreamSubscription<int> registration =
visits.onSnapshot().listen((totalVisits) {
print("registration listen totalVisits: $totalVisits");
});
// clean up event listeners once finished
registration.cancel();
// if you don't mind counter delays, you can listen to the document directly.
db.doc("pages/hello-world").snapshots().listen((querySnapshot) {
// total page visits
int totalVisits = querySnapshot.get("visits");
print("onSnapshot totalVisits: $totalVisits");
});
Any update on this?
@rlee1990 Quickfix (as this might need some time): Simply implement it yourself. This is just a library that would you would have access to when merged. All the code including the documentation is in this issue. Take the latest edits and use the package like this
@rlee1990 Quickfix (as this might need some time): Simply implement it yourself. This is just a library that would you would have access to when merged. All the code including the documentation is in this issue. Take the latest edits and use the package like this
Thanks will give it a try.
@PaulLVoelker I keep getting this error:
Unhandled Exception: Null check operator used on a null value
Looks like This should be added:
import 'package:uuid/uuid.dart';
var uuid = Uuid();
factory DistributedCounter(DocumentReference<Object?> doc, String field) {
//Add init(uuid.v4());
init(uuid.v4());
final shardsRef = doc.collection(SHARD_COLLECTION_ID);
Map<String, int> shards = {};
shards[doc.path] = 0;
shards[shardsRef.doc(shardId).path] = 0;
shards[shardsRef.doc('\t' + shardId!.substring(0, 4)).path] = 0;
shards[shardsRef.doc('\t\t' + shardId!.substring(0, 3)).path] = 0;
shards[shardsRef.doc('\t\t\t' + shardId!.substring(0, 2)).path] = 0;
shards[shardsRef.doc('\t\t\t\t' + shardId!.substring(0, 1)).path] = 0;
Stream<int> snapshots = StreamGroup.mergeBroadcast<int>(shards.keys.map(
(path) => doc.firestore
.doc(path)
.snapshots()
.map<int>((DocumentSnapshot snap) {
shards[snap.reference.path] = snap.exists ? snap.get(field) : 0;
return shards.values.reduce((a, b) => a + b);
})));
return DistributedCounter._(doc, field, snapshots);
}
Absolutely! Unfortunately, you need to install the package. I mentioned something like that here.
Is the error referring to the import? Or to the shardId!.substring(0, 4)).path
?
To shardId!.substring(0, 4)).path
once I added that it all was working.
@rlee1990 Great! Yea that's something the guys from firebase either need to import or find another solution. But for now, you have to use this package, as you somehow need a unique id. BTW: you could you use something with time
@Matrinica Can you update and add the uuid
package in the example that @polarby mentioned. Thanks!
Any status on this?
Hi guys, would be great if we can merge this PR :)
Closing in favor of #1375.