|
|
|
|
@ -215,39 +215,26 @@ actor ApiCallCollection<T: Storable>: SomeCallCollection { |
|
|
|
|
// MARK: - Synchronization |
|
|
|
|
|
|
|
|
|
/// Returns an APICall instance for the Storable [instance] and an HTTP [method] |
|
|
|
|
/// The method updates existing calls or creates a new one |
|
|
|
|
/// The method makes some clean up when necessary: |
|
|
|
|
/// - When deleting, we delete other calls as they are unecessary |
|
|
|
|
/// - When updating, we delete other PUT as we don't want them to be executed in random orders |
|
|
|
|
func callForInstance(_ instance: T, method: HTTPMethod) throws -> ApiCall<T>? { |
|
|
|
|
|
|
|
|
|
// cleanup |
|
|
|
|
let existingCalls = self.items.filter { $0.dataId == instance.stringId } |
|
|
|
|
if existingCalls.count > 1 { |
|
|
|
|
StoreCenter.main.log(message: "There are multiple calls registered for a single item: \(T.resourceName()), id = \(instance.stringId)") |
|
|
|
|
} |
|
|
|
|
let currentHTTPMethod = existingCalls.first?.method |
|
|
|
|
var call: ApiCall<T>? = nil |
|
|
|
|
if let currentHTTPMethod { |
|
|
|
|
switch (currentHTTPMethod, method) { |
|
|
|
|
case (.post, .put): |
|
|
|
|
call = try self._createCall(instance, method: .post) |
|
|
|
|
case (.post, .delete): |
|
|
|
|
call = try self._createCall(instance, method: .delete) |
|
|
|
|
case (.put, .put): |
|
|
|
|
call = try self._createCall(instance, method: .put) |
|
|
|
|
case (.put, .delete): |
|
|
|
|
call = try self._createCall(instance, method: .delete) |
|
|
|
|
default: |
|
|
|
|
call = try self._createCall(instance, method: method) |
|
|
|
|
StoreCenter.main.log(message: "case \(currentHTTPMethod) / \(method) should not happen") |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
call = try self._createCall(instance, method: method) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let call { |
|
|
|
|
// cleanup if necessary |
|
|
|
|
switch method { |
|
|
|
|
case .delete: // we don't want anything else than a DELETE in the queue |
|
|
|
|
let existingCalls = self.items.filter { $0.dataId == instance.stringId } |
|
|
|
|
self._deleteCalls(existingCalls) |
|
|
|
|
self._prepareCall(apiCall: call) |
|
|
|
|
case .put: // we don't want mixed PUT calls so we delete the others |
|
|
|
|
let existingPuts = self.items.filter { $0.dataId == instance.stringId && $0.method == .put } |
|
|
|
|
self._deleteCalls(existingPuts) |
|
|
|
|
default: |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let call: ApiCall<T> = try self._createCall(instance, method: method) |
|
|
|
|
self._prepareCall(apiCall: call) |
|
|
|
|
|
|
|
|
|
return call |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|