Skip to content

Commit 67c2308

Browse files
gh-action-runnergh-action-runner
authored andcommitted
Squashed 'apollo-ios/' changes from 28c559ea..6200e120
6200e120 Make cache public within ReadTransaction (#661) git-subtree-dir: apollo-ios git-subtree-split: 6200e12074f7935b919451442160f8bc21040da6
1 parent 2b7a7ac commit 67c2308

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Sources/Apollo/ApolloStore.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,21 @@ public class ApolloStore {
201201
}
202202

203203
public class ReadTransaction {
204-
fileprivate let cache: any NormalizedCache
205-
204+
fileprivate let _cache: any NormalizedCache
205+
206+
public var readOnlyCache: any ReadOnlyNormalizedCache { _cache }
207+
206208
fileprivate lazy var loader: DataLoader<CacheKey, Record> = DataLoader { [weak self] batchLoad in
207209
guard let self else { return [:] }
208-
return try cache.loadRecords(forKeys: batchLoad)
210+
return try _cache.loadRecords(forKeys: batchLoad)
209211
}
210212

211213
fileprivate lazy var executor = GraphQLExecutor(
212214
executionSource: CacheDataExecutionSource(transaction: self)
213215
)
214216

215217
fileprivate init(store: ApolloStore) {
216-
self.cache = store.cache
218+
self._cache = store.cache
217219
}
218220

219221
public func read<Query: GraphQLQuery>(query: Query) throws -> Query.Data {
@@ -264,6 +266,8 @@ public class ApolloStore {
264266

265267
public final class ReadWriteTransaction: ReadTransaction {
266268

269+
public var cache: any NormalizedCache { _cache }
270+
267271
fileprivate var updateChangedKeysFunc: DidChangeKeysFunc?
268272

269273
override init(store: ApolloStore) {

Sources/Apollo/NormalizedCache.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
public protocol NormalizedCache: AnyObject {
2-
1+
public protocol NormalizedCache: AnyObject, ReadOnlyNormalizedCache {
2+
33
/// Loads records corresponding to the given keys.
44
///
55
/// - Parameters:
66
/// - key: The cache keys to load data for
77
/// - Returns: A dictionary of cache keys to records containing the records that have been found.
88
func loadRecords(forKeys keys: Set<CacheKey>) throws -> [CacheKey: Record]
9-
9+
1010
/// Merges a set of records into the cache.
1111
///
1212
/// - Parameters:
@@ -46,3 +46,15 @@ public protocol NormalizedCache: AnyObject {
4646
/// Clears all records.
4747
func clear() throws
4848
}
49+
50+
/// A read-only view of a `NormalizedCache` for use within a `ReadTransaction`.
51+
public protocol ReadOnlyNormalizedCache: AnyObject {
52+
53+
/// Loads records corresponding to the given keys.
54+
///
55+
/// - Parameters:
56+
/// - key: The cache keys to load data for
57+
/// - Returns: A dictionary of cache keys to records containing the records that have been found.
58+
func loadRecords(forKeys keys: Set<CacheKey>) throws -> [CacheKey: Record]
59+
60+
}

0 commit comments

Comments
 (0)