Skip to content

Commit 6da184c

Browse files
committed
Add throttling of orders sync to OrderListViewController
1 parent dabe790 commit 6da184c

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

WooCommerce/Classes/ViewRelated/Orders/OrderListSyncActionUseCase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ struct OrderListSyncActionUseCase {
7575

7676
/// The reasons passed to `SyncCoordinator` when synchronizing.
7777
///
78-
/// We're only currently tracking one reason.
7978
enum SyncReason: String {
8079
case pullToRefresh = "pull_to_refresh"
80+
case viewWillAppear = "view_will_appear"
8181
}
8282

8383
let siteID: Int64

WooCommerce/Classes/ViewRelated/Orders/OrderListViewController.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ final class OrderListViewController: UIViewController {
7171
///
7272
private let syncingCoordinator = SyncingCoordinator()
7373

74+
/// Timestamp for last successful sync.
75+
///
76+
private var lastFullSyncTimestamp: Date?
77+
78+
/// Minimum time interval allowed between full sync.
79+
///
80+
private let minimalIntervalBetweenSync: TimeInterval = 30
7481

7582
/// UI Active State
7683
///
@@ -144,7 +151,7 @@ final class OrderListViewController: UIViewController {
144151
override func viewWillAppear(_ animated: Bool) {
145152
super.viewWillAppear(animated)
146153

147-
syncingCoordinator.resynchronize()
154+
syncingCoordinator.resynchronize(reason: SyncReason.viewWillAppear.rawValue)
148155

149156
// Fix any incomplete animation of the refresh control
150157
// when switching tabs mid-animation
@@ -276,6 +283,15 @@ extension OrderListViewController: SyncingCoordinatorDelegate {
276283
/// Synchronizes the Orders for the Default Store (if any).
277284
///
278285
func sync(pageNumber: Int, pageSize: Int, reason: String? = nil, onCompletion: ((Bool) -> Void)? = nil) {
286+
if pageNumber == syncingCoordinator.pageFirstIndex,
287+
reason == SyncReason.viewWillAppear.rawValue,
288+
let lastFullSyncTimestamp = lastFullSyncTimestamp,
289+
Date().timeIntervalSince(lastFullSyncTimestamp) < minimalIntervalBetweenSync {
290+
// less than 30 s from last full sync
291+
onCompletion?(true)
292+
return
293+
}
294+
279295
transitionToSyncingState()
280296
setErrorLoadingData(to: false)
281297

@@ -292,6 +308,10 @@ extension OrderListViewController: SyncingCoordinatorDelegate {
292308
DDLogError("⛔️ Error synchronizing orders: \(error)")
293309
self.setErrorLoadingData(to: true)
294310
} else {
311+
if pageNumber == self.syncingCoordinator.pageFirstIndex {
312+
// save timestamp of last successful update
313+
self.lastFullSyncTimestamp = Date()
314+
}
295315
ServiceLocator.analytics.track(event: .ordersListLoaded(totalDuration: totalDuration,
296316
pageNumber: pageNumber,
297317
status: self.viewModel.statusFilter))

0 commit comments

Comments
 (0)