@@ -39,15 +39,6 @@ public final class Store<Item: Codable & Equatable>: ObservableObject {
39
39
self . objectStorage = ObjectStorage ( storagePath: storagePath)
40
40
self . cacheIdentifier = cacheIdentifier
41
41
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
-
51
42
Task { @MainActor in
52
43
self . items = await self . allPersistedItems ( )
53
44
}
@@ -70,10 +61,10 @@ public final class Store<Item: Codable & Equatable>: ObservableObject {
70
61
/// - items: The items to add to the store.
71
62
/// - invalidationStrategy: An optional invalidation strategy for this add operation.
72
63
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
74
65
75
66
try await self . removePersistedItems ( strategy: invalidationStrategy)
76
- self . invalidateCache ( strategy: invalidationStrategy, items: & currentItems )
67
+ self . invalidateCache ( strategy: invalidationStrategy, items: & updatedItems )
77
68
78
69
// Prevent duplicate values from being written multiple times.
79
70
// This could cause a discrepancy between the data in memory
@@ -85,39 +76,32 @@ public final class Store<Item: Codable & Equatable>: ObservableObject {
85
76
for item in uniqueItems {
86
77
if let matchingIdentifierIndex = itemKeys. firstIndex ( of: item [ keyPath: self . cacheIdentifier] ) ,
87
78
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 } ) {
89
80
// 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)
92
83
} else {
93
84
// Append it to the cache if it doesn't already exist
94
- currentItems . append ( item)
85
+ updatedItems . append ( item)
95
86
}
96
87
97
88
itemKeys. removeAll ( where: { $0 == item [ keyPath: self . cacheIdentifier] } )
98
89
}
99
90
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.
101
94
// An implicitly captured closure variable is captured by reference while
102
95
// 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
105
98
}
106
99
}
107
100
108
101
/// Removes an item from the store.
109
102
/// - Parameter item: The item you are removing from the `Store`.
110
103
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] )
121
105
}
122
106
123
107
/// Removes a list of items from the store.
@@ -126,12 +110,9 @@ public final class Store<Item: Codable & Equatable>: ObservableObject {
126
110
/// avoid making multiple separate dispatches to the `@MainActor`.
127
111
/// - Parameter item: The items you are removing from the `Store`.
128
112
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] } )
131
114
132
- for cacheKey in cacheKeys {
133
- try await self . removePersistedItem ( forKey: cacheKey)
134
- }
115
+ try await self . removePersistedItems ( items: items)
135
116
136
117
await MainActor . run {
137
118
self . items. removeAll ( where: { item in
@@ -174,8 +155,13 @@ private extension Store {
174
155
}
175
156
}
176
157
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
+ }
179
165
}
180
166
181
167
func removeAllPersistedItems( ) async throws {
0 commit comments