diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2022-06-20 12:22:05 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2022-06-20 12:22:05 +0200 |
| commit | 19bb6e6220d79a899409755d187ad641679f327d (patch) | |
| tree | 6bc525a14295775fb6a5b9e11a6894d72e30f687 /ios/MullvadVPN/AddressCache/UpdateAddressCacheOperation.swift | |
| parent | d88d90c5a937ffbf42111e8721f6829e1040efdf (diff) | |
| parent | f456963c67db335bb448d901065e453e0408deb8 (diff) | |
| download | mullvadvpn-19bb6e6220d79a899409755d187ad641679f327d.tar.xz mullvadvpn-19bb6e6220d79a899409755d187ad641679f327d.zip | |
Merge branch 'background-task-appdelegate'
Diffstat (limited to 'ios/MullvadVPN/AddressCache/UpdateAddressCacheOperation.swift')
| -rw-r--r-- | ios/MullvadVPN/AddressCache/UpdateAddressCacheOperation.swift | 86 |
1 files changed, 0 insertions, 86 deletions
diff --git a/ios/MullvadVPN/AddressCache/UpdateAddressCacheOperation.swift b/ios/MullvadVPN/AddressCache/UpdateAddressCacheOperation.swift deleted file mode 100644 index 4eaeda18e1..0000000000 --- a/ios/MullvadVPN/AddressCache/UpdateAddressCacheOperation.swift +++ /dev/null @@ -1,86 +0,0 @@ -// -// UpdateAddressCacheOperation.swift -// MullvadVPN -// -// Created by pronebird on 08/12/2021. -// Copyright © 2021 Mullvad VPN AB. All rights reserved. -// - -import Foundation - -extension AddressCache { - - enum CacheUpdateResult { - /// Address cache update was throttled as it was requested too early. - case throttled(_ lastUpdateDate: Date) - - /// Address cache is successfully updated. - case finished - } - - class UpdateAddressCacheOperation: ResultOperation<CacheUpdateResult, Error> { - private let apiProxy: REST.APIProxy - private let store: AddressCache.Store - private let updateInterval: TimeInterval - - private var requestTask: Cancellable? - - init( - dispatchQueue: DispatchQueue, - apiProxy: REST.APIProxy, - store: AddressCache.Store, - updateInterval: TimeInterval, - completionHandler: CompletionHandler? - ) - { - self.apiProxy = apiProxy - self.store = store - self.updateInterval = updateInterval - - super.init( - dispatchQueue: dispatchQueue, - completionQueue: dispatchQueue, - completionHandler: completionHandler - ) - } - - override func main() { - let lastUpdate = store.getLastUpdateDate() - let nextUpdate = Date(timeInterval: updateInterval, since: lastUpdate) - - guard nextUpdate <= Date() else { - finish(completion: .success(.throttled(lastUpdate))) - return - } - - requestTask = apiProxy.getAddressList(retryStrategy: .default) { [weak self] completion in - self?.dispatchQueue.async { - self?.handleResponse(completion) - } - } - } - - override func operationDidCancel() { - requestTask?.cancel() - requestTask = nil - } - - private func handleResponse(_ completion: OperationCompletion<[AnyIPEndpoint], REST.Error>) { - let mappedCompletion = completion - .flatMapError { error -> OperationCompletion<[AnyIPEndpoint], Error> in - if case URLError.cancelled = error { - return .cancelled - } else { - return .failure(error) - } - } - .tryMap { endpoints -> CacheUpdateResult in - try store.setEndpoints(endpoints) - - return .finished - } - - finish(completion: mappedCompletion) - } - } -} |
