diff --git a/desktop/flipper-plugin/src/__tests__/api.node.tsx b/desktop/flipper-plugin/src/__tests__/api.node.tsx index 8233da242..6bc8abffd 100644 --- a/desktop/flipper-plugin/src/__tests__/api.node.tsx +++ b/desktop/flipper-plugin/src/__tests__/api.node.tsx @@ -110,6 +110,7 @@ test('Correct top level API exposed', () => { "FileDescriptor", "FileEncoding", "FlipperLib", + "FlipperPluginInstance", "FlipperServerForServerAddOn", "HighlightManager", "Idler", diff --git a/desktop/flipper-plugin/src/index.tsx b/desktop/flipper-plugin/src/index.tsx index b0656be65..8ff6e604e 100644 --- a/desktop/flipper-plugin/src/index.tsx +++ b/desktop/flipper-plugin/src/index.tsx @@ -25,7 +25,10 @@ export { CrashLogListener, SandyDevicePluginInstance as _SandyDevicePluginInstance, } 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 { SandyPluginContext as _SandyPluginContext, diff --git a/desktop/flipper-plugin/src/plugin/PluginBase.tsx b/desktop/flipper-plugin/src/plugin/PluginBase.tsx index e41cca8a2..ee41e00e2 100644 --- a/desktop/flipper-plugin/src/plugin/PluginBase.tsx +++ b/desktop/flipper-plugin/src/plugin/PluginBase.tsx @@ -223,6 +223,8 @@ export abstract class BasePluginInstance { definition: SandyPluginDefinition; /** the plugin instance api as used inside components and such */ instanceApi: any; + /** the plugin public api exposed over the wire via flipper-server-companion */ + companionApi?: any; /** the device owning this plugin */ readonly device: Device; @@ -276,6 +278,11 @@ export abstract class BasePluginInstance { setCurrentPluginInstance(this); try { this.instanceApi = batched(factory)(); + + const apiFactory = this.definition.module.API; + if (apiFactory) { + this.companionApi = apiFactory(this.instanceApi); + } } finally { // check if we have both an import handler and rootStates; probably dev error if (this.importHandler && Object.keys(this.rootStates).length > 0) { diff --git a/desktop/flipper-plugin/src/plugin/SandyPluginDefinition.tsx b/desktop/flipper-plugin/src/plugin/SandyPluginDefinition.tsx index 610103886..5bd77bb94 100644 --- a/desktop/flipper-plugin/src/plugin/SandyPluginDefinition.tsx +++ b/desktop/flipper-plugin/src/plugin/SandyPluginDefinition.tsx @@ -11,12 +11,21 @@ import {ActivatablePluginDetails} from 'flipper-common'; import {PluginFactory, FlipperPluginComponent} from './Plugin'; import {DevicePluginPredicate, DevicePluginFactory} from './DevicePlugin'; +export type FlipperPluginAPI object> = ( + pluginInstance: ReturnType, +) => object; + +export type FlipperPluginInstance object> = + Parameters>[0]; + /** * FlipperPluginModule describe the exports that are provided by a typical Flipper Desktop plugin */ export type FlipperDevicePluginModule = { /** 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. + /** the factory function that exposes plugin API over the wire */ + API?: FlipperPluginAPI; /** the factory function that initializes a plugin instance */ devicePlugin: DevicePluginFactory; /** the component type that can render this plugin */ @@ -29,6 +38,8 @@ export type FlipperDevicePluginModule = { export type FlipperPluginModule< Factory extends PluginFactory, > = { + /** the factory function that exposes plugin API over the wire */ + API?: FlipperPluginAPI; /** the factory function that initializes a plugin instance */ plugin: Factory; /** the component type that can render this plugin */