diff --git a/docs/assets/bg-plugin-lifecycle.png b/docs/assets/bg-plugin-lifecycle.png new file mode 100644 index 000000000..9151fa78d Binary files /dev/null and b/docs/assets/bg-plugin-lifecycle.png differ diff --git a/docs/assets/regular-plugin-lifecycle.png b/docs/assets/regular-plugin-lifecycle.png new file mode 100644 index 000000000..44aac2a43 Binary files /dev/null and b/docs/assets/regular-plugin-lifecycle.png differ diff --git a/docs/extending/client-plugin-lifecycle.md b/docs/extending/client-plugin-lifecycle.md new file mode 100644 index 000000000..eed3843cd --- /dev/null +++ b/docs/extending/client-plugin-lifecycle.md @@ -0,0 +1,23 @@ +--- +id: client-plugin-lifecycle +title: Client Plugin Lifecycle +--- + +There are two types of client plugin: Regular and Background plugins. We recommend starting off as a regular plugin and switching it to a background plugin if necessary. + +For both types of plugin, we recommend starting work after `onConnect` is called, and terminating it after `onDisconnect`, when possible. This prevents wasted computation when Flipper isn't connected. If the plugin needs to keep track of events that occur before it gets connected (such as initial network requests on app startup), you should do so in the plugin constructor (or ideally in a separate class). + +## Regular Plugin Lifecycle +For regular plugins, `onConnect` and `onDisconnect` are triggered when the user opens the plugin in the Flipper UI, and when they switch to another plugin, respectively. +![Regular Plugin Lifecycle diagram](/docs/assets/regular-plugin-lifecycle.png) + +## Background Plugin Lifecycle +For background plugins, `onConnect` is called when Flipper first connects, and `onDisconnect` when it disconnects. The user does not need to be viewing the plugin for it to send messages; they will be queued up until the next time the user opens the plugin where they will be processed. + +Even for background plugins, `onDisconnect` and `onConnect` may be called on a plugin (e.g. if the user restarts Flipper). Plugins should handle this accordingly by making sure to resend any important data to the reconnected instance. +
+Note that a plugin must be enabled by the user for its messages to be queued up. +
+ +![Background Plugin Lifecycle diagram](/docs/assets/bg-plugin-lifecycle.png) + diff --git a/docs/extending/create-plugin.md b/docs/extending/create-plugin.md index 48dc09cc3..d82e95b0c 100644 --- a/docs/extending/create-plugin.md +++ b/docs/extending/create-plugin.md @@ -200,7 +200,7 @@ addPlugin({ ## Background Plugins -In some cases you may want to provide data to Flipper even when your plugin is not currently active. Returning true in `runInBackground()` will result in `onConnect` being called as soon as Flipper connects, and allow you to use the connection at any time. +In some cases you may want to provide data to Flipper even when your plugin is not currently active. Returning true in `runInBackground()` will result in `onConnect` being called as soon as Flipper connects, and allow you to use the connection at any time. See the [Client Plugin Lifecycle](client-plugin-lifecycle) for more details. This should be used in combination with a `persistedStateReducer` on the desktop side. See the [JS Plugin API](js-plugin-api#background-plugins) for details. diff --git a/website/i18n/en.json b/website/i18n/en.json index b90894ad0..bba1b6c85 100644 --- a/website/i18n/en.json +++ b/website/i18n/en.json @@ -12,6 +12,9 @@ "extending/arch": { "title": "Architecture" }, + "extending/client-plugin-lifecycle": { + "title": "Client Plugin Lifecycle" + }, "extending/create-plugin": { "title": "Client Plugin API" }, diff --git a/website/sidebars.json b/website/sidebars.json index 6e25c5c5e..14916cde2 100644 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -50,6 +50,7 @@ "extending/styling-components", "extending/search-and-filter", "extending/create-plugin", + "extending/client-plugin-lifecycle", "extending/send-data", "extending/error-handling", "extending/testing",