FlipperClientConnection no longer redefines the RSocket Socket interface

Summary:
FlipperClientConnection used to define an interface which closely matched the Socket interface defined in RSocket.

Presumably it tried to 'decouple' RSocket from consumers of the client socket connection. It also limited the amount of actions that could be triggered on said socket connection.

This diff does two things:
- Renames FlipperClientConnection to ClientConnection.
- Changes the interface so that it no longer matches the RSocket Socket interface. The new interface doesn't use RSocket reactive types either.

As a result, current implementations of the interface will act as a proxy to the RSocket Socket type. The change allows the usage of other implementations such as WebSocket.

Reviewed By: fabiomassimo

Differential Revision: D29934090

fbshipit-source-id: 02742e50cd6e801310698969c81b3bf1ef0fa2a2
This commit is contained in:
Lorenzo Blasa
2021-07-27 08:28:37 -07:00
committed by Facebook GitHub Bot
parent 8bb47a38a1
commit c1496f621e
6 changed files with 307 additions and 253 deletions

View File

@@ -11,8 +11,6 @@ import {PluginDefinition} from './plugin';
import BaseDevice, {OS} from './devices/BaseDevice';
import {Logger} from './fb-interfaces/Logger';
import {Store} from './reducers/index';
import {Payload, ConnectionStatus} from 'rsocket-types';
import {Flowable, Single} from 'rsocket-flowable';
import {performance} from 'perf_hooks';
import {reportPluginFailures} from './utils/metrics';
import {default as isProduction} from './utils/isProduction';
@@ -34,6 +32,11 @@ import {
isFlipperMessageDebuggingEnabled,
registerFlipperDebugMessage,
} from './chrome/FlipperMessages';
import {
ConnectionStatus,
ErrorType,
ClientConnection,
} from './comms/ClientConnection';
type Plugins = Set<string>;
type PluginsArr = Array<string>;
@@ -51,7 +54,6 @@ export type ClientExport = {
query: ClientQuery;
};
type ErrorType = {message: string; stacktrace: string; name: string};
type Params = {
api: string;
method: string;
@@ -85,13 +87,6 @@ const handleError = (store: Store, device: BaseDevice, error: ErrorType) => {
crashReporterPlugin.instanceApi.reportCrash(payload);
};
export interface FlipperClientConnection<D, M> {
connectionStatus(): Flowable<ConnectionStatus>;
close(): void;
fireAndForget(payload: Payload<D, M>): void;
requestResponse(payload: Payload<D, M>): Single<Payload<D, M>>;
}
export default class Client extends EventEmitter {
connected = createState(false);
id: string;
@@ -100,7 +95,7 @@ export default class Client extends EventEmitter {
messageIdCounter: number;
plugins: Plugins;
backgroundPlugins: Plugins;
connection: FlipperClientConnection<any, any> | null | undefined;
connection: ClientConnection | null | undefined;
store: Store;
activePlugins: Set<string>;
freezeData = GK.get('flipper_frozen_data');
@@ -135,7 +130,7 @@ export default class Client extends EventEmitter {
constructor(
id: string,
query: ClientQuery,
conn: FlipperClientConnection<any, any> | null | undefined,
conn: ClientConnection | null | undefined,
logger: Logger,
store: Store,
plugins: Plugins | null | undefined,
@@ -161,18 +156,13 @@ export default class Client extends EventEmitter {
const client = this;
if (conn) {
conn.connectionStatus().subscribe({
onNext(payload) {
if (payload.kind == 'ERROR' || payload.kind == 'CLOSED') {
client.connected.set(false);
}
},
onSubscribe(subscription) {
subscription.request(Number.MAX_SAFE_INTEGER);
},
onError(payload) {
console.error('[client] connection status error ', payload);
},
conn.subscribeToEvents((status) => {
if (
status === ConnectionStatus.CLOSED ||
status === ConnectionStatus.ERROR
) {
client.connected.set(false);
}
});
}
}
@@ -531,7 +521,7 @@ export default class Client extends EventEmitter {
}
rawCall<T>(method: string, fromPlugin: boolean, params?: Params): Promise<T> {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
const id = this.messageIdCounter++;
const metadata: RequestMetadata = {
method,
@@ -556,7 +546,7 @@ export default class Client extends EventEmitter {
if (this.sdkVersion < 1) {
this.startTimingRequestResponse({method, id, params});
if (this.connection) {
this.connection.fireAndForget({data: JSON.stringify(data)});
this.connection.send(data);
}
return;
}
@@ -573,41 +563,33 @@ export default class Client extends EventEmitter {
return;
}
if (!fromPlugin || this.isAcceptingMessagesFromPlugin(plugin)) {
this.connection!.requestResponse({
data: JSON.stringify(data),
}).subscribe({
onComplete: (payload) => {
if (!fromPlugin || this.isAcceptingMessagesFromPlugin(plugin)) {
const logEventName = this.getLogEventName(data);
this.logger.trackTimeSince(mark, logEventName);
emitBytesReceived(plugin || 'unknown', payload.data.length * 2);
const response: {
success?: Object;
error?: ErrorType;
} = JSON.parse(payload.data);
try {
const response = await this.connection!.sendExpectResponse(data);
if (!fromPlugin || this.isAcceptingMessagesFromPlugin(plugin)) {
const logEventName = this.getLogEventName(data);
this.logger.trackTimeSince(mark, logEventName);
emitBytesReceived(plugin || 'unknown', response.length * 2);
this.onResponse(response, resolve, reject);
this.onResponse(response, resolve, reject);
if (isFlipperMessageDebuggingEnabled()) {
registerFlipperDebugMessage({
device: this.deviceSync?.displayTitle(),
app: this.query.app,
flipperInternalMethod: method,
payload: response,
plugin,
pluginMethod: params?.method,
direction: 'toFlipper:response',
});
}
if (isFlipperMessageDebuggingEnabled()) {
registerFlipperDebugMessage({
device: this.deviceSync?.displayTitle(),
app: this.query.app,
flipperInternalMethod: method,
payload: response,
plugin,
pluginMethod: params?.method,
direction: 'toFlipper:response',
});
}
},
onError: (e) => {
// This is only called if the connection is dead. Not in expected
// and recoverable cases like a missing receiver/method.
this.disconnect();
reject(new Error('Connection disconnected: ' + e));
},
});
}
} catch (error) {
// This is only called if the connection is dead. Not in expected
// and recoverable cases like a missing receiver/method.
this.disconnect();
reject(new Error('Unable to send, connection error: ' + error));
}
} else {
reject(
new Error(
@@ -699,7 +681,7 @@ export default class Client extends EventEmitter {
};
console.debug(data, 'message:send');
if (this.connection) {
this.connection.fireAndForget({data: JSON.stringify(data)});
this.connection.send(data);
}
if (isFlipperMessageDebuggingEnabled()) {