aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/message-manager.js
diff options
context:
space:
mode:
authorFrankie <frankie.diamond@gmail.com>2017-01-28 08:11:59 +0800
committerFrankie <frankie.diamond@gmail.com>2017-01-29 12:28:20 +0800
commit8be68575bbef1dcc89b51355abaee90dbf018387 (patch)
treec8da5e217b838bff46bcc9ddfb8300133022ca91 /app/scripts/lib/message-manager.js
parenta8ed780d9b3659474c59c7856ab2ee1430c17b42 (diff)
downloadtangerine-wallet-browser-8be68575bbef1dcc89b51355abaee90dbf018387.tar
tangerine-wallet-browser-8be68575bbef1dcc89b51355abaee90dbf018387.tar.gz
tangerine-wallet-browser-8be68575bbef1dcc89b51355abaee90dbf018387.tar.bz2
tangerine-wallet-browser-8be68575bbef1dcc89b51355abaee90dbf018387.tar.lz
tangerine-wallet-browser-8be68575bbef1dcc89b51355abaee90dbf018387.tar.xz
tangerine-wallet-browser-8be68575bbef1dcc89b51355abaee90dbf018387.tar.zst
tangerine-wallet-browser-8be68575bbef1dcc89b51355abaee90dbf018387.zip
Clean up message manger includes:
Provider egine bump Remove presence of message manger in keyring controller Change the status wording fom conf to approved make Message manager a class fix messages not being apart of the badge re write message manger to better reflect controller pattern
Diffstat (limited to 'app/scripts/lib/message-manager.js')
-rw-r--r--app/scripts/lib/message-manager.js77
1 files changed, 67 insertions, 10 deletions
diff --git a/app/scripts/lib/message-manager.js b/app/scripts/lib/message-manager.js
index 379f38917..bc9a9e6c8 100644
--- a/app/scripts/lib/message-manager.js
+++ b/app/scripts/lib/message-manager.js
@@ -1,23 +1,61 @@
const EventEmitter = require('events')
+const ObservableStore = require('obs-store')
+const createId = require('./random-id')
+
module.exports = class MessageManager extends EventEmitter{
constructor (opts) {
super()
- this.messages = []
+ this.memStore = new ObservableStore({ messages: [] })
+ }
+
+ getState() {
+ return {
+ unapprovedMsgs: this.unapprovedMsgs(),
+ messages: this.getMsgList(),
+ }
}
getMsgList () {
- return this.messages
+ return this.memStore.getState().messages
}
- unconfirmedMsgs () {
+ get unapprovedMsgCount () {
+ return Object.keys(this.unapprovedMsgs()).length
+ }
+
+ unapprovedMsgs () {
let messages = this.getMsgList()
- return messages.filter(msg => msg.status === 'unconfirmed')
+ return messages.filter(msg => msg.status === 'unapproved')
.reduce((result, msg) => { result[msg.id] = msg; return result }, {})
}
_saveMsgList (msgList) {
- this.messages = msgList
+ this.emit('updateBadge')
+ let state = this.memStore.getState()
+ state.messages = msgList
+ this.memStore.putState(state)
+ }
+
+ addUnapprovedMessage (msgParams) {
+ // create txData obj with parameters and meta data
+ var time = (new Date()).getTime()
+ var msgId = createId()
+ var msgData = {
+ id: msgId,
+ msgParams: msgParams,
+ time: time,
+ 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')
+ return msgId
}
addMsg (msg) {
@@ -32,8 +70,28 @@ module.exports = class MessageManager extends EventEmitter{
return matching.length > 0 ? matching[0] : null
}
- confirmMsg (msgId) {
- this._setMsgStatus(msgId, 'confirmed')
+ brodcastMessage (rawSig, msgId, status) {
+ this.emit(`${msgId}:finished`, {status, rawSig})
+ }
+
+ approveMessage (msgParams) {
+ this.setMessageApproved(msgParams.metamaskId)
+ return this.prepMsgForSigning(msgParams)
+ }
+
+ setMessageApproved (msgId) {
+ this._setMsgStatus(msgId, 'approved')
+ }
+ prepMsgForSigning (msgParams) {
+ delete msgParams.metamaskId
+ return Promise.resolve(msgParams)
+ }
+
+ cancelMessage (msgId) {
+ // reject tx
+ // clean up
+ this.brodcastMessage(null, msgId, 'rejected')
+ this.rejectMsg(msgId)
}
rejectMsg (msgId) {
@@ -43,14 +101,13 @@ module.exports = class MessageManager extends EventEmitter{
_setMsgStatus (msgId, status) {
let msg = this.getMsg(msgId)
if (msg) msg.status = status
- this.updateMsg(msg)
+ this._updateMsg(msg)
}
- updateMsg (msg) {
+ _updateMsg (msg) {
let messages = this.getMsgList()
let index = messages.findIndex((message) => message.id === msg.id)
if (index !== -1) {
- this.emit('update', msg.id)
messages[index] = msg
}
this._saveMsgList(messages)