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:
committed by
Facebook GitHub Bot
parent
f8ff6dc393
commit
b9c9e89b53
@@ -151,7 +151,10 @@ export class SandyDevicePluginInstance {
|
||||
}
|
||||
|
||||
deactivate() {
|
||||
if (!this.destroyed && this.activated) {
|
||||
if (this.destroyed) {
|
||||
return;
|
||||
}
|
||||
if (this.activated) {
|
||||
this.lastDeeplink = undefined;
|
||||
this.activated = false;
|
||||
this.events.emit('deactivate');
|
||||
|
||||
@@ -32,6 +32,16 @@ export interface FlipperClient<
|
||||
*/
|
||||
onDestroy(cb: () => void): void;
|
||||
|
||||
/**
|
||||
* the onActivate event is fired whenever the plugin is actived in the UI
|
||||
*/
|
||||
onActivate(cb: () => void): void;
|
||||
|
||||
/**
|
||||
* The counterpart of the `onActivate` handler.
|
||||
*/
|
||||
onDeactivate(cb: () => void): void;
|
||||
|
||||
/**
|
||||
* 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,
|
||||
@@ -124,6 +134,7 @@ export class SandyPluginInstance {
|
||||
/** the plugin instance api as used inside components and such */
|
||||
instanceApi: any;
|
||||
|
||||
activated = false;
|
||||
connected = false;
|
||||
destroyed = false;
|
||||
events = new EventEmitter();
|
||||
@@ -146,6 +157,12 @@ export class SandyPluginInstance {
|
||||
onDestroy: (cb) => {
|
||||
this.events.on('destroy', cb);
|
||||
},
|
||||
onActivate: (cb) => {
|
||||
this.events.on('activate', cb);
|
||||
},
|
||||
onDeactivate: (cb) => {
|
||||
this.events.on('deactivate', cb);
|
||||
},
|
||||
onConnect: (cb) => {
|
||||
this.events.on('connect', cb);
|
||||
},
|
||||
@@ -181,22 +198,30 @@ export class SandyPluginInstance {
|
||||
// the plugin is selected in the UI
|
||||
activate() {
|
||||
this.assertNotDestroyed();
|
||||
const pluginId = this.definition.id;
|
||||
if (!this.realClient.isBackgroundPlugin(pluginId)) {
|
||||
this.realClient.initPlugin(pluginId); // will call connect() if needed
|
||||
if (!this.activated) {
|
||||
this.activated = true;
|
||||
this.events.emit('activate');
|
||||
const pluginId = this.definition.id;
|
||||
if (!this.realClient.isBackgroundPlugin(pluginId)) {
|
||||
this.realClient.initPlugin(pluginId); // will call connect() if needed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the plugin is deselected in the UI
|
||||
deactivate() {
|
||||
this.lastDeeplink = undefined;
|
||||
if (this.destroyed) {
|
||||
// this can happen if the plugin is disabled while active in the UI.
|
||||
// In that case deinit & destroy is already triggered from the STAR_PLUGIN action
|
||||
return;
|
||||
}
|
||||
if (this.activated) {
|
||||
this.lastDeeplink = undefined;
|
||||
this.activated = false;
|
||||
this.events.emit('deactivate');
|
||||
}
|
||||
const pluginId = this.definition.id;
|
||||
if (!this.realClient.isBackgroundPlugin(pluginId)) {
|
||||
if (this.connected && !this.realClient.isBackgroundPlugin(pluginId)) {
|
||||
this.realClient.deinitPlugin(pluginId);
|
||||
}
|
||||
}
|
||||
@@ -219,6 +244,7 @@ export class SandyPluginInstance {
|
||||
|
||||
destroy() {
|
||||
this.assertNotDestroyed();
|
||||
this.deactivate();
|
||||
if (this.connected) {
|
||||
this.realClient.deinitPlugin(this.definition.id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user