extensions icon indicating copy to clipboard operation
extensions copied to clipboard

feat(firestore-counter): Create distributed_counter.dart

Open Matrinica opened this issue 3 years ago • 17 comments

Translated from the TypeScript version

Matrinica avatar Feb 28 '21 23:02 Matrinica

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
Corporate signers

ℹ️ Googlers: Go here for more info.

google-cla[bot] avatar Feb 28 '21 23:02 google-cla[bot]

@googlebot I signed it!

Matrinica avatar Feb 28 '21 23:02 Matrinica

cc @rrousselGit - would you remind reviewing this one? If you need more context feel free to ping me internally

Salakar avatar Apr 27 '21 16:04 Salakar

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

jhuleatt avatar May 25 '21 16:05 jhuleatt

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!

dackers86 avatar Sep 14 '21 09:09 dackers86

@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!

polarby avatar Nov 18 '21 12:11 polarby

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");
    });

polarby avatar Nov 22 '21 17:11 polarby

Any update on this?

rlee1990 avatar Jan 18 '22 14:01 rlee1990

@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

polarby avatar Jan 18 '22 14:01 polarby

@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.

rlee1990 avatar Jan 18 '22 14:01 rlee1990

@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);
  }

rlee1990 avatar Jan 18 '22 17:01 rlee1990

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?

polarby avatar Jan 18 '22 17:01 polarby

To shardId!.substring(0, 4)).path once I added that it all was working.

rlee1990 avatar Jan 18 '22 17:01 rlee1990

@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

polarby avatar Jan 18 '22 17:01 polarby

@Matrinica Can you update and add the uuid package in the example that @polarby mentioned. Thanks!

dackers86 avatar Feb 08 '22 14:02 dackers86

Any status on this?

ymerdrengene avatar Jun 16 '22 14:06 ymerdrengene

Hi guys, would be great if we can merge this PR :)

submetu avatar Jun 16 '22 14:06 submetu

Closing in favor of #1375.

pr-Mais avatar Jan 03 '23 15:01 pr-Mais