How would you create a dependency key for something that has an async initializer? #271
Answered
by
stephencelis
BrentMifsud
asked this question in
Q&A
-
For example, CLMonitor: |
Beta Was this translation helpful? Give feedback.
Answered by
stephencelis
Sep 7, 2024
Replies: 1 comment
-
Generally you should consider dependency values as static interfaces, so the key could be of a sendable closure instead: static var liveValue: @Sendable () async -> CLMonitor = {
await CLMonitor("my-location-monitor)
}
Or if you need a long-living monitor, you could wrap it in an actor: actor Monitor {
private var _rawValue: CLMonitor?
var rawValue: CLMonitor {
get async {
guard let _rawValue else {
let rawValue = await CLMonitor("my-location-monitor")
_rawValue = rawValue
return rawValue
}
return _rawValue
}
}
}
// ...
static var liveValue: Monitor { Monitor() } More work would need to be done to make things testable, though. Check out Designing dependencies for more info. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
BrentMifsud
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generally you should consider dependency values as static interfaces, so the key could be of a sendable closure instead:
Or if you need a long-living monitor, you could wrap it in an actor:
More work would need to be done to make things testable, though. C…