From 2dc4983a3a74d67239281c7686be03f0173a5a6c Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sun, 22 Nov 2020 12:15:09 +0800 Subject: [PATCH] Optimize Firebase --- layout/_third-party/statistics/firestore.njk | 29 +++++++------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/layout/_third-party/statistics/firestore.njk b/layout/_third-party/statistics/firestore.njk index e80fa0e..0c18d7c 100644 --- a/layout/_third-party/statistics/firestore.njk +++ b/layout/_third-party/statistics/firestore.njk @@ -10,25 +10,16 @@ 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; }); } @@ -36,7 +27,7 @@ function appendCountTo(el) { return count => { el.innerText = count; - } + }; }