Created
June 13, 2018 21:17
-
-
Save suatkarakusoglu/b9b2cc0a8247d67b0c58fce2e874d2e9 to your computer and use it in GitHub Desktop.
ZMLocalBroadcastType
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
typealias ZMBroadcastReceivedBlock = ([AnyHashable: Any]?) -> Void | |
enum ZMLocalBroadcastType: String | |
{ | |
case someMagicHappened | |
func broadcast(with data: [AnyHashable: Any]? = nil) | |
{ | |
debugPrint("Broadcasting: \(self.rawValue) with data: \(data?.description ?? " none")") | |
NotificationCenter.default.post( | |
name: self.createNotificationName(), | |
object: nil, | |
userInfo: data | |
) | |
} | |
func observeBroadcast(receivingBlock: @escaping ZMBroadcastReceivedBlock) -> ZMLocalBroadcastObserver | |
{ | |
let localObserver = ZMLocalBroadcastObserver( | |
broadcastType: self, | |
broadcastReceived: receivingBlock | |
) | |
localObserver.startObserving() | |
return localObserver | |
} | |
func createNotificationName() -> Notification.Name | |
{ | |
return Notification.Name(self.rawValue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment