|
|
|
|
@ -170,13 +170,13 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
func rescheduleApiCallsIfNecessary() { |
|
|
|
|
if self.items.isNotEmpty && !self._isRescheduling { |
|
|
|
|
self._schedulingTask = Task { |
|
|
|
|
await self._rescheduleApiCalls() |
|
|
|
|
await self._waitAndExecuteApiCalls() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Reschedule the execution of API calls |
|
|
|
|
fileprivate func _rescheduleApiCalls() async { |
|
|
|
|
fileprivate func _waitAndExecuteApiCalls() async { |
|
|
|
|
|
|
|
|
|
// Logger.log("\(T.resourceName()) > RESCHED") |
|
|
|
|
guard !self._isRescheduling, StoreCenter.main.collectionsCanSynchronize else { return } |
|
|
|
|
@ -199,7 +199,6 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
if T.copyServerResponse { |
|
|
|
|
StoreCenter.main.updateLocalInstances(results) |
|
|
|
|
} |
|
|
|
|
// self._attemptLoops = -1 |
|
|
|
|
} |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
@ -208,7 +207,7 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
|
|
|
|
|
self._isRescheduling = false |
|
|
|
|
if self.items.isNotEmpty { |
|
|
|
|
await self._rescheduleApiCalls() |
|
|
|
|
await self._waitAndExecuteApiCalls() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Logger.log("\(T.resourceName()) > isRescheduling = \(self._isRescheduling)") |
|
|
|
|
@ -218,7 +217,7 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
fileprivate func _wait() async { |
|
|
|
|
|
|
|
|
|
#if DEBUG |
|
|
|
|
let seconds = 2 * self._attemptLoops |
|
|
|
|
let seconds = self._attemptLoops |
|
|
|
|
#else |
|
|
|
|
let delay = pow(2, self._attemptLoops) |
|
|
|
|
let seconds = NSDecimalNumber(decimal: delay).intValue |
|
|
|
|
@ -236,10 +235,10 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
|
|
|
|
|
/// Returns an APICall instance for the Storable [instance] and an HTTP [method] |
|
|
|
|
/// The method updates existing calls or creates a new one |
|
|
|
|
fileprivate func _call(method: HTTPMethod, instance: T? = nil) throws -> ApiCall<T>? { |
|
|
|
|
fileprivate func _call(method: HTTPMethod, instance: T? = nil) async throws -> ApiCall<T>? { |
|
|
|
|
|
|
|
|
|
if let instance { |
|
|
|
|
return try self._callForInstance(method, instance: instance) |
|
|
|
|
return try await self._callForInstance(method, instance: instance) |
|
|
|
|
} else { |
|
|
|
|
if self.items.contains(where: { $0.method == .get }) { |
|
|
|
|
return nil |
|
|
|
|
@ -249,7 +248,7 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _callForInstance(_ method: HTTPMethod, instance: T) throws -> ApiCall<T>? { |
|
|
|
|
fileprivate func _callForInstance(_ method: HTTPMethod, instance: T) async throws -> ApiCall<T>? { |
|
|
|
|
|
|
|
|
|
if let existingCall = self.items.first(where: { $0.dataId == instance.stringId }) { |
|
|
|
|
switch method { |
|
|
|
|
@ -322,10 +321,8 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
|
|
|
|
|
/// Initiates the process of sending the data with the server |
|
|
|
|
fileprivate func _sendServerRequest<V: Decodable>(_ method: HTTPMethod, instance: T? = nil) async throws -> V? { |
|
|
|
|
if let apiCall = try self._call(method: method, instance: instance) { |
|
|
|
|
if let apiCall = try await self._call(method: method, instance: instance) { |
|
|
|
|
return try await self._prepareAndSendCall(apiCall) |
|
|
|
|
// try self._prepareCall(apiCall: apiCall) |
|
|
|
|
// return try await self._executeApiCall(apiCall) |
|
|
|
|
} else { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|