Implement sending messages from desktop to add-on

Reviewed By: antonk52

Differential Revision: D34239803

fbshipit-source-id: 1a18d522ba877ade3d0703fbb374c15b596241d7
This commit is contained in:
Andrey Goncharov
2022-02-28 03:50:34 -08:00
committed by Facebook GitHub Bot
parent 2ce037d96b
commit 4067f5bd88
9 changed files with 54 additions and 7 deletions

View File

@@ -16,6 +16,11 @@ import {
export interface ServerAddOnControls { export interface ServerAddOnControls {
start: FlipperServerCommands['plugins-server-add-on-start']; start: FlipperServerCommands['plugins-server-add-on-start'];
stop: FlipperServerCommands['plugins-server-add-on-stop']; stop: FlipperServerCommands['plugins-server-add-on-stop'];
sendMessage: (
pluginName: string,
method: string,
params?: unknown,
) => Promise<object | string | number | boolean | null>;
} }
// TODO: Share with js-flipper? Is it worth it? // TODO: Share with js-flipper? Is it worth it?

View File

@@ -45,6 +45,7 @@ export {
isAuthError, isAuthError,
getStringFromErrorLike, getStringFromErrorLike,
getErrorFromErrorLike, getErrorFromErrorLike,
deserializeRemoteError,
} from './utils/errors'; } from './utils/errors';
export * from './GK'; export * from './GK';
export * from './clientUtils'; export * from './clientUtils';

View File

@@ -7,6 +7,8 @@
* @format * @format
*/ */
import {ClientErrorType} from '../server-types';
export function isAuthError( export function isAuthError(
err: any, err: any,
): err is UserNotSignedInError | UserUnauthorizedError { ): err is UserNotSignedInError | UserUnauthorizedError {
@@ -114,3 +116,10 @@ export function getStringFromErrorLike(e: any): string {
} }
} }
} }
export const deserializeRemoteError = (serializedError: ClientErrorType) => {
const err = new Error(serializedError.message);
err.name = serializedError.name;
err.stack += `. Caused by: ${serializedError.stacktrace}`;
return err;
};

View File

@@ -69,7 +69,7 @@ export class SandyDevicePluginInstance extends BasePluginInstance {
} }
/** client that is bound to this instance */ /** client that is bound to this instance */
readonly client: DevicePluginClient; readonly client: DevicePluginClient<any, any>;
constructor( constructor(
serverAddOnControls: ServerAddOnControls, serverAddOnControls: ServerAddOnControls,

View File

@@ -379,9 +379,13 @@ export abstract class BasePluginInstance {
this.flipperLib.showNotification(this.pluginKey, notification); this.flipperLib.showNotification(this.pluginKey, notification);
}, },
logger: this.flipperLib.logger, logger: this.flipperLib.logger,
sendToServerAddOn: (_method, _params): any => { sendToServerAddOn: (method, params) =>
// TODO: Implement me this.serverAddOnControls.sendMessage(
}, this.definition.packageName,
// TODO: Remove type cast
method as string,
params,
),
onServerAddOnMessage: (_event, _cb) => { onServerAddOnMessage: (_event, _cb) => {
// TODO: Implement me // TODO: Implement me
}, },

View File

@@ -647,5 +647,6 @@ function createServerAddOnControlsMock(): ServerAddOnControls {
return { return {
start: createStubFunction(), start: createStubFunction(),
stop: createStubFunction(), stop: createStubFunction(),
sendMessage: createStubFunction(),
}; };
} }

View File

@@ -64,7 +64,7 @@ export class ServerAddOn {
); );
const serverAddOnModuleToDesktopConnection = const serverAddOnModuleToDesktopConnection =
new ServerAddOnModuleToDesktopConnection(); new ServerAddOnModuleToDesktopConnection(pluginName);
const cleanup = await serverAddOn(serverAddOnModuleToDesktopConnection, { const cleanup = await serverAddOn(serverAddOnModuleToDesktopConnection, {
flipperServer, flipperServer,

View File

@@ -27,6 +27,10 @@ export class ServerAddOnModuleToDesktopConnection
{ {
private subscriptions: Map<string, FlipperPluginReceiver> = new Map(); private subscriptions: Map<string, FlipperPluginReceiver> = new Map();
constructor(private readonly pluginName: string) {
super();
}
send(method: string, params: unknown) { send(method: string, params: unknown) {
const event = 'message'; const event = 'message';
const message: ServerAddOnModuleToDesktopConnectionEvents[typeof event] = { const message: ServerAddOnModuleToDesktopConnectionEvents[typeof event] = {
@@ -34,7 +38,7 @@ export class ServerAddOnModuleToDesktopConnection
params: { params: {
method, method,
params, params,
api: '', // TODO: Consider using here pluginName and validate it api: this.pluginName,
}, },
}; };
this.emit('message', message); this.emit('message', message);

View File

@@ -7,7 +7,11 @@
* @format * @format
*/ */
import {FlipperServer, ServerAddOnControls} from 'flipper-common'; import {
FlipperServer,
ServerAddOnControls,
deserializeRemoteError,
} from 'flipper-common';
export const createServerAddOnControls = ( export const createServerAddOnControls = (
flipperServer: Pick<FlipperServer, 'exec'>, flipperServer: Pick<FlipperServer, 'exec'>,
@@ -16,4 +20,23 @@ export const createServerAddOnControls = (
flipperServer.exec('plugins-server-add-on-start', pluginName, owner), flipperServer.exec('plugins-server-add-on-start', pluginName, owner),
stop: (pluginName, owner) => stop: (pluginName, owner) =>
flipperServer.exec('plugins-server-add-on-stop', pluginName, owner), flipperServer.exec('plugins-server-add-on-stop', pluginName, owner),
sendMessage: async (pluginName, method, params) => {
const res = await flipperServer.exec(
'plugins-server-add-on-request-response',
{
method: 'execute',
params: {
method,
api: pluginName,
params,
},
},
);
if (res.error) {
throw deserializeRemoteError(res.error);
}
return res.success;
},
}); });