File tree Expand file tree Collapse file tree 4 files changed +20
-5
lines changed Expand file tree Collapse file tree 4 files changed +20
-5
lines changed Original file line number Diff line number Diff line change @@ -114,8 +114,17 @@ impl YDoc {
114
114
115
115
#[ napi]
116
116
pub fn apply_update ( & mut self , update : JsBuffer ) -> Result < ( ) > {
117
+ let client = self . doc . client ( ) ;
118
+ let before_current_state = self . doc . get_state_vector ( ) . get ( & client) ;
119
+
117
120
self . doc . apply_update_from_binary_v1 ( update) ?;
118
121
122
+ // if update received from remote and current client state has been changed
123
+ // that means another client using same client id, we need to change the client id to avoid conflict
124
+ if self . doc . get_state_vector ( ) . get ( & client) != before_current_state {
125
+ self . doc . renew_client ( ) ;
126
+ }
127
+
119
128
Ok ( ( ) )
120
129
}
121
130
Original file line number Diff line number Diff line change @@ -44,7 +44,9 @@ test("y-octo doc update should be apply", (t) => {
44
44
let map2 = doc2 . getOrCreateMap ( "map" ) ;
45
45
let text2 = doc2 . getOrCreateText ( "text" ) ;
46
46
47
- t . is ( doc2 . clientId , client_id ) ;
47
+ // after apply update that include same client id's change
48
+ // the client id should be changed
49
+ t . not ( doc2 . clientId , client_id ) ;
48
50
t . is ( array2 . length , 4 ) ;
49
51
t . is ( array2 . get ( 0 ) , true ) ;
50
52
t . is ( array2 . get ( 1 ) , false ) ;
Original file line number Diff line number Diff line change @@ -41,13 +41,13 @@ test.skip("testOriginInTransaction", (t) => {
41
41
/**
42
42
* Client id should be changed when an instance receives updates from another client using the same client id.
43
43
*/
44
- test . skip ( "testClientIdDuplicateChange" , ( t ) => {
44
+ test ( "testClientIdDuplicateChange" , ( t ) => {
45
45
const doc1 = new Y . Doc ( 0 ) ;
46
46
const doc2 = new Y . Doc ( 0 ) ;
47
- t . assert ( doc2 . clientID === doc1 . clientID ) ;
47
+ t . assert ( doc2 . clientId === doc1 . clientId ) ;
48
48
doc1 . getArray ( "a" ) . insert ( 0 , [ 1 , 2 ] ) ;
49
- Y . applyUpdate ( doc2 , Y . encodeStateAsUpdate ( doc1 ) ) ;
50
- t . assert ( doc2 . clientID !== doc1 . clientID ) ;
49
+ Y . applyUpdate ( doc2 , Y . encodeStateAsUpdate ( doc1 ) , false ) ;
50
+ t . assert ( doc2 . clientId !== doc1 . clientId ) ;
51
51
} ) ;
52
52
53
53
test . skip ( "testGetTypeEmptyId" , ( t ) => {
Original file line number Diff line number Diff line change @@ -162,6 +162,10 @@ impl Doc {
162
162
self . client_id = client_id;
163
163
}
164
164
165
+ pub fn renew_client ( & mut self ) {
166
+ self . client_id = prefer_small_random ( ) ;
167
+ }
168
+
165
169
pub fn clients ( & self ) -> Vec < u64 > {
166
170
self . store . read ( ) . unwrap ( ) . clients ( )
167
171
}
You can’t perform that action at this time.
0 commit comments