From 44cea875e34bfe286b1029ae40321189ec041669 Mon Sep 17 00:00:00 2001 From: Mykyta Konopelko Date: Thu, 18 Jul 2024 23:29:38 +0200 Subject: [PATCH] fix EnvironmentLazyObject.projectedValue as lazy property --- .../EnvironmentLazyObject.swift | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Source/PropertyWrapper/EnvironmentLazyObject.swift b/Source/PropertyWrapper/EnvironmentLazyObject.swift index 11163a6..499411c 100644 --- a/Source/PropertyWrapper/EnvironmentLazyObject.swift +++ b/Source/PropertyWrapper/EnvironmentLazyObject.swift @@ -8,13 +8,7 @@ public struct EnvironmentLazyObject: DynamicProperty { @ObservedObject private var holder: Holder = .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? @@ -27,9 +21,20 @@ public struct EnvironmentLazyObject: DynamicProperty { } public var projectedValue: Binding { - 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: ObservableObject {