Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow MainActor Dependency Registration #225

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/Factory/Factory/Containers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ extension ManagedContainer {
@inlinable @inline(__always) public func callAsFunction<P,T>(key: StaticString = #function, _ factory: @escaping @Sendable (P) -> T) -> ParameterFactory<P,T> {
ParameterFactory(self, key: key, factory)
}

/// Syntactic sugar allows container to create a properly bound Factory on MainActor.
@MainActor
@inlinable @inline(__always) public func callAsFunction<T: Sendable>(key: StaticString = #function, _ factory: @escaping @MainActor () -> T) -> Factory<T> {
return Factory(self, key: key, factory)
}

/// Syntactic sugar allows container to create a properly bound ParameterFactory on MainActor.
@MainActor
@inlinable @inline(__always) public func callAsFunction<P,T: Sendable>(key: StaticString = #function, _ factory: @escaping @MainActor (P) -> T) -> ParameterFactory<P,T> {
return ParameterFactory(self, key: key, factory)
}

/// Syntactic sugar allows container to create a factory whose optional registration is promised before resolution.
/// ```swift
/// extension Container {
Expand Down
6 changes: 2 additions & 4 deletions Sources/Factory/Factory/Factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ 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.
public init(_ container: ManagedContainer, key: StaticString = #function, _ factory: @escaping @Sendable () -> T) {
public init(_ container: ManagedContainer, key: StaticString = #function, _ factory: @escaping () -> T) {
self.registration = FactoryRegistration<Void,T>(key: key, container: container, factory: factory)
}

Expand Down Expand Up @@ -144,8 +144,6 @@ public struct Factory<T>: FactoryModifying {

}

extension Factory: Sendable where T: Sendable {}

// MARK: - ParameterFactory

/// Factory capable of taking parameters at runtime
Expand Down Expand Up @@ -200,7 +198,7 @@ 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.
public init(_ container: ManagedContainer, key: StaticString = #function, _ factory: @escaping @Sendable (P) -> T) {
public init(_ container: ManagedContainer, key: StaticString = #function, _ factory: @escaping (P) -> T) {
self.registration = FactoryRegistration<P,T>(key: key, container: container, factory: factory)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Factory/Factory/Globals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Foundation
nonisolated(unsafe) internal var globalGraphResolutionDepth = 0

/// Internal key used for Resolver mode
nonisolated(unsafe) internal let globalResolverKey: StaticString = "*"
internal let globalResolverKey: StaticString = "*"

#if DEBUG
/// Internal variables used for debugging
Expand Down
6 changes: 3 additions & 3 deletions Sources/Factory/Factory/Registrations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
import Foundation

/// Shared registration type for Factory and ParameterFactory. Used internally to manage the registration and resolution process.
public struct FactoryRegistration<P,T>: Sendable {
public struct FactoryRegistration<P,T> {

/// Key used to manage registrations and cached values.
internal let key: FactoryKey
/// A strong reference to the container supporting this Factory.
internal let container: ManagedContainer
/// Typed factory with scope and factory.
internal let factory: @Sendable (P) -> T
internal let factory: (P) -> T

#if DEBUG
/// Internal debug
Expand All @@ -45,7 +45,7 @@ public struct FactoryRegistration<P,T>: Sendable {
internal var once: Bool = false

/// Initializer for registration sets passed values and default scope from container manager.
internal init(key: StaticString, container: ManagedContainer, factory: @escaping @Sendable (P) -> T) {
internal init(key: StaticString, container: ManagedContainer, factory: @escaping (P) -> T) {
self.key = FactoryKey(type: T.self, key: key)
self.container = container
self.factory = factory
Expand Down
3 changes: 2 additions & 1 deletion Tests/FactoryTests/FactoryIsolationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ final class FactoryIsolationTests: XCTestCase {
func testInjectSendableDependency() {
let _: SomeSendableType = Container.shared.sendable()
}


@MainActor
func testInjectMainActorDependency() async {
let _: SomeMainActorType = await Container.shared.mainActor().wrapped()
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/FactoryTests/MockServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
@testable import Factory

// Swift 6
extension Container: @retroactive AutoRegistering {
extension Container: AutoRegistering {
public func autoRegister() {
// print("Container.autoRegister")
}
Expand Down