CocoaPods trunk is moving to be read-only. Read more on the blog, there are 13 months to go.
| TestsTested | ✓ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | May 2016 |
| SPMSupports SPM | ✗ |
Maintained by Brendan Conron.
Events are a core part of the iOS ecosystem and a search through Cocoapods yields a ton of results. Not to mention, Apple provides NSNotification and NSNotificationCenter out of the box. So what makes EventRouter different/better?
EventRouter clocks in at just under 20KB making it incredibly lightweight.
Every function, class, and structure is documented extensively (Cocoadocs to come).
Code-coverage currently sits at 99%.
EventRouter has no other dependencies making it an unopinionated framework. Want to use EventRouter and NSNotificationCenter? Go for it! Note: EventRouter now has a subspec supporting ReactiveCocoa, completely optional. See below for usage.
When using EventRouter, there’s two options.
Use the singleton instance (useful for app-wide messaging):
EventRouter.router.addObserver(self, forEvent: "some-event")or create a standalone instance (useful for containing messages to a set of classes such as a view controller and all its components):
let router = EventRouter()
router.addObserver(self, forEvent: "some-event")Unlike NSNotificationCenter, classes that want to receive messages must conform to EventRouterObserver. EventRouterObserver only has one method, routedEventOccurred:onRouter:withUserInfo.
class MyViewController: UIViewController {
}
extension MyViewController: EventRouterObserver {
func routedEventOccurred(event: String, onRouter router: EventBroadcaster, withUserInfo userInfo: [NSObject: AnyObject]) {
if event == "my-awesome-event" {
// do something
}
}
}Adding observers is easy, just:
let router = EventRouter()
router.addObserver(self, forEvent: "my-awesome-event")To remove it later:
router.removeObserver(self, forEvent: 'my-awesome-event')
// or remove from all events
router.removeObserver(self)Because the router holds weak references to the observers (EventRouterObserver is a :class restricted protocol), if your observers deallocate, they’re removed from observer list and events are no longer sent.
If you’re a fan of ReactiveCocoa, you can install the EventRouter/ReactiveCocoa subspec. Unlike EventRouter, RACRouter doesn’t use observers. Instead, it broadcasts events out on router.signal and router.producer.
Both signal and producer send a tuple with each event, (String, EventBroadcaster, [NSObject: AnyObject]?).
let router = RACRouter()
router.signal.filter { $0.0 == "my-custom-event}.map { $0.2 }.ignoreNil()EventRouter is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "EventRouter"Brendan Conron, [email protected]
EventRouter is available under the MIT license. See the LICENSE file for more info.