@@ -7,6 +7,7 @@ import { interpolate } from './utils.ts'
7
7
import { Entry } from './entry.ts'
8
8
import * as yaml from 'js-yaml'
9
9
import * as path from 'path'
10
+ import { EntryGroup } from './group.ts'
10
11
11
12
declare module '@cordisjs/core' {
12
13
interface Events {
@@ -313,17 +314,27 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
313
314
entry . patch ( entry . fork . parent , ctx )
314
315
}
315
316
316
- paths ( scope : EffectScope ) : string [ ] {
317
+ locate ( ctx = this [ Context . current ] ) {
318
+ return this . _locate ( ctx . scope ) . map ( entry => entry . options . id )
319
+ }
320
+
321
+ _locate ( scope : EffectScope ) : Entry [ ] {
317
322
// root scope
318
323
if ( scope === scope . parent . scope ) return [ ]
319
324
320
325
// runtime scope
321
326
if ( scope . runtime === scope ) {
322
- return ( [ ] as string [ ] ) . concat ( ...scope . runtime . children . map ( child => this . paths ( child ) ) )
327
+ return ( [ ] as Entry [ ] ) . concat ( ...scope . runtime . children . map ( child => this . _locate ( child ) ) )
323
328
}
324
329
325
- if ( scope . entry ) return [ scope . entry . options . id ]
326
- return this . paths ( scope . parent . scope )
330
+ if ( scope . entry ) return [ scope . entry ]
331
+ return this . _locate ( scope . parent . scope )
332
+ }
333
+
334
+ createGroup ( ) {
335
+ const ctx = this [ Context . current ]
336
+ // if (!ctx.scope.entry) throw new Error(`expected entry scope`)
337
+ return new EntryGroup ( this , ctx )
327
338
}
328
339
329
340
async start ( ) {
@@ -377,32 +388,10 @@ export function createGroup(config?: Entry.Options[], options: GroupOptions = {}
377
388
options . initial = config
378
389
379
390
function group ( ctx : Context , config : Entry . Options [ ] ) {
380
- const loader = ctx . get ( 'loader' ) !
381
- for ( const options of config ) {
382
- loader . _ensure ( ctx , options )
383
- }
384
-
385
- ctx . accept ( ( neo : Entry . Options [ ] ) => {
386
- // update config reference
387
- const old = ctx . scope . config as Entry . Options [ ]
388
- const oldMap : any = Object . fromEntries ( old . map ( entry => [ entry . id , entry ] ) )
389
- const neoMap : any = Object . fromEntries ( neo . map ( entry => [ entry . id , entry ] ) )
390
-
391
- // update inner plugins
392
- for ( const id in { ...oldMap , ...neoMap } ) {
393
- if ( ! neoMap [ id ] ) {
394
- loader . _remove ( id )
395
- } else {
396
- loader . _ensure ( ctx , neoMap [ id ] )
397
- }
398
- }
399
- } , { passive : true } )
400
-
401
- ctx . on ( 'dispose' , ( ) => {
402
- for ( const entry of ctx . scope . config as Entry . Options [ ] ) {
403
- loader . _remove ( entry . id )
404
- }
405
- } )
391
+ const group = ctx . get ( 'loader' ) ! . createGroup ( )
392
+ ctx . accept ( ( config : Entry . Options [ ] ) => {
393
+ group . update ( config )
394
+ } , { passive : true , immediate : true } )
406
395
}
407
396
408
397
defineProperty ( group , 'reusable' , true )
0 commit comments