Broadcaster.swift 984 B

12345678910111213141516171819202122232425262728
  1. import Foundation
  2. protocol Broadcaster {
  3. func register<T>(_ protocolType: T.Type, observer: T)
  4. func unregister<T>(_ protocolType: T.Type, observer: T)
  5. func unregister<T>(_ protocolType: T.Type)
  6. func notify<T>(_ protocolType: T.Type, on queue: DispatchQueue, block: @escaping (T) -> Void)
  7. }
  8. final class BaseBroadcaster: Broadcaster {
  9. func register<T>(_ protocolType: T.Type, observer: T) {
  10. SwiftNotificationCenter.register(protocolType, observer: observer)
  11. }
  12. func unregister<T>(_ protocolType: T.Type, observer: T) {
  13. SwiftNotificationCenter.unregister(protocolType, observer: observer)
  14. }
  15. func unregister<T>(_ protocolType: T.Type) {
  16. SwiftNotificationCenter.unregister(protocolType)
  17. }
  18. func notify<T>(_ protocolType: T.Type, on queue: DispatchQueue, block: @escaping (T) -> Void) {
  19. dispatchPrecondition(condition: .onQueue(queue))
  20. SwiftNotificationCenter.notify(protocolType, block: block)
  21. }
  22. }