Skip to content

Commit a1937ce

Browse files
committed
fix: Persist state more frequently when running in a bg worker
Signed-off-by: Marcel Klehr <[email protected]>
1 parent dd2c429 commit a1937ce

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/lib/strategies/Default.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ export default class SyncProcess {
9090
mappings:Mappings,
9191
localTree:TLocalTree,
9292
server:TAdapter,
93-
progressCb:(progress:number, actionsDone?:number)=>void
93+
progressCb:(progress:number, actionsDone?:number)=>Promise<void>
9494
) {
9595
this.mappings = mappings
9696
this.localTree = localTree
9797
this.server = server
9898

99-
this.progressCb = throttle(progressCb, 1500)
99+
const PROGRESS_INTERVAL = self.constructor.name === 'ServiceWorkerGlobalScope' ? 100 : 1500
100+
this.progressCb = throttle(progressCb, PROGRESS_INTERVAL)
100101
this.cancelPromise = new Promise<void>((resolve, reject) => {
101102
this.cancelCb = reject
102103
})
@@ -156,12 +157,12 @@ export default class SyncProcess {
156157
this.progressCb.cancel()
157158
}
158159

159-
updateProgress():void {
160+
async updateProgress():void {
160161
if (typeof this.actionsDone === 'undefined') {
161162
this.actionsDone = 0
162163
}
163164
this.actionsDone++
164-
this.progressCb(
165+
const promise = this.progressCb(
165166
Math.min(
166167
1,
167168
0.5 + (this.actionsDone / (this.actionsPlanned + 1)) * 0.5
@@ -173,6 +174,9 @@ export default class SyncProcess {
173174
}
174175
throw er
175176
})
177+
if (self.constructor.name === 'ServiceWorkerGlobalScope') {
178+
await promise
179+
}
176180
Logger.log(`Executed ${this.actionsDone} actions from ${this.actionsPlanned} actions`)
177181
}
178182

@@ -996,7 +1000,7 @@ export default class SyncProcess {
9961000
const done = () => {
9971001
diff.retract(action)
9981002
donePlan.CREATE.commit(action)
999-
this.updateProgress()
1003+
await this.updateProgress()
10001004
}
10011005

10021006
const id = await Promise.race([
@@ -1005,7 +1009,7 @@ export default class SyncProcess {
10051009
])
10061010
if (typeof id === 'undefined') {
10071011
// undefined means we couldn't create the item. we're ignoring it
1008-
done()
1012+
await done()
10091013
return
10101014
}
10111015

@@ -1017,12 +1021,12 @@ export default class SyncProcess {
10171021
}
10181022

10191023
if (action.payload instanceof Bookmark || action.oldItem instanceof Bookmark) {
1020-
done()
1024+
await done()
10211025
return
10221026
}
10231027

10241028
if (action.payload.children.length === 0) {
1025-
done()
1029+
await done()
10261030
return
10271031
}
10281032

@@ -1039,7 +1043,7 @@ export default class SyncProcess {
10391043
try {
10401044
// Try bulk import with sub folders
10411045
const imported = await resource.bulkImportFolder(id, action.oldItem.copyWithLocation(false, action.payload.location)) as Folder<typeof targetLocation>
1042-
done()
1046+
await done()
10431047
const newMappings = []
10441048
const subScanner = new Scanner(
10451049
action.oldItem,
@@ -1139,7 +1143,7 @@ export default class SyncProcess {
11391143
diff.commit(newAction)
11401144
})
11411145

1142-
done()
1146+
await done()
11431147

11441148
if ('orderFolder' in resource) {
11451149
// Order created items after the fact, as they've been created concurrently
@@ -1171,7 +1175,7 @@ export default class SyncProcess {
11711175
diff.commit(newAction)
11721176
})
11731177

1174-
done()
1178+
await done()
11751179

11761180
if ('orderFolder' in resource) {
11771181
// Order created items after the fact, as they've been created concurrently
@@ -1206,7 +1210,7 @@ export default class SyncProcess {
12061210
])
12071211
diff.retract(action)
12081212
donePlan.REMOVE.commit(action)
1209-
this.updateProgress()
1213+
await this.updateProgress()
12101214
}
12111215

12121216
async executeUpdate<L1 extends TItemLocation>(
@@ -1235,7 +1239,7 @@ export default class SyncProcess {
12351239
} else {
12361240
donePlan.MOVE.commit(action)
12371241
}
1238-
this.updateProgress()
1242+
await this.updateProgress()
12391243
}
12401244

12411245
reconcileReorderings<L1 extends TItemLocation, L2 extends TItemLocation>(
@@ -1375,7 +1379,7 @@ export default class SyncProcess {
13751379
Logger.log(e)
13761380
}
13771381
reorderings.retract(action)
1378-
this.updateProgress()
1382+
await this.updateProgress()
13791383
}, isUsingTabs ? 1 : ACTION_CONCURRENCY)
13801384
}
13811385

@@ -1526,7 +1530,7 @@ export default class SyncProcess {
15261530
static async fromJSON(mappings:Mappings,
15271531
localTree:TLocalTree,
15281532
server:TAdapter,
1529-
progressCb:(progress:number, actionsDone:number)=>void,
1533+
progressCb:(progress:number, actionsDone:number)=>Promise<void>,
15301534
json: any) {
15311535
let strategy: SyncProcess
15321536
switch (json.strategy) {

0 commit comments

Comments
 (0)