From 1b80877f1a5ba8fe7f3b97c8149214857a40e4b5 Mon Sep 17 00:00:00 2001 From: Rakha Kanz Kautsar Date: Wed, 9 Sep 2020 06:42:39 -0700 Subject: [PATCH] Implement requestResponse for websocket connection (#1510) Summary: This allow websocket client to receive and reply messages from desktop client. ## Changelog Implement requestResponse for websocket connection, allowing websocket client to receive and reply messages from desktop client Pull Request resolved: https://github.com/facebook/flipper/pull/1510 Test Plan: Tested with custom websocket client, allow communicating getPlugins, init, deinit, etc. But I think it's better to have dedicated unit tests for this (currently there's none?), let me know what you think. Reviewed By: passy Differential Revision: D23499396 Pulled By: jknoxville fbshipit-source-id: fb445c0634afd46a525fc52da33b487da4e591fe --- .../websocketClientFlipperConnection.tsx | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/desktop/app/src/utils/js-client-server-utils/websocketClientFlipperConnection.tsx b/desktop/app/src/utils/js-client-server-utils/websocketClientFlipperConnection.tsx index a6e448071..c4639ab46 100644 --- a/desktop/app/src/utils/js-client-server-utils/websocketClientFlipperConnection.tsx +++ b/desktop/app/src/utils/js-client-server-utils/websocketClientFlipperConnection.tsx @@ -59,24 +59,37 @@ export class WebsocketClientFlipperConnection ); } - // TODO: fully implement and return actual result requestResponse(payload: Payload): Single> { return new Single((subscriber) => { - const method = - payload.data != null ? JSON.parse(payload.data).method : 'not-defined'; + const {id: callId = undefined, method = undefined} = + payload.data != null ? JSON.parse(payload.data) : {}; + subscriber.onSubscribe(() => {}); - if (method != 'getPlugins') { - this.fireAndForget(payload); + + if (method === 'getPlugins') { + subscriber.onComplete({ + data: JSON.stringify({ + success: {plugins: this.plugins}, + }), + }); + return; } - subscriber.onComplete( - method == 'getPlugins' - ? { - data: JSON.stringify({ - success: {plugins: this.plugins}, - }), - } - : {data: JSON.stringify({success: null})}, + + this.websocket.send( + JSON.stringify({ + type: 'call', + app: this.app, + payload: payload.data != null ? payload.data : {}, + }), ); + + this.websocket.on('message', (message: string) => { + const {app, payload} = JSON.parse(message); + + if (app === this.app && payload?.id === callId) { + subscriber.onComplete({data: JSON.stringify(payload)}); + } + }); }); } }