-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor ServiceLocator to session-scoped ownership #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
79d9fb4
Refactor ServiceLocator to session-scoped ownership
g-enius ce51c79
Update docs for session-scoped ServiceLocator
g-enius 2e12f33
Remove throwaway ServiceLocator from AppCoordinator init
g-enius a70476a
Remove serviceLocator from BaseCoordinator
g-enius f66f96e
Pass session instead of serviceLocator through coordinators and VMs
g-enius 15eea16
Fix stale docs and SwiftLint warning from review
g-enius 1afaeb4
AppCoordinator conforms to SessionProvider, no throwaway ServiceLocator
g-enius 1c40140
Update in-app ServiceLocator description to session-scoped pattern
g-enius a8bd4ed
Address PR review comments
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,10 @@ | |
| // | ||
| // Central registry for dependency injection. | ||
| // | ||
| // Instance-based DI: the app creates one ServiceLocator() at the top (SceneDelegate) | ||
| // and threads it through coordinators, sessions, and ViewModels. No global singleton. | ||
| // Session-scoped DI: each Session creates its own ServiceLocator and registers | ||
| // services into it. On session transition, the old ServiceLocator is released | ||
| // with the session — no stale services. ViewModels receive the session and | ||
| // conform to SessionProvider, which auto-provides serviceLocator for @Service. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
@@ -60,10 +62,29 @@ public class ServiceLocator { | |
| // MARK: - ServiceLocatorProvider | ||
|
|
||
| /// Any type that holds a ServiceLocator instance for instance-based DI resolution. | ||
| /// | ||
| /// `@MainActor` is required because `ServiceLocator` itself is `@MainActor` — | ||
| /// any property that returns a `ServiceLocator` must also be isolated to the main actor. | ||
| @MainActor | ||
| public protocol ServiceLocatorProvider { | ||
| var serviceLocator: ServiceLocator { get } | ||
| } | ||
|
|
||
| // MARK: - SessionProvider | ||
|
|
||
| /// Types that hold a Session reference. Provides `serviceLocator` automatically | ||
| /// from `session.serviceLocator`, so conformers only need to store `let session: Session`. | ||
| @MainActor | ||
| public protocol SessionProvider: ServiceLocatorProvider { | ||
| var session: Session { get } | ||
| } | ||
|
|
||
| // MARK: - ServiceLocatorProvider | ||
|
|
||
| extension SessionProvider { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should write protocol conformance here |
||
| public var serviceLocator: ServiceLocator { session.serviceLocator } | ||
| } | ||
|
|
||
| // MARK: - @Service Property Wrapper | ||
|
|
||
| /// Property wrapper that resolves services from the enclosing instance's ServiceLocator. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why need main actor now but not before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claude
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude finished @g-enius's task in 1m 39s —— View job
Why
@MainActorwas added toServiceLocatorProviderBefore this PR (main branch):
ServiceLocatorProviderhad no@MainActor. All concrete conformers (ViewModels) happened to be@MainActorclasses, but the protocol requirement itself was nonisolated — Swift accepted this because the concrete types' isolation satisfied the call sites.What changed: This PR introduced
SessionProvider, a@MainActorprotocol that inherits fromServiceLocatorProviderand provides a default implementation:The Swift 6 strict concurrency rule: A
@MainActor-isolated implementation cannot satisfy a nonisolated protocol requirement. SinceSessionProvideris@MainActor, itsserviceLocatorimplementation is main-actor-isolated. IfServiceLocatorProvider.serviceLocatorremained nonisolated, Swift 6 would error — the protocol witness table requires isolation to match.Adding
@MainActortoServiceLocatorProvideraligns the requirement's isolation with the implementation's isolation, resolving the violation cleanly.In short: It wasn't needed before because no
@MainActorprotocol inherited from it. OnceSessionProvider: ServiceLocatorProviderwas introduced, Swift 6 required the requirement and implementation to agree on actor isolation.· Branch