loro 1.13.0

Loro is a high-performance CRDTs framework. Make your app collaborative efforlessly.
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Loro event handling.
use delta::array_vec::ArrayVec;
use delta::DeltaRope;
use enum_as_inner::EnumAsInner;
use loro_common::IdLp;
use loro_internal::container::ContainerID;
pub use loro_internal::delta::TreeDiff;
use loro_internal::delta::{ResolvedMapDelta, ResolvedMapValue};
use loro_internal::event::{EventTriggerKind, ListDeltaMeta};
use loro_internal::handler::{TextDelta, ValueOrHandler};
use loro_internal::undo::DiffBatch as InnerDiffBatch;
use loro_internal::{
    event::{Diff as DiffInner, Index},
    ContainerDiff as ContainerDiffInner, DiffEvent as DiffEventInner,
};
use loro_internal::{FxHashMap, ListDiffInsertItem};
use std::borrow::Cow;
use std::ops::Deref;
use std::sync::Arc;

use crate::ValueOrContainer;
use crate::{LoroError, LoroResult};

/// A subscriber to the event.
#[allow(clippy::unused_unit)]
pub type Subscriber = Arc<dyn for<'a> Fn(DiffEvent<'a>) + Send + Sync>;

/// An event that is triggered by a change in the state of a [super::LoroDoc].
#[derive(Debug)]
pub struct DiffEvent<'a> {
    /// How the event is triggered.
    pub triggered_by: EventTriggerKind,
    /// The origin of the event.
    pub origin: &'a str,
    /// The current receiver of the event.
    pub current_target: Option<ContainerID>,
    /// The diffs of the event.
    pub events: Vec<ContainerDiff<'a>>,
}

/// A diff of a container.
#[derive(Debug)]
pub struct ContainerDiff<'a> {
    /// The target container id of the diff.
    pub target: &'a ContainerID,
    /// The path of the diff.
    pub path: &'a [(ContainerID, Index)],
    /// Whether the diff is from unknown container.
    pub is_unknown: bool,
    /// The diff
    pub diff: Diff<'a>,
}

/// A concrete diff.
#[derive(Debug, EnumAsInner, Clone)]
pub enum Diff<'a> {
    /// A list diff.
    List(Vec<ListDiffItem>),
    /// A text diff.
    Text(Vec<TextDelta>),
    /// A map diff.
    Map(MapDelta<'a>),
    /// A tree diff.
    Tree(Cow<'a, TreeDiff>),
    #[cfg(feature = "counter")]
    /// A counter diff.
    Counter(f64),
    /// An unknown diff.
    Unknown,
}

/// A list diff item.
///
/// We use a `Vec<ListDiffItem>` to represent a list diff.
///
/// Each item can be either an insert, delete, or retain.
///
/// ## Example
///
/// `[Retain(3), Delete(1), Insert{insert: [Value(1), Value(2)], is_move: false}]`
///
/// It means that the list has 3 elements that are not changed, 1 element is deleted, and 2 elements are inserted.
///
/// If the original list is [1, 2, 3, 4, 5], the list after the diff is [1, 2, 3, 1, 2, 5].
#[derive(Debug, Clone)]
pub enum ListDiffItem {
    /// Insert a new element into the list.
    Insert {
        /// The new elements to insert.
        insert: Vec<ValueOrContainer>,
        /// Whether the new elements are created by moving
        is_move: bool,
    },
    /// Delete n elements from the list at the current index.
    Delete {
        /// The number of elements to delete.
        delete: usize,
    },
    /// Retain n elements in the list.
    ///
    /// This is used to keep the current index unchanged.
    Retain {
        /// The number of elements to retain.
        retain: usize,
    },
}

/// A map delta.
#[derive(Debug, Clone)]
pub struct MapDelta<'a> {
    /// All the updated keys and their new values.
    pub updated: FxHashMap<Cow<'a, str>, Option<ValueOrContainer>>,
}

impl<'a> From<DiffEventInner<'a>> for DiffEvent<'a> {
    fn from(value: DiffEventInner<'a>) -> Self {
        DiffEvent {
            triggered_by: value.event_meta.by,
            origin: &value.event_meta.origin,
            current_target: value.current_target,
            events: value.events.iter().map(|&diff| diff.into()).collect(),
        }
    }
}

impl<'a> From<&'a ContainerDiffInner> for ContainerDiff<'a> {
    fn from(value: &'a ContainerDiffInner) -> Self {
        ContainerDiff {
            target: &value.id,
            path: &value.path,
            is_unknown: value.is_unknown,
            diff: (&value.diff).into(),
        }
    }
}

impl<'a> From<&'a DiffInner> for Diff<'a> {
    fn from(value: &'a DiffInner) -> Self {
        match value {
            DiffInner::List(l) => {
                let mut ans = Vec::new();
                for item in l.iter() {
                    match item {
                        delta::DeltaItem::Retain { len, .. } => {
                            ans.push(ListDiffItem::Retain { retain: *len });
                        }
                        delta::DeltaItem::Replace {
                            value,
                            delete,
                            attr,
                        } => {
                            if !value.is_empty() {
                                ans.push(ListDiffItem::Insert {
                                    insert: value
                                        .iter()
                                        .map(|v| ValueOrContainer::from(v.clone()))
                                        .collect(),
                                    is_move: attr.from_move,
                                });
                            }
                            if *delete > 0 {
                                ans.push(ListDiffItem::Delete { delete: *delete });
                            }
                        }
                    }
                }

                Diff::List(ans)
            }
            DiffInner::Map(m) => Diff::Map(MapDelta {
                updated: m
                    .updated
                    .iter()
                    .map(|(k, v)| (Cow::Borrowed(k.as_str()), v.value.clone().map(|v| v.into())))
                    .collect(),
            }),
            DiffInner::Text(t) => {
                let text = TextDelta::from_text_diff(t.iter());
                Diff::Text(text)
            }
            DiffInner::Tree(t) => Diff::Tree(Cow::Borrowed(t)),
            #[cfg(feature = "counter")]
            DiffInner::Counter(c) => Diff::Counter(*c),
            DiffInner::Unknown => Diff::Unknown,
            _ => todo!(),
        }
    }
}

impl From<DiffInner> for Diff<'static> {
    fn from(value: DiffInner) -> Self {
        match value {
            DiffInner::List(l) => {
                let mut ans = Vec::new();
                for item in l.iter() {
                    match item {
                        delta::DeltaItem::Retain { len, .. } => {
                            ans.push(ListDiffItem::Retain { retain: *len });
                        }
                        delta::DeltaItem::Replace {
                            value,
                            delete,
                            attr,
                        } => {
                            if !value.is_empty() {
                                ans.push(ListDiffItem::Insert {
                                    insert: value
                                        .iter()
                                        .map(|v| ValueOrContainer::from(v.clone()))
                                        .collect(),
                                    is_move: attr.from_move,
                                });
                            }
                            if *delete > 0 {
                                ans.push(ListDiffItem::Delete { delete: *delete });
                            }
                        }
                    }
                }

                Diff::List(ans)
            }
            DiffInner::Map(m) => Diff::Map(MapDelta {
                updated: m
                    .updated
                    .iter()
                    .map(|(k, v)| (Cow::Owned(k.to_string()), v.value.clone().map(|v| v.into())))
                    .collect(),
            }),
            DiffInner::Text(t) => {
                let text = TextDelta::from_text_diff(t.iter());
                Diff::Text(text)
            }
            DiffInner::Tree(t) => Diff::Tree(Cow::Owned(t.clone())),
            #[cfg(feature = "counter")]
            DiffInner::Counter(c) => Diff::Counter(c),
            DiffInner::Unknown => Diff::Unknown,
            _ => todo!(),
        }
    }
}

impl From<ValueOrHandler> for ValueOrContainer {
    fn from(value: ValueOrHandler) -> Self {
        match value {
            ValueOrHandler::Value(v) => ValueOrContainer::Value(v),
            ValueOrHandler::Handler(h) => ValueOrContainer::Container(h.into()),
        }
    }
}

/// A batch of diffs.
#[derive(Default, Clone)]
pub struct DiffBatch {
    cid_to_events: FxHashMap<ContainerID, Diff<'static>>,
    order: Vec<ContainerID>,
}

impl std::fmt::Debug for DiffBatch {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let v: Vec<(&ContainerID, &Diff<'static>)> = self.iter().collect();
        write!(f, "{:#?}", v)
    }
}

impl DiffBatch {
    /// Returns an iterator over the diffs in this batch, in the order they were added.
    ///
    /// The iterator yields tuples of `(&ContainerID, &Diff)` where:
    /// - `ContainerID` is the ID of the container that was modified
    /// - `Diff` contains the actual changes made to that container
    ///
    /// The order of the diffs is preserved from when they were originally added to the batch.
    pub fn iter(&self) -> impl Iterator<Item = (&ContainerID, &Diff<'static>)> {
        self.order.iter().map(|id| (id, &self.cid_to_events[id]))
    }

    /// Push a new event to the batch.
    ///
    /// If the cid already exists in the batch, return Err
    pub fn push(&mut self, cid: ContainerID, diff: Diff<'static>) -> Result<(), Diff<'static>> {
        match self.cid_to_events.entry(cid.clone()) {
            std::collections::hash_map::Entry::Occupied(_) => Err(diff),
            std::collections::hash_map::Entry::Vacant(vacant_entry) => {
                vacant_entry.insert(diff);
                self.order.push(cid);
                Ok(())
            }
        }
    }

    pub(crate) fn validate_for_apply(&self) -> LoroResult<()> {
        for (_, diff) in self.iter() {
            validate_diff_for_apply(diff)?;
        }

        Ok(())
    }
}

fn validate_diff_for_apply(diff: &Diff<'_>) -> LoroResult<()> {
    let mut index = 0usize;
    match diff {
        Diff::Text(delta) => {
            for item in delta {
                match item {
                    TextDelta::Retain { retain, .. } => {
                        index = checked_apply_diff_index_end(index, *retain)?;
                    }
                    TextDelta::Delete { delete } => {
                        index = checked_apply_diff_index_end(index, *delete)?;
                    }
                    TextDelta::Insert { .. } => {}
                }
            }
        }
        Diff::List(delta) => {
            for item in delta {
                match item {
                    ListDiffItem::Retain { retain } => {
                        index = checked_apply_diff_index_end(index, *retain)?;
                    }
                    ListDiffItem::Delete { delete } => {
                        index = checked_apply_diff_index_end(index, *delete)?;
                    }
                    ListDiffItem::Insert { .. } => {}
                }
            }
        }
        Diff::Map(_) | Diff::Tree(_) | Diff::Unknown => {}
        #[cfg(feature = "counter")]
        Diff::Counter(_) => {}
    }

    Ok(())
}

fn checked_apply_diff_index_end(pos: usize, len: usize) -> LoroResult<usize> {
    pos.checked_add(len).ok_or_else(|| LoroError::OutOfBound {
        pos: usize::MAX,
        len: usize::MAX,
        info: "apply_diff".into(),
    })
}

impl From<InnerDiffBatch> for DiffBatch {
    fn from(value: InnerDiffBatch) -> Self {
        let mut map =
            FxHashMap::with_capacity_and_hasher(value.cid_to_events.len(), Default::default());
        for (id, diff) in value.cid_to_events.into_iter() {
            map.insert(id.clone(), diff.into());
        }

        DiffBatch {
            cid_to_events: map,
            order: value.order,
        }
    }
}

impl From<Diff<'static>> for DiffInner {
    fn from(value: Diff<'static>) -> Self {
        match value {
            Diff::List(vec) => {
                let mut ans: DeltaRope<ListDiffInsertItem, ListDeltaMeta> = DeltaRope::new();
                for item in vec.iter() {
                    match item {
                        ListDiffItem::Insert { insert, is_move } => {
                            for item in ArrayVec::from_many(
                                insert.iter().map(|v| v.clone().into_value_or_handler()),
                            ) {
                                ans.push_insert(
                                    item,
                                    ListDeltaMeta {
                                        from_move: *is_move,
                                    },
                                );
                            }
                        }
                        ListDiffItem::Delete { delete } => {
                            ans.push_delete(*delete);
                        }
                        ListDiffItem::Retain { retain } => {
                            ans.push_retain(*retain, ListDeltaMeta { from_move: false });
                        }
                    }
                }

                DiffInner::List(ans)
            }
            Diff::Text(t) => {
                let text = TextDelta::into_text_diff(t.into_iter());
                DiffInner::Text(text)
            }
            Diff::Map(map_delta) => DiffInner::Map(ResolvedMapDelta {
                updated: map_delta
                    .updated
                    .into_iter()
                    .map(|(k, v)| {
                        (
                            k.deref().into(),
                            ResolvedMapValue {
                                value: v.map(|v| v.into_value_or_handler()),
                                idlp: IdLp::new(0, 0),
                            },
                        )
                    })
                    .collect(),
            }),
            Diff::Tree(cow) => DiffInner::Tree(cow.into_owned()),
            #[cfg(feature = "counter")]
            Diff::Counter(c) => DiffInner::Counter(c),
            Diff::Unknown => DiffInner::Unknown,
        }
    }
}

impl From<DiffBatch> for InnerDiffBatch {
    fn from(value: DiffBatch) -> Self {
        let mut map =
            FxHashMap::with_capacity_and_hasher(value.cid_to_events.len(), Default::default());
        for (id, diff) in value.cid_to_events.into_iter() {
            map.insert(id.clone(), diff.into());
        }

        InnerDiffBatch {
            cid_to_events: map,
            order: value.order,
        }
    }
}