refactor(loader): resolve config after injected services
This commit is contained in:
35
vendor/loader/src/config/entry.ts
vendored
35
vendor/loader/src/config/entry.ts
vendored
@@ -3,7 +3,13 @@ import { deepEqual, isNullable } from '@deepseek-ai/cosmokit'
|
||||
import { Loader } from '../index.ts'
|
||||
import { EntryGroup } from './group.ts'
|
||||
import { EntryTree } from './tree.ts'
|
||||
import { evaluate, interpolate } from './utils.ts'
|
||||
import { evaluate } from './utils.ts'
|
||||
|
||||
/** Static plugin hook for resolving a container config while preserving nested entry configs. */
|
||||
export const EntryConfigResolver = Symbol.for('cordis.loader.entry-config-resolver')
|
||||
|
||||
/** Resolver installed at {@link EntryConfigResolver}. */
|
||||
export type EntryConfigResolver = (ctx: Context, config: any) => any
|
||||
|
||||
/** Serialized plugin entry options stored in loader config files. */
|
||||
export interface EntryOptions {
|
||||
@@ -101,17 +107,12 @@ export class Entry {
|
||||
return evaluate(this.ctx, expr)
|
||||
}
|
||||
|
||||
_resolveConfig(plugin: any): [any, any?] {
|
||||
if (plugin[EntryGroup.key]) return this.options.config
|
||||
return interpolate(this.ctx, this.options.config)
|
||||
}
|
||||
|
||||
private async _patchContext(diff: string[]) {
|
||||
await this.context.waterfall('loader/patch-context', this, async () => {
|
||||
Object.setPrototypeOf(this.ctx, this.parent.ctx)
|
||||
|
||||
if (this.fiber?.uid && (diff.includes('config') || this.options.group)) {
|
||||
await this.fiber.update(this._resolveConfig(this.fiber.runtime!.callback), true)
|
||||
await this.fiber.update(this.options.config, true)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -258,7 +259,15 @@ export class Entry {
|
||||
this._initTask = undefined
|
||||
if (!this.loader.getTasks().length) this.ctx.reflect.notify(['loader'])
|
||||
}
|
||||
await this.fiber?.await()
|
||||
await this._await()
|
||||
}
|
||||
|
||||
async _await() {
|
||||
try {
|
||||
await this.fiber?.await()
|
||||
} catch (error) {
|
||||
throw updateError('apply', this.options, error)
|
||||
}
|
||||
}
|
||||
|
||||
private async _init() {
|
||||
@@ -278,17 +287,13 @@ export class Entry {
|
||||
private async _start(plugin: any) {
|
||||
let fiber: Fiber | undefined
|
||||
try {
|
||||
fiber = await this._create(plugin)
|
||||
await this._patchContext([])
|
||||
this.loader.showLog(this, 'apply')
|
||||
fiber = this.fiber = this.ctx.registry.plugin(plugin, this.options.config, this.getOuterStack)
|
||||
await fiber.await()
|
||||
} catch (error) {
|
||||
await this._dispose(fiber)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
private async _create(plugin: any): Promise<Fiber> {
|
||||
await this._patchContext([])
|
||||
this.loader.showLog(this, 'apply')
|
||||
return this.fiber = this.ctx.registry.plugin(plugin, this._resolveConfig(plugin), this.getOuterStack)
|
||||
}
|
||||
}
|
||||
|
||||
4
vendor/loader/src/config/group.ts
vendored
4
vendor/loader/src/config/group.ts
vendored
@@ -69,6 +69,10 @@ export class EntryGroup {
|
||||
|
||||
try {
|
||||
const outcomes = await Promise.allSettled(config.map(options => this.create(options)))
|
||||
// Disposal owns termination: sibling starts can still be settling after
|
||||
// the containing tree has gone away, but their failures no longer
|
||||
// describe a live update to roll back.
|
||||
if (this.ctx.fiber.uid === null) return
|
||||
const failures = outcomes
|
||||
.filter((outcome): outcome is PromiseRejectedResult => outcome.status === 'rejected')
|
||||
.map(outcome => outcome.reason)
|
||||
|
||||
2
vendor/loader/src/config/tree.ts
vendored
2
vendor/loader/src/config/tree.ts
vendored
@@ -51,7 +51,7 @@ export abstract class EntryTree {
|
||||
continue
|
||||
}
|
||||
const outcomes = await Promise.allSettled(
|
||||
[...this.entries()].map(entry => entry.fiber?.await()),
|
||||
[...this.entries()].map(entry => entry._await()),
|
||||
)
|
||||
const failures = outcomes
|
||||
.filter((outcome): outcome is PromiseRejectedResult => outcome.status === 'rejected')
|
||||
|
||||
Reference in New Issue
Block a user