Giter Site home page Giter Site logo

eventhub's Introduction

Swift EventHub

Simple typesafe EventHub in Swift using callbacks/listeners defined by generic types.

Examples

Callback

struct CounterEvent: Event {
    let currentCount: Int
}

let eventHub = EventHub(queue: .global())
eventHub.subscribe { (event: CounterEvent)
    print(event.currentCount) // => 5
}

eventHub.trigger(CounterEvent(currentCount: 5))

Listener

struct SomeErrorEvent: Event {
    let message: String
    let code: Int 
}

class NotifyAdminListener: Listener<SomeErrorEvent> {
    override func handle(event: SomeErrorEvent) {
        print("Oh no! We got error \(error.code) with the message '\(error.message)'")
    }
}

let eventHub = EventHub(queue: .global())
eventHub.subscribe(NotifyAdminListener())
eventHub.trigger(SomeErrorEvent(message: "Fatal and dangerous error", code: 500))

Usage

  1. Import the library
import SwiftEventHub
  1. Initialize the EventHub class on a DispatchQueue. Add the EventHub to a global scope (e.g. shared instance), for cross-events/listeners, or use it in an internal scope
let hub = EventHub(queue: .global())
  1. Define events by making them comply to the Event protocol
struct MyEvent: Event {}
  1. Subscribe to the events either by callback or listener (see examples above)
hub.subscribe { (event: MyEvent) in 
    // Do something with the event
}
  1. Trigger events by calling the method .trigger(event: Event)
hub.trigger(MyEvent())
  1. The events triggered are distributed to all listeners attached to the hub, listening for that specific event.

  2. To unsubscribe, the returned UUID from the .subscribe method, can be used

let subscription = hub.subscribe { // ... }
hub.unsubscribe(subscription)

Requirements

Swift 4.1

Installation

With CocoaPods:

pod 'SwiftEventHub'

eventhub's People

Contributors

imbue11235 avatar

Stargazers

 avatar Rimanio Suvalio avatar moooaaz avatar prem avatar martin avatar Aleks avatar Chris A. Larson avatar Januar avatar JAMES ANDERSON  avatar Naveed Khan avatar  avatar Laura Dorothy avatar  avatar Remus Tofi avatar

Watchers

James Cloos avatar Januar avatar  avatar Naveed Khan avatar JAMES ANDERSON  avatar Aleks avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.