Update docs for JS only plugins

Summary: Added docs on how to write React-Native JavaScript based plugins

Reviewed By: passy

Differential Revision: D19344803

fbshipit-source-id: ad1ea66f1031760729fdaea8a7e6c1ef5dcd5439
This commit is contained in:
Michel Weststrate
2020-01-23 07:09:51 -08:00
committed by Facebook Github Bot
parent e306aeb010
commit 426d17b08d
6 changed files with 134 additions and 2 deletions

3
.gitignore vendored
View File

@@ -27,3 +27,6 @@ libs/*/bin/**/*.class
# Mac OS X
*.DS_Store
# Automatically generated
docs/extending/ui-components.md

View File

@@ -4,7 +4,8 @@ title: Client Plugin API
---
## FlipperPlugin
To build a client plugin, just implement the `FlipperPlugin` interface.
To build a client plugin, implement the `FlipperPlugin` interface.
The ID that is returned from your implementation needs to match the `name` defined in your JavaScript counterpart's `package.json`.
@@ -60,6 +61,32 @@ public:
bool runInBackground() override;
};
```
<!--React Native JS-->
<div class="warning">
Please note that using Flipper from JavaScript in React Native requires the package [`react-native-flipper`](https://www.npmjs.com/package/react-native-flipper) to be installed in the hosting application.
</div>
```javascript
import {addPlugin} from 'react-native-flipper';
addPlugin({
getId() {
return 'MyFlipperPlugin';
},
onConnect(connection) {
console.log("connected");
},
onDisconnect() {
console.log("disconnected");
},
runInBackground() {
return false;
}
})
```
<!--END_DOCUSAURUS_CODE_TABS-->
@@ -112,6 +139,25 @@ void MyFlipperPlugin::didConnect(std::shared_ptr<FlipperConnection> conn) {
});
}
```
<!--React Native JS-->
```javascript
addPlugin({
getId() {
return 'MyFlipperPlugin';
},
onConnect(connection) {
console.log("connected");
connection.receive("getData", (data, responder) => {
console.log("incoming data", data);
// respond with some data
responder.success({
ack: true
});
});
},
// ...as-is
})
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Push data to the desktop
@@ -137,12 +183,33 @@ void MyFlipperPlugin::didConnect(std::shared_ptr<FlipperConnection> conn) {
conn->send("getData", message);
}
```
<!--React Native JS-->
```javascript
addPlugin({
getId() {
return 'MyFlipperPlugin';
},
onConnect(connection) {
console.log("connected");
connection.send("newRow", { message: "Hello" });
},
// ...as-is
})
```
<!--END_DOCUSAURUS_CODE_TABS-->
## 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.
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.
The benefit is that the desktop plugin can process this data in the background and fire notifications. It also reduces the number of renders and time taken to display the data when the plugin becomes active.
<div class="warning">
Please note that a background plugin could keep some data in memory until a Flipper connection is available, for example to keep statistics about the app startup process.
However, a plugin shouldn't assume it will eventually get a connection, since this depends on whether the user has enabled the plugin on the Desktop side.
So make sure to not store unbounded amounts of data!
</div>

View File

@@ -3,6 +3,12 @@ id: js-plugin-api
title: JavaScript Plugin API
---
<div class="warning">
This page describes the JavaScript API that is used to implement plugins inside the Flipper Desktop application. For the JavaScript API that can be used inside React Native to communicate with the Flipper Desktop, see [Client Plugin API](create-plugin).
</div>
Provided a plugin is setup as defined in [JS Plugin Definiton](js-setup), the basic requirement of a Flipper plugin is that `index.tsx` exports a default class that extends `FlipperPlugin`.
`FlipperPlugin` is an extension of `React.Component` with extra Flipper-related functionality. This means to define the UI of your plugin, you just need to implement this React component.

View File

@@ -0,0 +1,52 @@
---
id: react-native
title: Building a React Native Plugin
---
<div class="warning">
This tutorial requires React Native 0.62 or higher.
</div>
Once you have connected Flipper to a React Native application,
writing your own Flipper plugin can be done without reaching into the native world.
To expose Flipper to the JavaScript world, the React Native Native Module `react-native-flipper` needs to be installed in the hosting application by running `yarn add react-native-flipper`. If you are creating develop a plugin that is distributed as NPM package, make sure to add this to the installation instruction of your package as well!
Registering a new plugin is done by importing `addPlugin` from `"react-native-flipper"` and providing it an object that at least implements the method `getId` (the plugin id that should be used in the desktop plugin as well to make the connection) and two event handlers for the `onConnect` and `onDisconnect` events.
These `onConnect` and `onDisconnect` events are triggered every time the plugin becomes (in)active in the Flipper desktop application.
If the plugin is a [background plugin](../extending/create-plugin.md#background-plugins), these events are triggered typically only once (they might be triggered never, if the Desktop user didn't enable the plugin, or multiple times if they enabled or disabled the plugin a few times).
The `onConnect` callback receive a `connection` which can be used to communicate with the backend:
```javascript
import {addPlugin} from "react-native-flipper"
addPlugin({
getId() {
return 'ReactNativeExamplePlugin';
},
onConnect(connection) {
mammmals.forEach(({ title, pictureUrl }, index) => {
connection.send('newRow', {
id: index,
title,
url: pictureUrl
})
})
},
onDisconnect() {
}
})
```
You might want to store the connection somewhere to be able to send more events as long as `onDisconnect` event hasn't been fired.
The `connection` object can also be used to listen to messages coming from the Desktop plugin. See [Client Plugin API](create-plugin) for details.
An example plugin to play a little Tic-Tac-Toe between the Flipper Desktop and a React Native app can be found inside this repository as well (run `yarn && yarn android` in `react-native/ReactNativeFlipperExample` to start the test project):
* The React Native JavaScript based plugin implementation: [FlipperTicTacToe.js](https://github.com/facebook/flipper/tree/master/react-native/ReactNativeFlipperExample/FlipperTicTacToe.js)
* The Flipper Desktop plugin implementation: [rn-tic-tac-toe/index.tsx](https://github.com/facebook/flipper/blob/master/src/plugins/rn-tic-tac-toe/index.tsx)

View File

@@ -166,6 +166,9 @@
},
"tutorial/js-table": {
"title": "Showing a table"
},
"tutorial/react-native": {
"title": "Building a React Native Plugin"
}
},
"links": {

View File

@@ -36,6 +36,7 @@
"tutorial/intro",
"tutorial/ios",
"tutorial/android",
"tutorial/react-native",
"tutorial/js-setup",
"tutorial/js-table",
"tutorial/js-custom",