diff --git a/es/utils/aepp-wallet-communication/helpers.js b/es/utils/aepp-wallet-communication/helpers.js index f1448e0cd1..2694562ab7 100644 --- a/es/utils/aepp-wallet-communication/helpers.js +++ b/es/utils/aepp-wallet-communication/helpers.js @@ -19,31 +19,33 @@ export const getWindow = () => { /** * RPC helper functions */ -export const sendMessage = (messageId, connection) => ({ id, method, params, result, error }, isNotificationOrResponse = false) => { - // Increment id for each request - isNotificationOrResponse || (messageId += 1) - id = isNotificationOrResponse ? (id || null) : messageId - const msgData = params - ? { params } - : result - ? { result } - : { error } - connection.sendMessage({ - jsonrpc: '2.0', - ...id ? { id } : {}, - method, - ...msgData - }) - return id +export const sendMessage = (connection) => { + let messageId = 0 + + return ({ id, method, params, result, error }, isNotificationOrResponse = false) => { + // Increment id for each request + isNotificationOrResponse || (messageId += 1) + id = isNotificationOrResponse ? (id || null) : messageId + const msgData = params + ? { params } + : result + ? { result } + : { error } + connection.sendMessage({ + jsonrpc: '2.0', + ...id ? { id } : {}, + method, + ...msgData + }) + return id + } } -export const receive = (handler, msgId) => (msg) => { +export const receive = (handler) => (msg) => { if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) { console.warn('Receive invalid message', msg) return } - // Increment id for each request - if (msg.id && +msg.id > msgId) msgId += 1 handler(msg) } diff --git a/es/utils/aepp-wallet-communication/rpc/rpc-clients.js b/es/utils/aepp-wallet-communication/rpc/rpc-clients.js index 3e89ed296c..c11e3760cd 100644 --- a/es/utils/aepp-wallet-communication/rpc/rpc-clients.js +++ b/es/utils/aepp-wallet-communication/rpc/rpc-clients.js @@ -122,7 +122,6 @@ export const RpcClients = stampit({ */ export const RpcClient = stampit({ init ({ id, name, networkId, icons, connection, handlers: [onMessage, onDisconnect] }) { - const messageId = 0 this.id = id this.connection = connection this.info = { name, networkId, icons } @@ -134,12 +133,12 @@ export const RpcClient = stampit({ this.addressSubscription = [] this.accounts = {} - this.sendMessage = sendMessage(messageId, this.connection) + this.sendMessage = sendMessage(this.connection) const disconnect = (aepp, connection) => { this.disconnect(true) typeof onDisconnect === 'function' && onDisconnect(connection, this) } - connection.connect(receive(onMessage, messageId), disconnect) + connection.connect(receive(onMessage), disconnect) }, methods: { /** diff --git a/test/integration/rpc.js b/test/integration/rpc.js index 46731c2e33..f3f8321b42 100644 --- a/test/integration/rpc.js +++ b/test/integration/rpc.js @@ -533,7 +533,7 @@ describe('Aepp<->Wallet', function () { }) describe('Rpc helpers', () => { it('Receive invalid message', () => { - (!receive(() => true, 1)(false)).should.be.equal(true) + (!receive(() => true)(false)).should.be.equal(true) }) it('receive unknown method', () => { getHandler({}, { method: 'hey' })()().should.be.equal(true)