|
|
|
|
@ -45,16 +45,16 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
fileprivate(set) var pendingOperationManager: PendingOperationManager<T>? = nil |
|
|
|
|
|
|
|
|
|
/// Indicates whether the collection has changed, thus requiring a write operation |
|
|
|
|
fileprivate var _hasChanged: Bool = false { |
|
|
|
|
fileprivate var _triggerWrite: Bool = false { |
|
|
|
|
didSet { |
|
|
|
|
if self._hasChanged == true { |
|
|
|
|
if self._triggerWrite == true { |
|
|
|
|
|
|
|
|
|
self._scheduleWrite() |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
NotificationCenter.default.post( |
|
|
|
|
name: NSNotification.Name.CollectionDidChange, object: self) |
|
|
|
|
} |
|
|
|
|
self._hasChanged = false |
|
|
|
|
self._triggerWrite = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -109,8 +109,8 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
// MARK: - Loading |
|
|
|
|
|
|
|
|
|
/// Sets the collection as changed to trigger a write |
|
|
|
|
func setChanged() { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
func triggerWrite() { |
|
|
|
|
self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Migrates if necessary and asynchronously decodes the json file |
|
|
|
|
@ -188,7 +188,7 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
func addOrUpdateItem(instance: T) { |
|
|
|
|
|
|
|
|
|
defer { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let index = self.items.firstIndex(where: { $0.id == instance.id }) { |
|
|
|
|
@ -202,23 +202,16 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
/// A method the treat the collection as a single instance holder |
|
|
|
|
func setSingletonNoSync(instance: T) { |
|
|
|
|
defer { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
self.items.removeAll() |
|
|
|
|
self.addItem(instance: instance) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Deletes an item by its id |
|
|
|
|
public func deleteById(_ id: T.ID) { |
|
|
|
|
if let instance = self.findById(id) { |
|
|
|
|
self.delete(instance: instance) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Deletes the instance in the collection and sets the collection as changed to trigger a write |
|
|
|
|
public func delete(instance: T) { |
|
|
|
|
defer { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
self.deleteItem(instance) |
|
|
|
|
} |
|
|
|
|
@ -227,7 +220,7 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
public func delete(contentOfs sequence: any RandomAccessCollection<T>) { |
|
|
|
|
|
|
|
|
|
defer { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for instance in sequence { |
|
|
|
|
@ -242,16 +235,16 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Adds a sequence of objects inside the collection and performs a write |
|
|
|
|
func addSequence(_ sequence: any Sequence<T>, checkLoaded: Bool = true) { |
|
|
|
|
func addSequence(_ sequence: any Sequence<T>) { |
|
|
|
|
defer { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for instance in sequence { |
|
|
|
|
if let index = self.items.firstIndex(where: { $0.id == instance.id }) { |
|
|
|
|
self.updateItem(instance, index: index, checkLoaded: checkLoaded) |
|
|
|
|
self.updateItem(instance, index: index) |
|
|
|
|
} else { // insert |
|
|
|
|
self.addItem(instance: instance, checkLoaded: checkLoaded) |
|
|
|
|
self.addItem(instance: instance) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -269,9 +262,9 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Adds an instance to the collection |
|
|
|
|
@discardableResult func addItem(instance: T, checkLoaded: Bool = true, shouldBeSynchronized: Bool = false) -> Bool { |
|
|
|
|
@discardableResult func addItem(instance: T, shouldBeSynchronized: Bool = false) -> Bool { |
|
|
|
|
|
|
|
|
|
if checkLoaded && !self.hasLoaded { |
|
|
|
|
if !self.hasLoaded { |
|
|
|
|
self.addPendingOperation(method: .addOrUpdate, instance: instance, shouldBeSynchronized: shouldBeSynchronized) |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
@ -285,9 +278,9 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Updates an instance to the collection by index |
|
|
|
|
@discardableResult func updateItem(_ instance: T, index: Int, checkLoaded: Bool = true, shouldBeSynchronized: Bool = false) -> Bool { |
|
|
|
|
@discardableResult func updateItem(_ instance: T, index: Int, shouldBeSynchronized: Bool = false) -> Bool { |
|
|
|
|
|
|
|
|
|
if checkLoaded && !self.hasLoaded { |
|
|
|
|
if !self.hasLoaded { |
|
|
|
|
self.addPendingOperation(method: .addOrUpdate, instance: instance, shouldBeSynchronized: shouldBeSynchronized) |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
@ -358,7 +351,7 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
/// Also removes related API calls |
|
|
|
|
public func deleteDependencies(_ items: any Sequence<T>) { |
|
|
|
|
defer { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
let itemsArray = Array(items) // fix error if items is self.items |
|
|
|
|
for item in itemsArray { |
|
|
|
|
@ -368,11 +361,6 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Proceeds to delete all instance of the collection, properly cleaning up dependencies and sending API calls |
|
|
|
|
public func deleteAll() throws { |
|
|
|
|
self.delete(contentOfs: self.items) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Pending operations |
|
|
|
|
|
|
|
|
|
func addPendingOperation(method: StorageMethod, instance: T, shouldBeSynchronized: Bool) { |
|
|
|
|
@ -443,7 +431,7 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
public func reset() { |
|
|
|
|
self.items.removeAll() |
|
|
|
|
self.store.removeFile(type: T.self) |
|
|
|
|
setChanged() |
|
|
|
|
triggerWrite() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public var type: any Storable.Type { return T.self } |
|
|
|
|
@ -488,7 +476,7 @@ public class StoredCollection<T: Storable>: BaseCollection<T>, RandomAccessColle |
|
|
|
|
} |
|
|
|
|
set(newValue) { |
|
|
|
|
self.items[index] = newValue |
|
|
|
|
self._hasChanged = true |
|
|
|
|
self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -510,7 +498,7 @@ extension SyncedCollection: RandomAccessCollection { |
|
|
|
|
} |
|
|
|
|
set(newValue) { |
|
|
|
|
self.items[index] = newValue |
|
|
|
|
self._hasChanged = true |
|
|
|
|
self._triggerWrite = true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|