Skip to content

Commit

Permalink
Small improvements and updates:
Browse files Browse the repository at this point in the history
- Add final to classes which should not be overridden.
- Better error loggin when dependency is missing
- Tweaks to comments
  • Loading branch information
ncipollo committed Dec 4, 2023
1 parent d9bbab6 commit fb5de75
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Sources/WhoopDIKit/DependencyDefinition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fileprivate extension DependencyDefinition {
}

/// Provides the definition of an object factory. A fresh version of this dependency will be provide each time one is requested.
class FactoryDefinition: DependencyDefinition {
final class FactoryDefinition: DependencyDefinition {
private let factory: (Any?) throws -> Any
let serviceKey: ServiceKey

Expand All @@ -30,7 +30,7 @@ class FactoryDefinition: DependencyDefinition {
}

/// Provides the definition of a singleton object (i.e an object we will only create once per graph).
class SingletonDefinition: DependencyDefinition {
final class SingletonDefinition: DependencyDefinition {
private let factory: (Any?) throws -> Any
private let lock = NSLock()
let serviceKey: ServiceKey
Expand Down
2 changes: 1 addition & 1 deletion Sources/WhoopDIKit/ModuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ public protocol ModuleList {
var modules: [DependencyModule] { get }
}

public class EmptyModuleList: ModuleList {
public final class EmptyModuleList: ModuleList {
public var modules: [DependencyModule] { [] }
}
2 changes: 1 addition & 1 deletion Sources/WhoopDIKit/ServiceDictionary.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class ServiceDictionary<Value> {
public final class ServiceDictionary<Value> {
private var valuesByType: [AnyHashable: Value]

convenience public init() {
Expand Down
8 changes: 6 additions & 2 deletions Sources/WhoopDIKit/WhoopDI.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public class WhoopDI: DependencyRegister {
import Foundation
public final class WhoopDI: DependencyRegister {
private static let serviceDict = ServiceDictionary<DependencyDefinition>()
private static var localServiceDict: ServiceDictionary<DependencyDefinition>? = nil

Expand All @@ -19,6 +20,8 @@ public class WhoopDI: DependencyRegister {
do {
return try get(name, params)
} catch {
print("Inject failed with stack trace:")
Thread.callStackSymbols.forEach { print($0) }
fatalError("WhoopDI inject failed with error: \(error)")
}
}
Expand Down Expand Up @@ -52,7 +55,8 @@ public class WhoopDI: DependencyRegister {
fatalError("Nesting WhoopDI.inject with local definitions is not currently supported")
}
// We need to maintain a reference to the local service dictionary because transient dependencies may also
// need to references dependencies from it.
// need to reference dependencies from it.
// ----
// This is a little dangerous since we are mutating a static variable but it should be fine as long as you
// don't use `inject { }` within the scope of another `inject { }`.
let serviceDict = ServiceDictionary<DependencyDefinition>()
Expand Down
2 changes: 1 addition & 1 deletion Sources/WhoopDIKit/WhoopDIValidator.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Provides verification that the object graph is complete. This is intended to be called from within a test.
public class WhoopDIValidator {
public final class WhoopDIValidator {
private let paramsDict = ServiceDictionary<Any>()

public init() { }
Expand Down

0 comments on commit fb5de75

Please sign in to comment.