@@ -44,14 +44,21 @@ class RemoteSyncEngine: RemoteSyncEngineBehavior {
44
44
let stateMachine : StateMachine < State , Action >
45
45
private var stateMachineSink : AnyCancellable ?
46
46
47
+ var networkReachabilityPublisher : AnyPublisher < ReachabilityUpdate , Never > ?
48
+ var mutationRetryNotifier : MutationRetryNotifier ?
49
+ let requestRetryablePolicy : RequestRetryablePolicy
50
+ var currentAttemptNumber : Int
51
+
47
52
/// Initializes the CloudSyncEngine with the specified storageAdapter as the provider for persistence of
48
53
/// MutationEvents, sync metadata, and conflict resolution metadata. Immediately initializes the incoming mutation
49
54
/// queue so it can begin accepting incoming mutations from DataStore.
50
55
convenience init ( storageAdapter: StorageEngineAdapter ,
51
56
outgoingMutationQueue: OutgoingMutationQueueBehavior ? = nil ,
52
57
initialSyncOrchestratorFactory: InitialSyncOrchestratorFactory ? = nil ,
53
58
reconciliationQueueFactory: IncomingEventReconciliationQueueFactory ? = nil ,
54
- stateMachine: StateMachine < State , Action > ? = nil ) throws {
59
+ stateMachine: StateMachine < State , Action > ? = nil ,
60
+ networkReachabilityPublisher: AnyPublisher < ReachabilityUpdate , Never > ? = nil ,
61
+ requestRetryablePolicy: RequestRetryablePolicy ? = nil ) throws {
55
62
let mutationDatabaseAdapter = try AWSMutationDatabaseAdapter ( storageAdapter: storageAdapter)
56
63
let awsMutationEventPublisher = AWSMutationEventPublisher ( eventSource: mutationDatabaseAdapter)
57
64
let outgoingMutationQueue = outgoingMutationQueue ?? OutgoingMutationQueue ( )
@@ -61,14 +68,18 @@ class RemoteSyncEngine: RemoteSyncEngineBehavior {
61
68
AWSInitialSyncOrchestrator . init ( api: reconciliationQueue: storageAdapter: )
62
69
let stateMachine = stateMachine ?? StateMachine ( initialState: . notStarted,
63
70
resolver: RemoteSyncEngine . Resolver. resolve ( currentState: action: ) )
71
+ let requestRetryablePolicy = requestRetryablePolicy ?? RequestRetryablePolicy ( )
72
+
64
73
65
74
self . init ( storageAdapter: storageAdapter,
66
75
outgoingMutationQueue: outgoingMutationQueue,
67
76
mutationEventIngester: mutationDatabaseAdapter,
68
77
mutationEventPublisher: awsMutationEventPublisher,
69
78
initialSyncOrchestratorFactory: initialSyncOrchestratorFactory,
70
79
reconciliationQueueFactory: reconciliationQueueFactory,
71
- stateMachine: stateMachine)
80
+ stateMachine: stateMachine,
81
+ networkReachabilityPublisher: networkReachabilityPublisher,
82
+ requestRetryablePolicy: requestRetryablePolicy)
72
83
}
73
84
74
85
init ( storageAdapter: StorageEngineAdapter ,
@@ -77,19 +88,25 @@ class RemoteSyncEngine: RemoteSyncEngineBehavior {
77
88
mutationEventPublisher: MutationEventPublisher ,
78
89
initialSyncOrchestratorFactory: @escaping InitialSyncOrchestratorFactory ,
79
90
reconciliationQueueFactory: @escaping IncomingEventReconciliationQueueFactory ,
80
- stateMachine: StateMachine < State , Action > ) {
91
+ stateMachine: StateMachine < State , Action > ,
92
+ networkReachabilityPublisher: AnyPublisher < ReachabilityUpdate , Never > ? ,
93
+ requestRetryablePolicy: RequestRetryablePolicy ) {
81
94
self . storageAdapter = storageAdapter
82
95
self . mutationEventIngester = mutationEventIngester
83
96
self . mutationEventPublisher = mutationEventPublisher
84
97
self . outgoingMutationQueue = outgoingMutationQueue
85
98
self . initialSyncOrchestratorFactory = initialSyncOrchestratorFactory
86
99
self . reconciliationQueueFactory = reconciliationQueueFactory
87
100
self . remoteSyncTopicPublisher = PassthroughSubject < RemoteSyncEngineEvent , DataStoreError > ( )
101
+ self . networkReachabilityPublisher = networkReachabilityPublisher
102
+ self . requestRetryablePolicy = requestRetryablePolicy
88
103
89
104
self . syncQueue = OperationQueue ( )
90
105
syncQueue. name = " com.amazonaws.Amplify. \( AWSDataStorePlugin . self) .CloudSyncEngine "
91
106
syncQueue. maxConcurrentOperationCount = 1
92
107
108
+ self . currentAttemptNumber = 1
109
+
93
110
self . stateMachine = stateMachine
94
111
self . stateMachineSink = self . stateMachine
95
112
. $state
@@ -128,10 +145,11 @@ class RemoteSyncEngine: RemoteSyncEngineBehavior {
128
145
case . syncEngineActive:
129
146
break
130
147
131
- case . cleanup( let amplifyError) :
132
- //todo
133
- print ( " error: \( amplifyError) " )
148
+ case . cleanup( let error) :
149
+ cleanup ( error: error)
134
150
151
+ case . scheduleRestart( let error) :
152
+ scheduleRestart ( error: error)
135
153
}
136
154
}
137
155
@@ -217,7 +235,7 @@ class RemoteSyncEngine: RemoteSyncEngineBehavior {
217
235
private func activateCloudSubscriptions( ) {
218
236
log. debug ( #function)
219
237
reconciliationQueue? . start ( )
220
-
238
+
221
239
//Notifying the publisher & state machine are handled in:
222
240
// RemoteSyncEngine+IncomingEventReconciliationQueueEvent.swift
223
241
}
@@ -231,10 +249,20 @@ class RemoteSyncEngine: RemoteSyncEngineBehavior {
231
249
stateMachine. notify ( action: . activatedMutationQueue)
232
250
}
233
251
252
+ private func cleanup( error: AmplifyError ? ) {
253
+ // TODO:
254
+ // handle clean up here
255
+ remoteSyncTopicPublisher. send ( . cleanedUp)
256
+ stateMachine. notify ( action: . cleanedUp( error) )
257
+ }
258
+
234
259
private func notifySyncStarted( ) {
260
+ resetCurrentAttemptNumber ( )
235
261
Amplify . Hub. dispatch ( to: . dataStore,
236
262
payload: HubPayload ( eventName: HubPayload . EventName. DataStore. syncStarted) )
263
+
237
264
remoteSyncTopicPublisher. send ( . syncStarted)
265
+ stateMachine. notify ( action: . notifiedSyncStarted)
238
266
}
239
267
240
268
func reset( onComplete: ( ) -> Void ) {
0 commit comments