Files
flipper/docs/create-plugin.md
Pritesh Nandgaonkar 3699a0667a Rename sonar to flipper in the docs
Summary: Sonar->Flipper

Reviewed By: passy

Differential Revision: D10032191

fbshipit-source-id: 5e5230a02e34b9d1c7c355701daef984dc779f35
2018-09-26 04:13:22 -07:00

2.8 KiB

id, title, sidebar_label
id title sidebar_label
create-plugin Mobile Setup Mobile Setup

Implement FlipperPlugin

Create a class implementing FlipperPlugin.

Android

public class MyFlipperPlugin implements FlipperPlugin {
  private FlipperConnection mConnection;

  @Override
  public String getId() {
    return "MyFlipperPlugin";
  }

  @Override
  public void onConnect(FlipperConnection connection) throws Exception {
    mConnection = connection;
  }

  @Override
  public void onDisconnect() throws Exception {
    mConnection = null;
  }
}

iOS

@interface MyFlipperPlugin : NSObject<FlipperPlugin>
@end

@implementation MyFlipperPlugin

- (NSString*)identifier { return @"MyFlipperPlugin"; }
- (void)didConnect:(FlipperConnection*)connection {}
- (void)didDisonnect {}

@end

C++

class MyFlipperPlugin : public FlipperPlugin {
public:
  std::string identifier() const override { return "MyFlipperPlugin"; }
  void didConnect(std::shared_ptr<FlipperConnection> conn) override;
  void didDisconnect() override;
};

Using FlipperConnection

Using the FlipperConnection object you can register a receiver of a desktop method call and respond with data.

Android

connection.receive("getData", new FlipperReceiver() {
  @Override
  public void onReceive(FlipperObject params, FlipperResponder responder) throws Exception {
    responder.success(
        new FlipperObject.Builder()
            .put("data", MyData.get())
            .build());
  }
});

iOS

@interface MyFlipperPlugin : NSObject<FlipperPlugin>
@end

@implementation MyFlipperPlugin

- (NSString*)identifier { return @"MyFlipperPlugin"; }

- (void)didConnect:(FlipperConnection*)connection
{
  [connection receive:@"getData" withBlock:^(NSDictionary *params, FlipperResponder *responder) {
    [responder success:@{
      @"data":[MyData get],
    }];
  }];
}

- (void)didDisonnect {}

@end

C++

void MyFlipperPlugin::didConnect(std::shared_ptr<FlipperConnection> conn) {
  conn->receive("getData", [](const folly::dynamic &params,
                             std::unique_ptr<FlipperResponder> responder) {
    dynamic response = folly::dynamic::object("data", getMyData());
    responder->success(response);
  });
}

Push data to the desktop

You don't have to wait for the desktop to request data though, you can also push data directly to the desktop.

Android

connection.send("MyMessage",
    new FlipperObject.Builder()
        .put("message", "Hello")
        .build()

iOS

[connection send:@"getData" withParams:@{@"message":@"hello"}];

C++

void MyFlipperPlugin::didConnect(std::shared_ptr<FlipperConnection> conn) {
  dynamic message = folly::dynamic::object("message", "hello");
  conn->send("getData", message);
}