diff --git a/Sources/Networking/Caching/CustomerInfoCallback.swift b/Sources/Networking/Caching/CustomerInfoCallback.swift index 33afb1786e..79c15b9d11 100644 --- a/Sources/Networking/Caching/CustomerInfoCallback.swift +++ b/Sources/Networking/Caching/CustomerInfoCallback.swift @@ -17,9 +17,9 @@ struct CustomerInfoCallback: CacheKeyProviding { typealias Completion = (Result) -> Void - let cacheKey: String - let source: NetworkOperation.Type - let completion: Completion + var cacheKey: String + var source: NetworkOperation.Type + var completion: Completion init(operation: CacheableNetworkOperation, completion: @escaping Completion) { self.cacheKey = operation.cacheKey @@ -28,3 +28,37 @@ struct CustomerInfoCallback: CacheKeyProviding { } } + +// MARK: - CallbackCache helpers + +extension CallbackCache where T == CustomerInfoCallback { + + func addOrAppendToPostReceiptDataOperation(callback: CustomerInfoCallback) -> CallbackCacheStatus { + if let existing = self.callbacks(ofType: PostReceiptDataOperation.self).last { + return self.add(callback: callback.withNewCacheKey(existing.cacheKey)) + } else { + return self.add(callback: callback) + } + } + + private func callbacks(ofType type: NetworkOperation.Type) -> [T] { + return self + .cachedCallbacksByKey + .value + .lazy + .flatMap(\.value) + .filter { $0.source == type } + } + +} + +private extension CustomerInfoCallback { + + func withNewCacheKey(_ newKey: String) -> Self { + var copy = self + copy.cacheKey = newKey + + return copy + } + +} diff --git a/Sources/Networking/CustomerAPI.swift b/Sources/Networking/CustomerAPI.swift index 80b11ef7ae..04c67fad04 100644 --- a/Sources/Networking/CustomerAPI.swift +++ b/Sources/Networking/CustomerAPI.swift @@ -38,7 +38,7 @@ final class CustomerAPI { customerInfoCallbackCache: self.customerInfoCallbackCache) let callback = CustomerInfoCallback(operation: operation, completion: completion) - let cacheStatus = self.customerInfoCallbackCache.add(callback: callback) + let cacheStatus = self.customerInfoCallbackCache.addOrAppendToPostReceiptDataOperation(callback: callback) self.backendConfig.addCacheableOperation(operation, withRandomDelay: randomDelay, cacheStatus: cacheStatus) diff --git a/Sources/Networking/Operations/GetCustomerInfoOperation.swift b/Sources/Networking/Operations/GetCustomerInfoOperation.swift index 425723ffa3..f3416261fc 100644 --- a/Sources/Networking/Operations/GetCustomerInfoOperation.swift +++ b/Sources/Networking/Operations/GetCustomerInfoOperation.swift @@ -26,18 +26,8 @@ class GetCustomerInfoOperation: CacheableNetworkOperation { self.customerInfoResponseHandler = customerInfoResponseHandler self.customerInfoCallbackCache = customerInfoCallbackCache - var individualizedCacheKeyPart = configuration.appUserID - - // If there is any enqueued `PostReceiptDataOperation` we don't want this new - // `GetCustomerInfoOperation` to share the same cache key. - // If it did, future `GetCustomerInfoOperation` would receive a cached value - // instead of an up-to-date `CustomerInfo` after those post receipt operations finish. - if customerInfoCallbackCache.hasPostReceiptOperations { - individualizedCacheKeyPart += "-\(customerInfoCallbackCache.numberOfGetCustomerInfoOperations)" - } - super.init(configuration: configuration, - individualizedCacheKeyPart: individualizedCacheKeyPart) + individualizedCacheKeyPart: configuration.appUserID) } override func begin(completion: @escaping () -> Void) { @@ -72,25 +62,3 @@ private extension GetCustomerInfoOperation { } } - -private extension CallbackCache where T == CustomerInfoCallback { - - var numberOfGetCustomerInfoOperations: Int { - return self.callbacks(ofType: GetCustomerInfoOperation.self) - } - - var hasPostReceiptOperations: Bool { - return self.callbacks(ofType: PostReceiptDataOperation.self) > 0 - } - - private func callbacks(ofType type: NetworkOperation.Type) -> Int { - return self - .cachedCallbacksByKey - .value - .lazy - .flatMap(\.value) - .filter { $0.source == type } - .count - } - -} diff --git a/Tests/UnitTests/Networking/Backend/BackendPostReceiptDataTests.swift b/Tests/UnitTests/Networking/Backend/BackendPostReceiptDataTests.swift index 0aefe95aa3..e53f772c3c 100644 --- a/Tests/UnitTests/Networking/Backend/BackendPostReceiptDataTests.swift +++ b/Tests/UnitTests/Networking/Backend/BackendPostReceiptDataTests.swift @@ -432,8 +432,7 @@ class BackendPostReceiptDataTests: BaseBackendTests { expect(self.httpClient.calls.map { $0.request.path }) == [ getCustomerInfoPath, - .postReceiptData, - getCustomerInfoPath + .postReceiptData ] }