Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions Sources/Swinject/InstanceWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,6 @@ protocol InstanceWrapper {
init?(inContainer container: SwinjectContainer, withInstanceFactory factory: ((GraphIdentifier?) -> Any?)?)
}

/// Wrapper to enable delayed dependency instantiation.
/// `Lazy<Type>` does not need to be explicitly registered into the ``Container`` - resolution will work
/// as long as there is a registration for the `Type`.
public final class Lazy<Service>: InstanceWrapper {
static var wrappedType: Any.Type { return Service.self }

private let factory: (GraphIdentifier?) -> Any?
private let graphIdentifier: GraphIdentifier?
private weak var container: SwinjectContainer?

init?(inContainer container: SwinjectContainer, withInstanceFactory factory: ((GraphIdentifier?) -> Any?)?) {
guard let factory = factory else { return nil }
self.factory = factory
graphIdentifier = container.currentObjectGraph
self.container = container
}

private var _instance: Service?

/// Getter for the wrapped object.
/// It will be resolved from the ``Container`` when first accessed, all other calls will return the same instance.
public var instance: Service {
if let instance = _instance {
return instance
} else {
_instance = makeInstance()
return _instance!
}
}

private func makeInstance() -> Service? {
factory(graphIdentifier) as? Service
}
}

/// Wrapper to enable delayed dependency instantiation.
/// `Provider<Type>` does not need to be explicitly registered into the ``Container`` - resolution will work
/// as long as there is a registration for the `Type`.
public final class Provider<Service>: InstanceWrapper {
static var wrappedType: Any.Type { return Service.self }

private let factory: (GraphIdentifier?) -> Any?

init?(inContainer _: SwinjectContainer, withInstanceFactory factory: ((GraphIdentifier?) -> Any?)?) {
guard let factory = factory else { return nil }
self.factory = factory
}

/// Getter for the wrapped object.
/// New instance will be resolved from the ``Container`` every time it is accessed.
public var instance: Service {
return factory(.none) as! Service
}
}

extension Optional: InstanceWrapper {
static var wrappedType: Any.Type { return Wrapped.self }

Expand Down
32 changes: 0 additions & 32 deletions Tests/SwinjectTests/Circularity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,3 @@ internal class DDependingOnBC: D {
internal class CDependingOnWeakB: C {
weak var b: B?
}

internal protocol LazyParentProtocol {
var child1: LazyChildProtocol { get }
var child2: LazyChildProtocol { get }
}
internal protocol LazyChildProtocol: AnyObject {
var lazy: Lazy<LazilyResolvedProtocol> { get }
}
internal protocol LazySingletonProtocol {
var lazy: Lazy<LazilyResolvedProtocol> { get }
}
internal protocol LazilyResolvedProtocol: AnyObject { }

internal class LazyParent: LazyParentProtocol {
let child1: LazyChildProtocol
let child2: LazyChildProtocol

init(child1: LazyChildProtocol, child2: LazyChildProtocol) {
self.child1 = child1
self.child2 = child2
}
}

internal class LazyChild: LazyChildProtocol, LazySingletonProtocol {
let lazy: Lazy<LazilyResolvedProtocol>

init(lazy: Lazy<LazilyResolvedProtocol>) {
self.lazy = lazy
}
}

internal class LazilyResolved: LazilyResolvedProtocol { }
2 changes: 0 additions & 2 deletions Tests/SwinjectTests/ContainerTests.Speed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ fileprivate extension SwinjectContainer {
for _ in 0..<500_000 {
_ = resolve(Animal.self) as? Cat
_ = resolve(Animal.self, argument: "Mimi") as? Cat
let lazy = resolve(Lazy<Animal>.self, arguments: "Mew", true)
_ = lazy?.instance as? Cat
}
}
}
80 changes: 0 additions & 80 deletions Tests/SwinjectTests/EmploymentAssembly.swift

This file was deleted.

111 changes: 0 additions & 111 deletions Tests/SwinjectTests/LazyTests.swift

This file was deleted.

Loading