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

Add support for registering factories on the main thread #174

Merged
merged 1 commit into from
Jul 17, 2024

Conversation

skorulis-ap
Copy link
Collaborator

@skorulis-ap skorulis-ap commented Jul 12, 2024

This allows container.register(ServiceA.self) { @MainActor in ServiceA() } to be generated as

extension Resolver {
  @MainActor func serviceA() -> ServiceA
}

Any other assembly registrations will then need to be marked as @mainactor in order to consume the resolver function. The structure can be extended to support other concurrency patterns like async in the future.

// thread using the knit generated resolver. If that call is not made on the main
// thread then a crash will occur.
@discardableResult
public func register<Service>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only added a single register function for now. I'll setup an ERB to generate them all in a future PR

@skorulis-ap skorulis-ap force-pushed the skorulis/main-actor branch from f491f3a to 82a60c7 Compare July 12, 2024 05:59
@skorulis-ap skorulis-ap marked this pull request as ready for review July 12, 2024 06:04
@skorulis-ap skorulis-ap requested a review from bradfol July 12, 2024 06:16
.macOS(.v13),
.macOS(.v14),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be somewhat nice to support back one version of macOS, was there an API you needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep error: 'assumeIsolated(_:file:line:)' is only available in macOS 14.0 or newer. Technically we don't actually use that in the knit code, it's part of the additions that are used by the app but due to the way the package is setup it will fail the build.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move the register functions into Swinject then we can drop back down.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok yeah let's bump the min platform version 👍

public func register<Service>(
_ serviceType: Service.Type,
name: String? = nil,
factory: @escaping @MainActor (Resolver) -> Service
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When introducing this override will the Swift compiler always match closures without the @MainActor attribute to the Swinject method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With adding @MainActor the compiler will pick the default one and then fail. I can't find any way to have a @MainActor function called without adding @MainActor to the closure signature. Which is good because it is always there for the parser to identify.

@skorulis-ap skorulis-ap force-pushed the skorulis/main-actor branch 5 times, most recently from 3326444 to 16be759 Compare July 17, 2024 03:12
@skorulis-ap skorulis-ap force-pushed the skorulis/main-actor branch from 16be759 to 8d3b336 Compare July 17, 2024 23:12
@skorulis-ap skorulis-ap merged commit 54054ea into main Jul 17, 2024
1 check passed
@skorulis-ap skorulis-ap deleted the skorulis/main-actor branch July 17, 2024 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants