99} from '../Tree'
1010import Logger from '../Logger'
1111import Diff , {
12+ Action ,
1213 ActionType ,
1314 CreateAction ,
1415 MoveAction ,
@@ -296,8 +297,8 @@ export default class SyncProcess {
296297 if ( 'orderFolder' in this . server && ! this . localReordersFinal ) {
297298 // mappings have been updated, reload
298299 mappingsSnapshot = this . mappings . getSnapshot ( )
299- this . localReordersFinal = this . reconcileReorderings ( this . localReorders , this . serverDonePlan , ItemLocation . LOCAL , mappingsSnapshot )
300- this . serverReorderFinal = this . reconcileReorderings ( this . serverReorders , this . localDonePlan , ItemLocation . SERVER , mappingsSnapshot )
300+ this . localReordersFinal = this . reconcileReorderings ( this . localReorders , this . localDonePlan , ItemLocation . LOCAL , mappingsSnapshot )
301+ this . serverReorderFinal = this . reconcileReorderings ( this . serverReorders , this . serverDonePlan , ItemLocation . SERVER , mappingsSnapshot )
301302 }
302303
303304 if ( this . canceled ) {
@@ -763,8 +764,9 @@ export default class SyncProcess {
763764 }
764765 }
765766
766- const concurrentRemoval = targetRemovals . find ( a =>
767- a . payload . findItem ( 'folder' , action . payload . id ) )
767+ const concurrentRemoval = targetRemovals . find ( targetRemoval =>
768+ Diff . findChain ( mappingsSnapshot , allCreateAndMoveActions , sourceTree , action . payload , targetRemoval )
769+ )
768770 if ( concurrentRemoval ) {
769771 // Already deleted on target, do nothing.
770772 return
@@ -1096,15 +1098,17 @@ export default class SyncProcess {
10961098
10971099 reconcileReorderings < L1 extends TItemLocation , L2 extends TItemLocation > (
10981100 targetReorders : Diff < L2 , TItemLocation , ReorderAction < L2 , TItemLocation > > ,
1099- sourceDonePlan : PlanStage3 < L1 , TItemLocation , L2 > ,
1101+ targetDonePlan : PlanStage3 < L2 , TItemLocation , L1 > ,
11001102 targetLocation : L1 ,
11011103 mappingSnapshot : MappingSnapshot
11021104 ) : Diff < L1 , TItemLocation , ReorderAction < L1 , TItemLocation > > {
11031105 Logger . log ( 'Reconciling reorders to create a plan' )
11041106
1105- const sourceCreations = sourceDonePlan . CREATE . getActions ( )
1106- const sourceRemovals = sourceDonePlan . REMOVE . getActions ( )
1107- const sourceMoves = sourceDonePlan . MOVE . getActions ( )
1107+ const sourceCreations = targetDonePlan . CREATE . getActions ( )
1108+ const sourceRemovals = targetDonePlan . REMOVE . getActions ( )
1109+ const sourceMoves = targetDonePlan . MOVE . getActions ( )
1110+ const sourceCreationsAndMoves : Action < TItemLocation , TItemLocation > [ ] = ( sourceCreations as Action < TItemLocation , TItemLocation > [ ] ) . concat ( sourceMoves )
1111+ const sourceTree = targetLocation === ItemLocation . LOCAL ? this . localTreeRoot : this . serverTreeRoot
11081112
11091113 const newReorders = new Diff < L2 , TItemLocation , ReorderAction < L2 , TItemLocation > >
11101114
@@ -1117,37 +1121,41 @@ export default class SyncProcess {
11171121 const reorderAction = { ...oldReorderAction , order : oldReorderAction . order . slice ( ) }
11181122
11191123 const removed = sourceRemovals
1120- . filter ( removal => removal . payload . findItem ( reorderAction . payload . type , removal . payload . id ) )
1124+ . filter ( removal =>
1125+ Diff . findChain ( mappingSnapshot , sourceCreationsAndMoves , sourceTree , oldReorderAction . payload , removal ) )
11211126 if ( removed . length ) {
11221127 return
11231128 }
11241129
11251130 // Find Away-moves
11261131 const childAwayMoves = sourceMoves
11271132 . filter ( move =>
1128- ( String ( reorderAction . payload . id ) !== String ( move . payload . parentId ) && // reorder IDs are from localTree (source of this plan), move.oldItem IDs are from server tree (source of other plan)
1129- reorderAction . order . find ( item => String ( item . id ) === String ( move . payload . id ) && item . type === move . payload . type ) ) // move.payload IDs are from localTree (target of the other plan
1133+ Mappings . mapId ( mappingSnapshot , reorderAction . payload , move . payload . location ) !== String ( move . payload . parentId ) &&
1134+ reorderAction . order . find ( item =>
1135+ Mappings . mapRawId ( mappingSnapshot , item . id , item . type , reorderAction . payload . location , move . payload . location ) === String ( move . payload . id ) && item . type === move . payload . type )
11301136 )
11311137
11321138 // Find removals
11331139 const concurrentRemovals = sourceRemovals
1134- . filter ( removal => reorderAction . order . find ( item => String ( item . id ) === String ( removal . payload . id ) && item . type === removal . payload . type ) )
1140+ . filter ( removal =>
1141+ reorderAction . order . find ( item =>
1142+ Mappings . mapRawId ( mappingSnapshot , item . id , item . type , reorderAction . payload . location , removal . payload . location ) === String ( removal . payload . id ) && item . type === removal . payload . type ) )
11351143
11361144 // Remove away-moves and removals
11371145 reorderAction . order = reorderAction . order . filter ( item => {
11381146 let action
11391147 if (
11401148 // eslint-disable-next-line no-cond-assign
11411149 action = childAwayMoves . find ( move =>
1142- String ( item . id ) === String ( move . payload . id ) && move . payload . type === item . type ) ) {
1150+ Mappings . mapRawId ( mappingSnapshot , item . id , item . type , reorderAction . payload . location , move . payload . location ) === String ( move . payload . id ) && move . payload . type === item . type ) ) {
11431151 Logger . log ( 'ReconcileReorders: Removing moved item from order' , { move : action , reorder : reorderAction } )
11441152 return false
11451153 }
11461154
11471155 if (
11481156 // eslint-disable-next-line no-cond-assign
11491157 action = concurrentRemovals . find ( removal =>
1150- String ( item . id ) === String ( removal . payload . id ) && removal . payload . type === item . type )
1158+ Mappings . mapRawId ( mappingSnapshot , item . id , item . type , reorderAction . payload . location , removal . payload . location ) === String ( removal . payload . id ) && removal . payload . type === item . type )
11511159 ) {
11521160 Logger . log ( 'ReconcileReorders: Removing removed item from order' , { item, reorder : reorderAction , removal : action } )
11531161 return false
0 commit comments