Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RpcClient: Simplify message id processing #916

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions es/utils/aepp-wallet-communication/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
5 changes: 2 additions & 3 deletions es/utils/aepp-wallet-communication/rpc/rpc-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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: {
/**
Expand Down
2 changes: 1 addition & 1 deletion test/integration/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down