Skip to content

Commit

Permalink
Optimize Firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Nov 22, 2020
1 parent 2767b08 commit 2dc4983
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions layout/_third-party/statistics/firestore.njk
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,24 @@
function getCount(doc, increaseCount) {
// IncreaseCount will be false when not in article page
return doc.get().then(d => {
let count = 0;
if (!d.exists) { // Has no data, initialize count
if (increaseCount) {
doc.set({
count: 1
});
count = 1;
}
} else { // Has data
count = d.data().count;
if (increaseCount) {
// If first view this article
doc.set({ // Increase count
count: count + 1
});
count++;
}
// Has no data, initialize count
let count = d.exists ? d.data().count : 0;
// If first view this article
if (increaseCount) {
// Increase count
count++;
doc.set({
count
});
}
return count;
});
}
function appendCountTo(el) {
return count => {
el.innerText = count;
}
};
}
</script>
<script{{ pjax }}>
Expand Down

0 comments on commit 2dc4983

Please sign in to comment.