11import Foundation
22
33public class Container {
4- static var root = Container ( )
4+ @MainActor
5+ static private( set) var root = Container ( )
56
67 /// Stored object instance factories.
78 var modules : [ String : Module ] = [ : ]
@@ -33,6 +34,7 @@ extension Container {
3334 /// Resolves through inference and returns an instance of the given type from the current default container.
3435 ///
3536 /// If the dependency is not found, an exception will occur.
37+ @MainActor
3638 static func resolve< T> ( for type: AnyObject . Type ) -> T {
3739 guard let component: T = weakResolve ( for: type) else {
3840 fatalError ( " Dependency ' \( T . self) ' not resolved! " )
@@ -44,6 +46,7 @@ extension Container {
4446 /// Resolves through inference and returns an instance of the given type from the current default container.
4547 ///
4648 /// If the dependency is not found, return nil
49+ @MainActor
4750 static func weakResolve< T> ( for type: AnyObject . Type ) -> T ? {
4851 root. module ( type) ? . resolve ( ) as? T
4952 }
@@ -78,10 +81,23 @@ public extension Container {
7881 self . init ( )
7982 register ( contentsOf: [ module ( ) ] )
8083 }
84+ }
8185
86+ public extension Container {
8287 /// Assigns the current container to the composition root.
88+ @MainActor
8389 func build( ) {
8490 // Used later in property wrapper
8591 Self . root = self
8692 }
8793}
94+
95+ #if DEBUG
96+ public extension Container {
97+ @MainActor
98+ static func clear( ) {
99+ root. modules. removeAll ( )
100+ }
101+ }
102+
103+ #endif
0 commit comments