multistore
Laurent 1 year ago
parent 0f2cdaca31
commit 10455f8715
  1. 6
      LeStorage/Store.swift
  2. 28
      LeStorage/StoredCollection.swift

@ -243,6 +243,12 @@ public class Store {
}
}
public func resetApiCalls() {
for collection in self._collections.values {
collection.resetApiCalls()
}
}
/// Loads all collection with the data from the server
public func loadCollectionFromServer() {
for collection in self._collections.values {

@ -14,15 +14,20 @@ enum StoredCollectionError: Error {
}
protocol SomeCollection: Identifiable {
var resourceName: String { get }
var synchronized: Bool { get }
func allItems() -> [any Storable]
func deleteById(_ id: String) throws
func deleteApiCallById(_ id: String) throws
func reset()
func loadDataFromServerIfAllowed() async throws
var synchronized: Bool { get }
func hasPendingAPICalls() -> Bool
var resourceName: String { get }
func contentOfApiCallFile() -> String?
func reset()
func resetApiCalls()
}
extension Notification.Name {
@ -403,17 +408,18 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
}
if let existingCall = apiCallCollection.first(where: { $0.dataId == instance.id }) {
switch existingCall.method {
case HTTPMethod.post.rawValue, HTTPMethod.put.rawValue:
switch method {
case .delete:
try self.deleteApiCallById(existingCall.id) // delete the existing call as we don't need it
if existingCall.method == HTTPMethod.post.rawValue {
return nil // if the post has not been done, we can just stop here
} else {
return try self._createCall(instance, method: method) // otherwise it's a put and we want to send the delete
}
default: // here we should only trying to PUT, so we update the existing POST/PUT with the instance new values
existingCall.body = try instance.jsonString()
return existingCall
case HTTPMethod.delete.rawValue:
try self.deleteApiCallById(existingCall.id)
return nil
default:
throw StoredCollectionError.unmanagedHTTPMethod(method: existingCall.method)
}
} else {
return try self._createCall(instance, method: method)
}

Loading…
Cancel
Save