loro_internal/
subscription.rs

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
use super::{
    arena::SharedArena,
    event::{DiffEvent, DocDiff},
};
use crate::{
    container::idx::ContainerIdx, utils::subscription::SubscriberSet, ContainerDiff, LoroDoc,
    Subscription,
};
use fxhash::FxHashMap;
use loro_common::{ContainerID, ID};
use smallvec::SmallVec;
use std::{
    collections::VecDeque,
    sync::{Arc, Mutex},
};

/// The callback of the local update.
pub type LocalUpdateCallback = Box<dyn Fn(&Vec<u8>) -> bool + Send + Sync + 'static>;
/// The callback of the peer id change. The second argument is the next counter for the peer.
pub type PeerIdUpdateCallback = Box<dyn Fn(&ID) -> bool + Send + Sync + 'static>;
pub type Subscriber = Arc<dyn (for<'a> Fn(DiffEvent<'a>)) + Send + Sync>;

impl LoroDoc {
    /// Subscribe to the changes of the peer id.
    pub fn subscribe_peer_id_change(&self, callback: PeerIdUpdateCallback) -> Subscription {
        let (s, enable) = self.peer_id_change_subs.inner().insert((), callback);
        enable();
        s
    }
}

struct ObserverInner {
    subscriber_set: SubscriberSet<Option<ContainerIdx>, Subscriber>,
    queue: Arc<Mutex<VecDeque<DocDiff>>>,
}

impl Default for ObserverInner {
    fn default() -> Self {
        Self {
            subscriber_set: SubscriberSet::new(),
            queue: Arc::new(Mutex::new(VecDeque::new())),
        }
    }
}

pub struct Observer {
    inner: ObserverInner,
    arena: SharedArena,
}

impl std::fmt::Debug for Observer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Observer").finish()
    }
}

impl Observer {
    pub fn new(arena: SharedArena) -> Self {
        Self {
            arena,
            inner: ObserverInner::default(),
        }
    }

    pub fn subscribe(&self, id: &ContainerID, callback: Subscriber) -> Subscription {
        let idx = self.arena.register_container(id);
        let inner = &self.inner;
        let (sub, enable) = inner.subscriber_set.insert(Some(idx), callback);
        enable();
        sub
    }

    pub fn subscribe_root(&self, callback: Subscriber) -> Subscription {
        let inner = &self.inner;
        let (sub, enable) = inner.subscriber_set.insert(None, callback);
        enable();
        sub
    }

    pub(crate) fn emit(&self, doc_diff: DocDiff) {
        let success = self.emit_inner(doc_diff);
        if success {
            let mut e = self.inner.queue.try_lock().unwrap().pop_front();
            while let Some(event) = e {
                self.emit_inner(event);
                e = self.inner.queue.try_lock().unwrap().pop_front();
            }
        }
    }

    // When emitting changes, we need to make sure that the observer is not locked.
    fn emit_inner(&self, doc_diff: DocDiff) -> bool {
        let inner = &self.inner;
        let mut container_events_map: FxHashMap<ContainerIdx, SmallVec<[&ContainerDiff; 1]>> =
            Default::default();
        for container_diff in doc_diff.diff.iter() {
            self.arena
                .with_ancestors(container_diff.idx, |ancestor, _| {
                    if inner.subscriber_set.may_include(&Some(ancestor)) {
                        container_events_map
                            .entry(ancestor)
                            .or_default()
                            .push(container_diff);
                    }
                });
        }

        {
            // Check whether we are calling events recursively.
            // If so, push the event to the queue
            if inner.subscriber_set.is_recursive_calling(&None)
                || container_events_map
                    .keys()
                    .any(|x| inner.subscriber_set.is_recursive_calling(&Some(*x)))
            {
                drop(container_events_map);
                inner.queue.try_lock().unwrap().push_back(doc_diff);
                return false;
            }
        }

        for (container_idx, container_diffs) in container_events_map {
            inner
                .subscriber_set
                .retain(&Some(container_idx), &mut |callback| {
                    (callback)(DiffEvent {
                        current_target: Some(self.arena.get_container_id(container_idx).unwrap()),
                        events: &container_diffs,
                        event_meta: &doc_diff,
                    });
                    true
                })
                .unwrap();
        }

        let events: Vec<_> = doc_diff.diff.iter().collect();
        inner
            .subscriber_set
            .retain(&None, &mut |callback| {
                (callback)(DiffEvent {
                    current_target: None,
                    events: &events,
                    event_meta: &doc_diff,
                });
                true
            })
            .unwrap();

        true
    }
}

#[cfg(test)]
mod test {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use tracing::trace;

    use super::*;
    use crate::{handler::HandlerTrait, LoroDoc};

    #[test]
    fn test_recursive_events() {
        let loro = Arc::new(LoroDoc::new());
        let loro_cp = loro.clone();
        let count = Arc::new(AtomicUsize::new(0));
        let count_cp = Arc::clone(&count);
        let _g = loro_cp.subscribe_root(Arc::new(move |_| {
            count_cp.fetch_add(1, Ordering::SeqCst);
            let mut txn = loro.txn().unwrap();
            let text = loro.get_text("id");
            if text.get_value().as_string().unwrap().len() > 10 {
                trace!("Skip");
                return;
            }
            text.insert_with_txn(&mut txn, 0, "123").unwrap();
            trace!("PRE Another commit");
            txn.commit().unwrap();
            trace!("AFTER Another commit");
        }));

        let loro = loro_cp;
        let mut txn = loro.txn().unwrap();
        let text = loro.get_text("id");
        text.insert_with_txn(&mut txn, 0, "123").unwrap();
        txn.commit().unwrap();
        let count = count.load(Ordering::SeqCst);
        assert!(count > 2, "{}", count);
    }

    #[test]
    fn unsubscribe() {
        let loro = Arc::new(LoroDoc::new());
        let count = Arc::new(AtomicUsize::new(0));
        let count_cp = Arc::clone(&count);
        let sub = loro.subscribe_root(Arc::new(move |_| {
            count_cp.fetch_add(1, Ordering::SeqCst);
        }));

        let text = loro.get_text("id");

        assert_eq!(count.load(Ordering::SeqCst), 0);
        {
            let mut txn = loro.txn().unwrap();
            text.insert_with_txn(&mut txn, 0, "123").unwrap();
            txn.commit().unwrap();
        }
        assert_eq!(count.load(Ordering::SeqCst), 1);
        {
            let mut txn = loro.txn().unwrap();
            text.insert_with_txn(&mut txn, 0, "123").unwrap();
            txn.commit().unwrap();
        }
        assert_eq!(count.load(Ordering::SeqCst), 2);
        sub.unsubscribe();
        {
            let mut txn = loro.txn().unwrap();
            text.insert_with_txn(&mut txn, 0, "123").unwrap();
            txn.commit().unwrap();
        }
        assert_eq!(count.load(Ordering::SeqCst), 2);
    }
}