Skip to content

Commit ad4d94e

Browse files
authored
chore: automatically map uid to client id (#434)
* chore: automatically map uid to client id * chore: add test to confirm that user data is not written automatically for readonly transactions * chore: improved editors between implementation
1 parent ca88546 commit ad4d94e

File tree

5 files changed

+205
-96
lines changed

5 files changed

+205
-96
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = ["collab"]
33
resolver = "2"
44

55
[workspace.dependencies]
6-
yrs = { version = "0.24", features = ["sync"] }
6+
yrs = { version = "0.25", features = ["sync"] }
77
anyhow = "1.0.94"
88
thiserror = "1.0.39"
99
serde = { version = "1.0.157", features = ["derive"] }

collab/src/core/collab.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use yrs::{
2929

3030
use crate::entity::{EncodedCollab, EncoderVersion};
3131
use crate::error::CollabError;
32-
use crate::preclude::JsonValue;
32+
use crate::preclude::{JsonValue, PermanentUserData};
3333
use uuid::Uuid;
3434

3535
pub const DATA_SECTION: &str = "data";
@@ -87,17 +87,25 @@ pub struct CollabContext {
8787
/// The current transaction that is being executed.
8888
current_txn: Option<TransactionMut<'static>>,
8989
version: Option<CollabVersion>,
90+
/// Structure managing list of editors.
91+
editors: Option<PermanentUserData>,
9092
}
9193

9294
unsafe impl Send for CollabContext {}
9395
unsafe impl Sync for CollabContext {}
9496

9597
impl CollabContext {
96-
fn new(origin: CollabOrigin, awareness: Awareness, version: Option<CollabVersion>) -> Self {
98+
fn new(
99+
origin: CollabOrigin,
100+
awareness: Awareness,
101+
version: Option<CollabVersion>,
102+
user_data: Option<PermanentUserData>,
103+
) -> Self {
97104
CollabContext {
98105
origin,
99106
awareness,
100107
version,
108+
editors: user_data,
101109
undo_manager: None,
102110
current_txn: None,
103111
}
@@ -111,6 +119,10 @@ impl CollabContext {
111119
&mut self.version
112120
}
113121

122+
pub fn user_data(&self) -> Option<&PermanentUserData> {
123+
self.editors.as_ref()
124+
}
125+
114126
pub fn with_txn<F, T>(&mut self, f: F) -> Result<T, CollabError>
115127
where
116128
F: FnOnce(&mut TransactionMut) -> T,
@@ -304,6 +316,7 @@ pub struct CollabOptions {
304316
pub data_source: Option<DataSource>,
305317
pub client_id: ClientID,
306318
pub skip_gc: bool,
319+
pub remember_user: bool,
307320
}
308321

309322
impl Display for CollabOptions {
@@ -324,6 +337,7 @@ impl CollabOptions {
324337
data_source: None,
325338
client_id,
326339
skip_gc: false,
340+
remember_user: false,
327341
}
328342
}
329343

@@ -332,6 +346,11 @@ impl CollabOptions {
332346
self
333347
}
334348

349+
pub fn with_remember_user(mut self, remember_user: bool) -> Self {
350+
self.remember_user = remember_user;
351+
self
352+
}
353+
335354
pub fn with_gc(mut self, gc: bool) -> Self {
336355
self.skip_gc = !gc;
337356
self
@@ -356,12 +375,18 @@ impl Collab {
356375
let plugins = Plugins::new(vec![]);
357376
let state = Arc::new(State::new(&object_id.to_string()));
358377
let awareness = Awareness::new(doc);
378+
let user_data = if options.remember_user {
379+
Some(PermanentUserData::new(awareness.doc(), origin.clone()))
380+
} else {
381+
None
382+
};
359383
let mut this = Self {
360384
object_id,
361385
context: CollabContext::new(
362386
origin,
363387
awareness,
364388
options.data_source.as_ref().and_then(DataSource::version),
389+
user_data,
365390
),
366391
state,
367392
data,
@@ -431,7 +456,7 @@ impl Collab {
431456
object_id,
432457
// if not the fact that we need origin here, it would be
433458
// not necessary either
434-
context: CollabContext::new(origin, awareness, None),
459+
context: CollabContext::new(origin, awareness, None, None),
435460
state,
436461
data,
437462
meta,

0 commit comments

Comments
 (0)