@@ -21,31 +21,37 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
21
21
22
22
private commBusListener : ViewListener . ViewListener
23
23
24
+ private wasmInitialized = false
25
+
24
26
constructor ( props : NavigraphLoginProps ) {
25
27
super ( props )
26
28
27
29
this . commBusListener = RegisterViewListener ( "JS_LISTENER_COMM_BUS" , ( ) => {
28
30
console . info ( "JS_LISTENER_COMM_BUS registered" )
29
31
} )
30
32
33
+ this . commBusListener . on ( "NAVIGRAPH_Heartbeat" , ( ) => {
34
+ if ( ! this . wasmInitialized ) {
35
+ this . wasmInitialized = true
36
+ console . log ( "WASM initialized" )
37
+ }
38
+ } )
39
+
31
40
this . commBusListener . on ( "NAVIGRAPH_NavdataDownloaded" , ( ) => {
32
41
console . info ( "WASM downloaded navdata" )
33
- this . navdataTextRef . instance . textContent = "Navdata downloaded!"
34
- this . navdataTextRef . instance . style . color = "white"
42
+ this . displayMessage ( "Navdata downloaded" )
35
43
} )
36
44
37
45
this . commBusListener . on ( "NAVIGRAPH_UnzippedFilesRemaining" , ( jsonArgs : string ) => {
38
46
const args = JSON . parse ( jsonArgs )
39
47
console . info ( "WASM unzipping files" , args )
40
48
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` )
43
50
} )
44
51
45
52
this . commBusListener . on ( "NAVIGRAPH_DownloadFailed" , ( jsonArgs : string ) => {
46
53
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 )
49
55
} )
50
56
}
51
57
@@ -77,20 +83,22 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
77
83
public onAfterRender ( node : VNode ) : void {
78
84
super . onAfterRender ( node )
79
85
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
+ )
81
89
this . downloadButtonRef . instance . addEventListener ( "click" , ( ) => this . handleDownloadClick ( ) )
82
90
83
91
AuthService . user . sub ( user => {
84
92
if ( user ) {
85
93
this . qrCodeRef . instance . src = ""
86
94
this . qrCodeRef . instance . style . display = "none"
87
95
this . loginButtonRef . instance . textContent = "Log out"
88
- this . textRef . instance . textContent = `Welcome, ${ user . preferred_username } `
96
+ this . displayMessage ( `Welcome, ${ user . preferred_username } ` )
89
97
90
98
this . handleLogin ( )
91
99
} else {
92
100
this . loginButtonRef . instance . textContent = "Sign in"
93
- this . textRef . instance . textContent = "Not signed in"
101
+ this . displayMessage ( "Not logged in" )
94
102
}
95
103
} , true )
96
104
}
@@ -106,7 +114,7 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
106
114
this . qrCodeRef . instance . style . display = "block"
107
115
console . info ( p . verification_uri_complete )
108
116
}
109
- } , this . cancelSource . token ) . catch ( e => console . error ( "Failed to sign in!" , e ) )
117
+ } , this . cancelSource . token ) . catch ( e => this . displayError ( e . message ) )
110
118
}
111
119
}
112
120
@@ -128,17 +136,30 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
128
136
. then ( pkg => {
129
137
const url = pkg . file . url
130
138
// 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
+ }
141
152
} )
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"
143
164
}
144
165
}
0 commit comments