Skip to content

Commit

Permalink
fix: Make it pass unidirectional tests
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Jul 28, 2024
1 parent ec567ec commit 11b9849
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/lib/Diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export interface PlanStage3<L1 extends TItemLocation, L2 extends TItemLocation,
export interface PlanRevert<L1 extends TItemLocation, L2 extends TItemLocation> {
CREATE: Diff<L1, L2, CreateAction<L1, L2>>
UPDATE: Diff<L1, L2, UpdateAction<L1, L2>>
MOVE: Diff<L1, L2, MoveAction<L1, L2>>
MOVE: Diff<L2, L1, MoveAction<L2, L1>>
REMOVE: Diff<L1, L2, RemoveAction<L1, L2>>
REORDER: Diff<L1, L2, ReorderAction<L1, L2>>
}
26 changes: 15 additions & 11 deletions src/lib/strategies/Unidirectional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {
}
}

async revertDiff<L1 extends TItemLocation, L2 extends TItemLocation>(targetScanResult: ScanResult<L1, L2>, sourceScanResult: ScanResult<L2, L1>, targetLocation: L1): Promise<PlanRevert<L1, L2>> {
async revertDiff<L1 extends TItemLocation, L2 extends TItemLocation>(
targetScanResult: ScanResult<L1, L2>,
sourceScanResult: ScanResult<L2, L1>,
targetLocation: L1
): Promise<PlanRevert<L1, L2>> {
const mappingsSnapshot = this.mappings.getSnapshot()

const slavePlan: PlanRevert<L1, L2> = {
Expand Down Expand Up @@ -201,15 +205,11 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {
}, ACTION_CONCURRENCY)

await Parallel.each(targetScanResult.MOVE.getActions(), async(action) => {
const oldPayload = action.payload.cloneWithLocation(false, action.oldItem.location)
oldPayload.id = action.oldItem.id
oldPayload.parentId = action.oldItem.parentId
const payload = action.payload.cloneWithLocation(false, action.oldItem.location)
payload.id = action.oldItem.id
payload.parentId = action.oldItem.parentId

const oldOldItem = action.oldItem.cloneWithLocation(false, action.payload.location)
oldOldItem.id = action.payload.id
oldOldItem.parentId = action.payload.parentId

slavePlan.MOVE.commit({ type: ActionType.MOVE, payload: oldOldItem, oldItem: oldPayload })
slavePlan.MOVE.commit({ type: ActionType.MOVE, payload }) // no oldItem, because we want to map the id after having executed the CREATEs
}, ACTION_CONCURRENCY)

return slavePlan
Expand Down Expand Up @@ -275,19 +275,23 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {
throw new CancelledSyncError()
}

const mappingsSnapshot = this.mappings.getSnapshot()
// TODO: Store this in continuation
const mappedMoves = planRevert.MOVE.map(mappingsSnapshot, targetLocation)

if (this.canceled) {
throw new CancelledSyncError()
}

const batches = Diff.sortMoves(planRevert.MOVE.getActions(), this.getTargetTree(targetLocation))
const batches = Diff.sortMoves(mappedMoves.getActions(), this.getTargetTree(targetLocation))

if (this.canceled) {
throw new CancelledSyncError()
}

Logger.log(targetLocation + ': executing MOVEs')
await Parallel.each(batches, batch => Parallel.each(batch, (action) => {
return this.executeUpdate(resource, action, targetLocation, planRevert.MOVE, donePlan)
return this.executeUpdate(resource, action, targetLocation, mappedMoves, donePlan)
}, ACTION_CONCURRENCY), 1)

if (this.canceled) {
Expand Down

0 comments on commit 11b9849

Please sign in to comment.