flipper-client-sdk
Version:
SDK to build Flipper clients for JS based apps
162 lines • 5.85 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlipperClient = exports.FlipperConnection = exports.FlipperResponder = void 0;
class FlipperResponder {
constructor(messageID, client) {
this.messageID = messageID;
this.client = client;
}
success(response) {
this.client.sendData({ id: this.messageID, success: response });
}
error(response) {
this.client.sendData({ id: this.messageID, error: response });
}
}
exports.FlipperResponder = FlipperResponder;
class FlipperConnection {
constructor(pluginId, client) {
this.subscriptions = new Map();
this.pluginId = pluginId;
this.client = client;
}
send(method, params) {
this.client.sendData({
method: 'execute',
params: {
api: this.pluginId,
method,
params,
},
});
}
receive(method, receiver) {
this.subscriptions.set(method, receiver);
}
call(method, params, responder) {
const receiver = this.subscriptions.get(method);
if (receiver == null) {
const errorMessage = `Receiver ${method} not found.`;
responder.error({ message: errorMessage });
return;
}
receiver.call(receiver, params, responder);
}
hasReceiver(method) {
return this.subscriptions.has(method);
}
}
exports.FlipperConnection = FlipperConnection;
class FlipperClient {
constructor() {
this._isConnected = false;
this.plugins = new Map();
this.connections = new Map();
}
addPlugin(plugin) {
if (this._isConnected) {
this.connectPlugin(plugin);
}
this.plugins.set(plugin.getId(), plugin);
}
getPlugin(id) {
return this.plugins.get(id);
}
onConnect() {
if (this._isConnected) {
return;
}
this._isConnected = true;
}
onDisconnect() {
this._isConnected = false;
for (const plugin of this.plugins.values()) {
this.disconnectPlugin(plugin);
}
}
onMessageReceived(message) {
let responder;
try {
const { method, params, id } = message;
responder = new FlipperResponder(id, this);
if (method === 'getPlugins') {
responder.success({ plugins: [...this.plugins.keys()] });
return;
}
if (method === 'getBackgroundPlugins') {
responder.success({
plugins: [...this.plugins.keys()].filter((key) => { var _a; return (_a = this.plugins.get(key)) === null || _a === void 0 ? void 0 : _a.runInBackground(); }),
});
return;
}
if (method === 'init') {
const identifier = params['plugin'];
const plugin = this.plugins.get(identifier);
if (plugin == null) {
const errorMessage = `Plugin ${identifier} not found for method ${method}`;
responder.error({ message: errorMessage, name: 'PluginNotFound' });
return;
}
this.connectPlugin(plugin);
return;
}
if (method === 'deinit') {
const identifier = params['plugin'];
const plugin = this.plugins.get(identifier);
if (plugin == null) {
const errorMessage = `Plugin ${identifier} not found for method ${method}`;
responder.error({ message: errorMessage, name: 'PluginNotFound' });
return;
}
this.disconnectPlugin(plugin);
return;
}
if (method === 'execute') {
const identifier = params['api'];
const connection = this.connections.get(identifier);
if (connection == null) {
const errorMessage = `Connection ${identifier} not found for plugin identifier`;
responder.error({ message: errorMessage, name: 'ConnectionNotFound' });
return;
}
connection.call(params['method'], params['params'], responder);
return;
}
if (method === 'isMethodSupported') {
const identifier = params['api'].getString();
const connection = this.connections.get(identifier);
if (connection == null) {
const errorMessage = `Connection ${identifier} not found for plugin identifier`;
responder.error({ message: errorMessage, name: 'ConnectionNotFound' });
return;
}
const isSupported = connection.hasReceiver(params['method'].getString());
responder.success({ isSupported: isSupported });
return;
}
const response = { message: 'Received unknown method: ' + method };
responder.error(response);
}
catch (e) {
if (responder) {
responder.error({
message: 'Unknown error during ' + JSON.stringify(message),
name: 'Unknown',
});
}
}
}
connectPlugin(plugin) {
const id = plugin.getId();
const connection = new FlipperConnection(id, this);
plugin.onConnect(connection);
this.connections.set(id, connection);
}
disconnectPlugin(plugin) {
const id = plugin.getId();
plugin.onDisconnect();
this.connections.delete(id);
}
}
exports.FlipperClient = FlipperClient;
//# sourceMappingURL=api.js.map