wayle_traits/lib.rs
1//! Common traits for Wayle services.
2
3use std::sync::Arc;
4
5/// Background state monitoring for a service.
6pub trait ServiceMonitoring {
7 /// Error type for monitoring operations.
8 type Error;
9
10 /// Starts monitoring for state changes.
11 ///
12 /// Sets up necessary watchers or listeners to detect and propagate
13 /// state changes to subscribers.
14 ///
15 /// # Errors
16 /// Returns error if monitoring setup fails.
17 #[allow(async_fn_in_trait)]
18 async fn start_monitoring(&self) -> Result<(), Self::Error>;
19}
20
21/// Background state monitoring for a model.
22pub trait ModelMonitoring {
23 /// Error type for monitoring operations.
24 type Error;
25
26 /// Starts monitoring for state changes with shared ownership.
27 ///
28 /// Arc-wrapped variant of service monitoring, enabling the model
29 /// to be shared across multiple owners while receiving updates.
30 ///
31 /// # Errors
32 /// Returns error if monitoring setup fails.
33 #[allow(async_fn_in_trait)]
34 async fn start_monitoring(self: Arc<Self>) -> Result<(), Self::Error>;
35}
36
37/// Static models - fetch once, no monitoring.
38pub trait Static {
39 /// Error type for static operations.
40 type Error;
41 /// Context type for static fetching.
42 type Context<'a>;
43
44 /// Retrieves a static instance from the provided context.
45 ///
46 /// Creates or retrieves an instance using the given context,
47 /// without setting up any monitoring.
48 ///
49 /// # Errors
50 /// Returns error if instance creation or retrieval fails.
51 #[allow(async_fn_in_trait)]
52 async fn get(context: Self::Context<'_>) -> Result<Self, Self::Error>
53 where
54 Self: Sized;
55}
56/// Reactive models - can fetch statically OR with live monitoring.
57pub trait Reactive {
58 /// Error type for reactive operations.
59 type Error;
60 /// Context type for static fetching.
61 type Context<'a>;
62 /// Context type for live monitoring.
63 type LiveContext<'a>;
64
65 /// Fetches a static snapshot without monitoring.
66 #[allow(async_fn_in_trait)]
67 async fn get(context: Self::Context<'_>) -> Result<Self, Self::Error>
68 where
69 Self: Sized;
70
71 /// Fetches with live monitoring and reactive updates.
72 #[allow(async_fn_in_trait)]
73 async fn get_live(context: Self::LiveContext<'_>) -> Result<Arc<Self>, Self::Error>
74 where
75 Self: Sized;
76}
77
78#[doc = include_str!("../README.md")]
79#[cfg(doctest)]
80pub struct ReadmeDocTests;