@@ -208,41 +208,32 @@ export default class SyncProcess {
208
208
const sourceCreations = sourceDiff . getActions ( ActionType . CREATE ) . map ( a => a as CreateAction )
209
209
const sourceRemovals = sourceDiff . getActions ( ActionType . REMOVE ) . map ( a => a as RemoveAction )
210
210
211
+ const allCreateAndMoveActions = targetDiff . getActions ( )
212
+ . filter ( a => a . type === ActionType . CREATE || a . type === ActionType . MOVE )
213
+ . map ( a => a as CreateAction | MoveAction )
214
+ . concat (
215
+ sourceDiff . getActions ( )
216
+ . filter ( a => a . type === ActionType . CREATE || a . type === ActionType . MOVE )
217
+ . map ( a => a as CreateAction | MoveAction )
218
+ )
219
+
211
220
const avoidTargetReorders = { }
212
221
213
222
// Prepare target plan
214
223
const targetPlan = new Diff ( ) // to be mapped
215
224
await Parallel . each ( sourceDiff . getActions ( ) , async ( action :Action ) => {
216
225
if ( action . type === ActionType . REMOVE ) {
217
- const concurrentRemoval = targetRemovals . find ( a =>
218
- ( action . payload . type === a . payload . type && Mappings . mappable ( mappingsSnapshot , action . payload , a . payload ) ) ||
219
- a . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , action . payload , a . payload . location ) ) )
226
+ const concurrentRemoval = targetRemovals . find ( targetRemoval =>
227
+ ( action . payload . type === targetRemoval . payload . type && Mappings . mappable ( mappingsSnapshot , action . payload , targetRemoval . payload ) ) ||
228
+ Diff . findChain ( mappingsSnapshot , allCreateAndMoveActions , action . payload , targetRemoval ) )
220
229
if ( concurrentRemoval ) {
221
230
// Already deleted on target, do nothing.
222
231
return
223
232
}
224
233
225
- const complexConcurrentRemoval = sourceRemovals . find ( sourceRemoval => {
226
- let currentAction : RemoveAction | CreateAction | MoveAction =
227
- targetDiff . getActions ( )
228
- . filter ( a => a . type === ActionType . CREATE || a . type === ActionType . MOVE )
229
- . map ( a => a as CreateAction | MoveAction )
230
- . find ( targetAction => targetAction . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , action . payload , targetAction . payload . location ) ) )
231
- while ( currentAction && ! sourceRemoval . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , currentAction . payload , sourceRemoval . payload . location ) ) ) {
232
- currentAction = targetDiff . getActions ( )
233
- . filter ( a => a . type === ActionType . CREATE || a . type === ActionType . MOVE )
234
- . map ( a => a as CreateAction | MoveAction )
235
- . find ( targetAction => targetAction . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , currentAction . payload , targetAction . payload . location ) ) )
236
- }
237
- return Boolean ( currentAction )
238
- } )
239
- if ( complexConcurrentRemoval ) {
240
- // already deleted by a different REMOVE from this diff (connected via source MOVE|CREATEs)
241
- return
242
- }
243
-
244
- const concurrentMove = targetMoves . find ( a =>
245
- action . payload . type === a . payload . type && Mappings . mappable ( mappingsSnapshot , action . payload , a . payload ) )
234
+ const concurrentMove = targetMoves . find ( targetMove =>
235
+ action . payload . type === targetMove . payload . type && Mappings . mappable ( mappingsSnapshot , action . payload , targetMove . payload )
236
+ )
246
237
if ( concurrentMove ) {
247
238
// moved on the target, moves take precedence, do nothing (i.e. leave target version intact)
248
239
return
@@ -278,15 +269,9 @@ export default class SyncProcess {
278
269
// TODO: subScanner may contain residual CREATE/REMOVE actions that need to be added to mappings
279
270
return
280
271
}
281
- const concurrentRemoval = targetRemovals . find ( a =>
282
- // target removal removed this creation's target
283
- a . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , action . payload , a . payload . location ) ) ||
284
- // or: target removal removed a move's target that was the target of this creation
285
- sourceDiff . getActions ( ) . filter ( a => a . type === ActionType . MOVE || a . type === ActionType . CREATE )
286
- . find ( a2 =>
287
- a . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , a2 . payload , a . payload . location ) ) &&
288
- a2 . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , action . payload , a2 . payload . location ) )
289
- )
272
+ const concurrentRemoval = targetRemovals . find ( targetRemoval =>
273
+ // target removal removed this creation's target (via some chain)
274
+ Diff . findChain ( mappingsSnapshot , allCreateAndMoveActions , action . payload , targetRemoval )
290
275
)
291
276
if ( concurrentRemoval ) {
292
277
avoidTargetReorders [ action . payload . parentId ] = true
@@ -295,22 +280,27 @@ export default class SyncProcess {
295
280
}
296
281
}
297
282
if ( action . type === ActionType . MOVE ) {
298
- const concurrentParentRemoval = targetRemovals . find ( a =>
299
- // target-side removal of this move's target
300
- a . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , action . payload , a . payload . location ) ) ||
301
- // or: target-side removal of a source creation's target which was the target of this move
302
- sourceDiff . getActions ( ) . filter ( a => a . type === ActionType . MOVE || a . type === ActionType . CREATE )
303
- . find ( a2 =>
304
- a2 . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , action . payload , a2 . payload . location ) ) &&
305
- a . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , a2 . payload , a . payload . location ) ) )
283
+ // FInd out if there's a removal in the target diff which already deletes this item (via some chain of MOVE|CREATEs)
284
+ const complexTargetTargetRemoval = targetRemovals . find ( targetRemoval => {
285
+ return Diff . findChain ( mappingsSnapshot , allCreateAndMoveActions , action . payload , targetRemoval )
286
+ } )
287
+ const concurrentTargetOriginRemoval = targetRemovals . find ( targetRemoval =>
288
+ ( action . payload . type === targetRemoval . payload . type && Mappings . mappable ( mappingsSnapshot , action . payload , targetRemoval . payload ) ) ||
289
+ Diff . findChain ( mappingsSnapshot , allCreateAndMoveActions , action . oldItem , targetRemoval )
306
290
)
307
- if ( concurrentParentRemoval ) {
291
+ const concurrentSourceOriginRemoval = sourceRemovals . find ( sourceRemoval => {
292
+ return Diff . findChain ( mappingsSnapshot , allCreateAndMoveActions , action . oldItem , sourceRemoval )
293
+ } )
294
+ if ( complexTargetTargetRemoval ) {
295
+ // target already deleted by a target REMOVE (connected via source MOVE|CREATEs)
296
+ if ( ! concurrentTargetOriginRemoval && ! concurrentSourceOriginRemoval ) {
297
+ // make sure this item is not already being removed, when it's no longer moved
298
+ targetPlan . commit ( { ...action , type : ActionType . REMOVE , payload : action . oldItem , oldItem : null } )
299
+ }
308
300
return
309
301
}
310
- const concurrentRemoval = targetRemovals . find ( a =>
311
- ( action . payload . type === a . payload . type && Mappings . mappable ( mappingsSnapshot , action . payload , a . payload ) ) ||
312
- a . payload . findItem ( ItemType . FOLDER , Mappings . mapParentId ( mappingsSnapshot , action . oldItem , a . payload . location ) ) )
313
- if ( concurrentRemoval && ! sourceRemovals . find ( a => a . payload . findItem ( action . payload . type , Mappings . mapId ( mappingsSnapshot , action . payload , a . payload . location ) ) ) ) {
302
+ if ( concurrentTargetOriginRemoval ) {
303
+ // if (!concurrentSourceOriginRemoval) {
314
304
// moved sourcely but removed on the target, recreate it on the target
315
305
const originalCreation = sourceCreations . find ( creation => creation . payload . findItem ( ItemType . FOLDER , action . payload . parentId ) )
316
306
if ( originalCreation && originalCreation . payload . type === ItemType . FOLDER ) {
@@ -320,6 +310,7 @@ export default class SyncProcess {
320
310
} else {
321
311
targetPlan . commit ( { ...action , type : ActionType . CREATE , oldItem : null } )
322
312
}
313
+ // }
323
314
return
324
315
}
325
316
0 commit comments