diff options
| author | Haishan <[email protected]> | 2018-10-30 22:29:36 +0800 |
|---|---|---|
| committer | Haishan <[email protected]> | 2018-10-30 22:29:36 +0800 |
| commit | d247e37890de06b17da34646c0fe227a9b6ef2a6 (patch) | |
| tree | 4913f0d1ca0b7783e5d23843da232b7da39ab4d6 /src/api/logs.js | |
| parent | 59cee786211114e6393f6218a040e2032d0000c7 (diff) | |
refactor: use react hooks
Diffstat (limited to 'src/api/logs.js')
| -rw-r--r-- | src/api/logs.js | 14 |
1 files changed, 11 insertions, 3 deletions
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); + }; } }; |
