firebase_rs_sdk/util/
subscribe.rs

1use std::error::Error;
2use std::sync::Arc;
3
4pub type NextFn<T> = Arc<dyn Fn(&T) + Send + Sync + 'static>;
5pub type ErrorFn = Arc<dyn Fn(&dyn Error) + Send + Sync + 'static>;
6pub type CompleteFn = Arc<dyn Fn() + Send + Sync + 'static>;
7
8#[derive(Clone)]
9pub struct PartialObserver<T> {
10    pub next: Option<NextFn<T>>,
11    pub error: Option<ErrorFn>,
12    pub complete: Option<CompleteFn>,
13}
14
15impl<T> PartialObserver<T> {
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    pub fn with_next<F>(mut self, callback: F) -> Self
21    where
22        F: Fn(&T) + Send + Sync + 'static,
23    {
24        self.next = Some(Arc::new(callback));
25        self
26    }
27
28    pub fn with_error<F>(mut self, callback: F) -> Self
29    where
30        F: Fn(&dyn Error) + Send + Sync + 'static,
31    {
32        self.error = Some(Arc::new(callback));
33        self
34    }
35
36    pub fn with_complete<F>(mut self, callback: F) -> Self
37    where
38        F: Fn() + Send + Sync + 'static,
39    {
40        self.complete = Some(Arc::new(callback));
41        self
42    }
43}
44
45impl<T> Default for PartialObserver<T> {
46    fn default() -> Self {
47        Self {
48            next: None,
49            error: None,
50            complete: None,
51        }
52    }
53}
54
55pub type Unsubscribe = Box<dyn FnOnce() + Send + 'static>;