Skip to content

Commit

Permalink
allow to disable thread safety on container level
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed Nov 10, 2019
1 parent 0f2cda0 commit eab2d42
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

* You can now use a shorthand syntax for resolving a single property using a key path, i.e. `resolvingProperty(\.value)`.
* Swift 5.0 support ([#224](https://github.com/AliSoftware/Dip/pull/224)).
* Fixed resolving nested types with the same local names ([#221](https://github.com/AliSoftware/Dip/pull/221))
* `@Injected` and `@IntectedWeak` property wrappers ([#225](https://github.com/AliSoftware/Dip/pull/225))
* Fixed resolving nested types with the same local names ([#221](https://github.com/AliSoftware/Dip/pull/221)).
* `@Injected` and `@IntectedWeak` property wrappers ([#225](https://github.com/AliSoftware/Dip/pull/225)).
* Thread safety can be disabled on container level.

## 7.0.1

Expand Down
9 changes: 8 additions & 1 deletion Sources/Dip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class DependencyContainer {
}

var autoInjectProperties: Bool
var threadSafe: Bool
internal(set) public var context: Context!
var definitions = [DefinitionKey: _Definition]()
var resolvedInstances = ResolvedInstances()
Expand All @@ -63,6 +64,7 @@ public final class DependencyContainer {

- Parameters:
- autoInjectProperties: Whether container should perform properties auto-injection. Default is `true`.
- threadSafe: Whether container should be thread-safe. Default is `true`. You may want to disable it for better performance.
- configBlock: A configuration block in which you typically put all you `register` calls.

- note: The `configBlock` is simply called at the end of the `init` to let you configure everything.
Expand All @@ -82,8 +84,9 @@ public final class DependencyContainer {

- returns: A new DependencyContainer.
*/
public init(autoInjectProperties: Bool = true, configBlock: (DependencyContainer)->() = { _ in }) {
public init(autoInjectProperties: Bool = true, threadSafe: Bool = true, configBlock: (DependencyContainer)->() = { _ in }) {
self.autoInjectProperties = autoInjectProperties
self.threadSafe = threadSafe
configBlock(self)
}

Expand All @@ -104,6 +107,10 @@ public final class DependencyContainer {
}

func threadSafe<T>(_ closure: () throws -> T) rethrows -> T {
guard threadSafe else {
return try closure()
}

lock.lock()
defer { lock.unlock() }
return try closure()
Expand Down

0 comments on commit eab2d42

Please sign in to comment.