@@ -21,13 +21,11 @@ private extension AdamantSecretWalletsManager {
2121final class AdamantSecretWalletsManager : SecretWalletsManagerProtocol {
2222 private let secretWalletsFactory : SecretWalletsFactory
2323
24- private var state : SecretWalletsManagerStateProtocol
24+ @ Atomic private var state : SecretWalletsManagerStateProtocol
2525 var statePublisher = ObservableSender < SecretWalletsManagerStateProtocol > ( )
2626
2727 var wallets : [ WalletStoreServiceProtocol ] { [ state. regularWallet] + state. secretWallets }
2828
29- private let lock = NSLock ( )
30-
3129 init (
3230 walletsStoreService: WalletStoreServiceProtocol ,
3331 secretWalletsFactory: SecretWalletsFactory
@@ -42,16 +40,16 @@ final class AdamantSecretWalletsManager: SecretWalletsManagerProtocol {
4240 // MARK: - Manage state
4341 func createSecretWallet( withPassword password: String ) {
4442 let wallet = secretWalletsFactory. makeSecretWallet ( withPassword: password)
45- lock . lock ( )
46- defer { lock . unlock ( ) }
47- state . secretWallets . append ( wallet )
43+ _state . mutate {
44+ $0 . secretWallets . append ( wallet )
45+ }
4846 }
4947
5048 func removeSecretWallet( at index: Int ) -> WalletStoreServiceProtocol ? {
51- lock. lock ( )
52- defer { lock. unlock ( ) }
5349 guard state. secretWallets. indices. contains ( index) else { return nil }
54- return state. secretWallets. remove ( at: index)
50+ return _state. mutate {
51+ return $0. secretWallets. remove ( at: index)
52+ }
5553 }
5654
5755 func getCurrentWallet( ) -> WalletStoreServiceProtocol {
@@ -63,17 +61,17 @@ final class AdamantSecretWalletsManager: SecretWalletsManagerProtocol {
6361 }
6462
6563 func activateSecretWallet( at index: Int ) {
66- lock. lock ( )
67- defer { lock. unlock ( ) }
6864 guard index < state. secretWallets. count else { return }
69- state. currentWallet = state. secretWallets [ index]
65+ _state. mutate {
66+ $0. currentWallet = state. secretWallets [ index]
67+ }
7068 statePublisher. send ( state)
7169 }
7270
7371 func activateDefaultWallet( ) {
74- lock . lock ( )
75- defer { lock . unlock ( ) }
76- state . currentWallet = state . regularWallet
72+ _state . mutate {
73+ $0 . currentWallet = state . regularWallet
74+ }
7775 statePublisher. send ( state)
7876 }
7977}
0 commit comments