Skip to content

Commit

Permalink
Inline receive helper that is used once
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 20, 2021
1 parent 21903f4 commit a4a1388
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
8 changes: 0 additions & 8 deletions src/utils/aepp-wallet-communication/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ export const sendMessage = (connection) => {
}
}

export const receive = (handler) => (msg, origin) => {
if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) {
console.warn('Receive invalid message', msg)
return
}
handler(msg, origin)
}

export const getHandler = (schema, msg, { debug = false } = {}) => {
const handler = schema[msg.method]
if (!handler || typeof handler !== 'function') {
Expand Down
13 changes: 11 additions & 2 deletions src/utils/aepp-wallet-communication/rpc/rpc-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import stampit from '@stamp/it'

import { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from '../schema'
import { receive, sendMessage, message, isValidAccounts } from '../helpers'
import { sendMessage, message, isValidAccounts } from '../helpers'

/**
* Contain functionality for managing multiple RPC clients (RpcClient stamp)
Expand Down Expand Up @@ -160,11 +160,20 @@ export const RpcClient = stampit({
this.accounts = {}

this.sendMessage = sendMessage(this.connection)

const handleMessage = (msg, origin) => {
if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) {
throw new Error(`Received invalid message: ${msg}`)
}
onMessage(msg, origin)
}

const disconnect = (aepp, connection) => {
this.disconnect(true)
typeof onDisconnect === 'function' && onDisconnect(connection, this)
}
connection.connect(receive(onMessage), disconnect)

connection.connect(handleMessage, disconnect)
},
propertyDescriptors: {
currentAccount: {
Expand Down
10 changes: 1 addition & 9 deletions test/integration/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ import { MemoryAccount, Node, RpcAepp, RpcWallet } from '../../src'
import { unpackTx } from '../../src/tx/builder'
import { decode } from '../../src/tx/builder/helpers'
import BrowserWindowMessageConnection from '../../src/utils/aepp-wallet-communication/connection/browser-window-message'
import {
getBrowserAPI,
getHandler,
isInIframe,
receive
} from '../../src/utils/aepp-wallet-communication/helpers'
import { getBrowserAPI, getHandler } from '../../src/utils/aepp-wallet-communication/helpers'
import { METHODS, RPC_STATUS } from '../../src/utils/aepp-wallet-communication/schema'
import { generateKeyPair, verify } from '../../src/utils/crypto'
import { compilerUrl, genesisAccount, internalUrl, networkId, publicKey, url } from './'
Expand Down Expand Up @@ -550,9 +545,6 @@ describe('Aepp<->Wallet', function () {
})

describe('Rpc helpers', () => {
it('Receive invalid message', () => {
(!receive(() => true)(false)).should.be.equal(true)
})
it('receive unknown method', async () => {
(await getHandler({}, { method: 'hey' })()()).should.be.equal(true)
})
Expand Down

0 comments on commit a4a1388

Please sign in to comment.