Skip to content

Commit

Permalink
Share state appstorage nullable (#3174)
Browse files Browse the repository at this point in the history
* failing AppStorageKey stored value and set to nil

* setting SharedAppStorageLocals when removing

* Clean up

---------

Co-authored-by: Brandon Williams <mbrandonw@hey.com>
  • Loading branch information
fdzsergio and mbrandonw authored Jun 17, 2024
1 parent fd9ef56 commit 964c9aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extension Shared {
line: UInt = #line
) where Key.Value == Value {
self.init(
wrappedValue: persistenceKey.load(initialValue: nil) ?? persistenceKey.defaultValue(),
wrappedValue: persistenceKey.defaultValue(),
persistenceKey.base,
fileID: fileID,
line: line
Expand Down Expand Up @@ -297,7 +297,7 @@ extension SharedReader {
line: UInt = #line
) where Key.Value == Value {
self.init(
wrappedValue: persistenceKey.load(initialValue: nil) ?? persistenceKey.defaultValue(),
wrappedValue: persistenceKey.defaultValue(),
persistenceKey.base,
fileID: fileID,
line: line
Expand Down
13 changes: 13 additions & 0 deletions Tests/ComposableArchitectureTests/SharedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,19 @@ final class SharedTests: XCTestCase {
XCTAssertEqual(count, count)
XCTAssertEqual(count.wrappedValue, count.wrappedValue)
}

func testDefaultVersusValueInExternalStorage() {
@Dependency(\.defaultAppStorage) var userDefaults
userDefaults.set(true, forKey: "optionalValueWithDefault")

@Shared(.optionalValueWithDefault) var optionalValueWithDefault

XCTAssertNotNil(optionalValueWithDefault)

$optionalValueWithDefault.withLock { $0 = nil }

XCTAssertNil(optionalValueWithDefault)
}
}

@Reducer
Expand Down

0 comments on commit 964c9aa

Please sign in to comment.