Optimize Firebase

This commit is contained in:
Mimi 2020-11-22 12:15:09 +08:00
parent 2767b08d67
commit 2dc4983a3a

View File

@ -10,25 +10,16 @@
function getCount(doc, increaseCount) { function getCount(doc, increaseCount) {
// IncreaseCount will be false when not in article page // IncreaseCount will be false when not in article page
return doc.get().then(d => { return doc.get().then(d => {
let count = 0; // Has no data, initialize count
if (!d.exists) { // Has no data, initialize count let count = d.exists ? d.data().count : 0;
if (increaseCount) { // If first view this article
doc.set({ if (increaseCount) {
count: 1 // Increase count
}); count++;
count = 1; doc.set({
} count
} else { // Has data });
count = d.data().count;
if (increaseCount) {
// If first view this article
doc.set({ // Increase count
count: count + 1
});
count++;
}
} }
return count; return count;
}); });
} }
@ -36,7 +27,7 @@
function appendCountTo(el) { function appendCountTo(el) {
return count => { return count => {
el.innerText = count; el.innerText = count;
} };
} }
</script> </script>
<script{{ pjax }}> <script{{ pjax }}>