From 070f4dd2c2db7c0e5ff5bcfb6d1e7b41aa5230ee Mon Sep 17 00:00:00 2001 From: naz_dou Date: Fri, 21 Feb 2020 16:31:50 +0200 Subject: [PATCH 1/3] feat(AEX-2): Add `removeClient` method to RpcClient stamp --- .../aepp-wallet-communication/rpc/rpc-clients.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/es/utils/aepp-wallet-communication/rpc/rpc-clients.js b/es/utils/aepp-wallet-communication/rpc/rpc-clients.js index e93084eb95..67d4dd5631 100644 --- a/es/utils/aepp-wallet-communication/rpc/rpc-clients.js +++ b/es/utils/aepp-wallet-communication/rpc/rpc-clients.js @@ -59,6 +59,21 @@ export const RpcClients = stampit({ getClient (id) { return this.clients.get(id) }, + /** + * Remove and disiconnect client by id + * @function removeClient + * @instance + * @rtype (id: (String|Number), { forceConnectionClose: boolean = false }) => boolean + * @param {(String|Number)} id Client ID + * @param forceConnectionClose + * @return {Boolean} + */ + removeClient (id, { forceConnectionClose = false } = {}) { + if (!this.hasClient(id)) throw new Error(`Wallet RpcClient with id ${id} do not exist`) + this.clients.get(id).disconnect(forceConnectionClose) + this.client.delete(id) + return true + }, /** * Update client info by id * @function updateClientInfo From b7a39d3b485e78bafeb891148d82fb58f907ff03 Mon Sep 17 00:00:00 2001 From: naz_dou Date: Fri, 21 Feb 2020 16:34:22 +0200 Subject: [PATCH 2/3] feat(AEX-2): Add `removeClient` method to WalletRpc stamp --- es/utils/aepp-wallet-communication/rpc/wallet-rpc.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js b/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js index 7d56c25d1b..732591efbb 100644 --- a/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +++ b/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js @@ -246,6 +246,18 @@ export const WalletRpc = Ae.compose(Accounts, Selector, { getClients () { return rpcClients }, + /** + * Remove specific RpcClient by id + * @function removeClient + * @instance + * @rtype (id: string) => Boolean + * @param {String} id Client id + * @param {Object} [opt = {}] + * @return {Object} + */ + removeClient (id, opt = { forceConnectionClose: false }) { + rpcClients.removeClient(id, opt) + }, /** * Add new AEPP connection * @function addRpcClient From bb29923dd74090bd2cc30018215db1a9e9222e86 Mon Sep 17 00:00:00 2001 From: naz_dou Date: Mon, 24 Feb 2020 12:06:10 +0200 Subject: [PATCH 3/3] feat(AEX-2): Rename `removeClient` to `remopveRpcCLient` in AEPP-RPC Return `clientId` in `addRpcClient` Add test's --- .../rpc/rpc-clients.js | 4 +-- .../rpc/wallet-rpc.js | 13 +++++----- test/integration/rpc.js | 25 +++++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/es/utils/aepp-wallet-communication/rpc/rpc-clients.js b/es/utils/aepp-wallet-communication/rpc/rpc-clients.js index 67d4dd5631..3e89ed296c 100644 --- a/es/utils/aepp-wallet-communication/rpc/rpc-clients.js +++ b/es/utils/aepp-wallet-communication/rpc/rpc-clients.js @@ -60,7 +60,7 @@ export const RpcClients = stampit({ return this.clients.get(id) }, /** - * Remove and disiconnect client by id + * Remove and disiconnect client by ID * @function removeClient * @instance * @rtype (id: (String|Number), { forceConnectionClose: boolean = false }) => boolean @@ -71,7 +71,7 @@ export const RpcClients = stampit({ removeClient (id, { forceConnectionClose = false } = {}) { if (!this.hasClient(id)) throw new Error(`Wallet RpcClient with id ${id} do not exist`) this.clients.get(id).disconnect(forceConnectionClose) - this.client.delete(id) + this.clients.delete(id) return true }, /** diff --git a/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js b/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js index 732591efbb..dd4a7c665e 100644 --- a/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +++ b/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js @@ -247,16 +247,16 @@ export const WalletRpc = Ae.compose(Accounts, Selector, { return rpcClients }, /** - * Remove specific RpcClient by id - * @function removeClient + * Remove specific RpcClient by ID + * @function removeRpcClient * @instance * @rtype (id: string) => Boolean - * @param {String} id Client id + * @param {String} id Client ID * @param {Object} [opt = {}] * @return {Object} */ - removeClient (id, opt = { forceConnectionClose: false }) { - rpcClients.removeClient(id, opt) + removeRpcClient (id, opt = { forceConnectionClose: false }) { + return rpcClients.removeClient(id, opt) }, /** * Add new AEPP connection @@ -264,7 +264,7 @@ export const WalletRpc = Ae.compose(Accounts, Selector, { * @instance * @rtype (clientConnection: Object) => Object * @param {Object} clientConnection AEPP connection object - * @return {void} + * @return {String} Client ID */ addRpcClient (clientConnection) { // @TODO detect if aepp has some history based on origin????: if yes use this instance for connection @@ -278,6 +278,7 @@ export const WalletRpc = Ae.compose(Accounts, Selector, { handlers: [handleMessage(this, id), this.onDisconnect] } ) + return id }, /** * Share wallet info diff --git a/test/integration/rpc.js b/test/integration/rpc.js index 160ca3fad9..8c71d3f160 100644 --- a/test/integration/rpc.js +++ b/test/integration/rpc.js @@ -441,6 +441,31 @@ describe('Aepp<->Wallet', function () { }) received.should.be.equal(true) }) + it('Remove rpc client', async () => { + wallet.onConnection = (aepp, actions) => { + actions.accept() + } + const id = wallet.addRpcClient(BrowserWindowMessageConnection({ + connectionInfo: { id: 'from_wallet_to_aepp_2' }, + self: connections.waelletConnection, + target: connections.aeppConnection + })) + await aepp.connectToWallet(BrowserWindowMessageConnection({ + connectionInfo: { id: 'from_aepp_to_wallet_2' }, + self: connections.aeppConnection, + target: connections.waelletConnection + })) + + wallet.removeRpcClient(id).should.be.equal(true) + wallet.getClients().clients.size.should.be.equal(1) + }) + it('Remove rpc client: client not found', async () => { + try { + wallet.removeRpcClient('a1') + } catch (e) { + e.message.should.be.equal('Wallet RpcClient with id a1 do not exist') + } + }) }) describe('Old RPC Wallet-AEPP', () => { let wallet