Make Client initialize server add-ons
Reviewed By: mweststrate Differential Revision: D34044353 fbshipit-source-id: 99bcb1559787b2a904bdd796233666a7a4783ea4
This commit is contained in:
committed by
Facebook GitHub Bot
parent
a60865f0be
commit
9113006851
@@ -11,7 +11,13 @@ import {SandyPluginDefinition} from './SandyPluginDefinition';
|
||||
import {BasePluginInstance, BasePluginClient} from './PluginBase';
|
||||
import {FlipperLib} from './FlipperLib';
|
||||
import {Atom, ReadOnlyAtom} from '../state/atom';
|
||||
import {DeviceOS, DeviceType, DeviceLogEntry, CrashLog} from 'flipper-common';
|
||||
import {
|
||||
DeviceOS,
|
||||
DeviceType,
|
||||
DeviceLogEntry,
|
||||
CrashLog,
|
||||
ServerAddOnControls,
|
||||
} from 'flipper-common';
|
||||
|
||||
export type DeviceLogListener = (entry: DeviceLogEntry) => void;
|
||||
export type CrashLogListener = (crash: CrashLog) => void;
|
||||
@@ -59,6 +65,7 @@ export class SandyDevicePluginInstance extends BasePluginInstance {
|
||||
readonly client: DevicePluginClient;
|
||||
|
||||
constructor(
|
||||
private readonly serverAddOnControls: ServerAddOnControls,
|
||||
flipperLib: FlipperLib,
|
||||
definition: SandyPluginDefinition,
|
||||
device: Device,
|
||||
@@ -79,9 +86,33 @@ export class SandyDevicePluginInstance extends BasePluginInstance {
|
||||
this.initializePlugin(() =>
|
||||
definition.asDevicePluginModule().devicePlugin(this.client),
|
||||
);
|
||||
this.startServerAddOn();
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return '[SandyDevicePluginInstance]';
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.stopServerAddOn();
|
||||
super.destroy();
|
||||
}
|
||||
|
||||
private startServerAddOn() {
|
||||
const {serverAddOn, name} = this.definition.details;
|
||||
if (serverAddOn) {
|
||||
this.serverAddOnControls.start(name).catch((e) => {
|
||||
console.warn('Failed to start a server add on', name, e);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private stopServerAddOn() {
|
||||
const {serverAddOn, name} = this.definition.details;
|
||||
if (serverAddOn) {
|
||||
this.serverAddOnControls.stop(name).catch((e) => {
|
||||
console.warn('Failed to start a server add on', name, e);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import {FlipperLib} from './FlipperLib';
|
||||
import {Device} from './DevicePlugin';
|
||||
import {batched} from '../state/batch';
|
||||
import {Atom, createState, ReadOnlyAtom} from '../state/atom';
|
||||
import {ServerAddOnControls} from 'flipper-common';
|
||||
|
||||
type EventsContract = Record<string, any>;
|
||||
type MethodsContract = Record<string, (params: any) => Promise<any>>;
|
||||
@@ -141,6 +142,7 @@ export class SandyPluginInstance extends BasePluginInstance {
|
||||
readonly connected = createState(false);
|
||||
|
||||
constructor(
|
||||
private readonly serverAddOnControls: ServerAddOnControls,
|
||||
flipperLib: FlipperLib,
|
||||
definition: SandyPluginDefinition,
|
||||
realClient: RealFlipperClient,
|
||||
@@ -229,6 +231,13 @@ export class SandyPluginInstance extends BasePluginInstance {
|
||||
connect() {
|
||||
this.assertNotDestroyed();
|
||||
if (!this.connected.get()) {
|
||||
const {serverAddOn, name} = this.definition.details;
|
||||
if (serverAddOn) {
|
||||
this.serverAddOnControls.start(name).catch((e) => {
|
||||
console.warn('Failed to start a server add on', name, e);
|
||||
});
|
||||
}
|
||||
|
||||
this.connected.set(true);
|
||||
this.events.emit('connect');
|
||||
}
|
||||
@@ -237,6 +246,13 @@ export class SandyPluginInstance extends BasePluginInstance {
|
||||
disconnect() {
|
||||
this.assertNotDestroyed();
|
||||
if (this.connected.get()) {
|
||||
const {serverAddOn, name} = this.definition.details;
|
||||
if (serverAddOn) {
|
||||
this.serverAddOnControls.stop(name).catch((e) => {
|
||||
console.warn('Failed to stop a server add on', name, e);
|
||||
});
|
||||
}
|
||||
|
||||
this.connected.set(false);
|
||||
this.events.emit('disconnect');
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
BundledPluginDetails,
|
||||
fsConstants,
|
||||
InstalledPluginDetails,
|
||||
ServerAddOnControls,
|
||||
} from 'flipper-common';
|
||||
|
||||
import {
|
||||
@@ -116,6 +117,11 @@ interface BasePluginResult {
|
||||
* Trigger menu entry by label
|
||||
*/
|
||||
triggerMenuEntry(label: string): void;
|
||||
// TODO: Refine server add-on test methods
|
||||
/**
|
||||
* Communication with a server add-on
|
||||
*/
|
||||
serverAddOnControls: ServerAddOnControls;
|
||||
}
|
||||
|
||||
interface StartPluginResult<Module extends FlipperPluginModule<any>>
|
||||
@@ -254,7 +260,10 @@ export function startPlugin<Module extends FlipperPluginModule<any>>(
|
||||
},
|
||||
};
|
||||
|
||||
const serverAddOnControls = createServerAddOnControlsMock();
|
||||
|
||||
const pluginInstance = new SandyPluginInstance(
|
||||
serverAddOnControls,
|
||||
flipperUtils,
|
||||
definition,
|
||||
fakeFlipperClient,
|
||||
@@ -263,7 +272,7 @@ export function startPlugin<Module extends FlipperPluginModule<any>>(
|
||||
);
|
||||
|
||||
const res: StartPluginResult<Module> = {
|
||||
...createBasePluginResult(pluginInstance),
|
||||
...createBasePluginResult(pluginInstance, serverAddOnControls),
|
||||
instance: pluginInstance.instanceApi,
|
||||
module,
|
||||
connect: () => pluginInstance.connect(),
|
||||
@@ -282,6 +291,7 @@ export function startPlugin<Module extends FlipperPluginModule<any>>(
|
||||
pluginInstance.receiveMessages(messages as any);
|
||||
});
|
||||
},
|
||||
serverAddOnControls,
|
||||
};
|
||||
(res as any)._backingInstance = pluginInstance;
|
||||
// we start activated
|
||||
@@ -337,7 +347,9 @@ export function startDevicePlugin<Module extends FlipperDevicePluginModule>(
|
||||
|
||||
const flipperLib = createMockFlipperLib(options);
|
||||
const testDevice = createMockDevice(options);
|
||||
const serverAddOnControls = createServerAddOnControlsMock();
|
||||
const pluginInstance = new SandyDevicePluginInstance(
|
||||
serverAddOnControls,
|
||||
flipperLib,
|
||||
definition,
|
||||
testDevice,
|
||||
@@ -346,7 +358,7 @@ export function startDevicePlugin<Module extends FlipperDevicePluginModule>(
|
||||
);
|
||||
|
||||
const res: StartDevicePluginResult<Module> = {
|
||||
...createBasePluginResult(pluginInstance),
|
||||
...createBasePluginResult(pluginInstance, serverAddOnControls),
|
||||
module,
|
||||
instance: pluginInstance.instanceApi,
|
||||
sendLogEntry: (entry) => {
|
||||
@@ -444,6 +456,7 @@ export function createMockFlipperLib(options?: StartPluginOptions): FlipperLib {
|
||||
|
||||
function createBasePluginResult(
|
||||
pluginInstance: BasePluginInstance,
|
||||
serverAddOnControls: ServerAddOnControls,
|
||||
): BasePluginResult {
|
||||
return {
|
||||
flipperLib: pluginInstance.flipperLib,
|
||||
@@ -469,6 +482,7 @@ function createBasePluginResult(
|
||||
}
|
||||
entry.handler();
|
||||
},
|
||||
serverAddOnControls,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -628,3 +642,10 @@ export function createFlipperServerMock(
|
||||
close: createStubFunction(),
|
||||
};
|
||||
}
|
||||
|
||||
function createServerAddOnControlsMock(): ServerAddOnControls {
|
||||
return {
|
||||
start: createStubFunction(),
|
||||
stop: createStubFunction(),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user