Skip to content

Commit 8bfca68

Browse files
committed
fix(SyncProcess): Make sure to cancel progress callback throttling in the end
Signed-off-by: Marcel Klehr <[email protected]>
1 parent 712ae0a commit 8bfca68

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/lib/strategies/Default.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Diff, {
2121
import Scanner, { ScanResult } from '../Scanner'
2222
import * as Parallel from 'async-parallel'
2323
import throttle from '@jcoreio/async-throttle'
24+
import type { ThrottledFunction } from '@jcoreio/async-throttle'
2425
import Mappings, { MappingSnapshot } from '../Mappings'
2526
import TResource, { IHashSettings, OrderFolderResource, TLocalTree } from '../interfaces/Resource'
2627
import { TAdapter } from '../interfaces/Adapter'
@@ -37,7 +38,7 @@ export default class SyncProcess {
3738
protected server: TAdapter
3839
protected cacheTreeRoot: Folder<typeof ItemLocation.LOCAL>|null
3940
protected canceled: boolean
40-
protected progressCb: (progress:number, actionsDone?:number)=>void
41+
protected progressCb: ThrottledFunction<[progress: number, actionsDone: number | undefined], void>
4142

4243
// Stage -1
4344
protected localTreeRoot: Folder<typeof ItemLocation.LOCAL> = null
@@ -88,7 +89,7 @@ export default class SyncProcess {
8889
this.localTree = localTree
8990
this.server = server
9091

91-
this.progressCb = throttle(progressCb, 1500) as (progress:number, actionsDone?:number)=>void
92+
this.progressCb = throttle(progressCb, 1500)
9293
this.canceled = false
9394
this.isFirefox = self.location.protocol === 'moz-extension:'
9495
}
@@ -141,6 +142,7 @@ export default class SyncProcess {
141142
this.canceled = true
142143
this.server.cancel()
143144
this.localTree.cancel()
145+
this.progressCb.cancel()
144146
}
145147

146148
updateProgress():void {
@@ -176,13 +178,13 @@ export default class SyncProcess {
176178

177179
async sync(): Promise<void> {
178180
// onSyncStart is already executed at this point
179-
this.progressCb(0.15)
181+
this.progressCb(0.15, 0)
180182

181183
this.masterLocation = ItemLocation.LOCAL
182184
await this.prepareSync()
183185

184186
// trees are loaded at this point
185-
this.progressCb(0.35)
187+
this.progressCb(0.35, 0)
186188

187189
if (this.canceled) {
188190
throw new CancelledSyncError()
@@ -195,7 +197,7 @@ export default class SyncProcess {
195197
Logger.log({ localScanResult, serverScanResult })
196198
this.localScanResult = localScanResult
197199
this.serverScanResult = serverScanResult
198-
this.progressCb(0.45)
200+
this.progressCb(0.45, 0)
199201
}
200202

201203
if (this.canceled) {
@@ -371,6 +373,8 @@ export default class SyncProcess {
371373
this.executeReorderings(this.localTree, this.localReorders),
372374
])
373375
}
376+
377+
this.progressCb.cancel()
374378
}
375379

376380
protected async prepareSync() {

src/lib/strategies/Unidirectional.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {
106106
}
107107

108108
async sync(): Promise<void> {
109-
this.progressCb(0.15)
109+
this.progressCb(0.15, 0)
110110

111111
this.masterLocation = this.direction === ItemLocation.SERVER ? ItemLocation.LOCAL : ItemLocation.SERVER
112112
await this.prepareSync()
113113

114-
this.progressCb(0.35)
114+
this.progressCb(0.35, 0)
115115

116116
if (this.canceled) {
117117
throw new CancelledSyncError()
@@ -124,7 +124,7 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {
124124
Logger.log({ localScanResult, serverScanResult })
125125
this.localScanResult = localScanResult
126126
this.serverScanResult = serverScanResult
127-
this.progressCb(0.45)
127+
this.progressCb(0.45, 0)
128128
}
129129

130130
if (this.canceled) {
@@ -196,6 +196,8 @@ export default class UnidirectionalSyncProcess extends DefaultStrategy {
196196
if ('orderFolder' in this.server && 'orderFolder' in target) {
197197
await this.executeReorderings(target, this.revertReorders)
198198
}
199+
200+
this.progressCb.cancel()
199201
}
200202

201203
async revertDiff<L1 extends TItemLocation, L2 extends TItemLocation>(

0 commit comments

Comments
 (0)