Skip to content

Commit f1e8bdc

Browse files
committed
Removing unneeded subscription and cleaning up some functions
1 parent 1fc3ce1 commit f1e8bdc

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

Sources/Boutique/Store.swift

+21-35
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ public final class Store<Item: Codable & Equatable>: ObservableObject {
3939
self.objectStorage = ObjectStorage(storagePath: storagePath)
4040
self.cacheIdentifier = cacheIdentifier
4141

42-
self.$items
43-
.removeDuplicates()
44-
.sink(receiveValue: { items in
45-
Task {
46-
try await self.persistItems(items)
47-
}
48-
})
49-
.store(in: &cancellables)
50-
5142
Task { @MainActor in
5243
self.items = await self.allPersistedItems()
5344
}
@@ -70,10 +61,10 @@ public final class Store<Item: Codable & Equatable>: ObservableObject {
7061
/// - items: The items to add to the store.
7162
/// - invalidationStrategy: An optional invalidation strategy for this add operation.
7263
public func add(_ items: [Item], invalidationStrategy: CacheInvalidationStrategy<Item> = .removeNone) async throws {
73-
var currentItems: [Item] = await self.items
64+
var updatedItems: [Item] = await self.items
7465

7566
try await self.removePersistedItems(strategy: invalidationStrategy)
76-
self.invalidateCache(strategy: invalidationStrategy, items: &currentItems)
67+
self.invalidateCache(strategy: invalidationStrategy, items: &updatedItems)
7768

7869
// Prevent duplicate values from being written multiple times.
7970
// This could cause a discrepancy between the data in memory
@@ -85,39 +76,32 @@ public final class Store<Item: Codable & Equatable>: ObservableObject {
8576
for item in uniqueItems {
8677
if let matchingIdentifierIndex = itemKeys.firstIndex(of: item[keyPath: self.cacheIdentifier]),
8778
case let matchingIdentifier = itemKeys[matchingIdentifierIndex],
88-
let index = currentItems.firstIndex(where: { $0[keyPath: self.cacheIdentifier] == matchingIdentifier }) {
79+
let index = updatedItems.firstIndex(where: { $0[keyPath: self.cacheIdentifier] == matchingIdentifier }) {
8980
// We found a matching element with potentially different data so replace it in-place
90-
currentItems.remove(at: index)
91-
currentItems.insert(item, at: index)
81+
updatedItems.remove(at: index)
82+
updatedItems.insert(item, at: index)
9283
} else {
9384
// Append it to the cache if it doesn't already exist
94-
currentItems.append(item)
85+
updatedItems.append(item)
9586
}
9687

9788
itemKeys.removeAll(where: { $0 == item[keyPath: self.cacheIdentifier] })
9889
}
9990

100-
// We can't capture a mutable array (currentItems) in the closure below so we make an immutable copy.
91+
try await self.persistItems(updatedItems)
92+
93+
// We can't capture a mutable array (updatedItems) in the closure below so we make an immutable copy.
10194
// An implicitly captured closure variable is captured by reference while
10295
// a variable captured in the capture group is captured by value.
103-
await MainActor.run { [currentItems] in
104-
self.items = currentItems
96+
await MainActor.run { [updatedItems] in
97+
self.items = updatedItems
10598
}
10699
}
107100

108101
/// Removes an item from the store.
109102
/// - Parameter item: The item you are removing from the `Store`.
110103
public func remove(_ item: Item) async throws {
111-
let itemKey = item[keyPath: self.cacheIdentifier]
112-
let cacheKey = CacheKey(itemKey)
113-
114-
try await self.removePersistedItem(forKey: cacheKey)
115-
116-
await MainActor.run {
117-
self.items.removeAll(where: {
118-
itemKey == $0[keyPath: self.cacheIdentifier]
119-
})
120-
}
104+
try await self.remove([item])
121105
}
122106

123107
/// Removes a list of items from the store.
@@ -126,12 +110,9 @@ public final class Store<Item: Codable & Equatable>: ObservableObject {
126110
/// avoid making multiple separate dispatches to the `@MainActor`.
127111
/// - Parameter item: The items you are removing from the `Store`.
128112
public func remove(_ items: [Item]) async throws {
129-
let itemKeys = items.map { $0[keyPath: self.cacheIdentifier] }
130-
let cacheKeys = itemKeys.map({ CacheKey($0) })
113+
let itemKeys = items.map({ $0[keyPath: self.cacheIdentifier] })
131114

132-
for cacheKey in cacheKeys {
133-
try await self.removePersistedItem(forKey: cacheKey)
134-
}
115+
try await self.removePersistedItems(items: items)
135116

136117
await MainActor.run {
137118
self.items.removeAll(where: { item in
@@ -174,8 +155,13 @@ private extension Store {
174155
}
175156
}
176157

177-
func removePersistedItem(forKey cacheKey: CacheKey) async throws {
178-
try await self.objectStorage.removeObject(forKey: cacheKey)
158+
func removePersistedItems(items: [Item]) async throws {
159+
let itemKeys = items.map({ $0[keyPath: self.cacheIdentifier] })
160+
.map({ CacheKey($0) })
161+
162+
for cacheKey in itemKeys {
163+
try await self.objectStorage.removeObject(forKey: cacheKey)
164+
}
179165
}
180166

181167
func removeAllPersistedItems() async throws {

0 commit comments

Comments
 (0)