@@ -58,7 +58,7 @@ export namespace Loader {
58
58
}
59
59
}
60
60
61
- export abstract class Loader < T extends Loader . Options = Loader . Options > extends Service < Entry . Options [ ] > {
61
+ export abstract class Loader < T extends Loader . Options = Loader . Options > extends Service < T > {
62
62
// TODO auto inject optional when provided?
63
63
static inject = {
64
64
optional : [ 'loader' ] ,
@@ -83,7 +83,7 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
83
83
84
84
private tasks = new Set < Promise < any > > ( )
85
85
86
- constructor ( public app : Context , public options : T ) {
86
+ constructor ( public app : Context , public config : T ) {
87
87
super ( app , 'loader' , true )
88
88
this . root = new EntryGroup ( this . app )
89
89
this . realms [ '#' ] = app . root [ Context . isolate ]
@@ -99,10 +99,10 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
99
99
100
100
this . app . on ( 'internal/before-update' , ( fork , config ) => {
101
101
if ( ! fork . entry ) return
102
- if ( fork . entry . isUpdate ) return fork . entry . isUpdate = false
102
+ if ( fork . entry . suspend ) return fork . entry . suspend = false
103
103
const { schema } = fork . runtime
104
104
fork . entry . options . config = schema ? schema . simplify ( config ) : config
105
- this . file . write ( this . config )
105
+ fork . entry . parent . write ( )
106
106
} )
107
107
108
108
this . app . on ( 'internal/fork' , ( fork ) => {
@@ -121,7 +121,7 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
121
121
fork . entry . options . disabled = true
122
122
fork . entry . fork = undefined
123
123
fork . entry . stop ( )
124
- this . file . write ( this . config )
124
+ fork . entry . parent . write ( )
125
125
} )
126
126
}
127
127
@@ -144,29 +144,30 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
144
144
} else {
145
145
await this . findConfig ( )
146
146
}
147
+ this . app . on ( 'dispose' , ( ) => this . file . dispose ( ) )
147
148
await this . file . checkAccess ( )
148
149
this . app . provide ( 'baseDir' , this . baseDir , true )
149
150
}
150
151
151
152
private async findConfig ( ) {
153
+ const { name, initial } = this . config
152
154
const dirents = await readdir ( this . baseDir , { withFileTypes : true } )
153
155
for ( const extension of supported ) {
154
- const dirent = dirents . find ( dirent => dirent . name === this . options . name + extension )
156
+ const dirent = dirents . find ( dirent => dirent . name === name + extension )
155
157
if ( ! dirent ) continue
156
158
if ( ! dirent . isFile ( ) ) {
157
159
throw new Error ( `config file "${ dirent . name } " is not a file` )
158
160
}
159
161
const type = writable [ extension ]
160
- const name = path . resolve ( this . baseDir , this . options . name + extension )
161
- this . file = new FileLoader ( this , name , type )
162
+ const filename = path . resolve ( this . baseDir , name + extension )
163
+ this . file = new FileLoader ( this , filename , type )
162
164
return
163
165
}
164
- if ( this . options . initial ) {
165
- this . config = this . options . initial as any
166
+ if ( initial ) {
166
167
const type = writable [ '.yml' ]
167
- const name = path . resolve ( this . baseDir , this . options . name + '.yml' )
168
- this . file = new FileLoader ( this , name , type )
169
- return this . file . write ( this . config )
168
+ const filename = path . resolve ( this . baseDir , name + '.yml' )
169
+ this . file = new FileLoader ( this , filename , type )
170
+ return this . file . write ( initial as any )
170
171
}
171
172
throw new Error ( 'config file not found' )
172
173
}
@@ -218,7 +219,7 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
218
219
override [ key ] = value
219
220
}
220
221
}
221
- this . file . write ( this . config )
222
+ entry . parent . write ( )
222
223
return entry . update ( override )
223
224
}
224
225
@@ -231,15 +232,15 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
231
232
async create ( options : Omit < Entry . Options , 'id' > , parent : string | null = null , position = Infinity ) {
232
233
const group = this . resolveGroup ( parent )
233
234
group . data . splice ( position , 0 , options as Entry . Options )
234
- this . file . write ( this . config )
235
+ group . write ( )
235
236
return group . _create ( options )
236
237
}
237
238
238
239
remove ( id : string ) {
239
240
const entry = this . entries [ id ]
240
241
if ( ! entry ) throw new Error ( `entry ${ id } not found` )
241
242
entry . parent . _remove ( id )
242
- this . file . write ( this . config )
243
+ entry . parent . write ( )
243
244
}
244
245
245
246
transfer ( id : string , parent : string | null , position = Infinity ) {
@@ -249,7 +250,8 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
249
250
const target = this . resolveGroup ( parent )
250
251
source . _unlink ( entry . options )
251
252
target . data . splice ( position , 0 , entry . options )
252
- this . file . write ( this . config )
253
+ source . write ( )
254
+ target . write ( )
253
255
if ( source === target ) return
254
256
entry . parent = target
255
257
if ( ! entry . fork ) return
@@ -275,8 +277,7 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
275
277
}
276
278
277
279
async start ( ) {
278
- this . config = await this . file . read ( )
279
- this . root . update ( this . config )
280
+ this . root . update ( await this . file . read ( ) )
280
281
281
282
while ( this . tasks . size ) {
282
283
await Promise . all ( this . tasks )
0 commit comments