y-rb 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3ae07975f48b94070d7eaf2f6e91ca62d32a8ce1d6053d09c4239d5838e2216
4
- data.tar.gz: 9b9786906b90c863509d32a1caf251921536d40cc7fb63e3be9409e56a608cad
3
+ metadata.gz: 67c773a475fbd0dda50a445e6f259e743b5cef16ebfea7c9e257e257d2208deb
4
+ data.tar.gz: 4a20e56183f89626e25f346425e6485450306ea2023e34198dfb1229711049bb
5
5
  SHA512:
6
- metadata.gz: d3ae3107801866825d56b010efc882e4cdf731bda4ac66b16cbf2803d188ded25043fc4fc34de7402a2bf25daa93e9965d4366192ef5f62782d98ecc8c2b58c2
7
- data.tar.gz: 96ba6a765ca4bd22405014c1c0485a5ce889ea7f77c5645a64240a1c55457c5582df2b663a5cdebb672c6ec19823e4f032102ec4eafc6e62fa51925ebed39ffd
6
+ metadata.gz: 15afe5cc508d34265c36ed5d77f784c9f00e15dc253573baafbbc848f1ec90c783ecce19a0234f6001c9d740897d54b4fad4941cd80f6536d4e2da4dfdbd8b05
7
+ data.tar.gz: 4853082536f8ee2d25a5db9b321230bc26f543c5a3b8e30716537f0b97ed7b7531e3a025955d8543c6a203d1e3dd434e1a3e2e736132b8314edfd4639f0d08db
data/ext/yrb/Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "yrb"
3
- version = "0.2.0"
3
+ version = "0.3.0"
4
4
  authors = ["Hannes Moser <box@hannesmoser.at>", "Hannes Moser <hmoser@gitlab.com>"]
5
5
  edition = "2021"
6
6
  homepage = "https://github.com/y-crdt/yrb"
@@ -27,7 +27,7 @@ pub struct Awareness {
27
27
  doc: Doc,
28
28
  states: HashMap<ClientID, String>,
29
29
  meta: HashMap<ClientID, MetaClientState>,
30
- on_update: Option<EventHandler<Event>>
30
+ on_update: Option<EventHandler<Event>>,
31
31
  }
32
32
 
33
33
  unsafe impl Send for Awareness {}
@@ -42,14 +42,14 @@ impl Awareness {
42
42
  doc,
43
43
  on_update: None,
44
44
  states: HashMap::new(),
45
- meta: HashMap::new()
45
+ meta: HashMap::new(),
46
46
  }
47
47
  }
48
48
 
49
49
  /// Returns a channel receiver for an incoming awareness events. This channel can be cloned.
50
50
  pub fn on_update<F>(&mut self, f: F) -> Subscription<Event>
51
51
  where
52
- F: Fn(&Awareness, &Event) -> () + 'static
52
+ F: Fn(&Awareness, &Event) + 'static,
53
53
  {
54
54
  let eh = self.on_update.get_or_insert_with(EventHandler::default);
55
55
  eh.subscribe(f)
@@ -91,19 +91,13 @@ impl Awareness {
91
91
  Entry::Occupied(mut e) => {
92
92
  e.insert(new);
93
93
  if let Some(eh) = self.on_update.as_ref() {
94
- eh.trigger(
95
- self,
96
- &Event::new(vec![], vec![client_id], vec![])
97
- );
94
+ eh.trigger(self, &Event::new(vec![], vec![client_id], vec![]));
98
95
  }
99
96
  }
100
97
  Entry::Vacant(e) => {
101
98
  e.insert(new);
102
99
  if let Some(eh) = self.on_update.as_ref() {
103
- eh.trigger(
104
- self,
105
- &Event::new(vec![client_id], vec![], vec![])
106
- );
100
+ eh.trigger(self, &Event::new(vec![client_id], vec![], vec![]));
107
101
  }
108
102
  }
109
103
  }
@@ -117,11 +111,7 @@ impl Awareness {
117
111
  if prev_state.is_some() {
118
112
  eh.trigger(
119
113
  self,
120
- &Event::new(
121
- Vec::default(),
122
- Vec::default(),
123
- vec![client_id]
124
- )
114
+ &Event::new(Vec::default(), Vec::default(), vec![client_id]),
125
115
  );
126
116
  }
127
117
  }
@@ -159,7 +149,7 @@ impl Awareness {
159
149
  /// otherwise a [Error::ClientNotFound] error will be returned.
160
150
  pub fn update_with_clients<I: IntoIterator<Item = ClientID>>(
161
151
  &self,
162
- clients: I
152
+ clients: I,
163
153
  ) -> Result<AwarenessUpdate, Error> {
164
154
  let mut res = HashMap::new();
165
155
  for client_id in clients {
@@ -183,10 +173,7 @@ impl Awareness {
183
173
  ///
184
174
  /// If current instance has an observer channel (see: [Awareness::with_observer]), applied
185
175
  /// changes will also be emitted as events.
186
- pub fn apply_update(
187
- &mut self,
188
- update: AwarenessUpdate
189
- ) -> Result<(), Error> {
176
+ pub fn apply_update(&mut self, update: AwarenessUpdate) -> Result<(), Error> {
190
177
  let now = Instant::now();
191
178
 
192
179
  let mut added = Vec::new();
@@ -199,9 +186,8 @@ impl Awareness {
199
186
  match self.meta.entry(client_id) {
200
187
  Entry::Occupied(mut e) => {
201
188
  let prev = e.get();
202
- let is_removed = prev.clock == clock
203
- && is_null
204
- && self.states.contains_key(&client_id);
189
+ let is_removed =
190
+ prev.clock == clock && is_null && self.states.contains_key(&client_id);
205
191
  let is_new = prev.clock < clock;
206
192
  if is_new || is_removed {
207
193
  if is_null {
@@ -267,15 +253,16 @@ impl Default for Awareness {
267
253
  }
268
254
  }
269
255
 
256
+ #[allow(clippy::type_complexity)]
270
257
  struct EventHandler<T> {
271
258
  seq_nr: u32,
272
- subscribers: Rc<RefCell<HashMap<u32, Box<dyn Fn(&Awareness, &T) -> ()>>>>
259
+ subscribers: Rc<RefCell<HashMap<u32, Box<dyn Fn(&Awareness, &T)>>>>,
273
260
  }
274
261
 
275
262
  impl<T> EventHandler<T> {
276
263
  pub fn subscribe<F>(&mut self, f: F) -> Subscription<T>
277
264
  where
278
- F: Fn(&Awareness, &T) -> () + 'static
265
+ F: Fn(&Awareness, &T) + 'static,
279
266
  {
280
267
  let subscription_id = self.seq_nr;
281
268
  self.seq_nr += 1;
@@ -286,7 +273,7 @@ impl<T> EventHandler<T> {
286
273
  }
287
274
  Subscription {
288
275
  subscription_id,
289
- subscribers: Rc::downgrade(&self.subscribers)
276
+ subscribers: Rc::downgrade(&self.subscribers),
290
277
  }
291
278
  }
292
279
 
@@ -307,18 +294,20 @@ impl<T> Default for EventHandler<T> {
307
294
  fn default() -> Self {
308
295
  EventHandler {
309
296
  seq_nr: 0,
310
- subscribers: Rc::new(RefCell::new(HashMap::new()))
297
+ subscribers: Rc::new(RefCell::new(HashMap::new())),
311
298
  }
312
299
  }
313
300
  }
314
301
 
315
302
  /// Whenever a new callback is being registered, a [Subscription] is made. Whenever this
316
303
  /// subscription a registered callback is cancelled and will not be called any more.
304
+ #[allow(clippy::type_complexity)]
317
305
  pub struct Subscription<T> {
318
306
  subscription_id: u32,
319
- subscribers: Weak<RefCell<HashMap<u32, Box<dyn Fn(&Awareness, &T) -> ()>>>>
307
+ subscribers: Weak<RefCell<HashMap<u32, Box<dyn Fn(&Awareness, &T)>>>>,
320
308
  }
321
309
 
310
+ #[allow(clippy::from_over_into)]
322
311
  impl<T> Into<SubscriptionId> for Subscription<T> {
323
312
  fn into(self) -> SubscriptionId {
324
313
  let id = self.subscription_id;
@@ -339,7 +328,7 @@ impl<T> Drop for Subscription<T> {
339
328
  /// A structure that represents an encodable state of an [Awareness] struct.
340
329
  #[derive(Debug, Eq, PartialEq)]
341
330
  pub struct AwarenessUpdate {
342
- clients: HashMap<ClientID, AwarenessUpdateEntry>
331
+ clients: HashMap<ClientID, AwarenessUpdateEntry>,
343
332
  }
344
333
 
345
334
  impl Encode for AwarenessUpdate {
@@ -373,7 +362,7 @@ impl Decode for AwarenessUpdate {
373
362
  #[derive(Debug, Eq, PartialEq)]
374
363
  pub struct AwarenessUpdateEntry {
375
364
  clock: u32,
376
- json: String
365
+ json: String,
377
366
  }
378
367
 
379
368
  /// Errors generated by an [Awareness] struct methods.
@@ -381,20 +370,20 @@ pub struct AwarenessUpdateEntry {
381
370
  pub enum Error {
382
371
  /// Client ID was not found in [Awareness] metadata.
383
372
  #[error("client ID `{0}` not found")]
384
- ClientNotFound(ClientID)
373
+ ClientNotFound(ClientID),
385
374
  }
386
375
 
387
376
  #[derive(Debug, Clone, PartialEq, Eq)]
388
377
  struct MetaClientState {
389
378
  clock: u32,
390
- last_updated: Instant
379
+ last_updated: Instant,
391
380
  }
392
381
 
393
382
  impl MetaClientState {
394
383
  fn new(clock: u32, last_updated: Instant) -> Self {
395
384
  MetaClientState {
396
385
  clock,
397
- last_updated
386
+ last_updated,
398
387
  }
399
388
  }
400
389
  }
@@ -404,19 +393,15 @@ impl MetaClientState {
404
393
  pub struct Event {
405
394
  added: Vec<ClientID>,
406
395
  updated: Vec<ClientID>,
407
- removed: Vec<ClientID>
396
+ removed: Vec<ClientID>,
408
397
  }
409
398
 
410
399
  impl Event {
411
- pub fn new(
412
- added: Vec<ClientID>,
413
- updated: Vec<ClientID>,
414
- removed: Vec<ClientID>
415
- ) -> Self {
400
+ pub fn new(added: Vec<ClientID>, updated: Vec<ClientID>, removed: Vec<ClientID>) -> Self {
416
401
  Event {
417
402
  added,
418
403
  updated,
419
- removed
404
+ removed,
420
405
  }
421
406
  }
422
407