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:
committed by
Facebook GitHub Bot
parent
7a7a88bfde
commit
1b80877f1a
@@ -59,24 +59,37 @@ export class WebsocketClientFlipperConnection<M>
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: fully implement and return actual result
|
||||
requestResponse(payload: Payload<string, M>): Single<Payload<string, M>> {
|
||||
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)});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user