aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers
diff options
context:
space:
mode:
authorThomas Huang <tmashuang@users.noreply.github.com>2017-12-21 02:07:34 +0800
committerGitHub <noreply@github.com>2017-12-21 02:07:34 +0800
commitd3f5ad874e4bf90cda4e440f9b0635c6ac416382 (patch)
treee9375bffd3fc868af4dce48b49732254bb2b59a0 /app/scripts/controllers
parent80b8098a27ed37e8644ef325d73e173cef033b96 (diff)
parent30b45c8a385e9ae4c6b78d0a7ec6929f7bad4d9a (diff)
downloadtangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.tar.gz
tangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.tar.zst
tangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.zip
Merge pull request #2763 from MetaMask/AddRecentBlocksController
Add Recent Blocks controller
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r--app/scripts/controllers/recent-blocks.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js
new file mode 100644
index 000000000..4a906261e
--- /dev/null
+++ b/app/scripts/controllers/recent-blocks.js
@@ -0,0 +1,44 @@
+const ObservableStore = require('obs-store')
+const extend = require('xtend')
+
+class RecentBlocksController {
+
+ constructor (opts = {}) {
+ const { blockTracker } = opts
+ this.blockTracker = blockTracker
+ this.historyLength = opts.historyLength || 40
+
+ const initState = extend({
+ recentBlocks: [],
+ }, opts.initState)
+ this.store = new ObservableStore(initState)
+
+ this.blockTracker.on('block', this.processBlock.bind(this))
+ }
+
+ resetState () {
+ this.store.updateState({
+ recentBlocks: [],
+ })
+ }
+
+ processBlock (newBlock) {
+ const block = extend(newBlock, {
+ gasPrices: newBlock.transactions.map((tx) => {
+ return tx.gasPrice
+ }),
+ })
+ delete block.transactions
+
+ const state = this.store.getState()
+ state.recentBlocks.push(block)
+
+ while (state.recentBlocks.length > this.historyLength) {
+ state.recentBlocks.shift()
+ }
+
+ this.store.updateState(state)
+ }
+}
+
+module.exports = RecentBlocksController