Support activate and deactivate in normal plugins

Summary:
Device plugins have an activate and deactivate hook, that reflects the plugin being selected in the UI. Added these same hooks to client plugins as well. In practice they are called at the same times as `onConnect` and `onDisconnect`, except for background plugins, which connect only once, so it is pretty useful to be still able to make the distinction.

Since there is now quite some common functionality between plugins and device plugins, will clean things a bit up in a next diff

[Interesting] as it explains the difference between the different lifecycle methods of plugins, and the impact of being a background plugin

LIfecycle summary:

1. app connects
2. for background plugins: connect them (`onConnect`)
3. user selects a plugin, triggers `onActivate`. will also trigger `onConnect` the plugin if it _isnt_ a bg plugin
4. user selects a different plugin, triggers `onDeactivate`. will also trigger `onDisconnect` if it isn't a bg plugin.
5. app is unloaded. Triggers `onDisconnect` for bg plugins. Triggers `onDestroy` for all plugins,

Reviewed By: jknoxville

Differential Revision: D22724791

fbshipit-source-id: 9fe2e666eb37fa2e0bd00fa61d78d2d4b1080137
This commit is contained in:
Michel Weststrate
2020-08-04 07:05:57 -07:00
committed by Facebook GitHub Bot
parent f8ff6dc393
commit b9c9e89b53
10 changed files with 288 additions and 21 deletions

View File

@@ -40,6 +40,7 @@ type Renderer = RenderResult<typeof queries>;
interface StartPluginOptions {
initialState?: Record<string, any>;
isArchived?: boolean;
isBackgroundPlugin?: boolean;
}
type ExtractClientType<Module extends FlipperPluginModule<any>> = Parameters<
@@ -67,6 +68,15 @@ interface StartPluginResult<Module extends FlipperPluginModule<any>> {
* module, from which any other exposed methods can be accessed during testing
*/
module: Module;
/**
* Emulates the 'onActivate' event (when the user opens the plugin in the UI).
* Will also trigger the `onConnect` event for non-background plugins
*/
activate(): void;
/**
* Emulatese the 'onDeactivate' event
*/
deactivate(): void;
/**
* Emulates the 'onConnect' event
*/
@@ -127,7 +137,7 @@ interface StartDevicePluginResult<Module extends FlipperDevicePluginModule> {
*/
activate(): void;
/**
* Emulatese the 'onDeactivate' event
* Emulates the 'onDeactivate' event
*/
deactivate(): void;
/**
@@ -164,15 +174,13 @@ export function startPlugin<Module extends FlipperPluginModule<any>>(
const sendStub = jest.fn();
const fakeFlipper: RealFlipperClient = {
isBackgroundPlugin(_pluginId: string) {
// we only reason about non-background plugins,
// as from testing perspective the difference shouldn't matter
return false;
isBackgroundPlugin() {
return !!options?.isBackgroundPlugin;
},
initPlugin(_pluginId: string) {
initPlugin() {
pluginInstance.connect();
},
deinitPlugin(_pluginId: string) {
deinitPlugin() {
pluginInstance.disconnect();
},
call(
@@ -190,12 +198,25 @@ export function startPlugin<Module extends FlipperPluginModule<any>>(
definition,
options?.initialState,
);
// we start connected
if (options?.isBackgroundPlugin) {
pluginInstance.connect(); // otherwise part of activate
}
// we start activated
pluginInstance.activate();
const res: StartPluginResult<Module> = {
module,
instance: pluginInstance.instanceApi,
activate() {
pluginInstance.activate();
pluginInstance.connect();
},
deactivate() {
pluginInstance.deactivate();
if (!fakeFlipper.isBackgroundPlugin) {
pluginInstance.disconnect();
}
},
connect: () => pluginInstance.connect(),
disconnect: () => pluginInstance.disconnect(),
destroy: () => pluginInstance.destroy(),