1
1
import { Container } from 'iti' ;
2
- import { SernEmitter } from '../' ;
3
- import { isAsyncFunction } from 'node:util/types' ;
4
-
2
+ import { Disposable , SernEmitter } from '../' ;
5
3
import * as assert from 'node:assert' ;
6
4
import { Subject } from 'rxjs' ;
7
5
import { DefaultServices , ModuleStore } from '../_internal' ;
6
+ import * as Hooks from './hooks'
7
+
8
8
9
9
/**
10
- * Provides all the defaults for sern to function properly.
11
- * The only user provided dependency needs to be @sern/client
10
+ * A semi-generic container that provides error handling, emitter, and module store.
11
+ * For the handler to operate correctly, The only user provided dependency needs to be @sern/client
12
12
*/
13
13
export class CoreContainer < T extends Partial < Dependencies > > extends Container < T , { } > {
14
- private ready$ = new Subject < never > ( ) ;
15
- private beenCalled = new Set < PropertyKey > ( ) ;
14
+ private ready$ = new Subject < void > ( ) ;
16
15
constructor ( ) {
17
16
super ( ) ;
17
+ assert . ok ( ! this . isReady ( ) , 'Listening for dispose & init should occur prior to sern being ready.' ) ;
18
18
19
- this . listenForInsertions ( ) ;
19
+ const { unsubscribe } = Hooks . createInitListener ( this ) ;
20
+ this . ready$
21
+ . subscribe ( { complete : unsubscribe } ) ;
20
22
21
23
( this as Container < { } , { } > )
22
24
. add ( {
@@ -32,36 +34,27 @@ export class CoreContainer<T extends Partial<Dependencies>> extends Container<T,
32
34
} ) ;
33
35
}
34
36
35
- private listenForInsertions ( ) {
36
- assert . ok (
37
- ! this . isReady ( ) ,
38
- 'listening for init functions should only occur prior to sern being ready.' ,
39
- ) ;
40
- const unsubscriber = this . on ( 'containerUpserted' , e => this . callInitHooks ( e ) ) ;
41
-
42
- this . ready$ . subscribe ( {
43
- complete : unsubscriber ,
44
- } ) ;
45
- }
46
-
47
- private async callInitHooks ( e : { key : keyof T ; newContainer : T [ keyof T ] | null } ) {
48
- const dep = e . newContainer ;
49
-
50
- assert . ok ( dep ) ;
51
- //Ignore any dependencies that are not objects or array
52
- if ( typeof dep !== 'object' || Array . isArray ( dep ) ) {
53
- return ;
54
- }
55
- if ( 'init' in dep && typeof dep . init === 'function' && ! this . beenCalled . has ( e . key ) ) {
56
- isAsyncFunction ( dep . init ) ? await dep . init ( ) : dep . init ( ) ;
57
- this . beenCalled . add ( e . key ) ;
58
- }
59
- }
60
37
61
38
isReady ( ) {
39
+
62
40
return this . ready$ . closed ;
63
41
}
42
+ override async disposeAll ( ) {
43
+
44
+ const otherDisposables = Object
45
+ . entries ( this . _context )
46
+ . flatMap ( ( [ key , value ] ) =>
47
+ 'dispose' in value
48
+ ? [ key ]
49
+ : [ ] ) ;
50
+
51
+ for ( const key of otherDisposables ) {
52
+ this . addDisposer ( { [ key ] : ( dep : Disposable ) => dep . dispose ( ) } as never ) ;
53
+ }
54
+ await super . disposeAll ( )
55
+ }
64
56
ready ( ) {
57
+ this . ready$ . complete ( ) ;
65
58
this . ready$ . unsubscribe ( ) ;
66
59
}
67
60
}
0 commit comments