improvements

upgrade
Laurent 5 days ago
parent d019e491af
commit 17148d498d
  1. 2
      LeStorage/Store.swift
  2. 25
      LeStorage/StoredCollection.swift
  3. 4
      LeStorage/SyncedCollection.swift

@ -274,7 +274,7 @@ final public class Store {
// MARK: - Synchronization // MARK: - Synchronization
fileprivate func _requestWrite<T: SyncedStorable>(type: T.Type) { fileprivate func _requestWrite<T: SyncedStorable>(type: T.Type) {
self._baseCollections[T.resourceName()]?.requestWrite() self._baseCollections[T.resourceName()]?.requestWriteIfNecessary()
} }
@MainActor @MainActor

@ -25,7 +25,7 @@ public protocol SomeCollection<Item>: Identifiable {
func deleteDependencies(actionOption: ActionOption, _ isIncluded: (Item) -> Bool) func deleteDependencies(actionOption: ActionOption, _ isIncluded: (Item) -> Bool)
func findById(_ id: Item.ID) -> Item? func findById(_ id: Item.ID) -> Item?
func requestWrite() func requestWriteIfNecessary()
} }
protocol CollectionDelegate<Item> { protocol CollectionDelegate<Item> {
@ -81,7 +81,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
/// Indicates whether the collection has changed, thus requiring a write operation /// Indicates whether the collection has changed, thus requiring a write operation
fileprivate var _triggerWrite: Bool = false { fileprivate var _triggerWrite: Bool = false {
didSet { didSet {
if self._triggerWrite == true && self.inMemory == false { if self._triggerWrite == true {
self._scheduleWrite() self._scheduleWrite()
self._triggerWrite = false self._triggerWrite = false
} }
@ -143,8 +143,10 @@ public class StoredCollection<T: Storable>: SomeCollection {
// MARK: - Loading // MARK: - Loading
/// Sets the collection as changed to trigger a write /// Sets the collection as changed to trigger a write
public func requestWrite() { public func requestWriteIfNecessary() {
self._triggerWrite = true if self.inMemory == false {
self._triggerWrite = true
}
} }
/// Migrates if necessary and asynchronously decodes the json file /// Migrates if necessary and asynchronously decodes the json file
@ -214,7 +216,6 @@ public class StoredCollection<T: Storable>: SomeCollection {
for item in items { for item in items {
self._addItem(instance: item) self._addItem(instance: item)
} }
self.requestWrite()
} }
@MainActor @MainActor
@ -226,7 +227,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
self.setAsLoaded() self.setAsLoaded()
self.addOrUpdate(contentOfs: items) self.addOrUpdate(contentOfs: items)
} }
self.requestWriteIfNecessary()
} }
/// Updates the whole index with the items array /// Updates the whole index with the items array
@ -242,7 +243,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
/// Adds it if its id is not found, and otherwise updates it /// Adds it if its id is not found, and otherwise updates it
@discardableResult public func addOrUpdate(instance: T) -> ActionResult<T> { @discardableResult public func addOrUpdate(instance: T) -> ActionResult<T> {
defer { defer {
self.requestWrite() self.requestWriteIfNecessary()
} }
return self._rawAddOrUpdate(instance: instance) return self._rawAddOrUpdate(instance: instance)
} }
@ -251,7 +252,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
public func addOrUpdate(contentOfs sequence: any Sequence<T>, _ handler: ((ActionResult<T>) -> ())? = nil) { public func addOrUpdate(contentOfs sequence: any Sequence<T>, _ handler: ((ActionResult<T>) -> ())? = nil) {
defer { defer {
self.requestWrite() self.requestWriteIfNecessary()
} }
for instance in sequence { for instance in sequence {
@ -274,7 +275,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
/// A method the treat the collection as a single instance holder /// A method the treat the collection as a single instance holder
func setSingletonNoSync(instance: T) { func setSingletonNoSync(instance: T) {
defer { defer {
self.requestWrite() self.requestWriteIfNecessary()
} }
self.items.removeAll() self.items.removeAll()
self._addItem(instance: instance) self._addItem(instance: instance)
@ -424,7 +425,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
self.deleteItem(instance, actionOption: actionOption) self.deleteItem(instance, actionOption: actionOption)
} }
if actionOption.write { if actionOption.write {
self.requestWrite() self.requestWriteIfNecessary()
} }
} }
@ -440,7 +441,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
/// Also removes related API calls /// Also removes related API calls
public func deleteDependencies(_ items: any Sequence<T>) { public func deleteDependencies(_ items: any Sequence<T>) {
defer { defer {
self.requestWrite() self.requestWriteIfNecessary()
} }
let itemsArray = Array(items) // fix error if items is self.items let itemsArray = Array(items) // fix error if items is self.items
for item in itemsArray { for item in itemsArray {
@ -567,7 +568,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
func updateLocalInstance(_ serverInstance: T) { func updateLocalInstance(_ serverInstance: T) {
if let localInstance = self.findById(serverInstance.id) { if let localInstance = self.findById(serverInstance.id) {
localInstance.copy(from: serverInstance) localInstance.copy(from: serverInstance)
self.requestWrite() self.requestWriteIfNecessary()
} }
} }

@ -413,8 +413,8 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect
return self.collection.items return self.collection.items
} }
public func requestWrite() { public func requestWriteIfNecessary() {
self.collection.requestWrite() self.collection.requestWriteIfNecessary()
} }
} }

Loading…
Cancel
Save