EventRouter 0.1.4

EventRouter 0.1.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release May 2016
SPMSupports SPM

Maintained by Brendan Conron.



EventRouter

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?

Tiny

EventRouter clocks in at just under 20KB making it incredibly lightweight.

Well-documented

Every function, class, and structure is documented extensively (Cocoadocs to come).

Well-tested

Code-coverage currently sits at 99%.

Zero dependencies

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.

Usage

Creating a Router

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")

Receiving Events

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 & Removing Observers

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.

ReactiveCocoa

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()

Future Features

  1. Ability to suppress and unsuppress observers.

Installation

EventRouter is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "EventRouter"

Author

Brendan Conron, [email protected]

License

EventRouter is available under the MIT license. See the LICENSE file for more info.