Files
flipper/js/js-flipper/src/connection.ts
Andres Suarez 79023ee190 Update copyright headers from Facebook to Meta
Reviewed By: bhamodi

Differential Revision: D33331422

fbshipit-source-id: 016e8dcc0c0c7f1fc353a348b54fda0d5e2ddc01
2021-12-27 14:31:45 -08:00

75 lines
2.2 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {FlipperErrorMessage, FlipperMessageBus} from './message';
import {FlipperPluginConnection, FlipperPluginReceiver} from './plugin';
import {FlipperResponder} from './responder';
import {isPromise, safeJSONStringify} from './util';
type FlipperReceiver = (data: unknown, responder: FlipperResponder) => void;
export class FlipperConnection implements FlipperPluginConnection {
pluginId: string;
private client: FlipperMessageBus;
private subscriptions: Map<string, FlipperReceiver> = new Map();
constructor(pluginId: string, client: FlipperMessageBus) {
this.pluginId = pluginId;
this.client = client;
}
send(method: string, params?: unknown) {
this.client.sendData({
method: 'execute',
params: {
api: this.pluginId,
method,
params,
},
});
}
receive(method: string, receiver: FlipperPluginReceiver) {
const wrappedReceiver: FlipperReceiver = (data, responder) => {
const handleError = (e: unknown) => {
const errorMessage: FlipperErrorMessage =
e instanceof Error
? {name: e.name, message: e.message, stacktrace: e.stack}
: {name: 'Unknown', message: safeJSONStringify(e)};
responder.error(errorMessage);
};
try {
const response = receiver(data);
if (isPromise(response)) {
response.then((data) => responder.success(data)).catch(handleError);
return;
}
responder.success(response);
} catch (e) {
handleError(e);
}
};
this.subscriptions.set(method, wrappedReceiver);
}
call(method: string, params: unknown, responder: FlipperResponder) {
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: string): boolean {
return this.subscriptions.has(method);
}
}