From 3b3fe2edfd0877e4fa4a45d44897dc2b82905f8d Mon Sep 17 00:00:00 2001 From: Kerr Marin Miller Date: Fri, 25 Oct 2024 21:23:10 +0100 Subject: [PATCH 1/3] Make a single dependency resolver, and everytime you build, it adds the dependencies --- Sources/Biodag/Biodag.swift | 18 +++++++++++------- Tests/BiodagTests/BiodagTests.swift | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Sources/Biodag/Biodag.swift b/Sources/Biodag/Biodag.swift index f03a430..cb3038b 100644 --- a/Sources/Biodag/Biodag.swift +++ b/Sources/Biodag/Biodag.swift @@ -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. diff --git a/Tests/BiodagTests/BiodagTests.swift b/Tests/BiodagTests/BiodagTests.swift index e3d36c8..61c2165 100644 --- a/Tests/BiodagTests/BiodagTests.swift +++ b/Tests/BiodagTests/BiodagTests.swift @@ -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 From 3000584e23587020b76351ce716f171706402a57 Mon Sep 17 00:00:00 2001 From: Kerr Marin Miller Date: Fri, 25 Oct 2024 21:27:48 +0100 Subject: [PATCH 2/3] Update workflow --- .github/workflows/test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f30332e..d0971a4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 From 745c978401366c6b06a5e4d79c45b7dec2754d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kerr=20Mar=C3=ADn=20Miller?= Date: Sun, 27 Oct 2024 11:29:19 +0000 Subject: [PATCH 3/3] Update test.yml --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d0971a4..9d0a3da 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,4 +18,4 @@ jobs: - name: Checkout uses: actions/checkout@v3 - name: Test - run: swift test --parallel + run: swift test