diff --git a/src/lifecycle.ts b/src/lifecycle.ts index 1c1429c..970b2e7 100644 --- a/src/lifecycle.ts +++ b/src/lifecycle.ts @@ -41,15 +41,18 @@ export class Lifecycle { constructor(private ctx: Context, private config: Lifecycle.Config) { (this as Lifecycle.Delegates).on('hook', (name, listener, prepend) => { const method = prepend ? 'unshift' : 'push' + const { runtime, disposables } = this.caller.state if (name === 'ready' && this.isActive) { this.queue(listener()) return () => false } else if (name === 'dispose') { - this.caller.state.disposables[method](listener as any) - return () => remove(this.caller.state.disposables, listener) + disposables[method](listener as any) + return () => remove(disposables, listener) } else if (name === 'fork') { - this.caller.state.runtime.forkers[method](listener as any) - return () => remove(this.caller.state.runtime.forkers, listener) + runtime.forkers[method](listener as any) + const dispose = () => remove(runtime.forkers, listener) + disposables.push(dispose) + return dispose } }) } @@ -221,5 +224,6 @@ export interface Events { 'fork': Plugin.Function 'dispose'(): Awaitable 'service'(name: string, oldValue: any): void + 'config'(state: Plugin.State, resolved: any, original: any): boolean | void 'hook'(name: string, listener: Function, prepend: boolean): () => boolean } diff --git a/src/plugin.ts b/src/plugin.ts index db121de..7e768b2 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -46,8 +46,9 @@ export namespace Plugin { } update(config: any) { - config = Registry.validate(this.runtime.plugin, config) - this.config = config + const resolved = Registry.validate(this.runtime.plugin, config) + if (this.context.bail('config', this, resolved, config)) return + this.config = resolved this.restart() } @@ -169,8 +170,8 @@ export namespace Plugin { this.apply(this.context, this.config) } - for (const state of this.children) { - state.restart() + for (const fork of this.children) { + fork.restart() } } } diff --git a/src/registry.ts b/src/registry.ts index 8a6c385..18b7cf8 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -19,7 +19,7 @@ export namespace Registry { export class Registry extends Map { constructor(public app: App, private config: Registry.Config) { super() - app.state = new Plugin.Runtime(this, null, null) + app.state = new Plugin.Runtime(this, null, config) } get caller(): Context { diff --git a/tests/fork.spec.ts b/tests/fork.spec.ts index 348fcc1..804efe9 100644 --- a/tests/fork.spec.ts +++ b/tests/fork.spec.ts @@ -92,8 +92,9 @@ describe('Fork', () => { }) const plugin = { using: ['foo'], - reusable: true, - apply: callback, + apply(ctx: Context) { + ctx.on('fork', callback) + }, } app.plugin(plugin) diff --git a/tests/update.spec.ts b/tests/update.spec.ts index 88ed794..e3fbad3 100644 --- a/tests/update.spec.ts +++ b/tests/update.spec.ts @@ -3,7 +3,7 @@ import { expect } from 'chai' import { event } from './shared' import * as jest from 'jest-mock' -describe('Config', () => { +describe('Update', () => { it('basic support', () => { const app = new App() const dispose = jest.fn(() => {})