Use AbstractClient from flipper-frontend-core in fliper-ui-core

Summary: This stack attempts to start using flipper-frontend-core from flipper-ui-core. Currently, flipper-frontend-core contains lots of copy-pasted code from flipper-ui-core.

Reviewed By: lblasa

Differential Revision: D37139198

fbshipit-source-id: 042db7492c550e10ea72c32fd15001c141bf53f9
This commit is contained in:
Andrey Goncharov
2022-06-20 12:18:40 -07:00
committed by Facebook GitHub Bot
parent c2e3c4eb50
commit ef5fa275a3
19 changed files with 251 additions and 596 deletions

View File

@@ -37,11 +37,6 @@ import isProduction from './utils/isProduction';
type Plugins = Set<string>;
type PluginsArr = Array<string>;
export type ClientExport = {
id: string;
query: ClientQuery;
};
export type Params = {
api: string;
method: string;
@@ -108,7 +103,7 @@ export default abstract class AbstractClient extends EventEmitter {
protected abstract shouldConnectAsBackgroundPlugin(pluginId: string): boolean;
async init() {
await this.loadPlugins();
await this.loadPlugins('init');
await Promise.all(
[...this.plugins].map(async (pluginId) =>
this.startPluginIfNeeded(await this.getPlugin(pluginId)),
@@ -124,7 +119,7 @@ export default abstract class AbstractClient extends EventEmitter {
}
// get the supported plugins
protected async loadPlugins(): Promise<Plugins> {
protected async loadPlugins(_phase: 'init' | 'refresh'): Promise<Plugins> {
const {plugins} = await timeout(
30 * 1000,
this.rawCall<{plugins: Plugins}>('getPlugins', false),
@@ -204,9 +199,9 @@ export default abstract class AbstractClient extends EventEmitter {
}
// get the plugins, and update the UI
protected async refreshPlugins() {
async refreshPlugins() {
const oldBackgroundPlugins = this.backgroundPlugins;
await this.loadPlugins();
await this.loadPlugins('refresh');
await Promise.all(
[...this.plugins].map(async (pluginId) =>
this.startPluginIfNeeded(await this.getPlugin(pluginId)),
@@ -291,13 +286,7 @@ export default abstract class AbstractClient extends EventEmitter {
);
}
const pluginInstance = this.getPluginInstanceForExecuteMessage(params);
let handled = false; // This is just for analysis
if (pluginInstance) {
handled = true;
pluginInstance.receiveMessages([params]);
}
const handled = this.handleExecuteMessage(params);
if (!handled && !isProduction()) {
console.warn(`Unhandled message ${params.api}.${params.method}`);
}
@@ -307,10 +296,13 @@ export default abstract class AbstractClient extends EventEmitter {
}
}
protected getPluginInstanceForExecuteMessage(
params: Params,
): _SandyPluginInstance | undefined {
return this.sandyPluginStates.get(params.api);
protected handleExecuteMessage(params: Params): boolean {
const pluginInstance = this.sandyPluginStates.get(params.api);
if (!pluginInstance) {
return false;
}
pluginInstance.receiveMessages([params]);
return true;
}
protected onResponse(
@@ -502,6 +494,17 @@ export default abstract class AbstractClient extends EventEmitter {
);
}
send(api: string, method: string, params?: Object): void {
if (!isProduction()) {
console.warn(
`${api}:${
method || ''
} client.send() is deprecated. Please use call() instead so you can handle errors.`,
);
}
return this.rawSend('execute', {api, method, params});
}
async supportsMethod(api: string, method: string): Promise<boolean> {
if (!this.connected.get()) {
return false;