Support exporting public API from a plugin

Reviewed By: passy

Differential Revision: D36098167

fbshipit-source-id: 90182248d0541a1d8eff92a391a56422537c0595
This commit is contained in:
Andrey Goncharov
2022-05-10 05:13:24 -07:00
committed by Facebook GitHub Bot
parent 3dd7583fdc
commit 2854e75e9c
4 changed files with 23 additions and 1 deletions

View File

@@ -110,6 +110,7 @@ test('Correct top level API exposed', () => {
"FileDescriptor", "FileDescriptor",
"FileEncoding", "FileEncoding",
"FlipperLib", "FlipperLib",
"FlipperPluginInstance",
"FlipperServerForServerAddOn", "FlipperServerForServerAddOn",
"HighlightManager", "HighlightManager",
"Idler", "Idler",

View File

@@ -25,7 +25,10 @@ export {
CrashLogListener, CrashLogListener,
SandyDevicePluginInstance as _SandyDevicePluginInstance, SandyDevicePluginInstance as _SandyDevicePluginInstance,
} from './plugin/DevicePlugin'; } from './plugin/DevicePlugin';
export {SandyPluginDefinition as _SandyPluginDefinition} from './plugin/SandyPluginDefinition'; export {
SandyPluginDefinition as _SandyPluginDefinition,
FlipperPluginInstance,
} from './plugin/SandyPluginDefinition';
export {SandyPluginRenderer as _SandyPluginRenderer} from './plugin/PluginRenderer'; export {SandyPluginRenderer as _SandyPluginRenderer} from './plugin/PluginRenderer';
export { export {
SandyPluginContext as _SandyPluginContext, SandyPluginContext as _SandyPluginContext,

View File

@@ -223,6 +223,8 @@ export abstract class BasePluginInstance {
definition: SandyPluginDefinition; definition: SandyPluginDefinition;
/** the plugin instance api as used inside components and such */ /** the plugin instance api as used inside components and such */
instanceApi: any; instanceApi: any;
/** the plugin public api exposed over the wire via flipper-server-companion */
companionApi?: any;
/** the device owning this plugin */ /** the device owning this plugin */
readonly device: Device; readonly device: Device;
@@ -276,6 +278,11 @@ export abstract class BasePluginInstance {
setCurrentPluginInstance(this); setCurrentPluginInstance(this);
try { try {
this.instanceApi = batched(factory)(); this.instanceApi = batched(factory)();
const apiFactory = this.definition.module.API;
if (apiFactory) {
this.companionApi = apiFactory(this.instanceApi);
}
} finally { } finally {
// check if we have both an import handler and rootStates; probably dev error // check if we have both an import handler and rootStates; probably dev error
if (this.importHandler && Object.keys(this.rootStates).length > 0) { if (this.importHandler && Object.keys(this.rootStates).length > 0) {

View File

@@ -11,12 +11,21 @@ import {ActivatablePluginDetails} from 'flipper-common';
import {PluginFactory, FlipperPluginComponent} from './Plugin'; import {PluginFactory, FlipperPluginComponent} from './Plugin';
import {DevicePluginPredicate, DevicePluginFactory} from './DevicePlugin'; import {DevicePluginPredicate, DevicePluginFactory} from './DevicePlugin';
export type FlipperPluginAPI<T extends (...args: any[]) => object> = (
pluginInstance: ReturnType<T>,
) => object;
export type FlipperPluginInstance<T extends (...args: any[]) => object> =
Parameters<FlipperPluginAPI<T>>[0];
/** /**
* FlipperPluginModule describe the exports that are provided by a typical Flipper Desktop plugin * FlipperPluginModule describe the exports that are provided by a typical Flipper Desktop plugin
*/ */
export type FlipperDevicePluginModule = { export type FlipperDevicePluginModule = {
/** predicate that determines if this plugin applies to the currently selcted device */ /** predicate that determines if this plugin applies to the currently selcted device */
supportsDevice?: DevicePluginPredicate; // TODO T84453692: remove this function after some transition period in favor of BaseDevice.supportsPlugin. supportsDevice?: DevicePluginPredicate; // TODO T84453692: remove this function after some transition period in favor of BaseDevice.supportsPlugin.
/** the factory function that exposes plugin API over the wire */
API?: FlipperPluginAPI<DevicePluginFactory>;
/** the factory function that initializes a plugin instance */ /** the factory function that initializes a plugin instance */
devicePlugin: DevicePluginFactory; devicePlugin: DevicePluginFactory;
/** the component type that can render this plugin */ /** the component type that can render this plugin */
@@ -29,6 +38,8 @@ export type FlipperDevicePluginModule = {
export type FlipperPluginModule< export type FlipperPluginModule<
Factory extends PluginFactory<any, any, any, any>, Factory extends PluginFactory<any, any, any, any>,
> = { > = {
/** the factory function that exposes plugin API over the wire */
API?: FlipperPluginAPI<Factory>;
/** the factory function that initializes a plugin instance */ /** the factory function that initializes a plugin instance */
plugin: Factory; plugin: Factory;
/** the component type that can render this plugin */ /** the component type that can render this plugin */