Latest 0.1.4
Homepage https://github.com/startupthekid/EventRouter
License MIT
Platforms ios 8.0
Authors

CI Status
Version
codecov.io
License
Platform

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?

Note: – EventRouter now has a subspec supporting ReactiveCocoa, completely optional. See below for usage.

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!

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 use RACRouter (See below for installation).
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"

To install the ReactiveCocoa flavored router:

pod "EventRouter/ReactiveCocoa"

Author

Brendan Conron, [email protected]

License

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

Latest podspec

{
    "name": "EventRouter",
    "version": "0.1.4",
    "summary": "A simple and clean alternative to NSNotificationCenter, written in Swift.",
    "description": "EventRouter is a simple and easy-to-use message router for sharing messages inside your app. Unlike with `NSNotificationCenter`,nclasses that want to receive notifications must explicitly conform to the observation protocol, allowing developers to more readily specifyntheir intent. EventRouter has 0 dependencies, making it a breeze to install.",
    "homepage": "https://github.com/startupthekid/EventRouter",
    "license": "MIT",
    "authors": {
        "Brendan Conron": "[email protected]"
    },
    "source": {
        "git": "https://github.com/startupthekid/EventRouter.git",
        "tag": "0.1.4"
    },
    "social_media_url": "https://twitter.com/startupthekid",
    "platforms": {
        "ios": "8.0"
    },
    "default_subspecs": "Core",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Source/Core/*.swift",
            "frameworks": "Foundation"
        },
        {
            "name": "ReactiveCocoa",
            "source_files": "Source/ReactiveCocoa/*.swift",
            "dependencies": {
                "EventRouter/Core": [],
                "ReactiveCocoa": [
                    "~> 4.1"
                ]
            }
        }
    ]
}

Pin It on Pinterest

Share This