Fix api call provisioning

sync2
Laurent 8 months ago
parent 3cf90172d4
commit 1ed8286213
  1. 43
      LeStorage/ApiCallCollection.swift
  2. 7
      LeStorageTests/ApiCallTests.swift

@ -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
}

@ -37,10 +37,13 @@ struct ApiCallTests {
thing.name = "woo"
let _ = try await collection.sendUpdate(thing)
await #expect(collection.items.count == 1)
await #expect(collection.items.count == 2) // one post and one put
if let apiCall = await collection.items.first {
#expect(apiCall.method == .post)
}
if let apiCall = await collection.items.last {
#expect(apiCall.method == .put)
}
let _ = try await collection.sendDeletion(thing)
await #expect(collection.items.count == 1)
@ -60,6 +63,8 @@ struct ApiCallTests {
thing.name = "woo"
let _ = try await collection.sendUpdate(thing)
let _ = try await collection.sendUpdate(thing)
let _ = try await collection.sendUpdate(thing)
await #expect(collection.items.count == 1)
if let apiCall = await collection.items.first {
#expect(apiCall.method == .put)

Loading…
Cancel
Save