Swiftpack.co - Package - Gujci/EventEmitter

EventEmitter

A lightweight tool to easily implement listener pattern.

Carthage compatible Build Status Swift

Example

Subscribe to events w parameters:

eventEmitterInstance.on("eventWithParameter") { (weak parameter: Any?) in
    //...
}

Or w/o parameters:

eventEmitterInstance.on("eventWithoutParameter") {
    //...
}

Defining events with an enum is also supported:

public enum AppEvent: String, Event {
    case some
    case other
}
eventEmitterInstance.on(AppEvent.some) {
    //...
}

Emit an event with an EventEmitter class

class EventEmitterClass: EventEmitter {
  var listeners: Dictionary<String, Array<Any>>? = [:]
  
  //...
  public func foo(parameter: Any?) {
    //...
    emit(AppEvent.some, information: parameter)
  }
  
  public func bar() {
    //...
    emit(AppEvent.some)
  }
}

Note: This is a protocoll so you can use it with structs as well.

String comforms to Event protocol, so this is also a valid code:

emit("some", information: parameter)

Documentation

Functions to be called from an EventEmitter instance

emit(_ event: Event)
emit<T>(_ event: Event, information: T)
emit(onMain event: Event)
emit<T>(onMain event: Event, information: T)

Functions to call on an EventEmitter instance

subscribe

on(_ event: Event, action: (() -> ()))
on(_ events: [Event], action: (() -> ()))
once(_ event: Event, action: (() -> ()))
on<T>(_ event: Event, action: ((T?) -> ()))
on<T>(_ events: [Event], action: ((T?) -> ()))
once<T>(_ event: Event, action: ((T?) -> ()))

unsubscribe

removeListeners(_ event: Event?)

Installation

SPM

Use the GitHub link in Xcode

Carthage

Put this line into your cartfile

github "Gujci/EventEmitter"

Swift 3

for Swift 3 use the 0.5.5 tag. This version will not be supported.

github "Gujci/EventEmitter" "== 0.5.5"

Debugging

To turn on logging add EventLoggingEnabled to 'Arguments passed on launch' at Schema/Run/Arguments

TODO

  • ☑ Travis support
  • ☐ Full code coverage
  • ☐ Support custom threads
  • ☑ Once
  • ☐ More documentation

Github

link
Stars: 5

Dependencies

Used By

Total: 0

Releases

- 2018-08-09 15:30:31

- 2017-12-06 09:21:03

Urgent bugfixes

- 2017-09-20 15:33:37

- 2017-09-13 08:35:09

- 2017-05-11 14:38:53

- 2017-05-11 14:02:55

- 2017-03-17 12:38:18

- 2017-03-17 10:38:29

- 2017-03-17 10:00:33

- 2017-02-28 13:40:45

- 2016-10-27 13:17:53

- 2016-10-12 09:14:33

- 2016-10-06 10:09:54

- 2016-10-05 08:49:22