1
- const sourcesByUrl = { } ;
2
- const sourcesByPort = { } ;
3
-
4
1
class Source {
5
2
url : string ;
6
3
eventSource : EventSource ;
7
- listening : Record < string , any > ;
8
- clients : Array < any > ;
4
+ listening : Record < string , boolean > ;
5
+ clients : Array < MessagePort > ;
9
6
10
- constructor ( url ) {
7
+ constructor ( url : string ) {
11
8
this . url = url ;
12
9
this . eventSource = new EventSource ( url ) ;
13
10
this . listening = { } ;
@@ -20,7 +17,7 @@ class Source {
20
17
this . listen ( 'error' ) ;
21
18
}
22
19
23
- register ( port ) {
20
+ register ( port : MessagePort ) {
24
21
if ( this . clients . includes ( port ) ) return ;
25
22
26
23
this . clients . push ( port ) ;
@@ -31,7 +28,7 @@ class Source {
31
28
} ) ;
32
29
}
33
30
34
- deregister ( port ) {
31
+ deregister ( port : MessagePort ) {
35
32
const portIdx = this . clients . indexOf ( port ) ;
36
33
if ( portIdx < 0 ) {
37
34
return this . clients . length ;
@@ -47,7 +44,7 @@ class Source {
47
44
this . eventSource = null ;
48
45
}
49
46
50
- listen ( eventType ) {
47
+ listen ( eventType : string ) {
51
48
if ( this . listening [ eventType ] ) return ;
52
49
this . listening [ eventType ] = true ;
53
50
this . eventSource . addEventListener ( eventType , ( event ) => {
@@ -58,21 +55,25 @@ class Source {
58
55
} ) ;
59
56
}
60
57
61
- notifyClients ( event ) {
58
+ notifyClients ( event : { type : string , data : any } ) {
62
59
for ( const client of this . clients ) {
63
60
client . postMessage ( event ) ;
64
61
}
65
62
}
66
63
67
- status ( port ) {
64
+ status ( port : MessagePort ) {
68
65
port . postMessage ( {
69
66
type : 'status' ,
70
67
message : `url: ${ this . url } readyState: ${ this . eventSource . readyState } ` ,
71
68
} ) ;
72
69
}
73
70
}
74
71
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 ) => {
76
77
for ( const port of e . ports ) {
77
78
port . addEventListener ( 'message' , ( event ) => {
78
79
if ( ! self . EventSource ) {
@@ -84,14 +85,14 @@ self.addEventListener('connect', (e: Event & {ports: Array<any>}) => {
84
85
}
85
86
if ( event . data . type === 'start' ) {
86
87
const url = event . data . url ;
87
- if ( sourcesByUrl [ url ] ) {
88
+ if ( sourcesByUrl . get ( url ) ) {
88
89
// we have a Source registered to this url
89
- const source = sourcesByUrl [ url ] ;
90
+ const source = sourcesByUrl . get ( url ) ;
90
91
source . register ( port ) ;
91
- sourcesByPort [ port ] = source ;
92
+ sourcesByPort . set ( port , source ) ;
92
93
return ;
93
94
}
94
- let source = sourcesByPort [ port ] ;
95
+ let source = sourcesByPort . get ( port ) ;
95
96
if ( source ) {
96
97
if ( source . eventSource && source . url === url ) return ;
97
98
@@ -101,30 +102,30 @@ self.addEventListener('connect', (e: Event & {ports: Array<any>}) => {
101
102
// Clean-up
102
103
if ( count === 0 ) {
103
104
source . close ( ) ;
104
- sourcesByUrl [ source . url ] = null ;
105
+ sourcesByUrl . set ( source . url , null ) ;
105
106
}
106
107
}
107
108
// Create a new Source
108
109
source = new Source ( url ) ;
109
110
source . register ( port ) ;
110
- sourcesByUrl [ url ] = source ;
111
- sourcesByPort [ port ] = source ;
111
+ sourcesByUrl . set ( url , source ) ;
112
+ sourcesByPort . set ( port , source ) ;
112
113
} else if ( event . data . type === 'listen' ) {
113
- const source = sourcesByPort [ port ] ;
114
+ const source = sourcesByPort . get ( port ) ;
114
115
source . listen ( event . data . eventType ) ;
115
116
} else if ( event . data . type === 'close' ) {
116
- const source = sourcesByPort [ port ] ;
117
+ const source = sourcesByPort . get ( port ) ;
117
118
118
119
if ( ! source ) return ;
119
120
120
121
const count = source . deregister ( port ) ;
121
122
if ( count === 0 ) {
122
123
source . close ( ) ;
123
- sourcesByUrl [ source . url ] = null ;
124
- sourcesByPort [ port ] = null ;
124
+ sourcesByUrl . set ( source . url , null ) ;
125
+ sourcesByPort . set ( port , null ) ;
125
126
}
126
127
} else if ( event . data . type === 'status' ) {
127
- const source = sourcesByPort [ port ] ;
128
+ const source = sourcesByPort . get ( port ) ;
128
129
if ( ! source ) {
129
130
port . postMessage ( {
130
131
type : 'status' ,
0 commit comments