From f8979b7feb38a4030f2e94e04ed0ad987c31c8e9 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 22 Nov 2023 14:23:33 +0100 Subject: [PATCH] Remove 'handle', add wasm friendly task spawning --- src/gateway/heartbeat.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/gateway/heartbeat.rs b/src/gateway/heartbeat.rs index ea8f965..1176517 100644 --- a/src/gateway/heartbeat.rs +++ b/src/gateway/heartbeat.rs @@ -4,7 +4,8 @@ use std::time::{self, Duration, Instant}; use tokio::sync::mpsc::{Receiver, Sender}; use safina_timer::sleep_until; -use tokio::task::{self, JoinHandle}; +#[cfg(not(target_arch = "wasm32"))] +use tokio::task; use super::*; use crate::types; @@ -20,8 +21,6 @@ pub(super) struct HeartbeatHandler { pub heartbeat_interval: Duration, /// The send channel for the heartbeat thread pub send: Sender, - /// The handle of the thread - handle: JoinHandle<()>, } impl HeartbeatHandler { @@ -33,15 +32,18 @@ impl HeartbeatHandler { let (send, receive) = tokio::sync::mpsc::channel(32); let kill_receive = kill_rc.resubscribe(); - // FIXME: Doesn't work in WASM - let handle: JoinHandle<()> = task::spawn(async move { + #[cfg(not(target_arch = "wasm32"))] + task::spawn(async move { + Self::heartbeat_task(websocket_tx, heartbeat_interval, receive, kill_receive).await; + }); + #[cfg(target_arch = "wasm32")] + wasm_bindgen_futures::spawn_local(async move { Self::heartbeat_task(websocket_tx, heartbeat_interval, receive, kill_receive).await; }); Self { heartbeat_interval, send, - handle, } }