Skip to content

Commit 5b31834

Browse files
committed
Split the child counting into two functions:
1. tracks add/remove of child nodes and increments/decrements the counter 2. one function is triggered when the counter is deleted and it recounts the child nodes The delta counter in #1 is more efficient than counting all children every time, since it only needs access to the delta. The recounter function in #2 is equally inefficient as before, but is needed a lot less.
1 parent 8c582a3 commit 5b31834

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

child-count/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ This template shows how to keep track of the number of elements in a Firebase Da
66

77
See file [functions/index.js](functions/index.js) for the code.
88

9-
This is done by simply updating a `likes_count` attribute on the parent of the list node which is tracked.
9+
This is done by updating a `likes_count` property on the parent of the list node which is tracked.
10+
11+
This counting is done in two cases:
12+
13+
1. When a like is added or deleted, the `likes_count` is incremented or decremented.
14+
2. When the `likes_count` is deleted, all likes are recounted.
1015

1116
The dependencies are listed in [functions/package.json](functions/package.json).
1217

child-count/functions/index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,28 @@ const functions = require('firebase-functions');
1919
const admin = require('firebase-admin');
2020
admin.initializeApp(functions.config().firebase);
2121

22-
// Keeps track of the length of the 'likes' child list in a separate attribute.
23-
exports.countlikes = functions.database.ref('/posts/{postid}/likes').onWrite(event => {
24-
return event.data.ref.parent.child('likes_count').set(event.data.numChildren());
22+
// Keeps track of the length of the 'likes' child list in a separate property.
23+
exports.countlikechange = functions.database.ref("/posts/{postid}/likes/{likeid}").onWrite((event) => {
24+
var collectionRef = event.data.ref.parent;
25+
var countRef = collectionRef.parent.child('likes_count');
26+
27+
return countRef.transaction(function(current) {
28+
if (event.data.exists() && !event.data.previous.exists()) {
29+
return (current || 0) + 1;
30+
}
31+
else if (!event.data.exists() && event.data.previous.exists()) {
32+
return (current || 0) - 1;
33+
}
34+
});
2535
});
36+
37+
// If the number of likes gets deleted, recount the number of likes
38+
exports.recountlikes = functions.database.ref("/posts/{postid}/likes_count").onWrite((event) => {
39+
if (!event.data.exists()) {
40+
var counterRef = event.data.ref;
41+
var collectionRef = counterRef.parent.child('likes');
42+
return collectionRef.once('value', function(messagesData) {
43+
return counterRef.set(messagesData.numChildren());
44+
});
45+
}
46+
});

0 commit comments

Comments
 (0)