1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! Real-time synchronization support for AnyList.
//!
//! This module provides WebSocket-based real-time updates, allowing your
//! application to receive notifications when data changes on the server.
//!
//! # Overview
//!
//! AnyList uses WebSockets to notify clients of changes in real-time. When
//! another user (or another device) makes changes to shared lists, recipes, or
//! other data, the server sends a notification message indicating what type of
//! data changed.
//!
//! This library **does not** retain any state. Instead, it notifies your
//! application via a callback, and you decide what action to take (re-fetch
//! data, update a cache, notify the user, etc.).
//!
//! # Examples
//!
//! ## Simple
//!
//! ```no_run
//! use anylist_rs::{AnyListClient, SyncEvent};
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = Arc::new(
//! AnyListClient::login("user@example.com", "password").await?
//! );
//!
//! // Connects immediately
//! let mut sync = client.start_realtime_sync(|event| {
//! match event {
//! SyncEvent::ShoppingListsChanged => {
//! println!("Lists changed - consider re-fetching");
//! }
//! SyncEvent::RecipeDataChanged => {
//! println!("Recipes changed");
//! }
//! _ => {}
//! }
//! }).await?;
//!
//! // Your application logic...
//!
//! sync.disconnect().await?;
//! Ok(())
//! }
//! ```
//!
//! ## Advanced
//!
//! For cases where you need more control (parallel connections, testing, etc.):
//!
//! ```no_run
//! use anylist_rs::{AnyListClient, SyncEvent};
//! use anylist_rs::realtime::RealtimeSync;
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = Arc::new(
//! AnyListClient::login("user@example.com", "password").await?
//! );
//!
//! // Create without connecting
//! let mut sync = RealtimeSync::new(client, |event| {
//! println!("Event: {:?}", event);
//! });
//!
//! // Connect when ready
//! sync.connect().await?;
//!
//! // Later...
//! sync.disconnect().await?;
//! Ok(())
//! }
//! ```
//!
//! # Connection Management
//!
//! The connection is managed automatically:
//! - **Heartbeats**: Sent every 5 seconds to keep the connection alive
//! - **Auto-reconnect**: If the connection drops, it will automatically
//! reconnect with exponential backoff
//! - **Token refresh**: If the access token expires (close code 4010), it will
//! refresh and reconnect
//!
//! # Event Types
//!
//! See [`SyncEvent`] for the full list of events you can receive.
//!
//! # Thread Safety
//!
//! All types in this module are thread-safe and can be used across async tasks.
pub use SyncEvent;
pub use ;
/// Callback type for sync events
pub type SyncCallback = Arc;