Make it possible to write JS only plugins

Summary:
This diff is part of the bigger task T60496135

This diff is based on D18706643, extracting only the react native module parts

It implements the entire Android client api for JavaScript, so that there is feature parity. However this implementation is happy path only, and edge cases will be handled in separate diffs

Reviewed By: jknoxville

Differential Revision: D19310265

fbshipit-source-id: 589716fe059952bdde98df84ed250c5c6feaa118
This commit is contained in:
Michel Weststrate
2020-01-16 04:45:03 -08:00
committed by Facebook Github Bot
parent 3fab1f8fd6
commit c7158f4517
19 changed files with 5308 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
declare namespace Flipper {
/**
* A FlipperPlugin is an object which exposes an API to the Desktop Flipper application. When a
* connection is established the plugin is given a FlipperConnection on which it can register
* request handlers and send messages. When the FlipperConnection is invalid onDisconnect is called.
* onConnect may be called again on the same plugin object if Flipper re-connects, this will provide
* a new FlipperConnection, do not attempt to re-use the previous connection.
*/
interface FlipperPlugin {
/**
* @return The id of this plugin. This is the namespace which Flipper desktop plugins will call
* methods on to route them to your plugin. This should match the id specified in your React
* plugin.
*/
getId(): string;
/**
* Called when a connection has been established. The connection passed to this method is valid
* until {@link FlipperPlugin#onDisconnect()} is called.
*/
onConnect(connection: FlipperConnection): void;
/**
* Called when the connection passed to `FlipperPlugin#onConnect(FlipperConnection)` is no
* longer valid. Do not try to use the connection in or after this method has been called.
*/
onDisconnect(): void;
/**
* Returns true if the plugin is meant to be run in background too, otherwise it returns false.
*/
runInBackground(): boolean;
}
export interface FlipperResponder {
success(response?: any): void;
error(response: any): void;
}
export interface FlipperConnection {
send(method: string, data: any): void;
reportErrorWithMetadata(reason: string, stackTrace: string): void;
reportError(error: Error): void;
receive(
method: string,
listener: (params: any, responder: FlipperResponder) => void,
): void;
}
}
declare module 'Flipper' {
export function registerPlugin(
pluginId: string,
runInBackground: boolean,
onConnect: () => void,
onDisconnect: () => void,
): void;
export function send(pluginId: string, method: string, data: string): void;
export function reportErrorWithMetadata(
pluginId: string,
reason: string,
stackTrace: string,
): void;
export function reportError(pluginId: string, error: string): void;
export function subscribe(
pluginId: string,
method: string,
listener: (data: string, responderId: number) => void,
): void;
export function respondSuccess(responderId: string, data?: string): void;
export function respondError(responderId: string, error: string): void;
}
export function addPlugin(plugin: Flipper.FlipperPlugin): void;