Skip to content

Commit 1cf3a05

Browse files
committed
fix: wasm websocket connect
1 parent 6304f20 commit 1cf3a05

File tree

23 files changed

+288
-177
lines changed

23 files changed

+288
-177
lines changed

Cargo.lock

Lines changed: 66 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/client-api-test-util/src/user.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,12 @@ use dotenv::dotenv;
55
use lazy_static::lazy_static;
66
use uuid::Uuid;
77

8-
#[cfg(not(target_arch = "wasm32"))]
98
lazy_static! {
109
pub static ref ADMIN_USER: User = {
1110
dotenv().ok();
1211
User {
13-
email: std::env::var("GOTRUE_ADMIN_EMAIL").unwrap(),
14-
password: std::env::var("GOTRUE_ADMIN_PASSWORD").unwrap(),
15-
}
16-
};
17-
}
18-
19-
#[cfg(target_arch = "wasm32")]
20-
lazy_static! {
21-
pub static ref ADMIN_USER: User = {
22-
dotenv().ok();
23-
User {
24-
email: "[email protected]".to_string(),
25-
password: "password".to_string(),
12+
email: std::env::var("GOTRUE_ADMIN_EMAIL").unwrap_or("[email protected]".to_string()),
13+
password: std::env::var("GOTRUE_ADMIN_PASSWORD").unwrap_or("password".to_string()),
2614
}
2715
};
2816
}

libs/client-api/Cargo.toml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ bytes = "1.5"
2020
uuid = "1.6.1"
2121
futures-util = "0.3.30"
2222
futures-core = "0.3.30"
23-
tokio-retry = "0.3"
2423
parking_lot = "0.12.1"
2524
brotli = "3.4.0"
2625
mime_guess = "2.0.4"
@@ -45,10 +44,8 @@ database-entity.workspace = true
4544
app-error = { workspace = true, features = ["tokio_error", "bincode_error"] }
4645
scraper = { version = "0.17.1", optional = true }
4746

48-
[target.'cfg(target_arch = "wasm32")'.dependencies]
49-
getrandom = { version = "0.2", features = ["js"]}
50-
tokio = { workspace = true, features = ["sync"]}
51-
47+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
48+
tokio-retry = "0.3"
5249

5350
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio]
5451
workspace = true
@@ -58,6 +55,11 @@ features = ["sync", "net"]
5855
workspace = true
5956
features = ["tungstenite"]
6057

58+
[target.'cfg(target_arch = "wasm32")'.dependencies]
59+
wasm-bindgen-futures = "0.4.40"
60+
getrandom = { version = "0.2", features = ["js"]}
61+
tokio = { workspace = true, features = ["sync"]}
62+
again = "0.1.2"
6163

6264
[features]
6365
collab-sync = ["collab", "yrs"]

libs/client-api/src/collab_sync/plugin.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::collab_sync::{SinkConfig, SyncQueue};
1515
use tokio_stream::wrappers::WatchStream;
1616
use tracing::trace;
1717

18+
use crate::platform_spawn;
1819
use crate::ws::{ConnectState, WSConnectStateReceiver};
1920
use yrs::updates::encoder::Encode;
2021

@@ -63,7 +64,7 @@ where
6364
);
6465

6566
let mut sync_state_stream = WatchStream::new(sync_queue.subscribe_sync_state());
66-
tokio::spawn(async move {
67+
platform_spawn(async move {
6768
while let Some(new_state) = sync_state_stream.next().await {
6869
if let Some(local_collab) = weak_local_collab.upgrade() {
6970
if let Some(local_collab) = local_collab.try_lock() {
@@ -76,7 +77,7 @@ where
7677
let sync_queue = Arc::new(sync_queue);
7778
let weak_local_collab = collab;
7879
let weak_sync_queue = Arc::downgrade(&sync_queue);
79-
tokio::spawn(async move {
80+
platform_spawn(async move {
8081
while let Ok(connect_state) = ws_connect_state.recv().await {
8182
match connect_state {
8283
ConnectState::Connected => {
@@ -132,7 +133,7 @@ where
132133
let object_id = self.object.object_id.clone();
133134
let cloned_origin = origin.clone();
134135

135-
tokio::spawn(async move {
136+
platform_spawn(async move {
136137
if let Some(sync_queue) = weak_sync_queue.upgrade() {
137138
let payload = Message::Sync(SyncMessage::Update(update)).encode_v1();
138139
sync_queue

libs/client-api/src/collab_sync/sink.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::collab_sync::pending_msg::{MessageState, PendingMsgQueue};
99
use crate::collab_sync::{SyncError, SyncObject, DEFAULT_SYNC_TIMEOUT};
1010
use futures_util::SinkExt;
1111

12+
use crate::platform_spawn;
1213
use realtime_entity::collab_msg::{CollabSinkMessage, MsgId};
13-
use tokio::spawn;
1414
use tokio::sync::{mpsc, oneshot, watch, Mutex};
1515
use tokio::time::{interval, Instant, Interval};
1616
use tracing::{debug, error, event, trace, warn};
@@ -96,7 +96,7 @@ where
9696
let weak_notifier = Arc::downgrade(&notifier);
9797
let (tx, rx) = mpsc::channel(1);
9898
interval_runner_stop_tx = Some(tx);
99-
spawn(IntervalRunner::new(*duration).run(weak_notifier, rx));
99+
platform_spawn(IntervalRunner::new(*duration).run(weak_notifier, rx));
100100
}
101101
Self {
102102
uid,
@@ -364,7 +364,7 @@ where
364364
}
365365

366366
fn retry_later(weak_notifier: Weak<watch::Sender<bool>>) {
367-
spawn(async move {
367+
platform_spawn(async move {
368368
interval(Duration::from_millis(100)).tick().await;
369369
if let Some(notifier) = weak_notifier.upgrade() {
370370
let _ = notifier.send(false);

libs/client-api/src/collab_sync/sync.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::collab_sync::{
22
CollabSink, CollabSinkRunner, SinkConfig, SinkState, SyncError, SyncObject,
33
};
4+
use crate::platform_spawn;
45
use bytes::Bytes;
56
use collab::core::awareness::Awareness;
67
use collab::core::collab::MutexCollab;
@@ -13,7 +14,6 @@ use realtime_protocol::{Message, MessageReader, SyncMessage};
1314
use std::marker::PhantomData;
1415
use std::ops::Deref;
1516
use std::sync::{Arc, Weak};
16-
use tokio::spawn;
1717
use tokio::sync::watch;
1818
use tokio_stream::wrappers::WatchStream;
1919
use tracing::{error, event, trace, warn, Level};
@@ -75,7 +75,7 @@ where
7575
pause,
7676
));
7777

78-
spawn(CollabSinkRunner::run(Arc::downgrade(&sink), notifier_rx));
78+
platform_spawn(CollabSinkRunner::run(Arc::downgrade(&sink), notifier_rx));
7979
let cloned_protocol = protocol.clone();
8080
let object_id = object.object_id.clone();
8181
let stream = SyncStream::new(
@@ -90,7 +90,7 @@ where
9090
let weak_sync_state = Arc::downgrade(&sync_state);
9191
let mut sink_state_stream = WatchStream::new(sink_state_rx);
9292
// Subscribe the sink state stream and update the sync state in the background.
93-
spawn(async move {
93+
platform_spawn(async move {
9494
while let Some(collab_state) = sink_state_stream.next().await {
9595
if let Some(sync_state) = weak_sync_state.upgrade() {
9696
match collab_state {
@@ -209,7 +209,7 @@ where
209209
P: CollabSyncProtocol + Send + Sync + 'static,
210210
{
211211
let cloned_weak_collab = weak_collab.clone();
212-
spawn(SyncStream::<Sink, Stream>::spawn_doc_stream::<P>(
212+
platform_spawn(SyncStream::<Sink, Stream>::spawn_doc_stream::<P>(
213213
origin,
214214
object_id.clone(),
215215
stream,

libs/client-api/src/native/http_native.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::http::log_request_id;
2-
use crate::retry::{RefreshTokenAction, RefreshTokenRetryCondition};
32
use crate::ws::{WSClientHttpSender, WSError};
43
use crate::{spawn_blocking_brotli_compress, Client};
4+
use crate::{RefreshTokenAction, RefreshTokenRetryCondition};
55
use app_error::AppError;
66
use async_trait::async_trait;
77
use database_entity::dto::CollabParams;
@@ -55,7 +55,7 @@ impl Client {
5555
.into_iter()
5656
.map(|params| {
5757
let config = self.config.clone();
58-
tokio::spawn(async move {
58+
platform_spawn(async move {
5959
let data = params.to_bytes().map_err(AppError::from)?;
6060
spawn_blocking_brotli_compress(
6161
data,
@@ -153,7 +153,7 @@ impl WSClientHttpSender for Client {
153153
}
154154
}
155155

156-
pub fn spawn<T>(future: T) -> tokio::task::JoinHandle<T::Output>
156+
pub fn platform_spawn<T>(future: T) -> tokio::task::JoinHandle<T::Output>
157157
where
158158
T: Future + Send + 'static,
159159
T::Output: Send + 'static,

0 commit comments

Comments
 (0)