Support supportsMethod in FlipperPlugin

Summary: `supportsMethod` wasn't implemented in the new sandy APIs so far, but is needed for D24108772

Reviewed By: cekkaewnumchai

Differential Revision: D24109496

fbshipit-source-id: 694344b423c1805baa193e4f2e1ad5f28483378b
This commit is contained in:
Michel Weststrate
2020-10-05 13:32:03 -07:00
committed by Facebook GitHub Bot
parent e40eb2dadd
commit 7b758d2697
3 changed files with 61 additions and 0 deletions

View File

@@ -346,3 +346,46 @@ test('plugins can create pastes', async () => {
plugin.instance.doIt(); plugin.instance.doIt();
expect(plugin.flipperLib.createPaste).toBeCalledWith('test'); expect(plugin.flipperLib.createPaste).toBeCalledWith('test');
}); });
test('plugins support all methods by default', async () => {
type Methods = {
doit(): Promise<boolean>;
};
const plugin = TestUtils.startPlugin({
plugin(client: PluginClient<{}, Methods>) {
return {
async checkEnabled() {
return client.supportsMethod('doit');
},
};
},
Component() {
return null;
},
});
expect(await plugin.instance.checkEnabled()).toBeTruthy();
});
test('available methods can be overridden', async () => {
type Methods = {
doit(): Promise<boolean>;
};
const plugin = TestUtils.startPlugin(
{
plugin(client: PluginClient<{}, Methods>) {
return {
async checkEnabled() {
return client.supportsMethod('doit');
},
};
},
Component() {
return null;
},
},
{
unsupportedMethods: ['doit'],
},
);
expect(await plugin.instance.checkEnabled()).toBeFalsy();
});

View File

@@ -59,6 +59,11 @@ export interface PluginClient<
method: Method, method: Method,
params: Parameters<Methods[Method]>[0], params: Parameters<Methods[Method]>[0],
): ReturnType<Methods[Method]>; ): ReturnType<Methods[Method]>;
/**
* Checks if a method is available on the client implementation
*/
supportsMethod(method: keyof Methods): Promise<boolean>;
} }
/** /**
@@ -75,6 +80,7 @@ export interface RealFlipperClient {
fromPlugin: boolean, fromPlugin: boolean,
params?: Object, params?: Object,
): Promise<Object>; ): Promise<Object>;
supportsMethod(api: string, method: string): Promise<boolean>;
} }
export type PluginFactory< export type PluginFactory<
@@ -125,6 +131,13 @@ export class SandyPluginInstance extends BasePluginInstance {
onMessage: (event, callback) => { onMessage: (event, callback) => {
this.events.on('event-' + event, callback); this.events.on('event-' + event, callback);
}, },
supportsMethod: async (method) => {
this.assertConnected();
return await realClient.supportsMethod(
this.definition.id,
method as any,
);
},
}; };
this.initializePlugin(() => this.initializePlugin(() =>
definition.asPluginModule().plugin(this.client), definition.asPluginModule().plugin(this.client),

View File

@@ -43,6 +43,8 @@ interface StartPluginOptions {
initialState?: Record<string, any>; initialState?: Record<string, any>;
isArchived?: boolean; isArchived?: boolean;
isBackgroundPlugin?: boolean; isBackgroundPlugin?: boolean;
/** Provide a set of unsupported methods to simulate older clients that don't support certain methods yet */
unsupportedMethods?: string[];
} }
type ExtractClientType<Module extends FlipperPluginModule<any>> = Parameters< type ExtractClientType<Module extends FlipperPluginModule<any>> = Parameters<
@@ -195,6 +197,9 @@ export function startPlugin<Module extends FlipperPluginModule<any>>(
): Promise<Object> { ): Promise<Object> {
return sendStub(method, params); return sendStub(method, params);
}, },
async supportsMethod(_api: string, method: string) {
return !options?.unsupportedMethods?.includes(method);
},
}; };
const pluginInstance = new SandyPluginInstance( const pluginInstance = new SandyPluginInstance(