Skip to content

Commit 4c7f8d3

Browse files
committed
fix: self update / loader update
1 parent a95c552 commit 4c7f8d3

File tree

5 files changed

+21
-22
lines changed

5 files changed

+21
-22
lines changed

packages/core/src/events.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ export class EventsService<C extends Context = Context> {
6969
})
7070
}
7171

72-
this.on('internal/update', function (config, _next) {
72+
this.on('internal/update', function (config, _, next) {
7373
const cbs = [...this._hooks['internal/update'] || []]
74-
const next = () => {
75-
const cb = cbs.shift() ?? _next
76-
return cb.call(this, config, next)
74+
const _next = () => {
75+
const cb = cbs.shift() ?? next
76+
return cb.call(this, config, _next)
7777
}
78-
return next()
78+
return _next()
7979
}, { global: true, prepend: true })
8080
}
8181

@@ -172,7 +172,7 @@ export interface Events<in C extends Context = Context> {
172172
'internal/error'(this: C, format: any, ...param: any[]): void
173173
'internal/warn'(this: C, format: any, ...param: any[]): void
174174
'internal/service'(this: C, name: string, value: any): void
175-
'internal/update'(this: Fiber<C>, config: any, next: () => void): void
175+
'internal/update'(this: Fiber<C>, config: any, noSave: boolean, next: () => void): void
176176
'internal/get'(ctx: C, name: string, error: Error, next: () => any): any
177177
'internal/set'(ctx: C, name: string, value: any, error: Error, next: () => boolean): boolean
178178
'internal/listener'(this: C, name: string, listener: any, prepend: boolean): void

packages/core/src/fiber.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ export class Fiber<out C extends Context = Context> {
430430
await this.await()
431431
}
432432

433-
update(config: any) {
433+
update(config: any, noSave = false) {
434434
this.assertActive()
435435
config = resolveConfig(this.runtime!, config)
436-
this.context.waterfall(this, 'internal/update', config, () => {
436+
this.context.waterfall(this, 'internal/update', config, noSave, () => {
437437
this.config = config
438438
this._error = undefined
439439
return this.restart()

packages/loader/src/config/entry.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export class Entry<C extends Context = Context> {
3636

3737
public ctx: C
3838
public fiber?: Fiber<C>
39-
public suspend = false
4039
public parent!: EntryGroup<C>
4140
// safety: call `entry.update()` immediately after creating an entry
4241
public options = {} as EntryOptions
@@ -87,8 +86,7 @@ export class Entry<C extends Context = Context> {
8786
Object.setPrototypeOf(this.ctx, this.parent.ctx)
8887

8988
if (this.fiber?.uid && (diff.includes('config') || this.options.group)) {
90-
this.suspend = true
91-
this.fiber.update(this._resolveConfig(this.fiber.runtime!.callback))
89+
this.fiber.update(this._resolveConfig(this.fiber.runtime!.callback), true)
9290
}
9391
})
9492
}

packages/loader/src/loader.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,16 @@ export abstract class Loader<C extends Context = Context> extends ImportTree<C>
7777

7878
ctx.reflect.provide('loader', this, this[Service.check])
7979

80-
ctx.on('internal/update', function (config, next) {
80+
ctx.on('internal/update', function (config, noSave, next) {
81+
if (!this.entry || noSave) return next()
82+
const unparse = this.runtime?.Config?.['simplify']
83+
this.entry.options.config = unparse ? unparse(config) : config
84+
this.entry.parent.tree.write()
85+
return next()
86+
}, { global: true, prepend: true })
87+
88+
ctx.on('internal/update', function (config, _, next) {
8189
if (!this.entry) return next()
82-
if (this.entry.suspend) {
83-
this.entry.suspend = false
84-
} else {
85-
const unparse = this.runtime?.Config?.['simplify']
86-
this.entry.options.config = unparse ? unparse(config) : config
87-
this.entry.parent.tree.write()
88-
}
8990
self.showLog(this.entry, 'reload')
9091
return next()
9192
}, { global: true })

packages/loader/tests/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ describe('Loader: basic support', () => {
1515
await root.plugin(MockLoader)
1616
loader = root.loader as any
1717

18-
foo = loader.mock('foo', (ctx: Context) => ctx.on('internal/update', () => true))
19-
bar = loader.mock('bar', (ctx: Context) => ctx.on('internal/update', () => true))
20-
qux = loader.mock('qux', (ctx: Context) => ctx.on('internal/update', () => true))
18+
foo = loader.mock('foo', (ctx: Context) => ctx.on('internal/update', () => {}))
19+
bar = loader.mock('bar', (ctx: Context) => ctx.on('internal/update', () => {}))
20+
qux = loader.mock('qux', (ctx: Context) => ctx.on('internal/update', () => {}))
2121
})
2222

2323
it('loader initiate', async () => {

0 commit comments

Comments
 (0)