@@ -50,12 +50,27 @@ export class ServerCrudLocalHistorySync {
5050 const core = this . core ;
5151 if ( ! core . connected$ . getValue ( ) ) return false ;
5252 const meta = await this . getMeta ( collection , id ) ;
53- const isNewBlock = meta . time < 1 ;
54- if ( isNewBlock ) {
55- await this . pushNewBlock ( collection , id ) ;
56- } else {
57- await this . pushExistingBlock ( collection , id , meta . time ) ;
53+ const blob = await core . read ( collection , id ) ;
54+ const { history} = core . decoder . decode ( blob , { format : 'seq.cbor' , history : true } ) ;
55+ const patches : RemoteBlockPatch [ ] = [ ] ;
56+ let time = 0 ;
57+ // TODO: perf: use a binary search to find the first patch to sync.
58+ history ! . patches . forEach ( ( { v : patch } ) => {
59+ const id = patch . getId ( ) ;
60+ if ( ! id ) return ;
61+ // TODO: also filter in SESSION.GLOBAL patches.
62+ if ( id . sid === core . sid && id . time > meta . time ) {
63+ patches . push ( { blob : patch . toBinary ( ) } ) ;
64+ time = id . time ;
65+ }
66+ } ) ;
67+ if ( ! patches . length ) {
68+ await this . putMeta ( collection , id , { time, ts : Date . now ( ) } ) ;
69+ return true ;
5870 }
71+ const remoteId = [ ...collection , id ] . join ( '/' ) ;
72+ await this . core . remote . update ( remoteId , patches ) ;
73+ await this . putMeta ( collection , id , { time, ts : Date . now ( ) } ) ;
5974 await this . markTidy ( collection , id ) ;
6075 return true ;
6176 } ) ;
@@ -66,51 +81,6 @@ export class ServerCrudLocalHistorySync {
6681 } ) ;
6782 }
6883
69- private async pushNewBlock ( collection : string [ ] , id : string ) : Promise < void > {
70- const core = this . core ;
71- const blob = await core . read ( collection , id ) ;
72- const { history} = core . decoder . decode ( blob , { format : 'seq.cbor' , history : true } ) ;
73- const patches : RemoteBlockPatch [ ] = [ ] ;
74- let time = 0 ;
75- history ! . patches . forEach ( ( { v : patch } ) => {
76- const id = patch . getId ( ) ;
77- if ( ! id ) return ;
78- if ( id . sid === core . sid ) {
79- patches . push ( { blob : patch . toBinary ( ) } ) ;
80- time = id . time ;
81- }
82- } ) ;
83- if ( ! patches . length ) return ;
84- const remoteId = [ ...collection , id ] . join ( '/' ) ;
85- await this . core . remote . create ( remoteId , patches ) ;
86- await this . putMeta ( collection , id , { time, ts : Date . now ( ) } ) ;
87- }
88-
89- /**
90- * @todo Unify this with `pushNewBlock`.
91- */
92- private async pushExistingBlock ( collection : string [ ] , id : string , syncedTime : number ) : Promise < void > {
93- const core = this . core ;
94- const blob = await core . read ( collection , id ) ;
95- const { history} = core . decoder . decode ( blob , { format : 'seq.cbor' , history : true } ) ;
96- const patches : RemoteBlockPatch [ ] = [ ] ;
97- let time = 0 ;
98- // TODO: perf: use a binary search to find the first patch to sync.
99- history ! . patches . forEach ( ( { v : patch } ) => {
100- const id = patch . getId ( ) ;
101- if ( ! id ) return ;
102- // TODO: also filter in SESSION.GLOBAL patches.
103- if ( id . sid === core . sid && id . time > syncedTime ) {
104- patches . push ( { blob : patch . toBinary ( ) } ) ;
105- time = id . time ;
106- }
107- } ) ;
108- if ( ! patches . length ) return ;
109- const remoteId = [ ...collection , id ] . join ( '/' ) ;
110- await this . core . remote . update ( remoteId , patches ) ;
111- await this . putMeta ( collection , id , { time, ts : Date . now ( ) } ) ;
112- }
113-
11484 public async lock < T > (
11585 {
11686 collection,
0 commit comments