@@ -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
785817pub 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+
899960pub trait Unsubscriber : Sync + Send {
900961 fn on_unsubscribe ( & self ) ;
901962}
0 commit comments