55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 1 | //! a browser-side slice of macltok, login through to a live sync, exported for |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 2 | //! wasm-bindgen so both the worker test and the size measurement reach the same code |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 3 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 4 | use futures_channel::oneshot; |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 5 | use macltok::{ |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 6 | ConnectionState, EncryptedSessionStore, EngineConfig, Event, PasswordLogin, Session, StoreConfig, |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 7 | }; |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 8 | use wasm_bindgen::prelude::*; |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 9 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 10 | #[wasm_bindgen] |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 11 | pub async fn smoke( |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 12 | homeserver: String, |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 13 | username: String, |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 14 | password: String, |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 15 | store_name: String, |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 16 | ) -> Result<(), JsError> { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 17 | let sessions = EncryptedSessionStore::indexeddb( |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 18 | StoreConfig::indexeddb(store_name, "macltok-web").with_lock_holder("macltok-web-worker"), |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 19 | ); |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 20 | let login = PasswordLogin::new(homeserver, username, password); |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 21 | let session = Session::login_password(login, &sessions).await?; |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 22 | let (engine, handle, mut events) = session.build(EngineConfig::default()).await?; |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 23 | let (done, running) = oneshot::channel(); |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 24 | wasm_bindgen_futures::spawn_local(async move { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 25 | let _ = done.send(engine.run().await); |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 26 | }); |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 27 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 28 | let mut online = false; |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 29 | let mut rooms = false; |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 30 | while !(online && rooms) { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 31 | match events.recv().await { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 32 | Some(Event::ConnectionChanged(ConnectionState::Online)) => online = true, |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 33 | Some(Event::RoomsChanged) => rooms = true, |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 34 | Some(Event::Stopped) | None => { |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 35 | return Err(JsError::new("engine stopped before it came online")); |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 36 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 37 | Some(_) => {} |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 38 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 39 | } |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 40 | |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 41 | handle.shutdown().await?; |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 42 | while events.recv().await.is_some() {} |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 43 | running |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 44 | .await |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 45 | .map_err(|_| JsError::new("engine task vanished"))??; |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 46 | Ok(()) |
55a1b8252814 | atagen | 2026-09-26 09:08 UTC | 47 | } |