@@ -56,14 +56,20 @@ export const injectExtensionAPIs = () => {
5656 extensionId : string ,
5757 application : string ,
5858 receive : ( message : any ) => void ,
59+ disconnect : ( ) => void ,
5960 callback : ConnectNativeCallback ,
6061 ) => {
6162 const connectionId = ( contextBridge as any ) . executeInMainWorld ( {
6263 func : ( ) => crypto . randomUUID ( ) ,
6364 } )
6465 invokeExtension ( extensionId , 'runtime.connectNative' , { } , connectionId , application )
65- ipcRenderer . on ( `crx-native-msg- ${ connectionId } ` , ( _event , message ) => {
66+ const onMessage = ( _event : Electron . IpcRendererEvent , message : any ) => {
6667 receive ( message )
68+ }
69+ ipcRenderer . on ( `crx-native-msg-${ connectionId } ` , onMessage )
70+ ipcRenderer . once ( `crx-native-msg-${ connectNative } -disconnect` , ( ) => {
71+ ipcRenderer . off ( `crx-native-msg-${ connectionId } ` , onMessage )
72+ disconnect ( )
6773 } )
6874 const send = ( message : any ) => {
6975 ipcRenderer . send ( `crx-native-msg-${ connectionId } ` , message )
@@ -182,39 +188,43 @@ export const injectExtensionAPIs = () => {
182188
183189 class NativePort implements chrome . runtime . Port {
184190 private connectionId : string = ''
191+ private connected = false
185192 private pending : any [ ] = [ ]
186193
187- name : string = 'NativePort '
194+ name : string = ''
188195
189- _init ( connectionId : string , send : ( message : any ) => void ) {
196+ _init = ( connectionId : string , send : ( message : any ) => void ) => {
197+ this . connected = true
190198 this . connectionId = connectionId
191199 this . _send = send
192200
193- this . pending . forEach ( ( msg ) => {
194- console . log ( 'sending pending' , JSON . stringify ( msg ) )
195- this . postMessage ( msg )
196- } )
201+ this . pending . forEach ( ( msg ) => this . postMessage ( msg ) )
197202 this . pending = [ ]
198203
199- console . log ( '***NativePort. _init')
204+ Object . defineProperty ( this , ' _init', { value : undefined } )
200205 }
201206
202207 _send ( message : any ) {
203208 this . pending . push ( message )
204209 }
205210
206211 _receive ( message : any ) {
207- console . log ( 'NativePort received' , JSON . stringify ( message ) )
208212 ; ( this . onMessage as any ) . _emit ( message )
209213 }
210214
215+ _disconnect ( ) {
216+ this . disconnect ( )
217+ }
218+
211219 postMessage ( message : any ) {
212- console . log ( '***NativePort.postMessage' , JSON . stringify ( message ) )
213220 this . _send ( message )
214221 }
215222 disconnect ( ) {
216- electron . disconnectNative ( extensionId , this . connectionId )
217- ; ( this . onDisconnect as any ) . _emit ( )
223+ if ( this . connected ) {
224+ electron . disconnectNative ( extensionId , this . connectionId )
225+ ; ( this . onDisconnect as any ) . _emit ( )
226+ this . connected = false
227+ }
218228 }
219229 onMessage : chrome . runtime . PortMessageEvent = new Event ( ) as any
220230 onDisconnect : chrome . runtime . PortDisconnectEvent = new Event ( ) as any
@@ -499,10 +509,11 @@ export const injectExtensionAPIs = () => {
499509 connectNative : ( application : string ) => {
500510 const port = new NativePort ( )
501511 const receive = port . _receive . bind ( port )
512+ const disconnect = port . _disconnect . bind ( port )
502513 const callback : ConnectNativeCallback = ( connectionId , send ) => {
503514 port . _init ( connectionId , send )
504515 }
505- electron . connectNative ( extensionId , application , receive , callback )
516+ electron . connectNative ( extensionId , application , receive , disconnect , callback )
506517 return port
507518 } ,
508519 openOptionsPage : invokeExtension ( 'runtime.openOptionsPage' ) ,
0 commit comments