import type { PluginMethodParameters, PluginMethods, PluginOptionsType } from "plugin"; import type { ClientInstance } from "client"; export class Plugin { public name: string; public data: PluginData; public client: Client | undefined = undefined; private pluginMethods: Partial> = {}; constructor(public config: PluginOptionsType) { this.name = config.name; if (config.data) { this.data = config.data; } } initialize = (client: Client) => { this.client = client; return this; }; trigger = >(method: Key, data: PluginMethodParameters) => { const callback = this.pluginMethods[method]; if (callback) { callback(data as any); } }; /* ------------------------------------------------------------------------------------------------- * Plugin lifecycle * -----------------------------------------------------------------------------------------------*/ /** * Callback that will be executed when plugin is mounted */ onMount = (callback: PluginMethods["onMount"]) => { this.pluginMethods.onMount = callback; return this; }; /** * Callback that will be executed when plugin is unmounted */ onUnmount = (callback: PluginMethods["onUnmount"]) => { this.pluginMethods.onUnmount = callback; return this; }; /* ------------------------------------------------------------------------------------------------- * Request lifecycle * -----------------------------------------------------------------------------------------------*/ /** * Callback that will be executed when request is created */ onRequestCreate = (callback: PluginMethods["onRequestCreate"]) => { this.pluginMethods.onRequestCreate = callback; return this; }; /** * Callback that will be executed when request gets triggered */ onRequestTrigger = (callback: PluginMethods["onRequestTrigger"]) => { this.pluginMethods.onRequestTrigger = callback; return this; }; /** * Callback that will be executed when request starts */ onRequestStart = (callback: PluginMethods["onRequestStart"]) => { this.pluginMethods.onRequestStart = callback; return this; }; /** * Callback that will be executed when response is successful */ onRequestSuccess = (callback: PluginMethods["onRequestSuccess"]) => { this.pluginMethods.onRequestSuccess = callback; return this; }; /** * Callback that will be executed when response is failed */ onRequestError = (callback: PluginMethods["onRequestError"]) => { this.pluginMethods.onRequestError = callback; return this; }; /** * Callback that will be executed when response is finished */ onRequestFinished = (callback: PluginMethods["onRequestFinished"]) => { this.pluginMethods.onRequestFinished = callback; return this; }; /* ------------------------------------------------------------------------------------------------- * Dispatcher lifecycle * -----------------------------------------------------------------------------------------------*/ onDispatcherCleared = (callback: PluginMethods["onDispatcherCleared"]) => { this.pluginMethods.onDispatcherCleared = callback; return this; }; onDispatcherQueueDrained = (callback: PluginMethods["onDispatcherQueueDrained"]) => { this.pluginMethods.onDispatcherQueueDrained = callback; return this; }; onDispatcherQueueRunning = (callback: PluginMethods["onDispatcherQueueRunning"]) => { this.pluginMethods.onDispatcherQueueRunning = callback; return this; }; onDispatcherItemAdded = (callback: PluginMethods["onDispatcherItemAdded"]) => { this.pluginMethods.onDispatcherItemAdded = callback; return this; }; onDispatcherItemDeleted = (callback: PluginMethods["onDispatcherItemDeleted"]) => { this.pluginMethods.onDispatcherItemDeleted = callback; return this; }; onDispatcherQueueCreated = (callback: PluginMethods["onDispatcherQueueCreated"]) => { this.pluginMethods.onDispatcherQueueCreated = callback; return this; }; onDispatcherQueueCleared = (callback: PluginMethods["onDispatcherQueueCleared"]) => { this.pluginMethods.onDispatcherQueueCleared = callback; return this; }; /* ------------------------------------------------------------------------------------------------- * Cache lifecycle * -----------------------------------------------------------------------------------------------*/ onCacheItemChange = (callback: PluginMethods["onCacheItemChange"]) => { this.pluginMethods.onCacheItemChange = callback; return this; }; onCacheItemDelete = (callback: PluginMethods["onCacheItemDelete"]) => { this.pluginMethods.onCacheItemDelete = callback; return this; }; /* ------------------------------------------------------------------------------------------------- * Adapter lifecycle * -----------------------------------------------------------------------------------------------*/ onAdapterFetch = (callback: PluginMethods["onAdapterFetch"]) => { this.pluginMethods.onAdapterFetch = callback; return this; }; }