1- const sourcesByUrl = { } ;
2- const sourcesByPort = { } ;
3-
41class Source {
52 url : string ;
63 eventSource : EventSource ;
7- listening : Record < string , any > ;
8- clients : Array < any > ;
4+ listening : Record < string , boolean > ;
5+ clients : Array < MessagePort > ;
96
10- constructor ( url ) {
7+ constructor ( url : string ) {
118 this . url = url ;
129 this . eventSource = new EventSource ( url ) ;
1310 this . listening = { } ;
@@ -20,7 +17,7 @@ class Source {
2017 this . listen ( 'error' ) ;
2118 }
2219
23- register ( port ) {
20+ register ( port : MessagePort ) {
2421 if ( this . clients . includes ( port ) ) return ;
2522
2623 this . clients . push ( port ) ;
@@ -31,7 +28,7 @@ class Source {
3128 } ) ;
3229 }
3330
34- deregister ( port ) {
31+ deregister ( port : MessagePort ) {
3532 const portIdx = this . clients . indexOf ( port ) ;
3633 if ( portIdx < 0 ) {
3734 return this . clients . length ;
@@ -47,7 +44,7 @@ class Source {
4744 this . eventSource = null ;
4845 }
4946
50- listen ( eventType ) {
47+ listen ( eventType : string ) {
5148 if ( this . listening [ eventType ] ) return ;
5249 this . listening [ eventType ] = true ;
5350 this . eventSource . addEventListener ( eventType , ( event ) => {
@@ -58,21 +55,25 @@ class Source {
5855 } ) ;
5956 }
6057
61- notifyClients ( event ) {
58+ notifyClients ( event : { type : string , data : any } ) {
6259 for ( const client of this . clients ) {
6360 client . postMessage ( event ) ;
6461 }
6562 }
6663
67- status ( port ) {
64+ status ( port : MessagePort ) {
6865 port . postMessage ( {
6966 type : 'status' ,
7067 message : `url: ${ this . url } readyState: ${ this . eventSource . readyState } ` ,
7168 } ) ;
7269 }
7370}
7471
75- self . addEventListener ( 'connect' , ( e : Event & { ports : Array < any > } ) => {
72+ const sourcesByUrl : Map < string , Source | null > = new Map ( ) ;
73+ const sourcesByPort : Map < MessagePort , Source | null > = new Map ( ) ;
74+
75+ // @ts -expect-error: typescript bug?
76+ self . addEventListener ( 'connect' , ( e : MessageEvent ) => {
7677 for ( const port of e . ports ) {
7778 port . addEventListener ( 'message' , ( event ) => {
7879 if ( ! self . EventSource ) {
@@ -84,14 +85,14 @@ self.addEventListener('connect', (e: Event & {ports: Array<any>}) => {
8485 }
8586 if ( event . data . type === 'start' ) {
8687 const url = event . data . url ;
87- if ( sourcesByUrl [ url ] ) {
88+ if ( sourcesByUrl . get ( url ) ) {
8889 // we have a Source registered to this url
89- const source = sourcesByUrl [ url ] ;
90+ const source = sourcesByUrl . get ( url ) ;
9091 source . register ( port ) ;
91- sourcesByPort [ port ] = source ;
92+ sourcesByPort . set ( port , source ) ;
9293 return ;
9394 }
94- let source = sourcesByPort [ port ] ;
95+ let source = sourcesByPort . get ( port ) ;
9596 if ( source ) {
9697 if ( source . eventSource && source . url === url ) return ;
9798
@@ -101,30 +102,30 @@ self.addEventListener('connect', (e: Event & {ports: Array<any>}) => {
101102 // Clean-up
102103 if ( count === 0 ) {
103104 source . close ( ) ;
104- sourcesByUrl [ source . url ] = null ;
105+ sourcesByUrl . set ( source . url , null ) ;
105106 }
106107 }
107108 // Create a new Source
108109 source = new Source ( url ) ;
109110 source . register ( port ) ;
110- sourcesByUrl [ url ] = source ;
111- sourcesByPort [ port ] = source ;
111+ sourcesByUrl . set ( url , source ) ;
112+ sourcesByPort . set ( port , source ) ;
112113 } else if ( event . data . type === 'listen' ) {
113- const source = sourcesByPort [ port ] ;
114+ const source = sourcesByPort . get ( port ) ;
114115 source . listen ( event . data . eventType ) ;
115116 } else if ( event . data . type === 'close' ) {
116- const source = sourcesByPort [ port ] ;
117+ const source = sourcesByPort . get ( port ) ;
117118
118119 if ( ! source ) return ;
119120
120121 const count = source . deregister ( port ) ;
121122 if ( count === 0 ) {
122123 source . close ( ) ;
123- sourcesByUrl [ source . url ] = null ;
124- sourcesByPort [ port ] = null ;
124+ sourcesByUrl . set ( source . url , null ) ;
125+ sourcesByPort . set ( port , null ) ;
125126 }
126127 } else if ( event . data . type === 'status' ) {
127- const source = sourcesByPort [ port ] ;
128+ const source = sourcesByPort . get ( port ) ;
128129 if ( ! source ) {
129130 port . postMessage ( {
130131 type : 'status' ,
0 commit comments