Fix issue with responses from de-inited plugins

Summary:
Now that flipper is using rsocket requestResponse, the SDK is responding with an error when the connection for this plugin has been torn down.

To avoid flipper interpreting these as bad, keep track of which plugins are inited, and only worry about responses from these.

Reviewed By: danielbuechele

Differential Revision: D13973745

fbshipit-source-id: d4e6916f89b3b562e5dcf23c4fe5b5cb384a6ec4
This commit is contained in:
John Knox
2019-02-11 14:01:33 -08:00
committed by Facebook Github Bot
parent ce5f739d81
commit fe0eeafd98
2 changed files with 43 additions and 17 deletions

View File

@@ -98,6 +98,7 @@ export default class Client extends EventEmitter {
this.store = store;
this.broadcastCallbacks = new Map();
this.requestCallbacks = new Map();
this.activePlugins = new Set();
const client = this;
this.responder = {
@@ -140,6 +141,7 @@ export default class Client extends EventEmitter {
connection: ?ReactiveSocket;
responder: PartialResponder;
store: Store;
activePlugins: Set<string>;
broadcastCallbacks: Map<?string, Map<string, Set<Function>>>;
@@ -355,6 +357,8 @@ export default class Client extends EventEmitter {
params,
};
const plugin = params?.api;
console.debug(data, 'message:call');
if (this.sdkVersion < 1) {
@@ -367,21 +371,29 @@ export default class Client extends EventEmitter {
const mark = this.getPerformanceMark(metadata);
performance.mark(mark);
if (this.connection) {
this.connection
.requestResponse({data: JSON.stringify(data)})
.subscribe({
onComplete: payload => {
const logEventName = this.getLogEventName(data);
this.logger.trackTimeSince(mark, logEventName);
const response: {|
success?: Object,
error?: Object,
|} = JSON.parse(payload.data);
this.onResponse(response, resolve, reject);
},
onError: reject,
});
if (this.isAcceptingMessagesFromPlugin(plugin)) {
this.connection &&
this.connection
.requestResponse({data: JSON.stringify(data)})
.subscribe({
onComplete: payload => {
if (this.isAcceptingMessagesFromPlugin(plugin)) {
const logEventName = this.getLogEventName(data);
this.logger.trackTimeSince(mark, logEventName);
const response: {|
success?: Object,
error?: Object,
|} = JSON.parse(payload.data);
this.onResponse(response, resolve, reject);
}
},
// Open fresco then layout and you get errors because responses come back after deinit.
onError: e => {
if (this.isAcceptingMessagesFromPlugin(plugin)) {
reject(e);
}
},
});
}
});
}
@@ -396,6 +408,10 @@ export default class Client extends EventEmitter {
this.logger.trackTimeSince(mark, logEventName);
}
isAcceptingMessagesFromPlugin(plugin: ?string) {
return this.connection && (!plugin || this.activePlugins.has(plugin));
}
getPerformanceMark(data: RequestMetadata): string {
const {method, id} = data;
return `request_response_${method}_${id}`;
@@ -408,6 +424,16 @@ export default class Client extends EventEmitter {
: `request_response_${method}`;
}
initPlugin(pluginId: string) {
this.activePlugins.add(pluginId);
this.rawSend('init', {plugin: pluginId});
}
deinitPlugin(pluginId: string) {
this.activePlugins.delete(pluginId);
this.rawSend('deinit', {plugin: pluginId});
}
rawSend(method: string, params?: Object): void {
const data = {
method,

View File

@@ -209,12 +209,12 @@ export class FlipperPlugin<S = *, A = *, P = *> extends FlipperBasePlugin<
// run plugin teardown
this.teardown();
if (this.realClient.connected) {
this.realClient.rawSend('deinit', {plugin: this.constructor.id});
this.realClient.deinitPlugin(this.constructor.id);
}
}
_init() {
this.realClient.rawSend('init', {plugin: this.constructor.id});
this.realClient.initPlugin(this.constructor.id);
this.init();
}
}