MessageBus is a small and simple library written in swift for the simple use of broadcasting and receiving events.
A MessageBus can be created as an object. Other then NSNotificationCenter in OSX you can have as many instances as you want.
let messageBus : MessageBus = SimpleMessageBus()
MessageBus clients can register to observe events from the message bus.
Note that MessageBus infers the type of e from the observation class type Event.self.
messageBus.observe(Event.self) { (e) in
}
// e preserves the type of MyCustomEvent
messageBus.observe(MyCustomEvent.self) { (e) in
}
MessageBus is being intended to be used accross services in a broader application.
As any event pattern this services can send events such as the following sample.
let event = MyCustomEvent()
messageBus.post(event)
MessageBus safely handles subsequentially posted events when it comes to order.
Note the following issue:
messageBus.observe(Purchase.self) { (purchaseEvent) in
let trackEvent = TrackEvent(purchaseEvent)
messageBus.post(trackEvent)
}
MessageBus defers the execution of the posting for the trackEvent right immediatly after the Purchase Event observation block has been exited its context, but still sustains the event order.