Skip to main content

max / goingson

1.6 KB · 47 lines History Blame Raw
1 //! GoingsOn's [`SyncObserver`]: maps the engine scheduler's callbacks onto the
2 //! Tauri events the frontend already listens for. The frontend (`js/app.js`)
3 //! consumes:
4 //! - `sync:status-changed`: payload `"syncing"` / `"error"` / anything else = idle
5 //! - `sync:changes-applied`: payload = the list of DB tables the pull touched
6 //! (drives selective cache invalidation)
7 //! - `sync:subscription-required`: no payload (shows the subscribe toast)
8
9 use std::collections::HashSet;
10
11 use synckit_client::{SyncObserver, SyncState};
12 use tauri::{AppHandle, Emitter};
13
14 /// Emits GoingsOn's sync events from engine scheduler callbacks.
15 pub(crate) struct GoSyncObserver {
16 app: AppHandle,
17 }
18
19 impl GoSyncObserver {
20 pub(crate) fn new(app: AppHandle) -> Self {
21 Self { app }
22 }
23 }
24
25 impl SyncObserver for GoSyncObserver {
26 fn on_status(&self, state: SyncState) {
27 // The frontend's dot only distinguishes syncing / error / idle. Idle,
28 // LoggedOut, and SubscriptionRequired all rest it to connected; the last
29 // additionally surfaces through on_subscription_required below.
30 let payload = match state {
31 SyncState::Syncing => "syncing",
32 SyncState::Error(_) => "error",
33 _ => "idle",
34 };
35 let _ = self.app.emit("sync:status-changed", payload);
36 }
37
38 fn on_changes_applied(&self, tables: &HashSet<String>) {
39 let tables: Vec<&String> = tables.iter().collect();
40 let _ = self.app.emit("sync:changes-applied", tables);
41 }
42
43 fn on_subscription_required(&self) {
44 let _ = self.app.emit("sync:subscription-required", ());
45 }
46 }
47