Skip to content

Commit

Permalink
Make PersistenceKeyDefault load its default value lazily (#3057)
Browse files Browse the repository at this point in the history
* Added autoclosure around default value for `PersistenceKeyDefault`

* Added autoclosure around default value for `PersistenceKeyDefault`

---------

Co-authored-by: Sean <[email protected]>
  • Loading branch information
seanmrich and Sean authored May 8, 2024
1 parent c0d10e2 commit 4c36b18
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
/// ```
public struct PersistenceKeyDefault<Base: PersistenceReaderKey>: 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
}
Expand All @@ -36,7 +36,7 @@ public struct PersistenceKeyDefault<Base: PersistenceReaderKey>: 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions Tests/ComposableArchitectureTests/SharedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1035,6 +1057,10 @@ extension PersistenceReaderKey where Self == PersistenceKeyDefault<AppStorageKey
static var isOn: Self {
PersistenceKeyDefault(.appStorage("isOn"), false)
}

static func isActive(default keyDefault: @escaping () -> Bool) -> Self {
PersistenceKeyDefault(.appStorage("isActive"), keyDefault())
}
}

// NB: This is a compile-time test to verify that optional shared state with defaults compiles.
Expand Down

0 comments on commit 4c36b18

Please sign in to comment.