File tree Expand file tree Collapse file tree 2 files changed +14
-0
lines changed
CodeEdit/Utils/DependencyInjection Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Original file line number Diff line number Diff line change 77
88import Foundation
99
10+ /// A service container that manages the registration and resolution of services.
1011enum ServiceContainer {
12+ /// A dictionary storing the closures for creating service instances.
1113 private static var factories : [ ObjectIdentifier : ( ) -> Any ] = [ : ]
14+ /// A dictionary storing the cached service instances.
1215 private static var cache : [ ObjectIdentifier : Any ] = [ : ]
16+ /// A dispatch queue used for synchronizing access to the factories and cache.
1317 private static let queue = DispatchQueue ( label: " ServiceContainerQueue " )
1418
19+ /// Registers a factory closure for creating instances of a service type.
20+ ///
21+ /// - Parameter factory: An autoclosure that returns an instance of the service type.
1522 static func register< Service> ( _ factory: @autoclosure @escaping ( ) -> Service ) {
1623 queue. sync {
1724 let key = ObjectIdentifier ( Service . Type. self)
1825 factories [ key] = factory
1926 }
2027 }
2128
29+ /// Resolves an instance of a service type based on the specified resolution type.
30+ ///
31+ /// - Parameters:
32+ /// - resolveType: The type of resolution to use for the service. Defaults to `.singleton`.
33+ /// - type: The type of the service to resolve.
34+ /// - Returns: An instance of the resolved service type, or `nil` if the service is not registered.
2235 static func resolve< Service> ( _ resolveType: ServiceType = . singleton, _ type: Service . Type ) -> Service ? {
2336 let serviceId = ObjectIdentifier ( Service . Type. self)
2437
Original file line number Diff line number Diff line change 55// Created by Abe Malla on 4/3/24.
66//
77
8+ /// A property wrapper that provides access to a service instance.
89@propertyWrapper
910struct Service < Service> {
1011 var service : Service
You can’t perform that action at this time.
0 commit comments