https://github.com/gujci/eventemitter
a lightweight tool to easily implement listener pattern
https://github.com/gujci/eventemitter
carthage eventemitter listener-pattern swift
Last synced: 8 months ago
JSON representation
a lightweight tool to easily implement listener pattern
- Host: GitHub
- URL: https://github.com/gujci/eventemitter
- Owner: Gujci
- License: mit
- Created: 2016-02-29T14:52:24.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-05-25T10:34:53.000Z (about 6 years ago)
- Last Synced: 2025-10-20T13:47:38.679Z (8 months ago)
- Topics: carthage, eventemitter, listener-pattern, swift
- Language: Swift
- Size: 72.3 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EventEmitter
A lightweight tool to easily implement listener pattern.
[](https://github.com/Carthage/Carthage)
[](https://travis-ci.org/Gujci/EventEmitter)

Example
========
Subscribe to events w parameters:
```swift
eventEmitterInstance.on("eventWithParameter") { (weak parameter: Any?) in
//...
}
```
Or w/o parameters:
```swift
eventEmitterInstance.on("eventWithoutParameter") {
//...
}
```
Defining events with an enum is also supported:
```swift
public enum AppEvent: String, Event {
case some
case other
}
```
```swift
eventEmitterInstance.on(AppEvent.some) {
//...
}
```
Emit an event with an EventEmitter class
```swift
class EventEmitterClass: EventEmitter {
var listeners: Dictionary>? = [:]
//...
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:
```swift
emit("some", information: parameter)
```
# Documentation
### Functions to be called from an EventEmitter instance
```swift
emit(_ event: Event)
```
```swift
emit(_ event: Event, information: T)
```
```swift
emit(onMain event: Event)
```
```swift
emit(onMain event: Event, information: T)
```
### Functions to call on an EventEmitter instance
#### subscribe
```swift
on(_ event: Event, action: (() -> ()))
```
```swift
on(_ events: [Event], action: (() -> ()))
```
```swift
once(_ event: Event, action: (() -> ()))
```
```swift
on(_ event: Event, action: ((T?) -> ()))
```
```swift
on(_ events: [Event], action: ((T?) -> ()))
```
```swift
once(_ event: Event, action: ((T?) -> ()))
```
#### unsubscribe
```swift
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
- [x] Travis support
- [ ] Full code coverage
- [ ] Support custom threads
- [x] Once
- [ ] More documentation