diff --git a/Sources/ComposableArchitecture/SharedState/PersistenceKey/PersistenceKeyDefault.swift b/Sources/ComposableArchitecture/SharedState/PersistenceKey/PersistenceKeyDefault.swift index 214d71c36bc9..ff2917f42b34 100644 --- a/Sources/ComposableArchitecture/SharedState/PersistenceKey/PersistenceKeyDefault.swift +++ b/Sources/ComposableArchitecture/SharedState/PersistenceKey/PersistenceKeyDefault.swift @@ -24,9 +24,9 @@ /// ``` public struct PersistenceKeyDefault: PersistenceReaderKey { let base: Base - let defaultValue: Base.Value + let defaultValue: () -> Base.Value - public init(_ key: Base, _ value: Base.Value) { + public init(_ key: Base, _ value: @autoclosure @escaping () -> Base.Value) { self.base = key self.defaultValue = value } @@ -36,7 +36,7 @@ public struct PersistenceKeyDefault: PersistenceRead } public func load(initialValue: Base.Value?) -> Base.Value? { - self.base.load(initialValue: initialValue ?? self.defaultValue) + self.base.load(initialValue: initialValue ?? self.defaultValue()) } public func subscribe( diff --git a/Sources/ComposableArchitecture/SharedState/References/ValueReference.swift b/Sources/ComposableArchitecture/SharedState/References/ValueReference.swift index 6671ee8bd56a..612f750b7300 100644 --- a/Sources/ComposableArchitecture/SharedState/References/ValueReference.swift +++ b/Sources/ComposableArchitecture/SharedState/References/ValueReference.swift @@ -73,7 +73,7 @@ extension Shared { line: UInt = #line ) where Key.Value == Value { self.init( - wrappedValue: persistenceKey.load(initialValue: nil) ?? persistenceKey.defaultValue, + wrappedValue: persistenceKey.load(initialValue: nil) ?? persistenceKey.defaultValue(), persistenceKey.base, fileID: fileID, line: line @@ -159,7 +159,7 @@ extension SharedReader { line: UInt = #line ) where Key.Value == Value { self.init( - wrappedValue: persistenceKey.load(initialValue: nil) ?? persistenceKey.defaultValue, + wrappedValue: persistenceKey.load(initialValue: nil) ?? persistenceKey.defaultValue(), persistenceKey.base, fileID: fileID, line: line diff --git a/Tests/ComposableArchitectureTests/SharedTests.swift b/Tests/ComposableArchitectureTests/SharedTests.swift index d309cd391dec..39d9bac8f46e 100644 --- a/Tests/ComposableArchitectureTests/SharedTests.swift +++ b/Tests/ComposableArchitectureTests/SharedTests.swift @@ -722,6 +722,28 @@ final class SharedTests: XCTestCase { XCTAssertEqual(isOn2, true) XCTAssertEqual(isOn3, true) } + + func testSharedDefaults_Used() { + let didAccess = LockIsolated(false) + let logDefault: () -> Bool = { + didAccess.setValue(true) + return true + } + @Shared(.isActive(default: logDefault)) var isActive + XCTAssertEqual(isActive, true) + XCTAssertEqual(didAccess.value, true) + } + + func testSharedDefaults_Unused() { + let didAccess = LockIsolated(false) + let logDefault: () -> Bool = { + didAccess.setValue(true) + return true + } + @Shared(.isActive(default: logDefault)) var isActive = false + XCTAssertEqual(isActive, false) + XCTAssertEqual(didAccess.value, false) + } func testSharedReaderDefaults_MultipleWithDifferentDefaults() async throws { @Shared(.appStorage("isOn")) var isOn = false @@ -1035,6 +1057,10 @@ extension PersistenceReaderKey where Self == PersistenceKeyDefault Bool) -> Self { + PersistenceKeyDefault(.appStorage("isActive"), keyDefault()) + } } // NB: This is a compile-time test to verify that optional shared state with defaults compiles.