Allow plugins to send messages
Summary: Sandy plugins can now send messages to plugins. The methods and params are strongly typed in implementation and unit tests, based on the <Methods> generic of FlipperClient. Reviewed By: nikoant Differential Revision: D22256972 fbshipit-source-id: 549523a402949b3eb6bb4b4ca160dedb5c5e722d
This commit is contained in:
committed by
Facebook GitHub Bot
parent
8b2d8498e6
commit
ec85dd5b01
@@ -39,6 +39,14 @@ export interface FlipperClient<
|
||||
* - when the plugin is disabled
|
||||
*/
|
||||
onDisconnect(cb: () => void): void;
|
||||
|
||||
/**
|
||||
* Send a message to the connected client
|
||||
*/
|
||||
send<Method extends keyof Methods>(
|
||||
method: Method,
|
||||
params: Parameters<Methods[Method]>[0],
|
||||
): ReturnType<Methods[Method]>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,6 +57,12 @@ export interface RealFlipperClient {
|
||||
isBackgroundPlugin(pluginId: string): boolean;
|
||||
initPlugin(pluginId: string): void;
|
||||
deinitPlugin(pluginId: string): void;
|
||||
call(
|
||||
api: string,
|
||||
method: string,
|
||||
fromPlugin: boolean,
|
||||
params?: Object,
|
||||
): Promise<Object>;
|
||||
}
|
||||
|
||||
export type FlipperPluginFactory<
|
||||
@@ -88,6 +102,15 @@ export class SandyPluginInstance {
|
||||
onDisconnect: (cb) => {
|
||||
this.events.on('disconnect', cb);
|
||||
},
|
||||
send: async (method, params) => {
|
||||
this.assertConnected();
|
||||
return await realClient.call(
|
||||
this.definition.id,
|
||||
method as any,
|
||||
true,
|
||||
params as any,
|
||||
);
|
||||
},
|
||||
};
|
||||
this.instanceApi = definition.module.plugin(this.client);
|
||||
}
|
||||
@@ -143,4 +166,11 @@ export class SandyPluginInstance {
|
||||
throw new Error('Plugin has been destroyed already');
|
||||
}
|
||||
}
|
||||
|
||||
private assertConnected() {
|
||||
this.assertNotDestroyed();
|
||||
if (!this.connected) {
|
||||
throw new Error('Plugin is not connected');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user