messageManager - move view state to obs-store

feature/default_network_editable
kumavis 8 years ago
parent 79ed2e902f
commit bcb86f38cb
  1. 50
      app/scripts/lib/message-manager.js
  2. 14
      app/scripts/metamask-controller.js
  3. 17
      test/unit/message-manager-test.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
//
_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 })
}
}

@ -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(),

@ -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')

Loading…
Cancel
Save