@@ -21,31 +21,37 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
2121
2222 private commBusListener : ViewListener . ViewListener
2323
24+ private wasmInitialized = false
25+
2426 constructor ( props : NavigraphLoginProps ) {
2527 super ( props )
2628
2729 this . commBusListener = RegisterViewListener ( "JS_LISTENER_COMM_BUS" , ( ) => {
2830 console . info ( "JS_LISTENER_COMM_BUS registered" )
2931 } )
3032
33+ this . commBusListener . on ( "NAVIGRAPH_Heartbeat" , ( ) => {
34+ if ( ! this . wasmInitialized ) {
35+ this . wasmInitialized = true
36+ console . log ( "WASM initialized" )
37+ }
38+ } )
39+
3140 this . commBusListener . on ( "NAVIGRAPH_NavdataDownloaded" , ( ) => {
3241 console . info ( "WASM downloaded navdata" )
33- this . navdataTextRef . instance . textContent = "Navdata downloaded!"
34- this . navdataTextRef . instance . style . color = "white"
42+ this . displayMessage ( "Navdata downloaded" )
3543 } )
3644
3745 this . commBusListener . on ( "NAVIGRAPH_UnzippedFilesRemaining" , ( jsonArgs : string ) => {
3846 const args = JSON . parse ( jsonArgs )
3947 console . info ( "WASM unzipping files" , args )
4048 const percent = Math . round ( ( args . unzipped / args . total ) * 100 )
41- this . navdataTextRef . instance . textContent = `Unzipping files... ${ percent } % complete`
42- this . navdataTextRef . instance . style . color = "white"
49+ this . displayMessage ( `Unzipping files... ${ percent } % complete` )
4350 } )
4451
4552 this . commBusListener . on ( "NAVIGRAPH_DownloadFailed" , ( jsonArgs : string ) => {
4653 const args = JSON . parse ( jsonArgs )
47- this . navdataTextRef . instance . textContent = `Download failed: ${ args . error } `
48- this . navdataTextRef . instance . style . color = "red"
54+ this . displayError ( args . error )
4955 } )
5056 }
5157
@@ -77,20 +83,22 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
7783 public onAfterRender ( node : VNode ) : void {
7884 super . onAfterRender ( node )
7985
80- this . loginButtonRef . instance . addEventListener ( "click" , ( ) => this . handleClick ( ) . catch ( e => console . error ( e ) ) )
86+ this . loginButtonRef . instance . addEventListener ( "click" , ( ) =>
87+ this . handleClick ( ) . catch ( e => this . displayError ( e . message ) ) ,
88+ )
8189 this . downloadButtonRef . instance . addEventListener ( "click" , ( ) => this . handleDownloadClick ( ) )
8290
8391 AuthService . user . sub ( user => {
8492 if ( user ) {
8593 this . qrCodeRef . instance . src = ""
8694 this . qrCodeRef . instance . style . display = "none"
8795 this . loginButtonRef . instance . textContent = "Log out"
88- this . textRef . instance . textContent = `Welcome, ${ user . preferred_username } `
96+ this . displayMessage ( `Welcome, ${ user . preferred_username } ` )
8997
9098 this . handleLogin ( )
9199 } else {
92100 this . loginButtonRef . instance . textContent = "Sign in"
93- this . textRef . instance . textContent = "Not signed in"
101+ this . displayMessage ( "Not logged in" )
94102 }
95103 } , true )
96104 }
@@ -106,7 +114,7 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
106114 this . qrCodeRef . instance . style . display = "block"
107115 console . info ( p . verification_uri_complete )
108116 }
109- } , this . cancelSource . token ) . catch ( e => console . error ( "Failed to sign in!" , e ) )
117+ } , this . cancelSource . token ) . catch ( e => this . displayError ( e . message ) )
110118 }
111119 }
112120
@@ -128,17 +136,30 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
128136 . then ( pkg => {
129137 const url = pkg . file . url
130138 // eslint-disable-next-line @typescript-eslint/no-floating-promises
131- this . commBusListener . call (
132- "COMM_BUS_WASM_CALLBACK" ,
133- "NAVIGRAPH_DownloadNavdata" ,
134- JSON . stringify ( {
135- url,
136- folder : pkg . format ,
137- } ) ,
138- )
139- this . navdataTextRef . instance . textContent = "Downloading navdata..."
140- this . navdataTextRef . instance . style . color = "white"
139+ if ( this . wasmInitialized ) {
140+ this . commBusListener . call (
141+ "COMM_BUS_WASM_CALLBACK" ,
142+ "NAVIGRAPH_DownloadNavdata" ,
143+ JSON . stringify ( {
144+ url,
145+ folder : pkg . format ,
146+ } ) ,
147+ )
148+ this . displayMessage ( "Downloading navdata..." )
149+ } else {
150+ this . displayError ( "WASM not initialized" )
151+ }
141152 } )
142- . catch ( e => console . error ( e ) )
153+ . catch ( e => this . displayError ( e . message ) )
154+ }
155+
156+ private displayMessage ( message : string ) {
157+ this . navdataTextRef . instance . textContent = message
158+ this . navdataTextRef . instance . style . color = "white"
159+ }
160+
161+ private displayError ( error : string ) {
162+ this . navdataTextRef . instance . textContent = error
163+ this . navdataTextRef . instance . style . color = "red"
143164 }
144165}
0 commit comments