From d247e37890de06b17da34646c0fe227a9b6ef2a6 Mon Sep 17 00:00:00 2001 From: Haishan Date: Tue, 30 Oct 2018 22:29:36 +0800 Subject: refactor: use react hooks --- src/api/logs.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/api') diff --git a/src/api/logs.js b/src/api/logs.js index 779a5b4..a9d358b 100644 --- a/src/api/logs.js +++ b/src/api/logs.js @@ -26,7 +26,7 @@ let even = false; const store = { logs: [], size: Size, - updateCallback: null, + subscribers: [], appendData(o) { const now = new Date(); const time = now.toLocaleString('zh-Hans'); @@ -36,8 +36,16 @@ const store = { o.even = even = !even; this.logs.unshift(o); if (this.logs.length > this.size) this.logs.pop(); - // TODO consider throttle this - if (this.updateCallback) this.updateCallback(); + this.subscribers.forEach(f => f(o)); + }, + + subscribe(listener) { + const me = this; + this.subscribers.push(listener); + return function unsubscribe() { + const idx = me.subscribers.indexOf(listener); + me.subscribers.splice(idx, 1); + }; } }; -- cgit v1.3.1