From bcb86f38cbe91fb814d30b3129de76dba45bff66 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 18:21:22 -0800 Subject: messageManager - move view state to obs-store --- app/scripts/lib/message-manager.js | 52 +++++++++++++++----------------------- app/scripts/metamask-controller.js | 14 +++++----- test/unit/message-manager-test.js | 17 +++---------- 3 files changed, 32 insertions(+), 51 deletions(-) diff --git a/app/scripts/lib/message-manager.js b/app/scripts/lib/message-manager.js index 18bf54ae1..38fa42017 100644 --- a/app/scripts/lib/message-manager.js +++ b/app/scripts/lib/message-manager.js @@ -6,18 +6,11 @@ const createId = require('./random-id') module.exports = class MessageManager extends EventEmitter{ constructor (opts) { super() - this.memStore = new ObservableStore({ messages: [] }) - } - - getState() { - return { - unapprovedMsgs: this.getUnapprovedMsgs(), - messages: this.getMsgList(), - } - } - - getMsgList () { - return this.memStore.getState().messages + this.memStore = new ObservableStore({ + unapprovedMsgs: {}, + unapprovedMsgCount: 0, + }) + this.messages = [] } get unapprovedMsgCount () { @@ -25,8 +18,7 @@ module.exports = class MessageManager extends EventEmitter{ } getUnapprovedMsgs () { - let messages = this.getMsgList() - return messages.filter(msg => msg.status === 'unapproved') + return this.messages.filter(msg => msg.status === 'unapproved') .reduce((result, msg) => { result[msg.id] = msg; return result }, {}) } @@ -41,10 +33,6 @@ module.exports = class MessageManager extends EventEmitter{ status: 'unapproved', } this.addMsg(msgData) - console.log('addUnapprovedMessage:', msgData) - - // keep the cb around for after approval (requires user interaction) - // This cb fires completion to the Dapp's write operation. // signal update this.emit('update') @@ -52,15 +40,12 @@ module.exports = class MessageManager extends EventEmitter{ } addMsg (msg) { - let messages = this.getMsgList() - messages.push(msg) - this._saveMsgList(messages) + this.messages.push(msg) + this._saveMsgList() } getMsg (msgId) { - let messages = this.getMsgList() - let matching = messages.filter(msg => msg.id === msgId) - return matching.length > 0 ? matching[0] : null + return this.messages.find(msg => msg.id === msgId) } approveMessage (msgParams) { @@ -85,7 +70,10 @@ module.exports = class MessageManager extends EventEmitter{ brodcastMessage (rawSig, msgId, status) { this.emit(`${msgId}:finished`, {status, rawSig}) } -// PRIVATE METHODS + + // + // PRIVATE METHODS + // _setMsgStatus (msgId, status) { let msg = this.getMsg(msgId) @@ -94,18 +82,18 @@ module.exports = class MessageManager extends EventEmitter{ } _updateMsg (msg) { - let messages = this.getMsgList() - let index = messages.findIndex((message) => message.id === msg.id) + let index = this.messages.findIndex((message) => message.id === msg.id) if (index !== -1) { - messages[index] = msg + this.messages[index] = msg } - this._saveMsgList(messages) + this._saveMsgList() } - _saveMsgList (msgList) { + _saveMsgList () { + const unapprovedMsgs = this.getUnapprovedMsgs() + const unapprovedMsgCount = Object.keys(unapprovedMsgs).length + this.memStore.updateState({ unapprovedMsgs, unapprovedMsgCount }) this.emit('updateBadge') - this.memStore.updateState({ messages: msgList }) } - } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 02dd60528..b6a096488 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -100,17 +100,19 @@ module.exports = class MetamaskController extends EventEmitter { configManager: this.configManager, }) - // manual state subscriptions - this.ethStore.on('update', this.sendUpdate.bind(this)) - this.keyringController.on('update', this.sendUpdate.bind(this)) - this.txManager.on('update', this.sendUpdate.bind(this)) - this.messageManager.on('update', this.sendUpdate.bind(this)) + // manual disk state subscriptions this.keyringController.store.subscribe((state) => { this.store.updateState({ KeyringController: state }) }) this.preferencesController.store.subscribe((state) => { this.store.updateState({ PreferencesController: state }) }) + + // manual mem state subscriptions + this.ethStore.on('update', this.sendUpdate.bind(this)) + this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) + this.txManager.on('update', this.sendUpdate.bind(this)) + this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) } // @@ -176,7 +178,7 @@ module.exports = class MetamaskController extends EventEmitter { this.state, this.ethStore.getState(), this.txManager.getState(), - this.messageManager.getState(), + this.messageManager.memStore.getState(), this.keyringController.memStore.getState(), this.preferencesController.store.getState(), this.noticeController.getState(), diff --git a/test/unit/message-manager-test.js b/test/unit/message-manager-test.js index 68b977058..faf7429d4 100644 --- a/test/unit/message-manager-test.js +++ b/test/unit/message-manager-test.js @@ -13,7 +13,7 @@ describe('Transaction Manager', function() { describe('#getMsgList', function() { it('when new should return empty array', function() { - var result = messageManager.getMsgList() + var result = messageManager.messages assert.ok(Array.isArray(result)) assert.equal(result.length, 0) }) @@ -22,20 +22,11 @@ describe('Transaction Manager', function() { }) }) - describe('#_saveMsgList', function() { - it('saves the submitted data to the Msg list', function() { - var target = [{ foo: 'bar', metamaskNetworkId: 'unit test' }] - messageManager._saveMsgList(target) - var result = messageManager.getMsgList() - assert.equal(result[0].foo, 'bar') - }) - }) - describe('#addMsg', function() { it('adds a Msg returned in getMsgList', function() { var Msg = { id: 1, status: 'approved', metamaskNetworkId: 'unit test' } messageManager.addMsg(Msg) - var result = messageManager.getMsgList() + var result = messageManager.messages assert.ok(Array.isArray(result)) assert.equal(result.length, 1) assert.equal(result[0].id, 1) @@ -47,7 +38,7 @@ describe('Transaction Manager', function() { var Msg = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test' } messageManager.addMsg(Msg) messageManager.setMsgStatusApproved(1) - var result = messageManager.getMsgList() + var result = messageManager.messages assert.ok(Array.isArray(result)) assert.equal(result.length, 1) assert.equal(result[0].status, 'approved') @@ -59,7 +50,7 @@ describe('Transaction Manager', function() { var Msg = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test' } messageManager.addMsg(Msg) messageManager.rejectMsg(1) - var result = messageManager.getMsgList() + var result = messageManager.messages assert.ok(Array.isArray(result)) assert.equal(result.length, 1) assert.equal(result[0].status, 'rejected') -- cgit v1.2.3