Skip to content

Commit 8df4dca

Browse files
committed
feat: replace initialized call with a heartbeat
1 parent 05dd57c commit 8df4dca

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

examples/gauge/Components/NavigraphLogin.tsx

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/wasm_navdata_interface/src/dispatcher.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
use std::rc::Rc;
22

33
use crate::download::downloader::NavdataDownloader;
4-
use msfs::{commbus::*, MSFSEvent};
4+
use msfs::{commbus::*, sys::sGaugeDrawData, MSFSEvent};
55

66
pub struct Dispatcher<'a> {
77
commbus: CommBus<'a>,
88
downloader: Rc<NavdataDownloader>,
9+
delta_time: std::time::Duration,
910
}
1011

1112
impl<'a> Dispatcher<'a> {
1213
pub fn new() -> Self {
1314
Dispatcher {
1415
commbus: CommBus::default(),
1516
downloader: Rc::new(NavdataDownloader::new()),
17+
delta_time: std::time::Duration::from_secs(0),
1618
}
1719
}
1820

@@ -21,8 +23,8 @@ impl<'a> Dispatcher<'a> {
2123
MSFSEvent::PostInitialize => {
2224
self.handle_initialized();
2325
}
24-
MSFSEvent::PreUpdate => {
25-
self.handle_update();
26+
MSFSEvent::PreDraw(data) => {
27+
self.handle_update(data);
2628
}
2729
MSFSEvent::PreKill => {
2830
self.commbus.unregister_all();
@@ -33,7 +35,6 @@ impl<'a> Dispatcher<'a> {
3335
}
3436

3537
fn handle_initialized(&mut self) {
36-
CommBus::call("NAVIGRAPH_Initialized", "", CommBusBroadcastFlags::All);
3738
{
3839
let captured_downloader = self.downloader.clone();
3940
self.commbus
@@ -60,8 +61,16 @@ impl<'a> Dispatcher<'a> {
6061
}
6162
}
6263

63-
fn handle_update(&mut self) {
64-
// update unzip
64+
fn handle_update(&mut self, data: &sGaugeDrawData) {
65+
// Accumulate delta time for heartbeat
66+
self.delta_time += data.delta_time();
67+
68+
// Send heartbeat every 5 seconds
69+
if self.delta_time >= std::time::Duration::from_secs(5) {
70+
CommBus::call("NAVIGRAPH_Heartbeat", "", CommBusBroadcastFlags::All);
71+
self.delta_time = std::time::Duration::from_secs(0);
72+
}
73+
6574
self.downloader.on_update();
6675
}
6776

0 commit comments

Comments
 (0)