File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ export interface ProjectConfigManager {
2
+ onReady ( ) : Promise < void > ;
3
+ getConfig ( ) : ProjectConfig | null ;
4
+ onUpdate ( listener : ( config : ProjectConfig ) => void ) : ( ) => void ;
5
+ }
Original file line number Diff line number Diff line change
1
+ type Consumer < T > = ( arg : T ) => void ;
2
+
3
+ type Disposer = ( ) => void ;
4
+
5
+ interface Listeners < T > {
6
+ [ index : string ] : {
7
+ // index is event name
8
+ [ index : string ] : Listener ; // index is listener id
9
+ } ;
10
+ }
11
+
12
+ export class EventEmitter < T > {
13
+ private listeners : Listeners = { } ;
14
+
15
+ private listenerId = 1 ;
16
+
17
+ on < E extends keyof T > ( eventName : E , listener : Consumer < T [ E ] > ) : Disposer {
18
+ if ( ! this . listeners [ eventName ] ) {
19
+ this . listeners [ eventName ] = { } ;
20
+ }
21
+ const currentListenerId = String ( this . listenerId ) ;
22
+ this . listenerId ++ ;
23
+ this . listeners [ eventName ] [ currentListenerId ] = listener ;
24
+ return ( ) : void => {
25
+ if ( this . listeners [ eventName ] ) {
26
+ delete this . listeners [ eventName ] [ currentListenerId ] ;
27
+ }
28
+ } ;
29
+ }
30
+
31
+ emit ( eventName : string , arg ?: DatafileUpdate ) : void {
32
+ const listeners = this . listeners [ eventName ] ;
33
+ if ( listeners ) {
34
+ Object . keys ( listeners ) . forEach ( listenerId => {
35
+ const listener = listeners [ listenerId ] ;
36
+ listener ( arg ) ;
37
+ } ) ;
38
+ }
39
+ }
40
+
41
+ removeAllListeners ( ) : void {
42
+ this . listeners = { } ;
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments