mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.5 KiB
Swift
136 lines
4.5 KiB
Swift
3 years ago
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||
|
|
||
|
import Foundation
|
||
3 years ago
|
import GRDB
|
||
3 years ago
|
|
||
2 years ago
|
public class Dependencies {
|
||
|
private var _storage: Atomic<Storage?>
|
||
|
public var storage: Storage {
|
||
|
get { Dependencies.getValueSettingIfNull(&_storage) { Storage.shared } }
|
||
|
set { _storage.mutate { $0 = newValue } }
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
private var _network: Atomic<NetworkType?>
|
||
|
public var network: NetworkType {
|
||
|
get { Dependencies.getValueSettingIfNull(&_network) { Network() } }
|
||
|
set { _network.mutate { $0 = newValue } }
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
private var _crypto: Atomic<CryptoType?>
|
||
|
public var crypto: CryptoType {
|
||
|
get { Dependencies.getValueSettingIfNull(&_crypto) { Crypto() } }
|
||
|
set { _crypto.mutate { $0 = newValue } }
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
private var _standardUserDefaults: Atomic<UserDefaultsType?>
|
||
|
public var standardUserDefaults: UserDefaultsType {
|
||
|
get { Dependencies.getValueSettingIfNull(&_standardUserDefaults) { UserDefaults.standard } }
|
||
|
set { _standardUserDefaults.mutate { $0 = newValue } }
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
private var _caches: CachesType
|
||
|
public var caches: CachesType {
|
||
|
get { _caches }
|
||
|
set { _caches = newValue }
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
private var _jobRunner: Atomic<JobRunnerType?>
|
||
2 years ago
|
public var jobRunner: JobRunnerType {
|
||
|
get { Dependencies.getValueSettingIfNull(&_jobRunner) { JobRunner.instance } }
|
||
|
set { _jobRunner.mutate { $0 = newValue } }
|
||
|
}
|
||
|
|
||
2 years ago
|
private var _scheduler: Atomic<ValueObservationScheduler?>
|
||
3 years ago
|
public var scheduler: ValueObservationScheduler {
|
||
|
get { Dependencies.getValueSettingIfNull(&_scheduler) { Storage.defaultPublisherScheduler } }
|
||
|
set { _scheduler.mutate { $0 = newValue } }
|
||
|
}
|
||
|
|
||
2 years ago
|
private var _dateNow: Atomic<Date?>
|
||
|
public var dateNow: Date {
|
||
|
get { (_dateNow.wrappedValue ?? Date()) }
|
||
|
set { _dateNow.mutate { $0 = newValue } }
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
private var _fixedTime: Atomic<Int?>
|
||
2 years ago
|
public var fixedTime: Int {
|
||
|
get { Dependencies.getValueSettingIfNull(&_fixedTime) { 0 } }
|
||
|
set { _fixedTime.mutate { $0 = newValue } }
|
||
|
}
|
||
|
|
||
2 years ago
|
private var _forceSynchronous: Bool
|
||
|
public var forceSynchronous: Bool {
|
||
|
get { _forceSynchronous }
|
||
|
set { _forceSynchronous = newValue }
|
||
|
}
|
||
|
|
||
|
public var asyncExecutions: [Int: [() -> Void]] = [:]
|
||
|
|
||
3 years ago
|
// MARK: - Initialization
|
||
|
|
||
|
public init(
|
||
3 years ago
|
storage: Storage? = nil,
|
||
2 years ago
|
network: NetworkType? = nil,
|
||
|
crypto: CryptoType? = nil,
|
||
|
standardUserDefaults: UserDefaultsType? = nil,
|
||
|
caches: CachesType = Caches(),
|
||
2 years ago
|
jobRunner: JobRunnerType? = nil,
|
||
3 years ago
|
scheduler: ValueObservationScheduler? = nil,
|
||
2 years ago
|
dateNow: Date? = nil,
|
||
|
fixedTime: Int? = nil,
|
||
|
forceSynchronous: Bool = false
|
||
3 years ago
|
) {
|
||
3 years ago
|
_storage = Atomic(storage)
|
||
2 years ago
|
_network = Atomic(network)
|
||
|
_crypto = Atomic(crypto)
|
||
|
_standardUserDefaults = Atomic(standardUserDefaults)
|
||
|
_caches = caches
|
||
2 years ago
|
_jobRunner = Atomic(jobRunner)
|
||
3 years ago
|
_scheduler = Atomic(scheduler)
|
||
2 years ago
|
_dateNow = Atomic(dateNow)
|
||
2 years ago
|
_fixedTime = Atomic(fixedTime)
|
||
2 years ago
|
_forceSynchronous = forceSynchronous
|
||
3 years ago
|
}
|
||
|
|
||
|
// MARK: - Convenience
|
||
3 years ago
|
|
||
2 years ago
|
private static func getValueSettingIfNull<T>(_ maybeValue: inout Atomic<T?>, _ valueGenerator: () -> T) -> T {
|
||
3 years ago
|
guard let value: T = maybeValue.wrappedValue else {
|
||
3 years ago
|
let value: T = valueGenerator()
|
||
3 years ago
|
maybeValue.mutate { $0 = value }
|
||
3 years ago
|
return value
|
||
|
}
|
||
|
|
||
|
return value
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
private static func getMutableValueSettingIfNull<T>(_ maybeValue: inout Atomic<T?>, _ valueGenerator: () -> T) -> Atomic<T> {
|
||
2 years ago
|
guard let value: T = maybeValue.wrappedValue else {
|
||
|
let value: T = valueGenerator()
|
||
|
maybeValue.mutate { $0 = value }
|
||
|
return Atomic(value)
|
||
|
}
|
||
|
|
||
|
return Atomic(value)
|
||
|
}
|
||
2 years ago
|
|
||
|
#if DEBUG
|
||
|
public func stepForwardInTime() {
|
||
|
let targetTime: Int = ((_fixedTime.wrappedValue ?? 0) + 1)
|
||
|
_fixedTime.mutate { $0 = targetTime }
|
||
|
|
||
|
if let currentDate: Date = _dateNow.wrappedValue {
|
||
|
_dateNow.mutate { $0 = Date(timeIntervalSince1970: currentDate.timeIntervalSince1970 + 1) }
|
||
|
}
|
||
|
|
||
|
// Run and clear any executions which should run at the target time
|
||
|
let targetKeys: [Int] = asyncExecutions.keys
|
||
|
.filter { $0 <= targetTime }
|
||
|
targetKeys.forEach { key in
|
||
|
asyncExecutions[key]?.forEach { $0() }
|
||
|
asyncExecutions[key] = nil
|
||
|
}
|
||
|
}
|
||
|
#endif
|
||
3 years ago
|
}
|