@deepseek-ai/cordis-plugin-loader 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,10 +15,7 @@ export declare abstract class EntryTree {
15
15
  entries(): Generator<Entry, void, void>;
16
16
  /** Return pending import and lifecycle tasks owned by this tree. */
17
17
  getTasks(): Promise<void>[];
18
- /**
19
- * Wait until this tree has no active import or lifecycle tasks.
20
- * @throws a settled fiber failure, or an aggregate when several fibers failed.
21
- */
18
+ /** Wait until this tree has no pending import or lifecycle tasks. */
22
19
  await(): Promise<void>;
23
20
  ensureId(options: Partial<EntryOptions>): string;
24
21
  /** Resolve an entry by id, including nested ids separated by `EntryTree.sep`. */
@@ -27,7 +24,7 @@ export declare abstract class EntryTree {
27
24
  /** Create an entry in the root group or a nested group. */
28
25
  create(options: Omit<EntryOptions, 'id'>, parent?: string | null, position?: number): Promise<string>;
29
26
  /** Stop and remove an entry from its parent group. */
30
- remove(id: string): Promise<void>;
27
+ remove(id: string): void;
31
28
  /** Update an entry and optionally move it to another group. */
32
29
  update(id: string, options: Omit<EntryOptions, 'id' | 'name'>, parent?: string | null, position?: number): Promise<void>;
33
30
  /** Import a plugin module from a specifier or `cordis:` builtin. */
@@ -1 +1 @@
1
- {"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../../src/config/tree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC3D,OAAO,EAAiB,KAAK,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAChE,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEvC,6EAA6E;AAC7E,8BAAsB,SAAS;IAC7B,MAAM,CAAC,QAAQ,CAAC,GAAG,OAAM;IAElB,GAAG,EAAE,OAAO,CAAA;IACZ,UAAU,CAAC,EAAE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const evaluate: ((ctx: object, expr: string) => any);
3
- /** Recursively replace YAML `!js` expression nodes with evaluated values. */
3
+ /** Recursively replace YAML `!!js` expression nodes with evaluated values. */
4
4
  export declare function interpolate(ctx: object, value: any): any;
5
5
  /** Return true when a value is a serialized loader JavaScript expression. */
6
6
  export declare function isJsExpr(value: any): value is JsExpr;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/config/utils.ts"],"names":[],"mappings":"AAGA,uEAAuE;AACvE,eAAO,MAAM,QAAQ,EAIf,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,GAAG,CAAC,CAAA;AAE1C,6EAA6E;AAC7E,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,OAUlD;AAED,6EAA6E;AAC7E,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,MAAM,CAEpD;AAED,yEAAyE;AACzE,MAAM,WAAW,MAAM;IACrB,QAAQ,EAAE,MAAM,CAAA;CACjB"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/config/utils.ts"],"names":[],"mappings":"AAGA,uEAAuE;AACvE,eAAO,MAAM,QAAQ,EAIf,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,GAAG,CAAC,CAAA;AAE1C,8EAA8E;AAC9E,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,OAUlD;AAED,6EAA6E;AAC7E,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,MAAM,CAEpD;AAED,yEAAyE;AACzE,MAAM,WAAW,MAAM;IACrB,QAAQ,EAAE,MAAM,CAAA;CACjB"}
@@ -21,7 +21,19 @@ declare module '@deepseek-ai/cordis' {
21
21
  'loader/config-update'(): void;
22
22
  'loader/entry-init'(entry: Entry): void;
23
23
  'loader/partial-dispose'(entry: Entry, legacy: Partial<EntryOptions>, active: boolean): void;
24
- 'loader/patch-context'(entry: Entry, next: () => void | Promise<void>): void | Promise<void>;
24
+ /**
25
+ * Volatile config values were committed into the running fiber without a remount; dispatched to the owning fiber only.
26
+ * @param paths - changed config paths as key arrays; every value is committed before dispatch.
27
+ * @mode emit
28
+ */
29
+ 'loader/volatile-update'(paths: readonly (readonly string[])[]): void;
30
+ /**
31
+ * Refresh entry context before applying config.
32
+ * @param entry - entry containing the new raw config and optional current fiber.
33
+ * @param next - continue context refresh and Loader's config update.
34
+ * @mode waterfall
35
+ */
36
+ 'loader/patch-context'(entry: Entry, next: () => void): void;
25
37
  }
26
38
  interface Context {
27
39
  loader: Loader;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAsB,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,qBAAqB,CAAA;AACtF,OAAO,EAA8B,KAAK,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAC7E,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAG5D,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAG5C,iCAAiC;AACjC,cAAc,mBAAmB,CAAA;AACjC,yCAAyC;AACzC,cAAc,mBAAmB,CAAA;AACjC,2CAA2C;AAC3C,cAAc,qBAAqB,CAAA;AACnC,6CAA6C;AAC7C,cAAc,kBAAkB,CAAA;AAChC,kDAAkD;AAClD,cAAc,mBAAmB,CAAA;AACjC,iEAAiE;AACjE,cAAc,eAAe,CAAA;AAE7B,OAAO,QAAQ,qBAAqB,CAAC;IACnC,UAAU,MAAM;QACd,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;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sBAAsB,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,GAAG,IAAI,CAAA;KAC7D;IAED,UAAU,OAAO;QACf,MAAM,EAAE,MAAM,CAAA;KACf;IAED,UAAU,OAAO;QACf,SAAS,CAAC,EAAE,MAAM,CAAA;KACnB;IAED,UAAU,KAAK;QACb,KAAK,CAAC,EAAE,KAAK,CAAA;KACd;CACF;AAED,wDAAwD;AACxD,yBAAiB,MAAM,CAAC;IACtB,iCAAiC;IACjC,UAAiB,MAAM;QACrB,4EAA4E;QAC5E,OAAO,CAAC,EAAE,MAAM,CAAA;KACjB;IAED,mEAAmE;IACnE,UAAiB,SAAS;QACxB,6EAA6E;QAC7E,KAAK,CAAC,EAAE,OAAO,CAAA;KAChB;CACF;AAED;;;;GAIG;AACH,qBAAa,MAAO,SAAQ,SAAS;IAYF,MAAM,EAAE,MAAM,CAAC,MAAM;IAX9C,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,SAAS,CAAA;IAEnC,OAAO,MAEe;IAEtB,IAAI,SAAW;IACf,QAAQ,2BAA8B;IAEtC,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC,CAAsB;gBAEpC,GAAG,EAAE,OAAO,EAAS,MAAM,GAAE,MAAM,CAAC,MAAW;IAkF3D,KAAK;IAIL,CAAC,OAAO,CAAC,KAAK,CAAC;IAMf,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM;IAKlC,4DAA4D;IAC5D,MAAM,CAAC,KAAK,QAAiB;IAS7B,2EAA2E;IAC3E,IAAI;IAGJ,wEAAwE;IACxE,aAAa,CAAC,OAAO,EAAE,GAAG;CAQ3B;AAED,eAAe,MAAM,CAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@deepseek-ai/cordis-plugin-loader",
3
3
  "description": "Plugin loader for cordis",
4
- "version": "1.0.3",
4
+ "version": "1.0.4",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -30,8 +30,8 @@
30
30
  "author": "Shigma <shigma10826@gmail.com>",
31
31
  "license": "MIT",
32
32
  "peerDependencies": {
33
- "node-addon-require-builtin": "^0.1.4",
34
- "@deepseek-ai/cordis": "^4.0.2"
33
+ "node-addon-require-builtin": "^0.1.6",
34
+ "@deepseek-ai/cordis": "^4.0.3"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "node-addon-require-builtin": {
@@ -39,6 +39,7 @@
39
39
  }
40
40
  },
41
41
  "dependencies": {
42
- "@deepseek-ai/cosmokit": "^1.8.3"
42
+ "@deepseek-ai/schemastery": "^3.18.3",
43
+ "@deepseek-ai/cosmokit": "^1.8.4"
43
44
  }
44
45
  }
@@ -0,0 +1,45 @@
1
+ /** Compare raw plugin configs using schema metadata without executing config hooks or validators. */
2
+ import type { Plugin } from '@deepseek-ai/cordis'
3
+ import { deepEqual } from '@deepseek-ai/cosmokit'
4
+ import type Schema from '@deepseek-ai/schemastery'
5
+ import { isJsExpr } from './utils.ts'
6
+
7
+ function isSchemastery(schema: Plugin.Runtime['Config']): schema is Schema {
8
+ return schema?.['~standard'].vendor === 'schemastery'
9
+ }
10
+
11
+ function isRecord(value: unknown): value is Record<string, unknown> {
12
+ if (!value || typeof value !== 'object' || isJsExpr(value)) return false
13
+ const prototype = Object.getPrototypeOf(value)
14
+ return prototype === Object.prototype || prototype === null
15
+ }
16
+
17
+ function equal(a: unknown, b: unknown, schema: Schema | undefined, ancestors: Set<Schema>): boolean {
18
+ if (schema?.meta?.volatile) return true
19
+ if (schema?.type !== 'object' || !schema.dict || ancestors.has(schema)) return deepEqual(a, b, true)
20
+ const left = a ?? schema.meta?.default
21
+ const right = b ?? schema.meta?.default
22
+ if (!isRecord(left) || !isRecord(right)) return deepEqual(left, right, true)
23
+ const { dict } = schema
24
+ ancestors.add(schema)
25
+ try {
26
+ return Object.keys({ ...left, ...right }).every(key =>
27
+ equal(left[key], right[key], Object.hasOwn(dict, key) ? dict[key] : undefined, ancestors),
28
+ )
29
+ } finally {
30
+ ancestors.delete(schema)
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Compare two raw configs, treating schema-declared volatile fields at fixed object paths as equal and absent objects as their schema default.
36
+ * Schema backedges, expressions, unknown fields and opaque values keep strict raw equality; an absent or non-Schemastery schema compares everything raw.
37
+ * @param previous - previous raw config.
38
+ * @param next - next raw config.
39
+ * @param schema - the plugin's config schema.
40
+ * @returns Whether the configs differ at most in volatile fields, without evaluating expressions, validating config or modifying inputs.
41
+ * @internal
42
+ */
43
+ export function equalExceptVolatile(previous: unknown, next: unknown, schema: Plugin.Runtime['Config']): boolean {
44
+ return isSchemastery(schema) ? equal(previous, next, schema, new Set()) : deepEqual(previous, next, true)
45
+ }
@@ -1,9 +1,10 @@
1
- import { Context, Fiber, Inject } from '@deepseek-ai/cordis'
2
- import { deepEqual, isNullable } from '@deepseek-ai/cosmokit'
1
+ import { Context, Fiber, FiberState, Inject, resolveConfig } from '@deepseek-ai/cordis'
2
+ import { deepEqual, isNullable, updateVolatile, volatileEntries, type Volatile } from '@deepseek-ai/cosmokit'
3
3
  import { Loader } from '../index.ts'
4
4
  import { EntryGroup } from './group.ts'
5
5
  import { EntryTree } from './tree.ts'
6
6
  import { evaluate, isJsExpr } from './utils.ts'
7
+ import { equalExceptVolatile } from './diff.ts'
7
8
 
8
9
  /** Serialized plugin entry options stored in loader config files. */
9
10
  export interface EntryOptions {
@@ -21,11 +22,6 @@ export interface EntryOptions {
21
22
  inject?: Inject | null
22
23
  }
23
24
 
24
- function updateError(stage: 'import' | 'dispose' | 'apply' | 'rollback', options: EntryOptions, cause: unknown) {
25
- const detail = cause instanceof Error ? cause.message : String(cause)
26
- return new Error(`failed to ${stage} loader entry ${options.id} (${options.name}): ${detail}`, { cause })
27
- }
28
-
29
25
  function takeEntries(object: {}, keys: string[]) {
30
26
  const result: [string, any][] = []
31
27
  for (const key of keys) {
@@ -43,11 +39,6 @@ function sortKeys<T extends {}>(object: T, prepend = ['id', 'name'], append = ['
43
39
  return Object.assign(object, Object.fromEntries([...part1, ...rest, ...part2]))
44
40
  }
45
41
 
46
- function replaceKeys<T extends {}>(target: T, source: T): T {
47
- for (const key of Object.keys(target)) Reflect.deleteProperty(target, key)
48
- return Object.assign(target, source)
49
- }
50
-
51
42
  /** One configured plugin node inside an `EntryTree`. */
52
43
  export class Entry {
53
44
  static readonly key = Symbol.for('cordis.entry')
@@ -61,7 +52,6 @@ export class Entry {
61
52
  public subtree?: EntryTree
62
53
 
63
54
  _initTask?: Promise<void>
64
- _disposing = 0
65
55
 
66
56
  constructor(public loader: Loader) {
67
57
  this.ctx = loader.ctx.extend({ [Entry.key]: this })
@@ -82,18 +72,13 @@ export class Entry {
82
72
 
83
73
  /** True when this entry or any owning parent entry is disabled. */
84
74
  get disabled() {
85
- return this._disabled(this.options)
86
- }
87
-
88
- private _disabled(options: EntryOptions) {
89
75
  // group is always enabled
90
- if (options.group) return false
91
- if (this.disabledOf(options)) return true
92
- let entry = this.parent.ctx.fiber.entry
93
- while (entry) {
76
+ if (this.options.group) return false
77
+ let entry: Entry | undefined = this
78
+ do {
94
79
  if (this.disabledOf(entry.options)) return true
95
80
  entry = entry.parent.ctx.fiber.entry
96
- }
81
+ } while (entry)
97
82
  return false
98
83
  }
99
84
 
@@ -111,12 +96,12 @@ export class Entry {
111
96
  return evaluate(this.ctx, expr)
112
97
  }
113
98
 
114
- private async _patchContext(diff: string[]) {
115
- await this.context.waterfall('loader/patch-context', this, async () => {
99
+ private _patchContext(diff: string[]) {
100
+ this.context.waterfall('loader/patch-context', this, () => {
116
101
  Object.setPrototypeOf(this.ctx, this.parent.ctx)
117
102
 
118
103
  if (this.fiber?.uid && (diff.includes('config') || this.options.group)) {
119
- await this.fiber.update(this.options.config, true)
104
+ this.fiber.update(this.options.config, true)
120
105
  }
121
106
  })
122
107
  }
@@ -127,122 +112,86 @@ export class Entry {
127
112
  await this.init()
128
113
  }
129
114
 
130
- async _dispose(fiber = this.fiber) {
131
- if (!fiber) return
132
- if (this.fiber === fiber) this.fiber = undefined
133
- this._disposing += 1
134
- try {
135
- await fiber.dispose()
136
- } finally {
137
- this._disposing -= 1
138
- }
139
- }
140
-
141
115
  /** Merge new options, restart as needed, and persist through the parent tree. */
142
116
  async update(options: Partial<EntryOptions>, create = false, force = false) {
143
- const previousOptions = this.options
144
- const legacy = { ...previousOptions }
145
- const candidate = create ? options as EntryOptions : { ...previousOptions }
146
- if (!create) {
117
+ const legacy = { ...this.options }
118
+
119
+ // step 1: update options
120
+ if (create) {
121
+ this.options = options as EntryOptions
122
+ } else {
147
123
  for (const [key, value] of Object.entries(options)) {
148
124
  if (isNullable(value)) {
149
- delete candidate[key as keyof EntryOptions]
125
+ delete this.options[key]
150
126
  } else {
151
- candidate[key as keyof EntryOptions] = value as never
127
+ this.options[key] = value
152
128
  }
153
129
  }
154
130
  }
155
- sortKeys(candidate)
131
+ sortKeys(this.options)
156
132
 
157
- const diff = Object
158
- .keys({ ...candidate, ...legacy })
159
- .filter(key => !deepEqual(candidate[key as keyof EntryOptions], legacy[key as keyof EntryOptions]))
160
- if (!diff.length && !force) return
161
-
162
- const commit = () => {
163
- if (create) return
164
- this.options = replaceKeys(previousOptions, candidate)
165
- }
166
-
167
- const previous = this.fiber
168
- if (!previous?.uid) {
169
- this.fiber = undefined
170
- this.options = candidate
171
- try {
172
- if (!this._disabled(candidate)) await this.init()
173
- } catch (error) {
174
- this.options = previousOptions
175
- throw error
176
- }
177
- commit()
133
+ // step 2: execute
134
+ if (this.disabled) {
135
+ this.fiber?.dispose()
178
136
  return
179
137
  }
180
138
 
181
- if (this._disabled(candidate)) {
182
- this.options = candidate
183
- try {
184
- await this._dispose(previous)
185
- } catch (error) {
186
- this.options = previousOptions
187
- throw updateError('dispose', candidate, error)
188
- }
189
- commit()
190
- this.context.emit('loader/partial-dispose', this, legacy, true)
191
- return
192
- }
193
-
194
- const replace = diff.some(key => key === 'name' || key === 'inject' || key === 'group')
195
- if (!replace) {
196
- this.options = candidate
197
- try {
198
- await this._patchContext(diff)
199
- } catch (error) {
200
- this.options = previousOptions
201
- try {
202
- await this._patchContext(diff)
203
- } catch (rollbackError) {
204
- throw updateError('rollback', legacy, new AggregateError([error, rollbackError]))
205
- }
206
- this.context.emit('loader/partial-dispose', this, candidate, true)
207
- throw updateError('apply', candidate, error)
208
- }
209
- commit()
139
+ // step 3: check if options are changed
140
+ if (this.fiber?.uid) {
141
+ const changes = Object.keys({ ...this.options, ...legacy })
142
+ .filter(key => !deepEqual(this.options[key], legacy[key], key === 'config'))
143
+ // Only an active fiber in an unchanged context takes volatile-only config changes without a remount.
144
+ const volatileOnly = changes.length === 1 && changes[0] === 'config'
145
+ && this.fiber.state === FiberState.ACTIVE && Object.getPrototypeOf(this.ctx) === this.parent.ctx
146
+ && equalExceptVolatile(legacy.config, this.options.config, this.fiber.runtime?.Config)
147
+ if (volatileOnly) this.fiber._config = this.options.config
148
+ const pending = volatileOnly && this._commitVolatile() ? [] : changes
149
+ if (!pending.length && !force) return
210
150
  this.context.emit('loader/partial-dispose', this, legacy, true)
211
- return
212
- }
213
-
214
- let plugin: any
215
- try {
216
- plugin = diff.includes('name')
217
- ? this.loader.unwrapExports(await this.parent.tree.import(candidate.name, this.getOuterStack))
218
- : previous.runtime!.callback
219
- } catch (error) {
220
- throw updateError('import', candidate, error)
151
+ this._patchContext(pending)
152
+ } else {
153
+ await this.init()
221
154
  }
155
+ }
222
156
 
223
- const previousPlugin = previous.runtime!.callback
224
- this.options = candidate
157
+ /**
158
+ * Parse a volatile-only raw config change and commit its values into the running fiber's references.
159
+ * An invalid candidate is logged and leaves the running references unchanged; the raw config stays retained for the next activation.
160
+ * @returns `false` when an ordinary effective value changed, so the caller applies the ordinary update lifecycle.
161
+ */
162
+ private _commitVolatile(): boolean {
163
+ const fiber = this.fiber!
164
+ const refs = volatileEntries(fiber.config)
165
+ if (!refs.length) return true
166
+ const raw = this.options.config
167
+ let candidate: unknown
225
168
  try {
226
- await this._dispose(previous)
169
+ candidate = resolveConfig(fiber.runtime!, fiber.ctx.waterfall(fiber, 'internal/config', raw, () => raw))
227
170
  } catch (error) {
228
- this.options = previousOptions
229
- throw updateError('dispose', candidate, error)
230
- }
231
-
171
+ this.ctx.logger.warn('volatile config update failed for %C', this.options.id)
172
+ this.ctx.logger.warn(error)
173
+ return true
174
+ }
175
+ if (!deepEqual(fiber.config, candidate, true)) {
176
+ this.ctx.logger.debug('ordinary config values of %C changed with its volatile values; applying the ordinary update', this.options.id)
177
+ return false
178
+ }
179
+ const paths = refs.flatMap(({ path, ref }) => {
180
+ const source = path.reduce<unknown>((value, key) => Reflect.get(value as object, key), candidate) as Volatile<unknown>
181
+ if (deepEqual(ref.get(), source.get(), true)) return []
182
+ updateVolatile(ref, source)
183
+ return [path]
184
+ })
185
+ if (!paths.length) return true
186
+ const self: Context = Object.create(fiber.ctx)
187
+ self[Context.filter] = (owner: Context) => owner.fiber === fiber
232
188
  try {
233
- await this._start(plugin)
189
+ fiber.ctx.emit(self, 'loader/volatile-update', paths)
234
190
  } catch (error) {
235
- this.options = previousOptions
236
- try {
237
- await this._start(previousPlugin)
238
- } catch (rollbackError) {
239
- throw updateError('rollback', legacy, new AggregateError([error, rollbackError]))
240
- }
241
- this.context.emit('loader/partial-dispose', this, candidate, true)
242
- throw updateError('apply', candidate, error)
191
+ // A listener failure must not fail the entry update; every value is already committed.
192
+ this.ctx.logger.warn(error)
243
193
  }
244
- commit()
245
- this.context.emit('loader/partial-dispose', this, legacy, true)
194
+ return true
246
195
  }
247
196
 
248
197
  getOuterStack = () => {
@@ -261,43 +210,27 @@ export class Entry {
261
210
  await (this._initTask ??= this._init())
262
211
  } finally {
263
212
  this._initTask = undefined
264
- if (!this.loader.getTasks().length) this.ctx.reflect.notify(['loader'])
265
213
  }
266
- await this._await()
267
- }
268
-
269
- async _await() {
270
- try {
271
- await this.fiber?.await()
272
- } catch (error) {
273
- throw updateError('apply', this.options, error)
214
+ const notify = () => {
215
+ if (this.loader.getTasks().length) return
216
+ this.ctx.reflect.notify(['loader'])
274
217
  }
218
+ this.fiber?.await().then(notify, notify)
275
219
  }
276
220
 
277
221
  private async _init() {
278
- let plugin: any
222
+ let exports: any
279
223
  try {
280
- plugin = this.loader.unwrapExports(await this.parent.tree.import(this.options.name, this.getOuterStack))
224
+ exports = await this.parent.tree.import(this.options.name, this.getOuterStack)
281
225
  } catch (error) {
282
- throw updateError('import', this.options, error)
283
- }
284
- try {
285
- await this._start(plugin)
286
- } catch (error) {
287
- throw updateError('apply', this.options, error)
288
- }
289
- }
290
-
291
- private async _start(plugin: any) {
292
- let fiber: Fiber | undefined
293
- try {
294
- await this._patchContext([])
295
- this.loader.showLog(this, 'apply')
296
- fiber = this.fiber = this.ctx.registry.plugin(plugin, this.options.config, this.getOuterStack)
297
- await fiber.await()
298
- } catch (error) {
299
- await this._dispose(fiber)
300
- throw error
226
+ this.ctx.logger.error(error)
227
+ return
228
+ } finally {
229
+ this._initTask = undefined
301
230
  }
231
+ const plugin = this.loader.unwrapExports(exports)
232
+ this._patchContext([])
233
+ this.loader.showLog(this, 'apply')
234
+ this.fiber = this.ctx.registry.plugin(plugin, this.options.config, this.getOuterStack).ctx.fiber
302
235
  }
303
236
  }
@@ -19,23 +19,12 @@ export class EntryGroup {
19
19
 
20
20
  async create(options: Omit<EntryOptions, 'id'>) {
21
21
  const id = this.tree.ensureId(options)
22
- const existing = this.tree.store[id]
23
- const entry: Entry = existing ?? (this.tree.store[id] = new Entry(this.ctx.loader))
24
- const previousParent = entry.parent
22
+ const entry: Entry = this.tree.store[id] ??= new Entry(this.ctx.loader)
25
23
  // Entry may be moved from another group,
26
24
  // so we need to update the parent reference.
27
25
  entry.parent = this
28
26
  // Use `create: true` to replace existing entry.options.
29
- try {
30
- await entry.update(options, true, true)
31
- } catch (error) {
32
- if (existing) {
33
- entry.parent = previousParent
34
- } else {
35
- delete this.tree.store[id]
36
- }
37
- throw error
38
- }
27
+ await entry.update(options, true, true)
39
28
  return entry.id
40
29
  }
41
30
 
@@ -45,10 +34,10 @@ export class EntryGroup {
45
34
  if (index >= 0) config.splice(index, 1)
46
35
  }
47
36
 
48
- async remove(id: string, isDispose = false) {
37
+ remove(id: string, isDispose = false) {
49
38
  const entry = this.tree.store[id]
50
39
  if (!entry) return
51
- await entry._dispose()
40
+ entry.fiber?.dispose()
52
41
  if (!isDispose) {
53
42
  this.unlink(entry.options)
54
43
  }
@@ -58,56 +47,26 @@ export class EntryGroup {
58
47
 
59
48
  async update(config: EntryOptions[]) {
60
49
  const oldConfig = this.data as EntryOptions[]
61
- const seen = new Set<string>()
62
- for (const options of config) {
63
- const id = this.tree.ensureId(options)
64
- if (seen.has(id)) throw new TypeError(`duplicate loader entry id: ${id}`)
65
- seen.add(id)
66
- }
50
+ this.data = config
67
51
  const oldMap = Object.fromEntries(oldConfig.map(options => [options.id, options]))
68
- const newMap = Object.fromEntries(config.map(options => [options.id, options]))
52
+ const newMap = Object.fromEntries(config.map(options => [options.id ?? Symbol('anonymous'), options]))
69
53
 
70
- try {
71
- const outcomes = await Promise.allSettled(config.map(options => this.create(options)))
72
- // Disposal owns termination: sibling starts can still be settling after
73
- // the containing tree has gone away, but their failures no longer
74
- // describe a live update to roll back.
75
- if (this.ctx.fiber.uid === null) return
76
- const failures = outcomes
77
- .filter((outcome): outcome is PromiseRejectedResult => outcome.status === 'rejected')
78
- .map(outcome => outcome.reason)
79
- if (failures.length === 1) throw failures[0]
80
- if (failures.length > 1) throw new AggregateError(failures, 'loader entries failed to apply')
81
- for (const id of Object.keys(oldMap)) {
82
- if (!newMap[id]) await this.remove(id, true)
83
- }
84
- this.data = config
85
- } catch (error) {
86
- const rollbackErrors: unknown[] = []
87
- for (const id of Object.keys(newMap).reverse()) {
88
- if (oldMap[id]) continue
89
- try {
90
- await this.remove(id, true)
91
- } catch (rollbackError) {
92
- rollbackErrors.push(rollbackError)
93
- }
94
- }
95
- for (const options of oldConfig) {
96
- try {
97
- await this.create(options)
98
- } catch (rollbackError) {
99
- rollbackErrors.push(rollbackError)
100
- }
54
+ // update inner plugins
55
+ const ids = Reflect.ownKeys({ ...oldMap, ...newMap }) as string[]
56
+ await Promise.all(ids.map(async (id) => {
57
+ if (newMap[id]) {
58
+ await this.create(newMap[id]).catch((error) => {
59
+ this.ctx.logger.error(error)
60
+ })
61
+ } else {
62
+ this.remove(id)
101
63
  }
102
- this.data = oldConfig
103
- if (rollbackErrors.length) throw new AggregateError([error, ...rollbackErrors], 'loader entry rollback failed')
104
- throw error
105
- }
64
+ }))
106
65
  }
107
66
 
108
- async stop() {
67
+ stop() {
109
68
  for (const options of this.data) {
110
- await this.remove(options.id, true)
69
+ this.remove(options.id, true)
111
70
  }
112
71
  }
113
72
  }
@@ -119,7 +78,9 @@ export class Group extends EntryGroup {
119
78
 
120
79
  constructor(public ctx: Context, public config: EntryOptions[]) {
121
80
  super(ctx, ctx.fiber.entry!.parent.tree)
122
- ctx.on('internal/update', config => this.update(config))
81
+ ctx.on('internal/update', (config) => {
82
+ this.update(config)
83
+ })
123
84
  }
124
85
 
125
86
  async* [Service.init]() {
@@ -93,7 +93,7 @@ export default function isolate(ctx: Context) {
93
93
  entry.ctx[Context.isolate] = Object.create(entry.ctx[Context.isolate])
94
94
  })
95
95
 
96
- ctx.on('loader/patch-context', async (entry, next) => {
96
+ ctx.on('loader/patch-context', (entry, next) => {
97
97
  // step 1: generate new isolate map
98
98
  const newMap: Dict<symbol> = Object.create(entry.parent.ctx[Context.isolate])
99
99
  for (const name of Object.keys(entry.options.isolate ?? {})) {
@@ -126,7 +126,7 @@ export default function isolate(ctx: Context) {
126
126
  swap(entry.ctx[Context.intercept], entry.options.intercept)
127
127
 
128
128
  // step 4: reload fiber
129
- await next()
129
+ next()
130
130
 
131
131
  // step 5: replace service impl
132
132
  for (const [symbol1, symbol2, flag1, flag2] of Object.values(diff)) {