-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#if canImport(SwiftUI) | ||
import Combine | ||
import SwiftUI | ||
|
||
@propertyWrapper | ||
public struct EnvironmentLazyObject<Value: ObservableObject>: DynamicProperty { | ||
@EnvironmentObject private var container: ObservableResolver | ||
@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 | ||
} | ||
|
||
private let name: String? | ||
private let arguments: Arguments | ||
|
||
public init(named name: String? = nil, | ||
with arguments: Arguments = .init()) { | ||
self.name = name | ||
self.arguments = arguments | ||
} | ||
|
||
public var projectedValue: Binding<Value> { | ||
assert(holder.instance != nil, "`projectedValue` is not available while the `wrappedValue` is not initialized. Should never happen.") | ||
return $holder.instance | ||
} | ||
} | ||
|
||
private final class Holder<Value: ObservableObject>: ObservableObject { | ||
private var observers: Set<AnyCancellable> = [] | ||
|
||
var instance: Value! { | ||
didSet { | ||
if oldValue !== instance { | ||
assert(observers.isEmpty, "Subscribed to `objectWillChange` multiple times. Should never happen.") | ||
observers = [] | ||
instance.objectWillChange.sink { [unowned self] _ in | ||
objectWillChange.send() | ||
}.store(in: &observers) | ||
} | ||
} | ||
} | ||
} | ||
#endif |