File tree 2 files changed +19
-2
lines changed
Sources/ComposableArchitecture/SharedState
Tests/ComposableArchitectureTests
2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ extension Shared {
73
73
/// This object is returned from ``PersistenceReaderKey/subscribe(initialValue:didSet:)``, which
74
74
/// will feed updates from an external system for its lifetime, or till ``cancel()`` is called.
75
75
public class Subscription {
76
- let onCancel : ( ) -> Void
76
+ var onCancel : ( ( ) -> Void ) ?
77
77
78
78
/// Initializes the subscription with the given cancel closure.
79
79
///
@@ -88,7 +88,8 @@ extension Shared {
88
88
89
89
/// Cancels the subscription.
90
90
public func cancel( ) {
91
- self . onCancel ( )
91
+ self . onCancel ? ( )
92
+ self . onCancel = nil
92
93
}
93
94
}
94
95
}
Original file line number Diff line number Diff line change @@ -1084,6 +1084,22 @@ final class SharedTests: XCTestCase {
1084
1084
}
1085
1085
}
1086
1086
}
1087
+
1088
+ func testPersistenceKeySubscription( ) async throws {
1089
+ let persistenceKey : AppStorageKey < Int > = . appStorage( " shared " )
1090
+ let changes = LockIsolated < [ Int ? ] > ( [ ] )
1091
+ var subscription : Optional = persistenceKey. subscribe ( initialValue: nil ) { value in
1092
+ changes. withValue { $0. append ( value) }
1093
+ }
1094
+ @Dependency ( \. defaultAppStorage) var userDefaults
1095
+ userDefaults. set ( 1 , forKey: " shared " )
1096
+ userDefaults. set ( 42 , forKey: " shared " )
1097
+ subscription? . cancel ( )
1098
+ userDefaults. set ( 123 , forKey: " shared " )
1099
+ subscription = nil
1100
+ XCTAssertEqual ( [ 1 , 42 ] , changes. value)
1101
+ XCTAssertEqual ( 123 , persistenceKey. load ( initialValue: nil ) )
1102
+ }
1087
1103
}
1088
1104
1089
1105
@globalActor actor GA : GlobalActor {
You can’t perform that action at this time.
0 commit comments