a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 1 | use std::{ |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 2 | collections::HashMap, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 3 | fmt, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 4 | pin::Pin, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 5 | sync::{Arc, Mutex, MutexGuard, PoisonError}, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 6 | task::{Context, Poll}, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 7 | time::Duration, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 8 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 9 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 10 | use futures_util::{Stream, StreamExt}; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 11 | use imbl::Vector; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 12 | use matrix_sdk::{ |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 13 | SendOutsideWasm, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 14 | executor::{AbortHandle, spawn}, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 15 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 16 | use ruma::{OwnedDeviceId, OwnedRoomId, OwnedTransactionId, OwnedUserId}; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 17 | #[cfg(feature = "serde")] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 18 | use serde::{Deserialize, Serialize}; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 19 | use tokio::sync::broadcast; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 20 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 21 | use crate::{ |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 22 | ClientHandle, Command, ConnectionState, EncryptionState, EngineFailure, ErrorKind, Event, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 23 | HistoryState, RoomSummary, TimelineCommand, TimelineDiff, TimelineId, TimelineItem, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 24 | TypingState, UploadOutcome, VerificationState, VerificationTarget, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 25 | engine::executor::EngineBoxFuture, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 26 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 27 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 28 | #[cfg(not(target_family = "wasm"))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 29 | type EngineBoxStream<T> = futures_util::stream::BoxStream<'static, T>; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 30 | #[cfg(target_family = "wasm")] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 31 | type EngineBoxStream<T> = futures_util::stream::LocalBoxStream<'static, T>; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 32 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 33 | const DEFAULT_CAPACITY: usize = 256; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 34 | const RESYNC_BACKOFF: Duration = Duration::from_millis(100); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 35 | const RESYNC_BACKOFF_CAP: Duration = Duration::from_secs(10); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 36 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 37 | /// everything the engine has said so far, folded together |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 38 | #[derive(Clone, Debug, PartialEq)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 39 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 40 | #[non_exhaustive] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 41 | pub struct State { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 42 | pub user_id: Option<OwnedUserId>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 43 | pub session: SessionStatus, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 44 | pub connection: ConnectionState, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 45 | pub encryption: Option<EncryptionState>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 46 | pub verification: Option<VerificationState>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 47 | pub verification_target: Option<VerificationTarget>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 48 | pub rooms: Vector<RoomSummary>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 49 | pub timelines: imbl::HashMap<TimelineId, TimelineState>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 50 | pub typing: imbl::HashMap<OwnedRoomId, Vec<OwnedUserId>>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 51 | pub uploads: imbl::HashMap<OwnedTransactionId, UploadState>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 52 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 53 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 54 | #[derive(Clone, Debug, PartialEq)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 55 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 56 | #[non_exhaustive] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 57 | pub struct TimelineState { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 58 | pub items: Vector<TimelineItem>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 59 | pub history: HistoryState, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 60 | /// a batch didn't fit, so `items` is the last good copy until the next reset |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 61 | pub diverged: bool, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 62 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 63 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 64 | #[derive(Clone, Debug, Eq, PartialEq)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 65 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 66 | #[non_exhaustive] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 67 | pub struct UploadState { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 68 | pub room_id: OwnedRoomId, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 69 | pub uploaded_bytes: u64, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 70 | pub total_bytes: u64, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 71 | pub complete: bool, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 72 | pub failure: Option<UploadFailure>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 73 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 74 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 75 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 76 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 77 | #[non_exhaustive] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 78 | pub struct UploadFailure { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 79 | pub kind: ErrorKind, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 80 | pub will_retry: bool, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 81 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 82 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 83 | /// the `Termination` it'll end in, as far as the events tell |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 84 | #[derive(Clone, Debug, Eq, PartialEq)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 85 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 86 | #[non_exhaustive] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 87 | pub enum SessionStatus { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 88 | Active, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 89 | SoftLoggedOut { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 90 | device_id: OwnedDeviceId, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 91 | }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 92 | AccessRevoked, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 93 | LoggedOut, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 94 | /// stopped with the session still good |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 95 | Stopped, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 96 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 97 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 98 | #[derive(Clone, Debug)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 99 | #[non_exhaustive] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 100 | pub struct Update { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 101 | /// one more than the update before it, and a `State` carries the one it's current as of |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 102 | pub sequence: u64, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 103 | pub change: StateChange, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 104 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 105 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 106 | #[derive(Clone, Debug)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 107 | #[non_exhaustive] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 108 | pub enum StateChange { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 109 | /// start over from this. a subscription opens with one and gets another after lagging |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 110 | State(Arc<State>), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 111 | Authenticated(OwnedUserId), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 112 | Session(SessionStatus), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 113 | /// the record on disk may now hold a spent refresh token |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 114 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 115 | SessionSaveFailed(Arc<crate::AccountError>), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 116 | Connection(ConnectionState), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 117 | Encryption(EncryptionState), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 118 | Verification(VerificationState), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 119 | VerificationTarget(VerificationTarget), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 120 | /// room modes arrive with the `Rooms` that follows |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 121 | NotificationSettings, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 122 | Rooms(Vector<RoomSummary>), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 123 | Timeline { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 124 | timeline_id: TimelineId, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 125 | change: TimelineChange, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 126 | }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 127 | History { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 128 | timeline_id: TimelineId, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 129 | state: HistoryState, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 130 | }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 131 | Typing(TypingState), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 132 | Upload { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 133 | transaction_id: OwnedTransactionId, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 134 | upload: UploadState, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 135 | }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 136 | UploadFinished { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 137 | room_id: OwnedRoomId, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 138 | transaction_id: OwnedTransactionId, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 139 | outcome: UploadOutcome, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 140 | }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 141 | Failed(EngineFailure), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 142 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 143 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 144 | #[derive(Clone, Debug)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 145 | #[non_exhaustive] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 146 | pub enum TimelineChange { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 147 | Reset { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 148 | items: Vector<TimelineItem>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 149 | }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 150 | /// `items` is what applying `diffs` left |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 151 | Changed { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 152 | diffs: Arc<[TimelineDiff]>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 153 | items: Vector<TimelineItem>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 154 | }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 155 | /// a batch didn't fit. a resync is on its way and nothing changes until its reset |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 156 | Diverged, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 157 | Closed, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 158 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 159 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 160 | /// drains the event stream into a `State` that any number of `MirrorHandle`s watch |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 161 | pub struct Mirror { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 162 | handle: ClientHandle, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 163 | events: EngineBoxStream<Event>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 164 | shared: Arc<Shared>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 165 | resyncs: HashMap<TimelineId, AbortHandle>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 166 | rooms_fetch: Option<AbortHandle>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 167 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 168 | session_store: Option<Arc<dyn crate::SessionStore>>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 169 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 170 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 171 | impl fmt::Debug for Mirror { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 172 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 173 | formatter.debug_struct("Mirror").finish_non_exhaustive() |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 174 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 175 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 176 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 177 | impl Mirror { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 178 | /// takes any stream of the engine's events, so a host can still look at them on the way past |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 179 | pub fn new( |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 180 | handle: ClientHandle, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 181 | events: impl Stream<Item = Event> + SendOutsideWasm + 'static, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 182 | ) -> (Self, MirrorHandle) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 183 | Self::with_capacity(handle, events, DEFAULT_CAPACITY) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 184 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 185 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 186 | /// `capacity` updates can queue for a subscriber before it lags and starts over |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 187 | pub fn with_capacity( |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 188 | handle: ClientHandle, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 189 | events: impl Stream<Item = Event> + SendOutsideWasm + 'static, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 190 | capacity: usize, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 191 | ) -> (Self, MirrorHandle) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 192 | let state = State { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 193 | user_id: None, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 194 | session: SessionStatus::Active, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 195 | connection: *handle.connection_state().borrow(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 196 | encryption: handle.encryption_state(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 197 | verification: None, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 198 | verification_target: None, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 199 | rooms: Vector::new(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 200 | timelines: imbl::HashMap::new(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 201 | typing: imbl::HashMap::new(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 202 | uploads: imbl::HashMap::new(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 203 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 204 | let shared = Arc::new(Shared { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 205 | published: Mutex::new(Published { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 206 | state: Arc::new(state), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 207 | sequence: 0, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 208 | updates: Some(broadcast::channel(capacity).0), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 209 | rooms_fetch: RoomsFetch::Idle, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 210 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 211 | }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 212 | #[cfg(not(target_family = "wasm"))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 213 | let events = events.boxed(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 214 | #[cfg(target_family = "wasm")] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 215 | let events = events.boxed_local(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 216 | let mirror = Self { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 217 | handle, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 218 | events, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 219 | shared: shared.clone(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 220 | resyncs: HashMap::new(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 221 | rooms_fetch: None, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 222 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 223 | session_store: None, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 224 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 225 | (mirror, MirrorHandle { shared }) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 226 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 227 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 228 | /// saves the record on every `SessionTokensChanged` before taking another event, and |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 229 | /// calls nothing but `save`. the store can't go while the client holds it open |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 230 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 231 | #[must_use] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 232 | pub fn with_session_store(mut self, store: Arc<dyn crate::SessionStore>) -> Self { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 233 | self.session_store = Some(store); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 234 | self |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 235 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 236 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 237 | /// resolves once the event stream ends, which is just after `Stopped` |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 238 | pub fn run(mut self) -> EngineBoxFuture<'static, ()> { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 239 | Box::pin(async move { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 240 | self.refetch_rooms(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 241 | while let Some(event) = self.events.next().await { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 242 | self.apply(event).await; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 243 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 244 | }) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 245 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 246 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 247 | async fn apply(&mut self, event: Event) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 248 | match event { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 249 | Event::ConnectionChanged(connection) => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 250 | state.connection = connection; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 251 | Some(StateChange::Connection(connection)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 252 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 253 | Event::Authenticated { user_id } => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 254 | state.user_id = Some(user_id.clone()); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 255 | Some(StateChange::Authenticated(user_id)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 256 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 257 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 258 | Event::SessionTokensChanged => self.save_session().await, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 259 | #[cfg(not(any(feature = "sqlite", feature = "indexeddb")))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 260 | Event::SessionTokensChanged => {} |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 261 | Event::SessionAccessRejected { soft_logout } => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 262 | state.session = match soft_logout { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 263 | Some(device_id) => SessionStatus::SoftLoggedOut { device_id }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 264 | None => SessionStatus::AccessRevoked, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 265 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 266 | Some(StateChange::Session(state.session.clone())) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 267 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 268 | Event::LoggedOut => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 269 | state.session = SessionStatus::LoggedOut; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 270 | Some(StateChange::Session(SessionStatus::LoggedOut)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 271 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 272 | Event::Stopped => { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 273 | self.cancel_resyncs(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 274 | self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 275 | if state.session != SessionStatus::Active { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 276 | return None; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 277 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 278 | state.session = SessionStatus::Stopped; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 279 | Some(StateChange::Session(SessionStatus::Stopped)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 280 | }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 281 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 282 | Event::Failed(failure) => self.publish(|_| Some(StateChange::Failed(failure))), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 283 | Event::NotificationSettingsChanged => { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 284 | self.publish(|_| Some(StateChange::NotificationSettings)); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 285 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 286 | Event::RoomsChanged => self.refetch_rooms(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 287 | Event::EncryptionChanged(encryption) => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 288 | state.encryption = Some(encryption.clone()); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 289 | Some(StateChange::Encryption(encryption)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 290 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 291 | Event::VerificationChanged(verification) => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 292 | state.verification = Some(verification.clone()); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 293 | Some(StateChange::Verification(verification)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 294 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 295 | Event::VerificationTargetChanged(target) => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 296 | state.verification_target = Some(target.clone()); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 297 | Some(StateChange::VerificationTarget(target)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 298 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 299 | Event::TimelineReset { timeline_id, items } => { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 300 | self.cancel_resync(&timeline_id); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 301 | self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 302 | let history = state |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 303 | .timelines |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 304 | .get(&timeline_id) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 305 | .map_or(HistoryState::Idle, |timeline| timeline.history.clone()); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 306 | let timeline = TimelineState { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 307 | items: items.clone(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 308 | history, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 309 | diverged: false, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 310 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 311 | state.timelines.insert(timeline_id.clone(), timeline); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 312 | Some(StateChange::Timeline { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 313 | timeline_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 314 | change: TimelineChange::Reset { items }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 315 | }) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 316 | }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 317 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 318 | Event::TimelineChanged { timeline_id, diffs } => self.apply_diffs(timeline_id, diffs), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 319 | Event::HistoryChanged { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 320 | timeline_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 321 | state: history, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 322 | } => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 323 | state.timelines.get_mut(&timeline_id)?.history = history.clone(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 324 | Some(StateChange::History { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 325 | timeline_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 326 | state: history, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 327 | }) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 328 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 329 | Event::TimelineClosed { timeline_id } => { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 330 | self.cancel_resync(&timeline_id); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 331 | self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 332 | state.timelines.remove(&timeline_id)?; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 333 | Some(StateChange::Timeline { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 334 | timeline_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 335 | change: TimelineChange::Closed, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 336 | }) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 337 | }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 338 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 339 | Event::TypingChanged(typing) => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 340 | if typing.users.is_empty() { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 341 | state.typing.remove(&typing.room_id); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 342 | } else { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 343 | state |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 344 | .typing |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 345 | .insert(typing.room_id.clone(), typing.users.clone()); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 346 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 347 | Some(StateChange::Typing(typing)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 348 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 349 | Event::MediaUploadProgress { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 350 | room_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 351 | transaction_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 352 | uploaded_bytes, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 353 | total_bytes, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 354 | complete, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 355 | } => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 356 | let upload = UploadState { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 357 | room_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 358 | uploaded_bytes, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 359 | total_bytes, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 360 | complete, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 361 | failure: None, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 362 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 363 | state.uploads.insert(transaction_id.clone(), upload.clone()); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 364 | Some(StateChange::Upload { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 365 | transaction_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 366 | upload, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 367 | }) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 368 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 369 | Event::MediaUploadFailed { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 370 | room_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 371 | transaction_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 372 | kind, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 373 | will_retry, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 374 | } => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 375 | let upload = state |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 376 | .uploads |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 377 | .entry(transaction_id.clone()) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 378 | .or_insert_with(|| UploadState { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 379 | room_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 380 | uploaded_bytes: 0, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 381 | total_bytes: 0, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 382 | complete: false, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 383 | failure: None, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 384 | }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 385 | upload.failure = Some(UploadFailure { kind, will_retry }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 386 | Some(StateChange::Upload { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 387 | transaction_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 388 | upload: upload.clone(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 389 | }) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 390 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 391 | Event::MediaUploadFinished { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 392 | room_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 393 | transaction_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 394 | outcome, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 395 | } => self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 396 | state.uploads.remove(&transaction_id); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 397 | Some(StateChange::UploadFinished { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 398 | room_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 399 | transaction_id, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 400 | outcome, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 401 | }) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 402 | }), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 403 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 404 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 405 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 406 | fn apply_diffs(&mut self, timeline_id: TimelineId, diffs: Vec<TimelineDiff>) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 407 | let mut diverged = false; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 408 | self.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 409 | let timeline = state.timelines.get_mut(&timeline_id)?; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 410 | if timeline.diverged { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 411 | return None; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 412 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 413 | let diffs: Arc<[TimelineDiff]> = diffs.into(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 414 | let change = match TimelineDiff::try_apply_all(diffs.to_vec(), &mut timeline.items) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 415 | Ok(()) => TimelineChange::Changed { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 416 | diffs, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 417 | items: timeline.items.clone(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 418 | }, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 419 | Err(error) => { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 420 | tracing::debug!(?timeline_id, length = error.length, "timeline diverged"); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 421 | timeline.diverged = true; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 422 | diverged = true; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 423 | TimelineChange::Diverged |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 424 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 425 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 426 | Some(StateChange::Timeline { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 427 | timeline_id: timeline_id.clone(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 428 | change, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 429 | }) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 430 | }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 431 | if diverged { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 432 | let task = spawn(resync(self.handle.clone(), timeline_id.clone())); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 433 | self.resyncs.insert(timeline_id, task.abort_handle()); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 434 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 435 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 436 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 437 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 438 | async fn save_session(&mut self) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 439 | let Some(store) = &self.session_store else { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 440 | return; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 441 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 442 | let saved = match self.handle.session_record() { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 443 | Ok(record) => store.save(&record).await, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 444 | Err(error) => Err(error), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 445 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 446 | if let Err(error) = saved { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 447 | tracing::warn!(%error, "saving the session record failed"); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 448 | self.publish(|_| Some(StateChange::SessionSaveFailed(Arc::new(error)))); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 449 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 450 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 451 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 452 | fn refetch_rooms(&mut self) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 453 | let mut published = self.shared.lock(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 454 | match published.rooms_fetch { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 455 | RoomsFetch::Idle => { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 456 | published.rooms_fetch = RoomsFetch::Running; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 457 | drop(published); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 458 | let task = spawn(fetch_rooms(self.handle.clone(), self.shared.clone())); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 459 | self.rooms_fetch = Some(task.abort_handle()); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 460 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 461 | RoomsFetch::Running => published.rooms_fetch = RoomsFetch::Again, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 462 | RoomsFetch::Again => {} |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 463 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 464 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 465 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 466 | fn cancel_resync(&mut self, timeline_id: &TimelineId) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 467 | if let Some(task) = self.resyncs.remove(timeline_id) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 468 | task.abort(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 469 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 470 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 471 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 472 | fn cancel_resyncs(&mut self) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 473 | for (_, task) in self.resyncs.drain() { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 474 | task.abort(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 475 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 476 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 477 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 478 | fn publish(&self, change: impl FnOnce(&mut State) -> Option<StateChange>) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 479 | self.shared.publish(change); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 480 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 481 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 482 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 483 | impl Drop for Mirror { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 484 | fn drop(&mut self) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 485 | self.cancel_resyncs(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 486 | if let Some(task) = self.rooms_fetch.take() { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 487 | task.abort(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 488 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 489 | self.shared.lock().updates = None; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 490 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 491 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 492 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 493 | /// asks until the engine answers, since a busy room or a full queue is only ever for now. |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 494 | /// a reset or a close for the timeline aborts it, and the engine sends a reset before it answers |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 495 | async fn resync(handle: ClientHandle, timeline_id: TimelineId) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 496 | let mut backoff = RESYNC_BACKOFF; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 497 | loop { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 498 | let command = Command::Timeline(TimelineCommand::Resync { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 499 | timeline_id: timeline_id.clone(), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 500 | }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 501 | match handle.execute(command).await { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 502 | Ok(_) => return, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 503 | Err(error) if error.kind == ErrorKind::Interrupted => return, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 504 | Err(error) => { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 505 | tracing::debug!(%error, ?timeline_id, "resync refused, trying again"); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 506 | matrix_sdk::sleep::sleep(backoff).await; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 507 | backoff = (backoff * 2).min(RESYNC_BACKOFF_CAP); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 508 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 509 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 510 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 511 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 512 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 513 | async fn fetch_rooms(handle: ClientHandle, shared: Arc<Shared>) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 514 | loop { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 515 | let rooms: Vector<RoomSummary> = handle.rooms().await.into(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 516 | shared.publish(|state| { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 517 | state.rooms = rooms.clone(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 518 | Some(StateChange::Rooms(rooms)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 519 | }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 520 | let mut published = shared.lock(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 521 | if published.rooms_fetch != RoomsFetch::Again { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 522 | published.rooms_fetch = RoomsFetch::Idle; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 523 | return; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 524 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 525 | published.rooms_fetch = RoomsFetch::Running; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 526 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 527 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 528 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 529 | /// cheap to clone, one per frontend |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 530 | #[derive(Clone, Debug)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 531 | pub struct MirrorHandle { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 532 | shared: Arc<Shared>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 533 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 534 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 535 | impl MirrorHandle { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 536 | #[must_use] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 537 | pub fn snapshot(&self) -> Arc<State> { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 538 | self.shared.lock().state.clone() |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 539 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 540 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 541 | /// opens with the current state, then every update after it. ends once the mirror stops |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 542 | #[must_use] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 543 | pub fn subscribe(&self) -> Subscription { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 544 | let (first, receiving) = self.shared.clone().subscribe(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 545 | let rest = futures_util::stream::unfold(receiving, |(shared, receiver)| async move { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 546 | let mut receiver = receiver?; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 547 | match receiver.recv().await { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 548 | Ok(update) => Some((update, (shared, Some(receiver)))), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 549 | Err(broadcast::error::RecvError::Lagged(missed)) => { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 550 | tracing::debug!(missed, "mirror subscriber lagged, starting it over"); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 551 | Some(shared.subscribe()) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 552 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 553 | Err(broadcast::error::RecvError::Closed) => None, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 554 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 555 | }); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 556 | let updates = futures_util::stream::once(std::future::ready(first)).chain(rest); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 557 | #[cfg(not(target_family = "wasm"))] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 558 | let updates = updates.boxed(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 559 | #[cfg(target_family = "wasm")] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 560 | let updates = updates.boxed_local(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 561 | Subscription(updates) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 562 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 563 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 564 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 565 | pub struct Subscription(EngineBoxStream<Update>); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 566 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 567 | impl fmt::Debug for Subscription { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 568 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 569 | formatter |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 570 | .debug_struct("Subscription") |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 571 | .finish_non_exhaustive() |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 572 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 573 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 574 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 575 | impl Stream for Subscription { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 576 | type Item = Update; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 577 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 578 | fn poll_next(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Update>> { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 579 | self.0.poll_next_unpin(context) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 580 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 581 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 582 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 583 | #[derive(Debug)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 584 | struct Shared { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 585 | published: Mutex<Published>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 586 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 587 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 588 | #[derive(Debug)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 589 | struct Published { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 590 | state: Arc<State>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 591 | sequence: u64, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 592 | /// gone once the mirror stops, which ends every subscription |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 593 | updates: Option<broadcast::Sender<Update>>, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 594 | rooms_fetch: RoomsFetch, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 595 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 596 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 597 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 598 | enum RoomsFetch { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 599 | Idle, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 600 | Running, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 601 | Again, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 602 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 603 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 604 | impl Shared { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 605 | fn lock(&self) -> MutexGuard<'_, Published> { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 606 | self.published |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 607 | .lock() |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 608 | .unwrap_or_else(PoisonError::into_inner) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 609 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 610 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 611 | /// state, sequence and send move together under the one lock, which is what lets |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 612 | /// `subscribe` take a snapshot and a receiver with nothing between them |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 613 | fn publish(&self, change: impl FnOnce(&mut State) -> Option<StateChange>) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 614 | let mut published = self.lock(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 615 | let Some(change) = change(Arc::make_mut(&mut published.state)) else { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 616 | return; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 617 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 618 | published.sequence += 1; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 619 | let update = Update { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 620 | sequence: published.sequence, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 621 | change, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 622 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 623 | if let Some(updates) = &published.updates { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 624 | let _ = updates.send(update); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 625 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 626 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 627 | |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 628 | fn subscribe(self: Arc<Self>) -> (Update, (Arc<Self>, Option<broadcast::Receiver<Update>>)) { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 629 | let published = self.lock(); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 630 | let update = Update { |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 631 | sequence: published.sequence, |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 632 | change: StateChange::State(published.state.clone()), |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 633 | }; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 634 | let receiver = published.updates.as_ref().map(broadcast::Sender::subscribe); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 635 | drop(published); |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 636 | (update, (self, receiver)) |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 637 | } |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 638 | } |