Skip to content

Commit

Permalink
Merge pull request #1 from kerrmarin/multi-dependency
Browse files Browse the repository at this point in the history
Make a single dependency resolver, and everytime you build, it adds t…
  • Loading branch information
kerrmarin authored Oct 28, 2024
2 parents 090e225 + 745c978 commit 33aca27
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
# Set timeout to prevent overcharging
timeout-minutes: 20
steps:
- uses: maxim-lobanov/setup-xcode@v1.2.3
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: 12.4
xcode-version: latest
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Test
run: swift test --parallel
run: swift test
18 changes: 11 additions & 7 deletions Sources/Biodag/Biodag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,40 @@ import Foundation

/// A dependency collection that provides resolutions for object instances.
final public class DependencyResolver {
private let moduleArray: [Module]
/// Stored object instance factories.
private var modules = [String: Module]()
private var instances = [String: Any]()

/// Construct dependency resolutions.
public init(@ModuleBuilder _ modules: () -> [Module]) {
modules().forEach { add(module: $0) }
self.moduleArray = modules()
}

/// Construct dependency resolution.
public init(@ModuleBuilder _ module: () -> Module) {
add(module: module())
self.moduleArray = [module()]
}

/// Assigns the current container to the composition root.
public func build() {
Self.root = self
for module in moduleArray {
Self.root.add(module: module)
}
}

fileprivate init() {}
deinit { modules.removeAll() }
fileprivate init() {
self.moduleArray = []
}
}

private extension DependencyResolver {
/// Composition root container of dependencies.
static var root = DependencyResolver()
static let root = DependencyResolver()

/// Registers a specific type and its instantiating factory.
func add(module: Module) {
modules[module.name] = module
self.modules[module.name] = module
}

/// Resolves through inference and returns an instance of the given type from the current default container.
Expand Down
18 changes: 18 additions & 0 deletions Tests/BiodagTests/BiodagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ extension DependencyTests {
// The singletons are the same instance
XCTAssertTrue(singletonModule === helperSingleton)
}

func testAddingDependencies() {
struct Added { }
struct NotAdded { }

let newResolver = DependencyResolver({
Module { Added() as Added }
})
newResolver.build()


@Inject var widgetModule: WidgetModuleType
@Inject var added: Added
@Inject var notAdded: NotAdded

XCTAssertNotNil(widgetModule)
XCTAssertNotNil(added)
}
}

// MARK: - Subtypes
Expand Down

0 comments on commit 33aca27

Please sign in to comment.