Skip to content

Instantly share code, notes, and snippets.

@mattmassicotte
Created June 23, 2024 00:49
Show Gist options
  • Save mattmassicotte/5adf94b59594d8557828bbba4800aab9 to your computer and use it in GitHub Desktop.
Save mattmassicotte/5adf94b59594d8557828bbba4800aab9 to your computer and use it in GitHub Desktop.
import Foundation
extension NotificationCenter {
public func addOIsolatedObserver<Payload>(
forName name: Notification.Name,
object obj: Any? = nil,
isolation: (any Actor)? = #isolation,
process: @Sendable @escaping (Notification) -> Payload,
using block: @Sendable @escaping (Payload) -> Void
) -> any NSObjectProtocol where Payload : Sendable {
return self.addObserver(forName: name, object: obj, queue: .main) { note in
let payload = process(note)
isolation?.assumeIsolated { _ in
block(payload)
}
}
}
}
@mattmassicotte
Copy link
Author

Optional is clearly a bug…

@mattmassicotte
Copy link
Author

I wrote this when I was tried and it is very wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment