feat(lib/plugins)!: bind lifecycle this to plugin
This commit is contained in:
parent
a843805456
commit
c8906b442a
2 changed files with 19 additions and 11 deletions
|
|
@ -404,7 +404,9 @@ export async function preInitPlugin(plugin: AnyPlugin) {
|
|||
plugin.status |= Status.PreIniting
|
||||
|
||||
try {
|
||||
const prom = lifecycles.preInit(plugin.api as PreInitPluginApi)
|
||||
const prom = lifecycles.preInit.apply(plugin, [
|
||||
plugin.api as PreInitPluginApi,
|
||||
])
|
||||
promises.push(prom)
|
||||
await prom
|
||||
} catch (e) {
|
||||
|
|
@ -447,7 +449,9 @@ export async function initPlugin(plugin: AnyPlugin) {
|
|||
plugin.status |= Status.Initing
|
||||
|
||||
try {
|
||||
const prom = lifecycles.init(plugin.api as InitPluginApi)
|
||||
const prom = lifecycles.init.apply(plugin, [
|
||||
plugin.api as InitPluginApi,
|
||||
])
|
||||
promises.push(prom)
|
||||
await prom
|
||||
} catch (e) {
|
||||
|
|
@ -489,7 +493,9 @@ export async function startPlugin(plugin: AnyPlugin) {
|
|||
plugin.status |= Status.Starting
|
||||
|
||||
try {
|
||||
const prom = lifecycles.start(plugin.api as PluginApi)
|
||||
const prom = lifecycles.start.apply(plugin, [
|
||||
plugin.api as PluginApi,
|
||||
])
|
||||
promises.push(prom)
|
||||
await prom
|
||||
} catch (e) {
|
||||
|
|
@ -548,7 +554,7 @@ export async function stopPlugin(plugin: AnyPlugin) {
|
|||
try {
|
||||
if (lifecycles.stop)
|
||||
await Promise.race([
|
||||
lifecycles.stop(plugin.api as PluginApi),
|
||||
lifecycles.stop.apply(plugin, [plugin.api as PluginApi]),
|
||||
sleepReject(
|
||||
MaxWaitTime,
|
||||
'Plugin stop lifecycle timed out, force stopping',
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ export interface PreInitPluginApi<
|
|||
decorate: PluginDecorateApi<O, 'PreInit'>
|
||||
unscoped: UnscopedPreInitPluginApi
|
||||
cleanup: PluginCleanupApi
|
||||
plugin: Plugin
|
||||
plugin: Plugin<O, 'PreInit'>
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -118,6 +118,7 @@ export interface InitPluginApi<
|
|||
> extends PreInitPluginApi<O> {
|
||||
decorate: PluginDecorateApi<O, 'Init'>
|
||||
unscoped: UnscopedInitPluginApi
|
||||
plugin: Plugin<O, 'Init'>
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -130,6 +131,7 @@ export interface PluginApi<
|
|||
> extends InitPluginApi<O> {
|
||||
decorate: PluginDecorateApi<O, 'Start'>
|
||||
unscoped: UnscopedPluginApi
|
||||
plugin: Plugin<O, 'Start'>
|
||||
}
|
||||
|
||||
// TODO(plugins): support plugin bundles
|
||||
|
|
@ -208,26 +210,26 @@ export interface PluginLifecycles<
|
|||
*
|
||||
* @param api Plugin API (very limited).
|
||||
*/
|
||||
preInit?: (api: PreInitPluginApi<O>) => any
|
||||
preInit?: (this: Plugin<O, 'PreInit'>, api: PreInitPluginApi<O>) => any
|
||||
/**
|
||||
* Runs as soon as all important modules are initialized.
|
||||
* After the index module (module 0)'s factory is run.
|
||||
*
|
||||
* @param api Plugin API (limited).
|
||||
*/
|
||||
init?: (api: InitPluginApi<O>) => any
|
||||
init?: (this: Plugin<O, 'Init'>, api: InitPluginApi<O>) => any
|
||||
/**
|
||||
* Runs when the plugin can be started with all APIs available.
|
||||
*
|
||||
* @param api Plugin API.
|
||||
*/
|
||||
start?: (api: PluginApi<O>) => any
|
||||
start?: (this: Plugin<O, 'Start'>, api: PluginApi<O>) => any
|
||||
/**
|
||||
* Runs when the plugin is stopped.
|
||||
*
|
||||
* @param api Plugin API.
|
||||
*/
|
||||
stop?: (api: PluginApi<O>) => any
|
||||
stop?: (this: Plugin<O, 'Start'>, api: PluginApi<O>) => any
|
||||
}
|
||||
|
||||
export interface Plugin<
|
||||
|
|
@ -262,11 +264,11 @@ export interface Plugin<
|
|||
* Disable the plugin.
|
||||
* This will also stop the plugin if it is running.
|
||||
*/
|
||||
disable(): Promise<void>
|
||||
disable(this: Plugin<O, S>): Promise<void>
|
||||
/**
|
||||
* Stop the plugin.
|
||||
*/
|
||||
stop(): Promise<void>
|
||||
stop(this: Plugin<O, S>): Promise<void>
|
||||
|
||||
/**
|
||||
* The plugin API.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue