Expose more meta information for plugins

Summary: expose `appName`, `appId` and `device` to Sandy plugins. Will be used in next diff to migrate navigation plugin

Reviewed By: cekkaewnumchai

Differential Revision: D24857253

fbshipit-source-id: 03ac3d376d5d1950bcf3d78386a65ce167b517e3
This commit is contained in:
Michel Weststrate
2020-11-11 07:57:14 -08:00
committed by Facebook GitHub Bot
parent 9b4e7e873c
commit 1157976eef
7 changed files with 87 additions and 23 deletions

View File

@@ -46,15 +46,14 @@ export type DevicePluginPredicate = (device: Device) => boolean;
export type DevicePluginFactory = (client: DevicePluginClient) => object;
export interface DevicePluginClient extends BasePluginClient {
readonly device: Device;
}
export interface DevicePluginClient extends BasePluginClient {}
/**
* Wrapper interface around BaseDevice in Flipper
*/
export interface RealFlipperDevice {
os: string;
serial: string;
isArchived: boolean;
deviceType: DeviceType;
addLogListener(callback: DeviceLogListener): Symbol;
@@ -76,25 +75,8 @@ export class SandyDevicePluginInstance extends BasePluginInstance {
realDevice: RealFlipperDevice,
initialStates?: Record<string, any>,
) {
super(flipperLib, definition, initialStates);
const device: Device = {
realDevice, // TODO: temporarily, clean up T70688226
// N.B. we model OS as string, not as enum, to make custom device types possible in the future
os: realDevice.os,
isArchived: realDevice.isArchived,
deviceType: realDevice.deviceType,
onLogEntry(cb) {
const handle = realDevice.addLogListener(cb);
return () => {
realDevice.removeLogListener(handle);
};
},
};
this.client = {
...this.createBasePluginClient(),
device,
};
super(flipperLib, definition, realDevice, initialStates);
this.client = this.createBasePluginClient();
this.initializePlugin(() =>
definition.asDevicePluginModule().devicePlugin(this.client),
);

View File

@@ -10,6 +10,7 @@
import {SandyPluginDefinition} from './SandyPluginDefinition';
import {BasePluginInstance, BasePluginClient} from './PluginBase';
import {FlipperLib} from './FlipperLib';
import {RealFlipperDevice} from './DevicePlugin';
type EventsContract = Record<string, any>;
type MethodsContract = Record<string, (params: any) => Promise<any>>;
@@ -26,6 +27,16 @@ export interface PluginClient<
Events extends EventsContract = {},
Methods extends MethodsContract = {}
> extends BasePluginClient {
/**
* Identifier that uniquely identifies the connected application
*/
readonly appId: string;
/**
* Registered name for the connected application
*/
readonly appName: string;
/**
* the onConnect event is fired whenever the plugin is connected to it's counter part on the device.
* For most plugins this event is fired if the user selects the plugin,
@@ -71,6 +82,14 @@ export interface PluginClient<
* Plugin Factory. For internal purposes only
*/
export interface RealFlipperClient {
id: string;
query: {
app: string;
os: string;
device: string;
device_id: string;
};
deviceSync: RealFlipperDevice;
isBackgroundPlugin(pluginId: string): boolean;
initPlugin(pluginId: string): void;
deinitPlugin(pluginId: string): void;
@@ -108,11 +127,17 @@ export class SandyPluginInstance extends BasePluginInstance {
realClient: RealFlipperClient,
initialStates?: Record<string, any>,
) {
super(flipperLib, definition, initialStates);
super(flipperLib, definition, realClient.deviceSync, initialStates);
this.realClient = realClient;
this.definition = definition;
this.client = {
...this.createBasePluginClient(),
get appId() {
return realClient.id;
},
get appName() {
return realClient.query.app;
},
onConnect: (cb) => {
this.events.on('connect', cb);
},

View File

@@ -12,8 +12,11 @@ import {EventEmitter} from 'events';
import {Atom} from '../state/atom';
import {MenuEntry, NormalizedMenuEntry, normalizeMenuEntry} from './MenuEntry';
import {FlipperLib} from './FlipperLib';
import {Device, RealFlipperDevice} from './DevicePlugin';
export interface BasePluginClient {
readonly device: Device;
/**
* the onDestroy event is fired whenever a device is unloaded from Flipper, or a plugin is disabled.
*/
@@ -65,6 +68,8 @@ export abstract class BasePluginInstance {
definition: SandyPluginDefinition;
/** the plugin instance api as used inside components and such */
instanceApi: any;
/** the device owning this plugin */
device: Device;
activated = false;
destroyed = false;
@@ -82,11 +87,29 @@ export abstract class BasePluginInstance {
constructor(
flipperLib: FlipperLib,
definition: SandyPluginDefinition,
realDevice: RealFlipperDevice,
initialStates?: Record<string, any>,
) {
this.flipperLib = flipperLib;
this.definition = definition;
this.initialStates = initialStates;
if (!realDevice) {
throw new Error('Illegal State: Device has not yet been loaded');
}
this.device = {
realDevice, // TODO: temporarily, clean up T70688226
// N.B. we model OS as string, not as enum, to make custom device types possible in the future
os: realDevice.os,
isArchived: realDevice.isArchived,
deviceType: realDevice.deviceType,
onLogEntry(cb) {
const handle = realDevice.addLogListener(cb);
return () => {
realDevice.removeLogListener(handle);
};
},
};
}
protected initializePlugin(factory: () => any) {
@@ -102,6 +125,7 @@ export abstract class BasePluginInstance {
protected createBasePluginClient(): BasePluginClient {
return {
device: this.device,
onActivate: (cb) => {
this.events.on('activate', cb);
},