File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed
Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ public final class Lazy<Service>: InstanceWrapper {
1616 private let factory : ( ) -> Any ?
1717 private let graphIdentifier : GraphIdentifier ?
1818 private weak var container : Container ?
19+ private( set) var initialized : Bool = false
1920
2021 init ? ( inContainer container: Container , withInstanceFactory factory: ( ( ) -> Any ? ) ? ) {
2122 guard let factory = factory else { return nil }
@@ -24,7 +25,11 @@ public final class Lazy<Service>: InstanceWrapper {
2425 self . container = container
2526 }
2627
27- private var _instance : Service ?
28+ private var _instance : Service ? {
29+ didSet {
30+ initialized = _instance != nil
31+ }
32+ }
2833
2934 /// Getter for the wrapped object.
3035 /// It will be resolved from the `Container` when first accessed, all other calls will return the same instance.
Original file line number Diff line number Diff line change @@ -104,5 +104,18 @@ class LazySpec: QuickSpec {
104104 expect ( employee? . instance. employer) . notTo ( beNil ( ) )
105105 }
106106 }
107+ describe ( " Initialized flag " ) {
108+ it ( " provides instance from container " ) {
109+ container. register ( Animal . self) { _ in Dog ( ) }
110+ let lazy = container. resolve ( Lazy< Animal> . self )
111+ expect ( lazy? . instance is Dog ) . to ( beTrue ( ) )
112+ expect ( lazy? . initialized) . to ( beTrue ( ) )
113+ }
114+ it ( " provides instance from container but not initialized " ) {
115+ container. register ( Animal . self) { _ in Dog ( ) }
116+ let lazy = container. resolve ( Lazy< Animal> . self )
117+ expect ( lazy? . initialized) . to ( beFalse ( ) )
118+ }
119+ }
107120 }
108121}
You can’t perform that action at this time.
0 commit comments