Skip to content

Commit

Permalink
Merge pull request #5 from willowtreeapps/feature/prep-for-release
Browse files Browse the repository at this point in the history
Preparing for 1.0.1 release
  • Loading branch information
rafcabezas committed Apr 1, 2024
2 parents f64bc47 + f69304b commit fc4e049
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Grove
Simple Swift Dependency Injection Container.
Somewhat like grove is a group of trees, Grove's container represents a group of dependencies, and it provides you with tools to easily manage them.
Somewhat like a grove is a group of trees, Grove's container represents a group of dependencies, and it provides you with tools to easily manage them.

## Description
Grove is a super simple and lightweight Dependency Injection library available for Swift. It's designed to help you manage a group of dependencies in a container, and easily resolve them when needed, thus helping make your project more modular, testable, and maintainable.
Expand All @@ -21,8 +21,6 @@ dependencies: [
]
```

(Note: Until it's public, use this repository address: `git@github.com:willowtreeapps/grove.git`, version "1.0.0-beta.3")

## Usage
Grove simplifies dependency registration and resolving. Here's an example:

Expand All @@ -31,6 +29,9 @@ Grove simplifies dependency registration and resolving. Here's an example:
```swift
let container = Grove.defaultContainer // You can use the default container or create your own

// Register specifying a type to use for resolution
container.register(as: NASARepositoryProtocol.self, NASARepository())

// Register a reference type dependency as a singleton
container.register(JSONEncoder())

Expand Down
20 changes: 10 additions & 10 deletions Sources/Grove/PropertyWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ import Foundation
/// ```
///
@propertyWrapper
public struct Resolve<Dependency> {
private var container: Grove
public class Resolve<Dependency>: @unchecked Sendable {
private let container: Grove
private let transientInstanceLock = NSLock()
private var transientInstance: Dependency?

public init(_ type: Dependency.Type, container: Grove = .defaultContainer) {
self.container = container

switch container.scope(for: type) {
case .singleton:
break
case .transient:
transientInstance = (container.resolve() as Dependency)
}
}

public var wrappedValue: Dependency {
switch container.scope(for: Dependency.self) {
case .singleton:
return container.resolve()
case .transient:
transientInstanceLock.lock()
defer {
transientInstanceLock.unlock()
}
guard let transientInstance else {
preconditionFailure("Grove: Error resolving transient dependency: '\(String(describing: Dependency.self))'")
let transientInstance = (container.resolve() as Dependency)
self.transientInstance = transientInstance
return transientInstance
}
return transientInstance
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/GroveTests/GrovePropertyWrapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ final class GrovePropertyWrapperTests: XCTestCase {
XCTAssertEqual(testClass2.value, 100)
}
}

final class GrovePropertyWrapperAsClassTests: XCTestCase {
@Resolve(TestProtocol.self) var testClass

func testUpdatedRegistration() {
// Given
Grove.defaultContainer.register(as: TestProtocol.self, scope: .singleton, TestClass(value: 10))

// When
Grove.defaultContainer.register(as: TestProtocol.self, scope: .singleton, TestClass(value: 20))

// Then
XCTAssertEqual(testClass.value, 20)
}
}

0 comments on commit fc4e049

Please sign in to comment.