1
0
mirror of git://jb55.com/damus synced 2024-09-30 00:40:45 +00:00

notify: add typesafe notify class

This commit is contained in:
William Casarin 2023-07-30 10:47:53 -07:00
parent 06a66a3709
commit df3b94a1fc
2 changed files with 56 additions and 0 deletions

View File

@ -202,6 +202,7 @@
4C9F18E229AA9B6C008C55EC /* CustomizeZapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */; };
4C9F18E429ABDE6D008C55EC /* MaybeAnonPfpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */; };
4CA2EFA0280E37AC0044ACD8 /* TimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */; };
4CA352A02A76AE80003BB08B /* Notify.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA3529F2A76AE80003BB08B /* Notify.swift */; };
4CA352A62A76B020003BB08B /* Pubkey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA352A52A76B020003BB08B /* Pubkey.swift */; };
4CA3FA1029F593D000FDB3C3 /* ZapTypePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA3FA0F29F593D000FDB3C3 /* ZapTypePicker.swift */; };
4CA5588329F33F5B00DC6A45 /* StringCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA5588229F33F5B00DC6A45 /* StringCodable.swift */; };
@ -704,6 +705,7 @@
4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomizeZapView.swift; sourceTree = "<group>"; };
4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaybeAnonPfpView.swift; sourceTree = "<group>"; };
4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineView.swift; sourceTree = "<group>"; };
4CA3529F2A76AE80003BB08B /* Notify.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notify.swift; sourceTree = "<group>"; };
4CA352A52A76B020003BB08B /* Pubkey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pubkey.swift; sourceTree = "<group>"; };
4CA3FA0F29F593D000FDB3C3 /* ZapTypePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZapTypePicker.swift; sourceTree = "<group>"; };
4CA5588229F33F5B00DC6A45 /* StringCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringCodable.swift; sourceTree = "<group>"; };
@ -1377,6 +1379,14 @@
path = Util;
sourceTree = "<group>";
};
4CA3529C2A76AE47003BB08B /* Notify */ = {
isa = PBXGroup;
children = (
4CA3529F2A76AE80003BB08B /* Notify.swift */,
);
path = Notify;
sourceTree = "<group>";
};
4CA9275B2A28FF570098A105 /* Longform */ = {
isa = PBXGroup;
children = (
@ -1552,6 +1562,7 @@
4CE6DEE527F7A08100C66700 /* damus */ = {
isa = PBXGroup;
children = (
4CA3529C2A76AE47003BB08B /* Notify */,
F7F0BA23297892AE009531F3 /* Modifiers */,
4C4A3A5A288A1B2200453788 /* damus.entitlements */,
4CE4F9DF285287A000C00DD9 /* Components */,
@ -2172,6 +2183,7 @@
4C3BEFD42819DE8F00B3DE84 /* NostrKind.swift in Sources */,
4C3EA66028FF5E7700C48A62 /* node_id.c in Sources */,
4C687C212A5F7ED00092C550 /* DamusBackground.swift in Sources */,
4CA352A02A76AE80003BB08B /* Notify.swift in Sources */,
4CE6DEE727F7A08100C66700 /* damusApp.swift in Sources */,
4C363A962827096D006E126D /* PostBlock.swift in Sources */,
4CA9275F2A2902B20098A105 /* LongformPreview.swift in Sources */,

44
damus/Notify/Notify.swift Normal file
View 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()
}