Skip to content

Commit fe469f8

Browse files
committed
feat: loro-ffi 1.5.0
1 parent 487ec6c commit fe469f8

5 files changed

Lines changed: 84 additions & 39 deletions

File tree

crates/loro-ffi/src/awareness.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ impl Awareness {
3535
}
3636

3737
pub fn get_local_state(&self) -> Option<LoroValue> {
38-
self.0
39-
.lock()
40-
.unwrap()
41-
.get_local_state()
42-
.map(|x| x.into())
38+
self.0.lock().unwrap().get_local_state().map(|x| x.into())
4339
}
4440

4541
pub fn remove_outdated(&self) -> Vec<PeerID> {

crates/loro-ffi/src/doc.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,38 @@ impl LoroDoc {
780780
pub fn has_container(&self, id: &ContainerID) -> bool {
781781
self.doc.has_container(&id.into())
782782
}
783+
784+
/// Subscribe to the first commit from a peer. Operations performed on the `LoroDoc` within this callback
785+
/// will be merged into the current commit.
786+
///
787+
/// This is useful for managing the relationship between `PeerID` and user information.
788+
/// For example, you could store user names in a `LoroMap` using `PeerID` as the key and the `UserID` as the value.
789+
pub fn subscribe_first_commit_from_peer(
790+
&self,
791+
subscriber: Arc<dyn FirstCommitFromPeerCallback>,
792+
) -> Arc<Subscription> {
793+
let subscriber: loro::FirstCommitFromPeerCallback = Box::new(move |e| {
794+
subscriber.on_first_commit_from_peer(FirstCommitFromPeerPayload { peer: e.peer });
795+
true
796+
});
797+
Arc::new(self.doc.subscribe_first_commit_from_peer(subscriber).into())
798+
}
799+
800+
/// Subscribe to the pre-commit event.
801+
///
802+
/// The callback will be called when the changes are committed but not yet applied to the OpLog.
803+
/// You can modify the commit message and timestamp in the callback by [`ChangeModifier`].
804+
pub fn subscribe_pre_commit(&self, callback: Arc<dyn PreCommitCallback>) -> Arc<Subscription> {
805+
let subscriber: loro::PreCommitCallback = Box::new(move |e| {
806+
callback.on_pre_commit(PreCommitCallbackPayload {
807+
change_meta: e.change_meta.clone().into(),
808+
origin: e.origin.clone(),
809+
modifier: Arc::new(ChangeModifier(e.modifier.clone())),
810+
});
811+
true
812+
});
813+
Arc::new(self.doc.subscribe_pre_commit(subscriber).into())
814+
}
783815
}
784816

785817
pub trait ChangeAncestorsTraveler: Sync + Send {
@@ -896,6 +928,35 @@ pub trait LocalUpdateCallback: Sync + Send {
896928
fn on_local_update(&self, update: Vec<u8>);
897929
}
898930

931+
pub trait FirstCommitFromPeerCallback: Sync + Send {
932+
fn on_first_commit_from_peer(&self, e: FirstCommitFromPeerPayload);
933+
}
934+
935+
pub struct FirstCommitFromPeerPayload {
936+
pub peer: PeerID,
937+
}
938+
939+
pub trait PreCommitCallback: Sync + Send {
940+
fn on_pre_commit(&self, e: PreCommitCallbackPayload);
941+
}
942+
943+
pub struct PreCommitCallbackPayload {
944+
pub change_meta: ChangeMeta,
945+
pub origin: String,
946+
pub modifier: Arc<ChangeModifier>,
947+
}
948+
949+
pub struct ChangeModifier(loro::ChangeModifier);
950+
951+
impl ChangeModifier {
952+
pub fn set_message(&self, msg: &str) {
953+
self.0.set_message(msg);
954+
}
955+
pub fn set_timestamp(&self, timestamp: Timestamp) {
956+
self.0.set_timestamp(timestamp);
957+
}
958+
}
959+
899960
pub trait Unsubscriber: Sync + Send {
900961
fn on_unsubscribe(&self);
901962
}

crates/loro-ffi/src/ephemeral.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::{LoroValue, LoroValueLike, Subscription};
2-
use loro::awareness::{EphemeralEventTrigger, EphemeralStore as InternalEphemeralStore};
2+
pub use loro::awareness::EphemeralEventTrigger;
3+
use loro::awareness::EphemeralStore as InternalEphemeralStore;
34
use std::sync::{Arc, Mutex};
45

56
#[derive(Debug, Clone)]
67
pub struct EphemeralStoreEvent {
78
pub by: EphemeralEventTrigger,
8-
pub added: Arc<Vec<String>>,
9-
pub removed: Arc<Vec<String>>,
10-
pub updated: Arc<Vec<String>>,
9+
pub added: Vec<String>,
10+
pub removed: Vec<String>,
11+
pub updated: Vec<String>,
1112
}
1213

1314
pub trait LocalEphemeralListener: Sync + Send {
@@ -88,19 +89,15 @@ impl EphemeralStore {
8889
}
8990

9091
pub fn subscribe(&self, listener: Arc<dyn EphemeralSubscriber>) -> Arc<Subscription> {
91-
let s = self
92-
.0
93-
.lock()
94-
.unwrap()
95-
.subscribe(Box::new(move |update| {
96-
listener.on_ephemeral_event(EphemeralStoreEvent {
97-
by: update.by,
98-
added: update.added.clone(),
99-
removed: update.removed.clone(),
100-
updated: update.updated.clone(),
101-
});
102-
true
103-
}));
92+
let s = self.0.lock().unwrap().subscribe(Box::new(move |update| {
93+
listener.on_ephemeral_event(EphemeralStoreEvent {
94+
by: update.by,
95+
added: update.added.to_vec(),
96+
removed: update.removed.to_vec(),
97+
updated: update.updated.to_vec(),
98+
});
99+
true
100+
}));
104101
Arc::new(Subscription(Mutex::new(Some(s))))
105102
}
106103
}

crates/loro-ffi/src/lib.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,21 @@ pub use std::cmp::Ordering;
1212
use std::sync::Arc;
1313
pub use value::{ContainerID, ContainerType, LoroValue, LoroValueLike};
1414
mod doc;
15-
pub use doc::{
16-
decode_import_blob_meta, ChangeAncestorsTraveler, ChangeMeta, CommitOptions, ContainerPath,
17-
FrontiersOrID, ImportBlobMetadata, ImportStatus, JsonSchemaLike, LocalUpdateCallback, LoroDoc,
18-
PosQueryResult, Subscription, Unsubscriber,
19-
};
15+
pub use doc::*;
2016
mod container;
21-
pub use container::{
22-
ContainerIdLike, Cursor, LoroCounter, LoroList, LoroMap, LoroMovableList, LoroText, LoroTree,
23-
LoroUnknown, TreeParentId,
24-
};
17+
pub use container::*;
2518
mod event;
26-
pub use event::{
27-
ContainerDiff, ContainerIDAndDiff, Diff, DiffBatch, DiffEvent, Index, ListDiffItem, MapDelta,
28-
PathItem, Subscriber, TextDelta, TreeDiff, TreeDiffItem, TreeExternalDiff,
29-
};
19+
pub use event::*;
3020
mod undo;
31-
pub use undo::{AbsolutePosition, CursorWithPos, OnPop, OnPush, UndoItemMeta, UndoManager};
21+
pub use undo::*;
3222
mod config;
3323
pub use config::{Configure, StyleConfigMap};
3424
mod version;
3525
pub use version::{Frontiers, VersionVector, VersionVectorDiff};
3626
mod awareness;
37-
pub use awareness::{Awareness, AwarenessPeerUpdate, PeerInfo};
27+
pub use awareness::*;
3828
mod ephemeral;
39-
pub use ephemeral::{EphemeralStore, LocalEphemeralListener};
29+
pub use ephemeral::*;
4030

4131
// https://github.com/mozilla/uniffi-rs/issues/1372
4232
pub trait ValueOrContainer: Send + Sync {

crates/loro/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub use loro_internal::encoding::ImportStatus;
1313
use loro_internal::handler::{HandlerTrait, ValueOrHandler};
1414
pub use loro_internal::loro::ChangeTravelError;
1515
pub use loro_internal::pre_commit::{
16-
ChangeModifier, FirstCommitFromPeerCallback, PreCommitCallback,
16+
ChangeModifier, FirstCommitFromPeerCallback, FirstCommitFromPeerPayload, PreCommitCallback,
17+
PreCommitCallbackPayload,
1718
};
1819
pub use loro_internal::sync;
1920
pub use loro_internal::undo::{OnPop, UndoItemMeta, UndoOrRedo};

0 commit comments

Comments
 (0)