Skip to content

Commit

Permalink
fix EnvironmentLazyObject.projectedValue as lazy property
Browse files Browse the repository at this point in the history
  • Loading branch information
NikSativa committed Jul 18, 2024
1 parent 84ee295 commit 44cea87
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions Source/PropertyWrapper/EnvironmentLazyObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ public struct EnvironmentLazyObject<Value: ObservableObject>: DynamicProperty {
@ObservedObject private var holder: Holder<Value> = .init()

public var wrappedValue: Value {
if let instance = holder.instance {
return instance
}

let instance: Value = container.resolve(named: name, with: arguments)
holder.instance = instance
return instance
return resolveInstance()
}

private let name: String?
Expand All @@ -27,9 +21,20 @@ public struct EnvironmentLazyObject<Value: ObservableObject>: DynamicProperty {
}

public var projectedValue: Binding<Value> {
assert(holder.instance != nil, "`projectedValue` is not available while the `wrappedValue` is not initialized. Should never happen.")
resolveInstance()
return $holder.instance
}

@discardableResult
private func resolveInstance() -> Value {
if let instance = holder.instance {
return instance
}

let instance: Value = container.resolve(named: name, with: arguments)
holder.instance = instance
return instance
}
}

private final class Holder<Value: ObservableObject>: ObservableObject {
Expand Down

0 comments on commit 44cea87

Please sign in to comment.