-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathdoc_connection.rs
More file actions
211 lines (186 loc) · 7.48 KB
/
Copy pathdoc_connection.rs
File metadata and controls
211 lines (186 loc) · 7.48 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::sync::{
self, awareness::Awareness, DefaultProtocol, Message, Protocol, SyncMessage, MSG_SYNC,
MSG_SYNC_UPDATE,
};
use std::sync::{Arc, OnceLock, RwLock};
use yrs::{
block::ClientID,
encoding::write::Write,
updates::{
decoder::Decode,
encoder::{Encode, Encoder, EncoderV1},
},
ReadTxn, Subscription, Transact, Update,
};
// TODO: this is an implementation detail and should not be exposed.
pub const DOC_NAME: &str = "doc";
#[cfg(not(feature = "sync"))]
type Callback = Arc<dyn Fn(&[u8]) + 'static>;
#[cfg(feature = "sync")]
type Callback = Arc<dyn Fn(&[u8]) + 'static + Send + Sync>;
const SYNC_STATUS_MESSAGE: u8 = 102;
pub struct DocConnection {
awareness: Arc<RwLock<Awareness>>,
#[allow(unused)] // acts as RAII guard
doc_subscription: Subscription,
#[allow(unused)] // acts as RAII guard
awareness_subscription: Subscription,
callback: Callback,
closed: Arc<OnceLock<()>>,
/// If the client sends an awareness state, this will be set to its client ID.
/// It is used to clear the awareness state when a client disconnects.
client_id: OnceLock<ClientID>,
}
impl DocConnection {
#[cfg(not(feature = "sync"))]
pub fn new<F>(awareness: Arc<RwLock<Awareness>>, callback: F) -> Self
where
F: Fn(&[u8]) + 'static,
{
Self::new_inner(awareness, Arc::new(callback))
}
#[cfg(feature = "sync")]
pub fn new<F>(awareness: Arc<RwLock<Awareness>>, callback: F) -> Self
where
F: Fn(&[u8]) + 'static + Send + Sync,
{
Self::new_inner(awareness, Arc::new(callback))
}
pub fn new_inner(awareness: Arc<RwLock<Awareness>>, callback: Callback) -> Self {
let closed = Arc::new(OnceLock::new());
let (doc_subscription, awareness_subscription) = {
let mut awareness = awareness.write().unwrap();
// Initial handshake is based on this:
// https://github.com/y-crdt/y-sync/blob/56958e83acfd1f3c09f5dd67cf23c9c72f000707/src/sync.rs#L45-L54
{
// Send a server-side state vector, so that the client can send
// updates that happened offline.
let sv = awareness.doc().transact().state_vector();
let sync_step_1 = Message::Sync(SyncMessage::SyncStep1(sv)).encode_v1();
callback(&sync_step_1);
}
{
// Send the initial awareness state.
let update = awareness.update().unwrap();
let awareness = Message::Awareness(update).encode_v1();
callback(&awareness);
}
let doc_subscription = {
let doc = awareness.doc();
let callback = callback.clone();
let closed = closed.clone();
doc.observe_update_v1(move |_, event| {
if closed.get().is_some() {
return;
}
// https://github.com/y-crdt/y-sync/blob/56958e83acfd1f3c09f5dd67cf23c9c72f000707/src/net/broadcast.rs#L47-L52
let mut encoder = EncoderV1::new();
encoder.write_var(MSG_SYNC);
encoder.write_var(MSG_SYNC_UPDATE);
encoder.write_buf(&event.update);
let msg = encoder.to_vec();
callback(&msg);
})
.unwrap()
};
let callback = callback.clone();
let closed = closed.clone();
let awareness_subscription = awareness.on_update(move |awareness, e| {
if closed.get().is_some() {
return;
}
// https://github.com/y-crdt/y-sync/blob/56958e83acfd1f3c09f5dd67cf23c9c72f000707/src/net/broadcast.rs#L59
let added = e.added();
let updated = e.updated();
let removed = e.removed();
let mut changed = Vec::with_capacity(added.len() + updated.len() + removed.len());
changed.extend_from_slice(added);
changed.extend_from_slice(updated);
changed.extend_from_slice(removed);
if let Ok(u) = awareness.update_with_clients(changed) {
let msg = Message::Awareness(u).encode_v1();
callback(&msg);
}
});
(doc_subscription, awareness_subscription)
};
Self {
awareness,
doc_subscription,
awareness_subscription,
callback,
client_id: OnceLock::new(),
closed,
}
}
pub async fn send(&self, update: &[u8]) -> Result<(), anyhow::Error> {
let msg = Message::decode_v1(update)?;
let result = self.handle_msg(&DefaultProtocol, msg)?;
if let Some(result) = result {
let msg = result.encode_v1();
(self.callback)(&msg);
}
Ok(())
}
// Adapted from:
// https://github.com/y-crdt/y-sync/blob/56958e83acfd1f3c09f5dd67cf23c9c72f000707/src/net/conn.rs#L184C1-L222C1
pub fn handle_msg<P: Protocol>(
&self,
protocol: &P,
msg: Message,
) -> Result<Option<Message>, sync::Error> {
let a = &self.awareness;
match msg {
Message::Sync(msg) => match msg {
SyncMessage::SyncStep1(sv) => {
let awareness = a.read().unwrap();
protocol.handle_sync_step1(&awareness, sv)
}
SyncMessage::SyncStep2(update) => {
let mut awareness = a.write().unwrap();
protocol.handle_sync_step2(&mut awareness, Update::decode_v1(&update)?)
}
SyncMessage::Update(update) => {
let mut awareness = a.write().unwrap();
protocol.handle_update(&mut awareness, Update::decode_v1(&update)?)
}
},
Message::Auth(reason) => {
let awareness = a.read().unwrap();
protocol.handle_auth(&awareness, reason)
}
Message::AwarenessQuery => {
let awareness = a.read().unwrap();
protocol.handle_awareness_query(&awareness)
}
Message::Awareness(update) => {
if update.clients.len() == 1 {
let client_id = update.clients.keys().next().unwrap();
self.client_id.get_or_init(|| *client_id);
} else {
tracing::warn!("Received awareness update with more than one client");
}
let mut awareness = a.write().unwrap();
protocol.handle_awareness_update(&mut awareness, update)
}
Message::Custom(SYNC_STATUS_MESSAGE, data) => {
// Respond to the client with the same payload it sent.
Ok(Some(Message::Custom(SYNC_STATUS_MESSAGE, data)))
}
Message::Custom(tag, data) => {
let mut awareness = a.write().unwrap();
protocol.missing_handle(&mut awareness, tag, data)
}
}
}
}
impl Drop for DocConnection {
fn drop(&mut self) {
self.closed.set(()).unwrap();
// If this client had an awareness state, remove it.
if let Some(client_id) = self.client_id.get() {
let mut awareness = self.awareness.write().unwrap();
awareness.remove_state(*client_id);
}
}
}