notify: add typesafe notify class
This commit is contained in:
44
damus/Notify/Notify.swift
Normal file
44
damus/Notify/Notify.swift
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Notify.swift
|
||||
// damus
|
||||
//
|
||||
// Created by William Casarin on 2023-07-30.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
protocol Notify {
|
||||
associatedtype Payload
|
||||
static var name: Notification.Name { get }
|
||||
var payload: Payload { get }
|
||||
}
|
||||
|
||||
extension Notify {
|
||||
static var name: Notification.Name {
|
||||
Notification.Name("\(Self.self)")
|
||||
}
|
||||
}
|
||||
|
||||
// needed because static dispatch off protocol extensions doesn't work so well
|
||||
struct Notifications<T: Notify> {
|
||||
let notify: T
|
||||
|
||||
init(_ notify: T) {
|
||||
self.notify = notify
|
||||
}
|
||||
}
|
||||
|
||||
struct NotifyHandler<T> { }
|
||||
|
||||
func notify_safe<T: Notify>(_ notify: Notifications<T>) {
|
||||
let notify = notify.notify
|
||||
NotificationCenter.default.post(name: T.name, object: notify.payload)
|
||||
}
|
||||
|
||||
func handle_notify_safe<T: Notify>(_ handler: NotifyHandler<T>) -> AnyPublisher<T.Payload, Never> {
|
||||
return NotificationCenter.default.publisher(for: T.name)
|
||||
//.compactMap { notification in notification.object as? T.Payload }
|
||||
.map { notification in notification.object as! T.Payload }
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
Reference in New Issue
Block a user