Skip to content

Instantly share code, notes, and snippets.

@suatkarakusoglu
Created June 13, 2018 21:17
Show Gist options
  • Save suatkarakusoglu/b9b2cc0a8247d67b0c58fce2e874d2e9 to your computer and use it in GitHub Desktop.
Save suatkarakusoglu/b9b2cc0a8247d67b0c58fce2e874d2e9 to your computer and use it in GitHub Desktop.
ZMLocalBroadcastType
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