@@ -71,6 +71,10 @@ export default class LNC {
7171 return globalThis [ this . _namespace ] as WasmGlobal ;
7272 }
7373
74+ private set wasm ( value : any ) {
75+ globalThis [ this . _namespace ] = value ;
76+ }
77+
7478 get isReady ( ) {
7579 return (
7680 this . wasm &&
@@ -137,27 +141,39 @@ export default class LNC {
137141 // make sure the WASM client binary is downloaded first
138142 if ( ! this . isReady ) await this . preload ( ) ;
139143
140- global . onLocalPrivCreate = ( keyHex : string ) => {
141- log . debug ( 'local private key created: ' + keyHex ) ;
142- this . credentials . localKey = keyHex ;
143- } ;
144-
145- global . onRemoteKeyReceive = ( keyHex : string ) => {
146- log . debug ( 'remote key received: ' + keyHex ) ;
147- this . credentials . remoteKey = keyHex ;
148- } ;
144+ // create the namespace object in the global scope if it doesn't exist
145+ // so that we can assign the WASM callbacks to it
146+ if ( typeof this . wasm !== 'object' ) {
147+ this . wasm = { } ;
148+ }
149149
150- global . onAuthData = ( keyHex : string ) => {
151- log . debug ( 'auth data received: ' + keyHex ) ;
152- } ;
150+ // assign the WASM callbacks to the namespace object if they haven't
151+ // already been assigned by the consuming app
152+ if ( ! this . wasm . onLocalPrivCreate ) {
153+ this . wasm . onLocalPrivCreate = ( keyHex : string ) => {
154+ log . debug ( 'local private key created: ' + keyHex ) ;
155+ this . credentials . localKey = keyHex ;
156+ } ;
157+ }
158+ if ( ! this . wasm . onRemoteKeyReceive ) {
159+ this . wasm . onRemoteKeyReceive = ( keyHex : string ) => {
160+ log . debug ( 'remote key received: ' + keyHex ) ;
161+ this . credentials . remoteKey = keyHex ;
162+ } ;
163+ }
164+ if ( ! this . wasm . onAuthData ) {
165+ this . wasm . onAuthData = ( keyHex : string ) => {
166+ log . debug ( 'auth data received: ' + keyHex ) ;
167+ } ;
168+ }
153169
154170 this . go . argv = [
155171 'wasm-client' ,
156172 '--debuglevel=trace' ,
157173 '--namespace=' + this . _namespace ,
158- ' --onlocalprivcreate=onLocalPrivCreate' ,
159- ' --onremotekeyreceive=onRemoteKeyReceive' ,
160- ' --onauthdata=onAuthData'
174+ ` --onlocalprivcreate=${ this . _namespace } . onLocalPrivCreate` ,
175+ ` --onremotekeyreceive=${ this . _namespace } . onRemoteKeyReceive` ,
176+ ` --onauthdata=${ this . _namespace } . onAuthData`
161177 ] ;
162178
163179 if ( this . result ) {
0 commit comments