From be5b1bdb86683a47ff094571fb18562cd6c98fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Tue, 16 Oct 2018 14:01:00 -0400 Subject: [PATCH] fix initialization of counter --- src/counter.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/counter.js b/src/counter.js index 436dc83..8cacd3b 100644 --- a/src/counter.js +++ b/src/counter.js @@ -1,6 +1,8 @@ class Counter { - constructor(redis) { + constructor(redis, name) { this.redis = redis + /* assume the key is called "clicks" */ + this.name = name == undefined ? "clicks" : name /* make sure we don't miss errors */ this.redis.on("error", (err) => { console.log("Error: " + err) }) } @@ -9,11 +11,13 @@ class Counter { /* default to incrementing by one */ val = val == undefined ? 1 : val /* increment */ - this.redis.incr('default') + this.redis.incr(this.name) } async state () { - return await this.redis.get('default') + let count = await this.redis.get(this.name) + /* keys in redis are set to null by default */ + return count == null ? 0 : count } }