-
Notifications
You must be signed in to change notification settings - Fork 199
Concurrency-Safe Notifications #822
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
Concurrency-Safe Notifications #822
Conversation
…to better explain Message/Notification conversion
6905774
to
d45df0c
Compare
fb8592c
to
3205afd
Compare
…kup, and provide a default value for Message.name.
static func makeNotification(_ message: Self) -> Notification | ||
} | ||
|
||
public protocol MainActorMessage: Message {} |
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.
I am wondering if this protocol should have a @MainActor
annotation. Those types will only be sent and received from the MainActor anyways.
```swift | ||
@available(FoundationPreview 0.5, *) | ||
extension NotificationCenter { | ||
public func addObserver<I: MessageIdentifier, M: AsyncMessage>(of subject: M.Subject, |
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.
How can those methods take an async
closure while not being async themselves? Where is the task spawned that calls the observer closures?
If the addObserver
method takes an async closure I would have expected that method to become async
itself. Something more like this
await center.observe(of: subject, for: identifier) { message
// do something async with the message
await process(message)
}
This way you also don't need ObservationToken
s but can just rely on task cancellation which IMO fits better into the Concurrency paradigms.
@swift-ci please test |
This PR introduces a proposal for adopting Swift Concurrency in
NotificationCenter
.