yrs 0.18.8

High performance implementation of the Yjs CRDT
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use crate::TransactionMut;
use arc_swap::ArcSwapOption;
use smallvec::{smallvec, SmallVec};
use std::fmt::Debug;
use std::sync::{Arc, Weak};

pub type Callback<E> = Arc<dyn Fn(&TransactionMut, &E) + 'static>;
type WeakCallback<E> = Weak<dyn Fn(&TransactionMut, &E) + 'static>;
type WeakCallbacks<E> = SmallVec<[WeakCallback<E>; 1]>;

/// Data structure used to handle publish/subscribe callbacks of specific type. Observers perform
/// subscriber changes in thread-safe manner, using atomic hardware intrinsics.
#[derive(Debug)]
pub struct Observer<E> {
    inner: ArcSwapOption<WeakCallbacks<E>>,
}

unsafe impl<E: Send> Send for Observer<E> {}
unsafe impl<E: Sync> Sync for Observer<E> {}

impl<E> Observer<E> {
    /// Creates a new [Observer] with no active callbacks.
    pub fn new() -> Self {
        Observer {
            inner: ArcSwapOption::new(None),
        }
    }

    pub fn has_subscribers(&self) -> bool {
        let callbacks = self.inner.load();
        if let Some(callbacks) = &*callbacks {
            !callbacks.is_empty()
        } else {
            false
        }
    }

    /// Cleanup already released subscriptions. Whenever a [Subscription] is dropped, the callback is released. However,
    /// the weak reference to callback may still be kept around until it becomes touched by operations such as
    /// [Observer::subscribe] or [Observer::callbacks].
    ///
    /// This method allows to perform stale callback cleanup without waiting for callbacks to be visited.
    pub fn clean(&self) {
        self.inner.rcu(|callbacks| match callbacks {
            None => None,
            Some(links) => {
                let mut res = WeakCallbacks::with_capacity(links.len() - 1);
                for l in links.iter() {
                    if Weak::strong_count(l) != 0 {
                        res.push(l.clone());
                    }
                }
                Some(Arc::new(res))
            }
        });
    }

    /// Subscribes a callback parameter to a current [Observer].
    /// Returns a subscription object which - when dropped - will unsubscribe current callback.
    pub fn subscribe<F>(&self, callback: F) -> Subscription
    where
        F: Fn(&TransactionMut, &E) -> () + 'static,
    {
        let strong = Arc::new(callback);
        let subscription: Arc<dyn std::any::Any> = strong.clone();
        let generic: Callback<E> = strong;
        let weak = Arc::downgrade(&generic);
        self.inner.rcu(|callbacks| match callbacks {
            None => Arc::new(smallvec![weak.clone()]),
            Some(links) => {
                let mut res = WeakCallbacks::with_capacity(links.len() + 1);
                for l in links.iter() {
                    if Weak::strong_count(l) != 0 {
                        res.push(l.clone());
                    }
                }
                res.push(weak.clone());
                Arc::new(res)
            }
        });
        Subscription {
            callback: subscription,
        }
    }

    /// Returns a snapshot of callbacks subscribed to this observer at the moment when this method
    /// has been called. This snapshot can be iterated over to get access to individual callbacks
    /// and trigger them.
    pub fn callbacks(&self) -> Option<Callbacks<E>> {
        Callbacks::new(self)
    }
}

impl<E> Default for Observer<E> {
    fn default() -> Self {
        Observer {
            inner: ArcSwapOption::from(None),
        }
    }
}

#[derive(Debug)]
pub struct Callbacks<'a, E> {
    observer: &'a Observer<E>,
    callbacks: Arc<WeakCallbacks<E>>,
    index: usize,
    should_cleanup: bool,
}

unsafe impl<'a, E: Send> Send for Callbacks<'a, E> {}
unsafe impl<'a, E: Sync> Sync for Callbacks<'a, E> {}

impl<'a, E> Callbacks<'a, E> {
    fn new(observer: &'a Observer<E>) -> Option<Self> {
        let callbacks = observer.inner.load_full()?;
        Some(Callbacks {
            observer,
            callbacks,
            index: 0,
            should_cleanup: false,
        })
    }

    pub fn trigger(&mut self, txn: &TransactionMut, e: &E) {
        for cb in self {
            cb(txn, e);
        }
    }
}

impl<'a, E> Iterator for Callbacks<'a, E> {
    type Item = Callback<E>;

    fn next(&mut self) -> Option<Self::Item> {
        let callbacks = &*self.callbacks;
        while self.index < callbacks.len() {
            let weak = &callbacks[self.index];
            self.index += 1;
            if let Some(strong) = weak.upgrade() {
                return Some(strong);
            }
        }
        None
    }
}

impl<'a, E> Drop for Callbacks<'a, E> {
    fn drop(&mut self) {
        if self.should_cleanup {
            self.observer.clean();
        }
    }
}

pub type CallbackMut<E> = Arc<dyn Fn(&mut TransactionMut, &mut E) + 'static>;
type WeakCallbackMut<E> = Weak<dyn Fn(&mut TransactionMut, &mut E) + 'static>;
type WeakCallbacksMut<E> = SmallVec<[WeakCallbackMut<E>; 1]>;

/// Data structure used to handle publish/subscribe callbacks of specific type. Observers perform
/// subscriber changes in thread-safe manner, using atomic hardware intrinsics.
#[derive(Debug)]
pub struct ObserverMut<E> {
    inner: ArcSwapOption<WeakCallbacksMut<E>>,
}

unsafe impl<E: Send> Send for ObserverMut<E> {}
unsafe impl<E: Sync> Sync for ObserverMut<E> {}

impl<E> ObserverMut<E> {
    /// Creates a new [ObserverMut with no active callbacks.
    pub fn new() -> Self {
        ObserverMut {
            inner: ArcSwapOption::new(None),
        }
    }

    /// Cleanup already released subscriptions. Whenever a [Subscription] is dropped, the callback is released. However,
    /// the weak reference to callback may still be kept around until it becomes touched by operations such as
    /// [Observer::subscribe] or [Observer::callbacks].
    ///
    /// This method allows to perform stale callback cleanup without waiting for callbacks to be visited.
    pub fn clean(&self) {
        self.inner.rcu(|callbacks| match callbacks {
            None => None,
            Some(links) => {
                let mut res = WeakCallbacksMut::with_capacity(links.len() - 1);
                for l in links.iter() {
                    if Weak::strong_count(l) != 0 {
                        res.push(l.clone());
                    }
                }
                Some(Arc::new(res))
            }
        });
    }

    /// Subscribes a callback parameter to a current [Observer].
    /// Returns a subscription object which - when dropped - will unsubscribe current callback.
    pub fn subscribe<F>(&self, callback: F) -> Subscription
    where
        F: Fn(&mut TransactionMut, &mut E) + 'static,
    {
        let strong = Arc::new(callback);
        let subscription: Arc<dyn std::any::Any> = strong.clone();
        let generic: CallbackMut<E> = strong;
        let weak = Arc::downgrade(&generic);
        self.inner.rcu(|callbacks| match callbacks {
            None => Arc::new(smallvec![weak.clone()]),
            Some(links) => {
                let mut res = WeakCallbacksMut::with_capacity(links.len() + 1);
                for l in links.iter() {
                    if Weak::strong_count(l) != 0 {
                        res.push(l.clone());
                    }
                }
                res.push(weak.clone());
                Arc::new(res)
            }
        });
        Subscription {
            callback: subscription,
        }
    }

    /// Returns a snapshot of callbacks subscribed to this observer at the moment when this method
    /// has been called. This snapshot can be iterated over to get access to individual callbacks
    /// and trigger them.
    pub fn callbacks(&self) -> Option<CallbacksMut<E>> {
        CallbacksMut::new(self)
    }
}

impl<E> Default for ObserverMut<E> {
    fn default() -> Self {
        ObserverMut {
            inner: ArcSwapOption::from(None),
        }
    }
}

#[derive(Debug)]
pub struct CallbacksMut<'a, E> {
    observer: &'a ObserverMut<E>,
    callbacks: Arc<WeakCallbacksMut<E>>,
    index: usize,
    should_cleanup: bool,
}

unsafe impl<'a, E: Send> Send for CallbacksMut<'a, E> {}
unsafe impl<'a, E: Sync> Sync for CallbacksMut<'a, E> {}

impl<'a, E> CallbacksMut<'a, E> {
    fn new(observer: &'a ObserverMut<E>) -> Option<Self> {
        let callbacks = observer.inner.load_full()?;
        Some(CallbacksMut {
            observer,
            callbacks,
            index: 0,
            should_cleanup: false,
        })
    }

    pub fn trigger(&mut self, txn: &mut TransactionMut, e: &mut E) {
        for cb in self {
            cb(txn, e);
        }
    }
}

impl<'a, E> Iterator for CallbacksMut<'a, E> {
    type Item = CallbackMut<E>;

    fn next(&mut self) -> Option<Self::Item> {
        let callbacks = &*self.callbacks;
        while self.index < callbacks.len() {
            let weak = &callbacks[self.index];
            self.index += 1;
            if let Some(strong) = weak.upgrade() {
                return Some(strong);
            }
        }
        None
    }
}

impl<'a, E> Drop for CallbacksMut<'a, E> {
    fn drop(&mut self) {
        if self.should_cleanup {
            self.observer.clean();
        }
    }
}

/// Subscription handle returned by [Observer::subscribe] methods, which will unsubscribe corresponding
/// callback when dropped.
///
/// If implicit callback unsubscribe on drop is undesired, this structure can be cast [into](Subscription::into)
/// [SubscriptionId] which is an identifier of the same subscription, which in turn must be used
/// manually via [Observer::unsubscribe] to perform usubscribe.
#[repr(transparent)]
pub struct Subscription {
    callback: Arc<dyn std::any::Any>,
}

unsafe impl Send for Subscription {}
unsafe impl Sync for Subscription {}

#[cfg(test)]
mod test {
    use crate::observer::Observer;
    use crate::Transact;
    use std::sync::atomic::{AtomicU32, Ordering};
    use std::sync::Arc;
    use std::thread::spawn;

    #[test]
    fn subscription() {
        let doc = crate::Doc::new();
        let txn = doc.transact_mut();
        let o: Observer<u32> = Observer::new();
        let s1_state = Arc::new(AtomicU32::new(0));
        let s2_state = Arc::new(AtomicU32::new(0));

        {
            let a = s1_state.clone();
            let b = s2_state.clone();

            let _s1 = o.subscribe(move |_, &value| a.store(value, Ordering::Release));
            let _s2 = o.subscribe(move |_, &value| b.store(value * 2, Ordering::Release));

            for fun in o.callbacks().unwrap() {
                fun(&txn, &1)
            }
            assert_eq!(s1_state.load(Ordering::Acquire), 1);
            assert_eq!(s2_state.load(Ordering::Acquire), 2);

            for fun in o.callbacks().unwrap() {
                fun(&txn, &2)
            }
            assert_eq!(s1_state.load(Ordering::Acquire), 2);
            assert_eq!(s2_state.load(Ordering::Acquire), 4);
        }

        // subscriptions were dropped, we don't expect updates to be propagated
        for fun in o.callbacks().unwrap() {
            fun(&txn, &3)
        }
        assert_eq!(s1_state.load(Ordering::Acquire), 2);
        assert_eq!(s2_state.load(Ordering::Acquire), 4);
    }

    #[test]
    fn subscribers_predicate() {
        let o: Observer<u32> = Observer::new();
        assert!(!o.has_subscribers());

        let _sub = o.subscribe(move |_txn, _e| {});
        assert!(o.has_subscribers());

        drop(_sub);
        o.clean();

        assert!(!o.has_subscribers());
    }

    #[test]
    fn multi_threading() {
        let o: Observer<u32> = Observer::new();

        let s1_state = Arc::new(AtomicU32::new(0));
        let a = s1_state.clone();
        let sub1 = o.subscribe(move |_, &value| a.store(value, Ordering::Release));

        let s2_state = Arc::new(AtomicU32::new(0));
        let b = s2_state.clone();
        let sub2 = o.subscribe(move |_, &value| b.store(value, Ordering::Release));

        let handle = spawn(move || {
            let doc = crate::Doc::new();
            let txn = doc.transact_mut();
            for fun in o.callbacks().unwrap() {
                fun(&txn, &1)
            }
            drop(sub1);
            drop(sub2);
        });

        handle.join().unwrap();

        assert_eq!(s1_state.load(Ordering::Acquire), 1);
        assert_eq!(s2_state.load(Ordering::Acquire), 1);
    }

    #[test]
    fn unsubscribe_calls_drop() {
        let o: Observer<()> = Observer::new();
        let c = Arc::new(());
        let subscription = {
            let inner = c.clone();
            o.subscribe(move |_, _| {
                let count = Arc::strong_count(&inner);
                assert_eq!(count, 2);
            })
        };
        let doc = crate::Doc::new();
        let txn = doc.transact_mut();
        for cb in o.callbacks().unwrap() {
            cb(&txn, &());
        }
        drop(subscription);

        let count = Arc::strong_count(&c);
        assert_eq!(count, 1);
    }
}