A lightweight tool to easily implement listener pattern.
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 struct
s as well.
String
comforms to Event
protocol, so this is also a valid code:
emit("some", information: parameter)
emit(_ event: Event)
emit<T>(_ event: Event, information: T)
emit(onMain event: Event)
emit<T>(onMain event: Event, information: T)
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?) -> ()))
removeListeners(_ event: Event?)
Use the GitHub link in Xcode
Put this line into your cartfile
github "Gujci/EventEmitter"
for Swift 3 use the 0.5.5
tag. This version will not be supported.
github "Gujci/EventEmitter" "== 0.5.5"
To turn on logging add EventLoggingEnabled
to 'Arguments passed on launch' at Schema/Run/Arguments
- Travis support
- Full code coverage
- Support custom threads
- Once
- More documentation