@@ -290,15 +290,21 @@ export default class SyncProcess {
290290 Logger . log ( 'Executing local plan' )
291291 await this . execute ( this . localTree , this . localPlanStage2 , ItemLocation . LOCAL , this . localDonePlan , this . localReorders )
292292
293+ // Remove mappings only after both plans have been executed
294+ this . localDonePlan . REMOVE . getActions ( ) . forEach ( action => this . removeMapping ( this . localTree , action . payload ) )
295+ this . serverDonePlan . REMOVE . getActions ( ) . forEach ( action => this . removeMapping ( this . localTree , action . payload ) )
296+
293297 if ( this . canceled ) {
294298 throw new CancelledSyncError ( )
295299 }
296300
297301 if ( 'orderFolder' in this . server && ! this . localReordersFinal ) {
298302 // mappings have been updated, reload
299303 mappingsSnapshot = this . mappings . getSnapshot ( )
300- this . localReordersFinal = this . reconcileReorderings ( this . localReorders , this . localDonePlan , ItemLocation . LOCAL , mappingsSnapshot )
301- this . serverReorderFinal = this . reconcileReorderings ( this . serverReorders , this . serverDonePlan , ItemLocation . SERVER , mappingsSnapshot )
304+ const localReorders = this . reconcileReorderings ( this . localReorders , this . localDonePlan , ItemLocation . LOCAL , mappingsSnapshot )
305+ const serverReorders = this . reconcileReorderings ( this . serverReorders , this . serverDonePlan , ItemLocation . SERVER , mappingsSnapshot )
306+ this . localReordersFinal = this . reconcileReorderings ( localReorders , this . serverDonePlan , ItemLocation . LOCAL , mappingsSnapshot ) . map ( mappingsSnapshot , ItemLocation . LOCAL )
307+ this . serverReorderFinal = this . reconcileReorderings ( serverReorders , this . localDonePlan , ItemLocation . SERVER , mappingsSnapshot ) . map ( mappingsSnapshot , ItemLocation . SERVER )
302308 }
303309
304310 if ( this . canceled ) {
@@ -623,7 +629,7 @@ export default class SyncProcess {
623629 const concurrentMove = targetMoves . find ( a =>
624630 action . payload . type === a . payload . type && Mappings . mappable ( mappingsSnapshot , action . payload , a . payload ) )
625631 if ( concurrentMove ) {
626- // Moved both on target and sourcely, source has precedence: do nothing sourcely
632+ // Moved both on target and sourcely, master has precedence: do nothing on master
627633 return
628634 }
629635 }
@@ -1065,7 +1071,6 @@ export default class SyncProcess {
10651071 }
10661072
10671073 await action . payload . visitRemove ( resource )
1068- await this . removeMapping ( resource , action . payload )
10691074 diff . retract ( action )
10701075 donePlan . REMOVE . commit ( action )
10711076 this . updateProgress ( )
@@ -1098,15 +1103,15 @@ export default class SyncProcess {
10981103
10991104 reconcileReorderings < L1 extends TItemLocation , L2 extends TItemLocation > (
11001105 targetReorders : Diff < L2 , TItemLocation , ReorderAction < L2 , TItemLocation > > ,
1101- targetDonePlan : PlanStage3 < L2 , TItemLocation , L1 > ,
1106+ targetOrSourceDonePlan : PlanStage3 < TItemLocation , TItemLocation , TItemLocation > ,
11021107 targetLocation : L1 ,
11031108 mappingSnapshot : MappingSnapshot
1104- ) : Diff < L1 , TItemLocation , ReorderAction < L1 , TItemLocation > > {
1109+ ) : Diff < L2 , TItemLocation , ReorderAction < L2 , TItemLocation > > {
11051110 Logger . log ( 'Reconciling reorders to create a plan' )
11061111
1107- const targetCreations = targetDonePlan . CREATE . getActions ( )
1108- const targetRemovals = targetDonePlan . REMOVE . getActions ( )
1109- const targetMoves = targetDonePlan . MOVE . getActions ( )
1112+ const targetCreations = targetOrSourceDonePlan . CREATE . getActions ( )
1113+ const targetRemovals = targetOrSourceDonePlan . REMOVE . getActions ( )
1114+ const targetMoves = targetOrSourceDonePlan . MOVE . getActions ( )
11101115 const targetCreationsAndMoves : Action < TItemLocation , TItemLocation > [ ] = ( targetCreations as Action < TItemLocation , TItemLocation > [ ] ) . concat ( targetMoves )
11111116 const targetTree = targetLocation === ItemLocation . LOCAL ? this . localTreeRoot : this . serverTreeRoot
11121117
@@ -1122,7 +1127,8 @@ export default class SyncProcess {
11221127
11231128 const removed = targetRemovals
11241129 . filter ( removal =>
1125- Diff . findChain ( mappingSnapshot , targetCreationsAndMoves , targetTree , oldReorderAction . payload , removal ) )
1130+ removal . payload . findItem ( reorderAction . payload . type , reorderAction . payload . id ) ||
1131+ Diff . findChain ( mappingSnapshot , targetCreationsAndMoves , targetTree , reorderAction . payload , removal ) )
11261132 if ( removed . length ) {
11271133 return
11281134 }
@@ -1186,7 +1192,7 @@ export default class SyncProcess {
11861192 newReorders . commit ( reorderAction )
11871193 } )
11881194
1189- return newReorders . map ( mappingSnapshot , targetLocation )
1195+ return newReorders
11901196 }
11911197
11921198 async executeReorderings ( resource :OrderFolderResource < TItemLocation > , reorderings :Diff < TItemLocation , TItemLocation , ReorderAction < TItemLocation , TItemLocation > > ) :Promise < void > {
0 commit comments