Skip to content

Commit

Permalink
Conditional code support to fix isolated(any) issue - Issue #250
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlongco committed Nov 26, 2024
1 parent 51660d7 commit e26eb29
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Factory Changelog

## 2.4.3
* Conditional code support to fix isolated(any) issue - Issue #250

## 2.4.2
* Adds isolated(any) in order to properly resolve actor-isolated Factory's'

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.

## Factory 2.4.2
## Factory 2.4.3

Factory is strongly influenced by SwiftUI, and in my opinion is highly suited for use in that environment. Factory is...

Expand Down
16 changes: 16 additions & 0 deletions Sources/Factory/Factory/Containers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ public protocol ManagedContainer: AnyObject, Sendable {
/// Defines the default factory helpers for containers
extension ManagedContainer {
/// Syntactic sugar allows container to create a properly bound Factory.
#if swift(<6.0)
@inlinable @inline(__always) public func callAsFunction<T>(
key: StaticString = #function,
_ factory: @escaping @Sendable () -> T
) -> Factory<T> {
Factory(self, key: key, factory)
}
/// Syntactic sugar allows container to create a properly bound ParameterFactory.
@inlinable @inline(__always) public func callAsFunction<P,T>(
key: StaticString = #function,
_ factory: @escaping @Sendable (P) -> T
) -> ParameterFactory<P,T> {
ParameterFactory(self, key: key, factory)
}
#else
@inlinable @inline(__always) public func callAsFunction<T>(
key: StaticString = #function,
_ factory: @escaping @Sendable @isolated(any) () -> T
Expand All @@ -111,6 +126,7 @@ extension ManagedContainer {
) -> ParameterFactory<P,T> {
ParameterFactory(self, key: key, factory)
}
#endif
/// Syntactic sugar allows container to create a factory whose optional registration is promised before resolution.
/// ```swift
/// extension Container {
Expand Down
32 changes: 31 additions & 1 deletion Sources/Factory/Factory/Factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,17 @@ public struct Factory<T>: FactoryModifying {
/// current container as well defining the scope.
/// - key: Hidden value used to differentiate different instances of the same type in the same container.
/// - factory: A factory closure that produces an object of the desired type when required.
#if swift(<6.0)
public init(_ container: ManagedContainer, key: StaticString = #function,
_ factory: @escaping @Sendable @isolated(any) () -> T) {
_ factory: @escaping @Sendable () -> T) {
self.registration = FactoryRegistration<Void,T>(key: key, container: container, factory: factory)
}
#else
public init(_ container: ManagedContainer, key: StaticString = #function,
_ factory: @escaping @Sendable @isolated(any) () -> T) {
self.registration = FactoryRegistration<Void,T>(key: key, container: container, factory: factory)
}
#endif

/// Evaluates the factory and returns an object or service of the desired type. The resolved instance may be brand new or Factory may
/// return a cached value from the specified scope.
Expand Down Expand Up @@ -133,11 +140,19 @@ public struct Factory<T>: FactoryModifying {
/// - Parameters:
/// - factory: A new factory closure that produces an object of the desired type when needed.
/// Allows updating registered factory and scope.
#if swift(<6.0)
@discardableResult
public func register(factory: @escaping @Sendable () -> T) -> Self {
registration.register(factory)
return self
}
#else
@discardableResult
public func register(factory: @escaping @Sendable @isolated(any) () -> T) -> Self {
registration.register(factory)
return self
}
#endif

/// Internal parameters for this Factory including id, container, the factory closure itself, the scope,
/// and others.
Expand Down Expand Up @@ -201,10 +216,17 @@ public struct ParameterFactory<P,T>: FactoryModifying {
/// current container as well defining the scope.
/// - key: Hidden value used to differentiate different instances of the same type in the same container.
/// - factory: A factory closure that produces an object of the desired type when required.
#if swift(<6.0)
public init(_ container: ManagedContainer, key: StaticString = #function,
_ factory: @escaping @Sendable (P) -> T) {
self.registration = FactoryRegistration<P,T>(key: key, container: container, factory: factory)
}
#else
public init(_ container: ManagedContainer, key: StaticString = #function,
_ factory: @escaping @Sendable @isolated(any) (P) -> T) {
self.registration = FactoryRegistration<P,T>(key: key, container: container, factory: factory)
}
#endif

/// Resolves a factory capable of taking parameters at runtime.
/// ```swift
Expand All @@ -227,11 +249,19 @@ public struct ParameterFactory<P,T>: FactoryModifying {
/// ```
/// - Parameters:
/// - factory: A new factory closure that produces an object of the desired type when needed.
#if swift(<6.0)
@discardableResult
public func register(factory: @escaping @Sendable (P) -> T) -> Self {
registration.register(factory)
return self
}
#else
@discardableResult
public func register(factory: @escaping @Sendable @isolated(any) (P) -> T) -> Self {
registration.register(factory)
return self
}
#endif

/// Required registration
public var registration: FactoryRegistration<P,T>
Expand Down

0 comments on commit e26eb29

Please sign in to comment.