Skip to content

Commit

Permalink
fix: fork event should be disposable
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 5, 2022
1 parent 09fcf24 commit f969f9d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
12 changes: 8 additions & 4 deletions src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
}
Expand Down Expand Up @@ -221,5 +224,6 @@ export interface Events {
'fork': Plugin.Function
'dispose'(): Awaitable<void>
'service'(name: string, oldValue: any): void
'config'(state: Plugin.State, resolved: any, original: any): boolean | void
'hook'(name: string, listener: Function, prepend: boolean): () => boolean
}
9 changes: 5 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export namespace Registry {
export class Registry extends Map<Plugin, Plugin.Runtime> {
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 {
Expand Down
5 changes: 3 additions & 2 deletions tests/fork.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ describe('Fork', () => {
})
const plugin = {
using: ['foo'],
reusable: true,
apply: callback,
apply(ctx: Context) {
ctx.on('fork', callback)
},
}

app.plugin(plugin)
Expand Down
2 changes: 1 addition & 1 deletion tests/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {})
Expand Down

0 comments on commit f969f9d

Please sign in to comment.