|
|
|
|
@ -19,10 +19,10 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect |
|
|
|
|
let store: Store |
|
|
|
|
let collection: StoredCollection<T> |
|
|
|
|
|
|
|
|
|
init(store: Store, indexed: Bool = false, inMemory: Bool = false, limit: Int? = nil, synchronousLoading: Bool = false, noLoad: Bool = false) { |
|
|
|
|
init(store: Store, indexed: Bool = false, inMemory: Bool = false, limit: Int? = nil, noLoad: Bool = false) { |
|
|
|
|
|
|
|
|
|
self.store = store |
|
|
|
|
self.collection = StoredCollection<T>(store: store, indexed: indexed, inMemory: inMemory, limit: limit, synchronousLoading: synchronousLoading, noLoad: noLoad) |
|
|
|
|
self.collection = StoredCollection<T>(store: store, indexed: indexed, inMemory: inMemory, limit: limit, noLoad: noLoad) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -42,19 +42,6 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect |
|
|
|
|
return SyncedCollection<T>(store: Store(storeCenter: StoreCenter.main)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Migrates if necessary and asynchronously decodes the json file |
|
|
|
|
// override func load() async { |
|
|
|
|
// do { |
|
|
|
|
// if self.inMemory { |
|
|
|
|
// try await self.loadDataFromServerIfAllowed() |
|
|
|
|
// } else { |
|
|
|
|
// await self.loadFromFile() |
|
|
|
|
// } |
|
|
|
|
// } catch { |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
/// Loads the collection using the server data only if the collection file doesn't exists |
|
|
|
|
func loadCollectionsFromServerIfNoFile() async throws { |
|
|
|
|
let fileURL: URL = try self.store.fileURL(type: T.self) |
|
|
|
|
@ -190,41 +177,6 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect |
|
|
|
|
return batch |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Deletes an instance without writing, logs the operation and sends an API call |
|
|
|
|
// fileprivate func _deleteNoWrite(instance: T) { |
|
|
|
|
// self.deleteItem(instance, shouldBeSynchronized: true) |
|
|
|
|
// self.storeCenter.createDeleteLog(instance) |
|
|
|
|
//// await self._sendDeletion(instance) |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
// public func deleteDependencies(_ items: any RandomAccessCollection<T>, actionOption: ActionOption) { |
|
|
|
|
// guard items.isNotEmpty else { return } |
|
|
|
|
// if actionOption.synchronize { |
|
|
|
|
// self.delete(contentOfs: items) |
|
|
|
|
// } else { |
|
|
|
|
// self.deleteNoSync(contentOfs: items) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
// public func deleteDependencies(_ items: any Sequence<T>) { |
|
|
|
|
// |
|
|
|
|
// self.collection.deleteDependencies(items) |
|
|
|
|
// |
|
|
|
|
//// super.deleteDependencies(items) |
|
|
|
|
// |
|
|
|
|
// let batch = OperationBatch<T>() |
|
|
|
|
// batch.deletes = Array(items) |
|
|
|
|
// Task { await self._sendOperationBatch(batch) } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
// public func deleteDependenciesAsync(_ items: any Sequence<T>) async { |
|
|
|
|
// super.deleteDependencies(items) |
|
|
|
|
// |
|
|
|
|
// let batch = OperationBatch<T>() |
|
|
|
|
// batch.deletes = Array(items) |
|
|
|
|
// await self._sendOperationBatch(batch) |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
fileprivate func _cleanUpSharedDependencies() { |
|
|
|
|
for relationship in T.relationships() { |
|
|
|
|
if let syncedType = relationship.type as? (any SyncedStorable.Type) { |
|
|
|
|
@ -246,11 +198,6 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect |
|
|
|
|
case .child: |
|
|
|
|
throw StoreError.invalidStoreLookup(from: type, to: relationship.type) |
|
|
|
|
} |
|
|
|
|
// if relationship.storeLookup { |
|
|
|
|
// store = self.store.storeCenter.mainStore |
|
|
|
|
// } else { |
|
|
|
|
// store = self.store |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
let collection: SyncedCollection<S> = try store.syncedCollection() |
|
|
|
|
collection._deleteUnusedGrantedInstances(originStoreId: originStoreId) |
|
|
|
|
@ -326,11 +273,6 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect |
|
|
|
|
self.collection.addOrUpdate(contentOfs: sequence) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Deletes the instance in the collection without synchronization |
|
|
|
|
// func deleteNoSync(instance: T) { |
|
|
|
|
// self.collection.delete(instance: instance) |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
public func deleteNoSync(contentOfs sequence: any RandomAccessCollection<T>) { |
|
|
|
|
self.collection.delete(contentOfs: sequence) |
|
|
|
|
} |
|
|
|
|
@ -346,16 +288,6 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect |
|
|
|
|
instance.deleteUnusedSharedDependencies(store: self.store) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Deletes the instance in the collection without synchronization |
|
|
|
|
// func deleteNoSyncNoCascade(id: String) { |
|
|
|
|
// self.collection.deleteByStringId(id, actionOption: .standard) |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// /// Deletes the instance in the collection without synchronization |
|
|
|
|
// func deleteNoSyncNoCascadeNoWrite(id: String) { |
|
|
|
|
// self.collection.deleteByStringId(id, actionOption: .noCascadeNoWrite) |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
func deleteByStringId(_ id: String, actionOption: ActionOption = .standard) { |
|
|
|
|
self.collection.deleteByStringId(id, actionOption: actionOption) |
|
|
|
|
} |
|
|
|
|
@ -431,10 +363,6 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect |
|
|
|
|
|
|
|
|
|
/// Adds or update an instance if it is newer than the local instance |
|
|
|
|
func addOrUpdateIfNewer(_ instance: T, shared: SharingStatus?) { |
|
|
|
|
|
|
|
|
|
// defer { |
|
|
|
|
// self.triggerWrite() |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
if let index = self.collection.items.firstIndex(where: { $0.id == instance.id }) { |
|
|
|
|
let localInstance = self.collection.items[index] |
|
|
|
|
@ -485,8 +413,8 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect |
|
|
|
|
return self.collection.items |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public func requestWrite() { |
|
|
|
|
self.collection.requestWrite() |
|
|
|
|
public func requestWriteIfNecessary() { |
|
|
|
|
self.collection.requestWriteIfNecessary() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -536,8 +464,6 @@ extension SyncedCollection: RandomAccessCollection { |
|
|
|
|
} |
|
|
|
|
set(newValue) { |
|
|
|
|
self.collection.update(newValue, index: index, actionOption: .standard) |
|
|
|
|
// self.collection.items[index] = newValue |
|
|
|
|
// self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|