Skip to content

Commit

Permalink
fix: sort out some basic bugs
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed Apr 2, 2024
1 parent 6f6bcea commit 1f4f1ae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/lib/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ export default class Account {
mappings = await this.storage.getMappings()
const cacheTree = localResource.constructor.name !== 'LocalTabs' ? await this.storage.getCache() : new Folder({title: '', id: 'tabs', location: ItemLocation.LOCAL})

const continuation = this.storage.getCurrentContinuation()
const continuation = await this.storage.getCurrentContinuation()

if (continuation === null) {
if (typeof continuation === 'undefined') {
// If there is no pending continuation, we just sync normally

let strategyClass: typeof DefaultSyncProcess|typeof MergeSyncProcess|typeof UnidirectionalSyncProcess, direction: TItemLocation
Expand Down Expand Up @@ -248,6 +248,7 @@ export default class Account {
},
continuation
)
this.syncProcess.setCacheTree(cacheTree)
await this.syncProcess.resumeSync()
}

Expand Down
8 changes: 7 additions & 1 deletion src/lib/Diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,13 @@ export default class Diff {
}

toJSON() {
return this.getActions()
return this.getActions().map((action: Action) => {
return {
...action,
payload: action.payload.clone(false),
oldItem: action.payload.clone(false),
}
})
}

static fromJSON(json) {
Expand Down
29 changes: 20 additions & 9 deletions src/lib/strategies/Default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default class SyncProcess {
protected progressCb: (progress:number)=>void
protected localTreeRoot: Folder
protected serverTreeRoot: Folder
protected actionsDone: number
protected actionsPlanned: number
protected actionsDone = 0
protected actionsPlanned = 0
protected isFirefox: boolean
protected doneLocalPlan: Diff
protected doneServerPlan: Diff
Expand Down Expand Up @@ -60,9 +60,12 @@ export default class SyncProcess {
}

setState({localPlan, serverPlan, serverReorderPlan, localReorderPlan, flagPostMoveMapping, flagPostReorderReconciliation}: any) {
this.localPlan = Diff.fromJSON(localPlan)
this.localPlan = Diff.fromJSON(localPlan)
this.serverPlan = Diff.fromJSON(serverPlan)
if (typeof localPlan !== 'undefined') {
this.localPlan = Diff.fromJSON(localPlan)
}
if (typeof serverPlan !== 'undefined') {
this.serverPlan = Diff.fromJSON(serverPlan)
}
if (typeof localReorderPlan !== 'undefined') {
this.localReorderPlan = Diff.fromJSON(localReorderPlan)
}
Expand Down Expand Up @@ -182,6 +185,9 @@ export default class SyncProcess {
}

async resumeSync(): Promise<void> {
if (typeof this.localPlan === 'undefined' || typeof this.serverPlan === 'undefined') {
return this.sync()
}
Logger.log({localPlan: this.localPlan, serverPlan: this.serverPlan})

Logger.log('Executing server plan')
Expand Down Expand Up @@ -614,6 +620,8 @@ export default class SyncProcess {
this.serverPlan = mappedPlan
}
this.flagPostMoveMapping = true
} else {
mappedPlan = plan
}

if (this.canceled) {
Expand Down Expand Up @@ -966,10 +974,10 @@ export default class SyncProcess {
toJSON(): ISerializedSyncProcess {
return {
strategy: 'default',
localPlan: this.localPlan.toJSON(),
serverPlan: this.serverPlan.toJSON(),
serverReorderPlan: this.serverReorderPlan.toJSON(),
localReorderPlan: this.serverPlan.toJSON(),
localPlan: this.localPlan && this.localPlan.toJSON(),
serverPlan: this.serverPlan && this.serverPlan.toJSON(),
serverReorderPlan: this.serverReorderPlan && this.serverReorderPlan.toJSON(),
localReorderPlan: this.localReorderPlan && this.localReorderPlan.toJSON(),
actionsDone: this.actionsDone,
actionsPlanned: this.actionsPlanned,
flagPostMoveMapping: this.flagPostMoveMapping,
Expand All @@ -996,6 +1004,9 @@ export default class SyncProcess {
case 'unidirectional':
UnidirectionalSyncProcess = (await import('./Unidirectional')).default
strategy = new UnidirectionalSyncProcess(mappings, localTree, server, progressCb)
break
default:
throw new Error('Unknown strategy: ' + json.strategy)
}
strategy.setProgress(json)
strategy.setState(json)
Expand Down
11 changes: 9 additions & 2 deletions src/lib/strategies/Unidirectional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {
}

async resumeSync(): Promise<void> {
if (typeof this.revertPlan === 'undefined') {
return this.sync()
}
Logger.log({revertPlan: this.revertPlan})

let target: IResource|OrderFolderResource
Expand Down Expand Up @@ -200,8 +203,12 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {

setState({direction, revertPlan, revertOrderings, flagPreReordering, sourceDiff}: any) {
this.setDirection(direction)
this.revertPlan = Diff.fromJSON(revertPlan)
this.sourceDiff = Diff.fromJSON(sourceDiff)
if (typeof revertPlan !== 'undefined') {
this.revertPlan = Diff.fromJSON(revertPlan)
}
if (typeof sourceDiff !== 'undefined') {
this.sourceDiff = Diff.fromJSON(sourceDiff)
}
if (typeof revertOrderings !== 'undefined') {
this.revertOrderings = Diff.fromJSON(revertOrderings)
}
Expand Down

0 comments on commit 1f4f1ae

Please sign in to comment.