UNPKG

3.79 kBTypeScriptView Raw
1import type { Api, EventKey, EventParams, MethodKey, MethodParams, EventCallback, ApiMap, Profile, PluginRequest, PluginApi, PluginBase, IPluginService } from '@remixproject/plugin-utils';
2export interface RequestParams {
3 name: string;
4 key: string;
5 payload: any[];
6}
7export interface PluginOptions {
8 /** The time to wait for a call to be executed before going to next call in the queue */
9 queueTimeout?: number;
10}
11export declare class Plugin<T extends Api = any, App extends ApiMap = any> implements PluginBase<T, App> {
12 profile: Profile<T>;
13 activateService: Record<string, () => Promise<any>>;
14 protected requestQueue: Array<() => Promise<any>>;
15 protected currentRequest: PluginRequest;
16 /** Give access to all the plugins registered by the engine */
17 protected app: PluginApi<App>;
18 protected options: PluginOptions;
19 onRegistration?(): void;
20 onActivation?(): void;
21 onDeactivation?(): void;
22 constructor(profile: Profile<T>);
23 get name(): string;
24 get methods(): Extract<keyof T['methods'], string>[];
25 set methods(methods: Extract<keyof T['methods'], string>[]);
26 activate(): any | Promise<any>;
27 deactivate(): any | Promise<any>;
28 setOptions(options?: Partial<PluginOptions>): void;
29 /** Call a method from this plugin */
30 protected callPluginMethod(key: string, args: any[]): any;
31 /** Add a request to the list of current requests */
32 protected addRequest(request: PluginRequest, method: Profile<T>['methods'][number], args: any[]): Promise<unknown>;
33 /**
34 * Ask the plugin manager if current request can call a specific method
35 * @param method The method to call
36 * @param message An optional message to show to the user
37 */
38 askUserPermission(method: MethodKey<T>, message?: string): Promise<boolean>;
39 /**
40 * Called by the engine when a plugin try to activate it
41 * @param from the profile of the plugin activating this plugin
42 * @param method method used to activate this plugin if any
43 */
44 canActivate(from: Profile, method?: string): Promise<boolean>;
45 /**
46 * Called by the engine when a plugin try to deactivate it
47 * @param from the profile of the plugin deactivating this plugin
48 */
49 canDeactivate(from: Profile): Promise<boolean>;
50 /**
51 * Create a service under the client node
52 * @param name The name of the service
53 * @param service The service
54 */
55 createService<S extends Record<string, any>>(name: string, service: S): Promise<IPluginService<S>>;
56 /**
57 * Prepare a service to be lazy loaded
58 * @param name The name of the subservice inside this service
59 * @param factory A function to create the service on demand
60 */
61 prepareService<S extends Record<string, any>>(name: string, factory: () => S): () => Promise<IPluginService<S>>;
62 /** Listen on an event from another plugin */
63 on<Name extends Extract<keyof App, string>, Key extends EventKey<App[Name]>>(name: Name, key: Key, cb: EventCallback<App[Name], Key>): void;
64 /** Listen once an event from another plugin then remove event listener */
65 once<Name extends Extract<keyof App, string>, Key extends EventKey<App[Name]>>(name: Name, key: Key, cb: EventCallback<App[Name], Key>): void;
66 /** Stop listening on an event from another plugin */
67 off<Name extends Extract<keyof App, string>, Key extends EventKey<App[Name]>>(name: Name, key: Key): void;
68 /** Call a method of another plugin */
69 call<Name extends Extract<keyof App, string>, Key extends MethodKey<App[Name]>>(name: Name, key: Key, ...payload: MethodParams<App[Name], Key>): Promise<ReturnType<App[Name]['methods'][Key]>>;
70 /** Emit an event */
71 emit<Key extends EventKey<T>>(key: Key, ...payload: EventParams<T, Key>): void;
72}