-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.rs
More file actions
120 lines (107 loc) · 2.84 KB
/
state.rs
File metadata and controls
120 lines (107 loc) · 2.84 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::sync::atomic::{AtomicI64, AtomicU64};
use bon::bon;
use tower_cookies::Key;
#[derive(Clone)]
pub struct State {
pub user: app::user::Service,
pub auth: app::auth::Service,
pub chat: app::chat::Service,
pub sse: crate::sse::Registry,
pub cookie_key: Key,
pub trace_log: crate::trace_log::Store,
pub demo: DemoState,
}
#[derive(Clone)]
pub struct DemoState {
pub surreal: SurrealState,
pub counter: CounterState,
pub chat_room_bindings: crate::chat_demo::room::Bindings,
}
impl DemoState {
pub fn new() -> Self {
Self::default()
}
}
impl Default for DemoState {
fn default() -> Self {
Self {
surreal: SurrealState::new(),
counter: CounterState::new(),
chat_room_bindings: crate::chat_demo::room::Bindings::new(),
}
}
}
#[derive(Clone)]
pub struct CounterState {
pub server_count: std::sync::Arc<AtomicI64>,
}
impl CounterState {
pub fn new() -> Self {
Self::default()
}
}
impl Default for CounterState {
fn default() -> Self {
Self {
server_count: std::sync::Arc::new(AtomicI64::new(0)),
}
}
}
#[derive(Clone)]
pub struct SurrealState {
pub guard: std::sync::Arc<
dashmap::DashMap<crate::sse::StreamKey, std::sync::Arc<tokio::sync::Mutex<()>>>,
>,
pub cancel: std::sync::Arc<
dashmap::DashMap<crate::sse::StreamKey, tokio_util::sync::CancellationToken>,
>,
pub seq: std::sync::Arc<AtomicU64>,
}
impl SurrealState {
pub fn new() -> Self {
Self::default()
}
}
impl Default for SurrealState {
fn default() -> Self {
Self {
guard: std::sync::Arc::new(dashmap::DashMap::new()),
cancel: std::sync::Arc::new(dashmap::DashMap::new()),
seq: std::sync::Arc::new(AtomicU64::new(0)),
}
}
}
impl State {
pub fn new(
user: app::user::Service,
auth: app::auth::Service,
chat: app::chat::Service,
sse: crate::sse::Registry,
cookie_key: Key,
trace_log: crate::trace_log::Store,
) -> Self {
Self {
user,
auth,
chat,
sse,
cookie_key,
trace_log,
demo: DemoState::new(),
}
}
}
#[bon]
impl State {
#[builder]
pub fn builder(
#[builder(setters(name = with_user))] user: app::user::Service,
#[builder(setters(name = with_auth))] auth: app::auth::Service,
#[builder(setters(name = with_chat))] chat: app::chat::Service,
#[builder(setters(name = with_sse))] sse: crate::sse::Registry,
#[builder(setters(name = with_cookie_key))] cookie_key: Key,
#[builder(setters(name = with_trace_log))] trace_log: crate::trace_log::Store,
) -> Self {
Self::new(user, auth, chat, sse, cookie_key, trace_log)
}
}