-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathSyncStatusResolver.swift
136 lines (122 loc) · 4.46 KB
/
SyncStatusResolver.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Sync
import XCGLogger
import Deferred
import Shared
import Storage
public enum SyncDisplayState {
case inProgress
case good
case bad(message: String?)
case warning(message: String)
func asObject() -> [String: String]? {
switch self {
case .bad(let msg):
guard let message = msg else {
return ["state": "Error"]
}
return ["state": "Error",
"message": message]
case .warning(let message):
return ["state": "Warning",
"message": message]
default:
break
}
return nil
}
}
public func ==(a: SyncDisplayState, b: SyncDisplayState) -> Bool {
switch (a, b) {
case (.inProgress, .inProgress):
return true
case (.good, .good):
return true
case (.bad(let a), .bad(let b)) where a == b:
return true
case (.warning(let a), .warning(let b)) where a == b:
return true
default:
return false
}
}
private let log = Logger.syncLogger
/*
* Translates the fine-grained SyncStatuses of each sync engine into a more coarse-grained
* display-oriented state for displaying warnings/errors to the user.
*/
public struct SyncStatusResolver {
let engineResults: Maybe<EngineResults>
public func resolveResults() -> SyncDisplayState {
guard let results = engineResults.successValue else {
switch engineResults.failureValue {
case _ as BookmarksMergeError, _ as BufferInvalidError:
return SyncDisplayState.warning(message: String(format: Strings.FirefoxSyncPartialTitle, Strings.localizedStringForSyncComponent("bookmarks") ?? ""))
default:
return SyncDisplayState.bad(message: nil)
}
}
// Run through the engine results and produce a relevant display status for each one
let displayStates: [SyncDisplayState] = results.map { (engineIdentifier, syncStatus) in
log.debug("Sync status for \(engineIdentifier): \(syncStatus)")
// Explicitly call out each of the enum cases to let us lean on the compiler when
// we add new error states
switch syncStatus {
case .notStarted(let reason):
switch reason {
case .offline:
return .bad(message: Strings.FirefoxSyncOfflineTitle)
case .noAccount:
return .warning(message: Strings.FirefoxSyncOfflineTitle)
case .backoff(_):
return .good
case .engineRemotelyNotEnabled(_):
return .good
case .engineFormatOutdated(_):
return .good
case .engineFormatTooNew(_):
return .good
case .storageFormatOutdated(_):
return .good
case .storageFormatTooNew(_):
return .good
case .stateMachineNotReady:
return .good
case .redLight:
return .good
case .unknown:
return .good
}
case .completed:
return .good
case .partial:
return .good
}
}
// TODO: Instead of finding the worst offender in a list of statuses, we should better surface
// what might have happened with a particular engine when syncing.
let aggregate: SyncDisplayState = displayStates.reduce(.good) { carried, displayState in
switch displayState {
case .bad(_):
return displayState
case .warning(_):
// If the state we're carrying is worse than the stale one, keep passing
// along the worst one
switch carried {
case .bad(_):
return carried
default:
return displayState
}
default:
// This one is good so just pass on what was being carried
return carried
}
}
log.debug("Resolved sync display state: \(aggregate)")
return aggregate
}
}