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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user