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
This commit is contained in:
Rakha Kanz Kautsar
2020-09-09 06:42:39 -07:00
committed by Facebook GitHub Bot
parent 7a7a88bfde
commit 1b80877f1a

View File

@@ -59,24 +59,37 @@ export class WebsocketClientFlipperConnection<M>
); );
} }
// TODO: fully implement and return actual result
requestResponse(payload: Payload<string, M>): Single<Payload<string, M>> { requestResponse(payload: Payload<string, M>): Single<Payload<string, M>> {
return new Single((subscriber) => { return new Single((subscriber) => {
const method = const {id: callId = undefined, method = undefined} =
payload.data != null ? JSON.parse(payload.data).method : 'not-defined'; payload.data != null ? JSON.parse(payload.data) : {};
subscriber.onSubscribe(() => {}); subscriber.onSubscribe(() => {});
if (method != 'getPlugins') {
this.fireAndForget(payload); if (method === 'getPlugins') {
} subscriber.onComplete({
subscriber.onComplete(
method == 'getPlugins'
? {
data: JSON.stringify({ data: JSON.stringify({
success: {plugins: this.plugins}, success: {plugins: this.plugins},
}), }),
});
return;
} }
: {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)});
}
});
}); });
} }
} }