1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

messageManager - move view state to obs-store

This commit is contained in:
kumavis 2017-02-02 18:21:22 -08:00
parent 79ed2e902f
commit bcb86f38cb
3 changed files with 32 additions and 51 deletions

View File

@ -6,18 +6,11 @@ const createId = require('./random-id')
module.exports = class MessageManager extends EventEmitter{ module.exports = class MessageManager extends EventEmitter{
constructor (opts) { constructor (opts) {
super() super()
this.memStore = new ObservableStore({ messages: [] }) this.memStore = new ObservableStore({
} unapprovedMsgs: {},
unapprovedMsgCount: 0,
getState() { })
return { this.messages = []
unapprovedMsgs: this.getUnapprovedMsgs(),
messages: this.getMsgList(),
}
}
getMsgList () {
return this.memStore.getState().messages
} }
get unapprovedMsgCount () { get unapprovedMsgCount () {
@ -25,8 +18,7 @@ module.exports = class MessageManager extends EventEmitter{
} }
getUnapprovedMsgs () { getUnapprovedMsgs () {
let messages = this.getMsgList() return this.messages.filter(msg => msg.status === 'unapproved')
return messages.filter(msg => msg.status === 'unapproved')
.reduce((result, msg) => { result[msg.id] = msg; return result }, {}) .reduce((result, msg) => { result[msg.id] = msg; return result }, {})
} }
@ -41,10 +33,6 @@ module.exports = class MessageManager extends EventEmitter{
status: 'unapproved', status: 'unapproved',
} }
this.addMsg(msgData) 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 // signal update
this.emit('update') this.emit('update')
@ -52,15 +40,12 @@ module.exports = class MessageManager extends EventEmitter{
} }
addMsg (msg) { addMsg (msg) {
let messages = this.getMsgList() this.messages.push(msg)
messages.push(msg) this._saveMsgList()
this._saveMsgList(messages)
} }
getMsg (msgId) { getMsg (msgId) {
let messages = this.getMsgList() return this.messages.find(msg => msg.id === msgId)
let matching = messages.filter(msg => msg.id === msgId)
return matching.length > 0 ? matching[0] : null
} }
approveMessage (msgParams) { approveMessage (msgParams) {
@ -85,7 +70,10 @@ module.exports = class MessageManager extends EventEmitter{
brodcastMessage (rawSig, msgId, status) { brodcastMessage (rawSig, msgId, status) {
this.emit(`${msgId}:finished`, {status, rawSig}) this.emit(`${msgId}:finished`, {status, rawSig})
} }
// PRIVATE METHODS
//
// PRIVATE METHODS
//
_setMsgStatus (msgId, status) { _setMsgStatus (msgId, status) {
let msg = this.getMsg(msgId) let msg = this.getMsg(msgId)
@ -94,18 +82,18 @@ module.exports = class MessageManager extends EventEmitter{
} }
_updateMsg (msg) { _updateMsg (msg) {
let messages = this.getMsgList() let index = this.messages.findIndex((message) => message.id === msg.id)
let index = messages.findIndex((message) => message.id === msg.id)
if (index !== -1) { 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.emit('updateBadge')
this.memStore.updateState({ messages: msgList })
} }
} }

View File

@ -100,17 +100,19 @@ module.exports = class MetamaskController extends EventEmitter {
configManager: this.configManager, configManager: this.configManager,
}) })
// manual state subscriptions // manual disk 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))
this.keyringController.store.subscribe((state) => { this.keyringController.store.subscribe((state) => {
this.store.updateState({ KeyringController: state }) this.store.updateState({ KeyringController: state })
}) })
this.preferencesController.store.subscribe((state) => { this.preferencesController.store.subscribe((state) => {
this.store.updateState({ PreferencesController: 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.state,
this.ethStore.getState(), this.ethStore.getState(),
this.txManager.getState(), this.txManager.getState(),
this.messageManager.getState(), this.messageManager.memStore.getState(),
this.keyringController.memStore.getState(), this.keyringController.memStore.getState(),
this.preferencesController.store.getState(), this.preferencesController.store.getState(),
this.noticeController.getState(), this.noticeController.getState(),

View File

@ -13,7 +13,7 @@ describe('Transaction Manager', function() {
describe('#getMsgList', function() { describe('#getMsgList', function() {
it('when new should return empty array', function() { it('when new should return empty array', function() {
var result = messageManager.getMsgList() var result = messageManager.messages
assert.ok(Array.isArray(result)) assert.ok(Array.isArray(result))
assert.equal(result.length, 0) 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() { describe('#addMsg', function() {
it('adds a Msg returned in getMsgList', function() { it('adds a Msg returned in getMsgList', function() {
var Msg = { id: 1, status: 'approved', metamaskNetworkId: 'unit test' } var Msg = { id: 1, status: 'approved', metamaskNetworkId: 'unit test' }
messageManager.addMsg(Msg) messageManager.addMsg(Msg)
var result = messageManager.getMsgList() var result = messageManager.messages
assert.ok(Array.isArray(result)) assert.ok(Array.isArray(result))
assert.equal(result.length, 1) assert.equal(result.length, 1)
assert.equal(result[0].id, 1) assert.equal(result[0].id, 1)
@ -47,7 +38,7 @@ describe('Transaction Manager', function() {
var Msg = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test' } var Msg = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test' }
messageManager.addMsg(Msg) messageManager.addMsg(Msg)
messageManager.setMsgStatusApproved(1) messageManager.setMsgStatusApproved(1)
var result = messageManager.getMsgList() var result = messageManager.messages
assert.ok(Array.isArray(result)) assert.ok(Array.isArray(result))
assert.equal(result.length, 1) assert.equal(result.length, 1)
assert.equal(result[0].status, 'approved') assert.equal(result[0].status, 'approved')
@ -59,7 +50,7 @@ describe('Transaction Manager', function() {
var Msg = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test' } var Msg = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test' }
messageManager.addMsg(Msg) messageManager.addMsg(Msg)
messageManager.rejectMsg(1) messageManager.rejectMsg(1)
var result = messageManager.getMsgList() var result = messageManager.messages
assert.ok(Array.isArray(result)) assert.ok(Array.isArray(result))
assert.equal(result.length, 1) assert.equal(result.length, 1)
assert.equal(result[0].status, 'rejected') assert.equal(result[0].status, 'rejected')