Compare commits

..

No commits in common. 'main' and 'upgrade' have entirely different histories.

  1. 39
      LeStorage/StoredCollection.swift
  2. 9
      LeStorage/SyncedCollection.swift

@ -212,7 +212,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
/// Sets a collection of items and indexes them /// Sets a collection of items and indexes them
func setItems(_ items: [T]) { func setItems(_ items: [T]) {
self.clear() self.items.removeAll()
for item in items { for item in items {
self._addItem(instance: item) self._addItem(instance: item)
} }
@ -277,7 +277,7 @@ public class StoredCollection<T: Storable>: SomeCollection {
defer { defer {
self.requestWriteIfNecessary() self.requestWriteIfNecessary()
} }
self.clear() self.items.removeAll()
self._addItem(instance: instance) self._addItem(instance: instance)
} }
@ -333,7 +333,6 @@ public class StoredCollection<T: Storable>: SomeCollection {
self.addPendingOperation(method: .add, instance: instance, actionOption: actionOption) self.addPendingOperation(method: .add, instance: instance, actionOption: actionOption)
return false return false
} }
self.invalidateCache()
self._affectStoreIdIfNecessary(instance: instance) self._affectStoreIdIfNecessary(instance: instance)
self.items.append(instance) self.items.append(instance)
@ -360,7 +359,6 @@ public class StoredCollection<T: Storable>: SomeCollection {
self.addPendingOperation(method: .update, instance: instance, actionOption: actionOption) self.addPendingOperation(method: .update, instance: instance, actionOption: actionOption)
return false return false
} }
self.invalidateCache()
let item = self.items[index] let item = self.items[index]
if item !== instance { if item !== instance {
@ -410,7 +408,6 @@ public class StoredCollection<T: Storable>: SomeCollection {
} }
func localDeleteOnly(instance: T) { func localDeleteOnly(instance: T) {
self.invalidateCache()
self.items.removeAll { $0.id == instance.id } self.items.removeAll { $0.id == instance.id }
self._indexes?.removeValue(forKey: instance.id) self._indexes?.removeValue(forKey: instance.id)
} }
@ -440,12 +437,12 @@ public class StoredCollection<T: Storable>: SomeCollection {
return self.items.first(where: { $0.id == id }) return self.items.first(where: { $0.id == id })
} }
/// Deletes a list of items /// Proceeds to "hard" delete the items without synchronizing them
/// Also removes related API calls
public func deleteDependencies(_ items: any Sequence<T>) { public func deleteDependencies(_ items: any Sequence<T>) {
defer { defer {
self.requestWriteIfNecessary() self.requestWriteIfNecessary()
} }
self.invalidateCache()
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 {
if let index = self.items.firstIndex(where: { $0.id == item.id }) { if let index = self.items.firstIndex(where: { $0.id == item.id }) {
@ -537,13 +534,12 @@ public class StoredCollection<T: Storable>: SomeCollection {
/// Simply clears the items of the collection /// Simply clears the items of the collection
public func clear() { public func clear() {
self.invalidateCache()
self.items.removeAll() self.items.removeAll()
} }
/// Removes the items of the collection and deletes the corresponding file /// Removes the items of the collection and deletes the corresponding file
public func reset() { public func reset() {
self.clear() self.items.removeAll()
self.store.removeFile(type: T.self) self.store.removeFile(type: T.self)
} }
@ -576,31 +572,6 @@ public class StoredCollection<T: Storable>: SomeCollection {
} }
} }
// MARK: - Cached queries
fileprivate var _cacheVersion = 0
fileprivate var _queryCache: [AnyHashable: (version: Int, result: Any)] = [:]
// Generic query method with caching
public func cached<Result>(
key: AnyHashable,
compute: ([T]) -> Result
) -> Result {
if let cached = self._queryCache[key],
cached.version == self._cacheVersion,
let result = cached.result as? Result {
return result
}
let result = compute(items)
self._queryCache[key] = (self._cacheVersion, result)
return result
}
private func invalidateCache() {
self._cacheVersion += 1
}
} }
extension StoredCollection: RandomAccessCollection { extension StoredCollection: RandomAccessCollection {

@ -417,15 +417,6 @@ public class SyncedCollection<T : SyncedStorable>: SomeSyncedCollection, Collect
self.collection.requestWriteIfNecessary() self.collection.requestWriteIfNecessary()
} }
// MARK: - Cached queries
public func cached<Result>(
key: AnyHashable,
compute: ([T]) -> Result
) -> Result {
return self.collection.cached(key: key, compute: compute)
}
} }
class OperationBatch<T> { class OperationBatch<T> {

Loading…
Cancel
Save