Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aleclarson/emitter-kit
Type-safe event handling for Swift
https://github.com/aleclarson/emitter-kit
Last synced: 2 days ago
JSON representation
Type-safe event handling for Swift
- Host: GitHub
- URL: https://github.com/aleclarson/emitter-kit
- Owner: aleclarson
- License: mit
- Created: 2014-08-01T06:25:36.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-10-14T08:30:53.000Z (about 2 years ago)
- Last Synced: 2024-10-20T12:25:47.687Z (3 months ago)
- Language: Swift
- Homepage:
- Size: 331 KB
- Stars: 566
- Watchers: 19
- Forks: 70
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swift-cn - EmitterKit - an implementation of event emitters and listeners in swift. (Libs / Events)
- fucking-awesome-swift - EmitterKit - Implementation of event emitters and listeners. (Libs / Events)
- awesome-swift - EmitterKit - Implementation of event emitters and listeners. (Libs / Events)
- awesome-swift - EmitterKit - An elegant event framework built in Swift (Events)
- awesome-swift - EmitterKit - Type-safe event handling for Swift ` 📝 2 years ago ` (Events [🔝](#readme))
- awesome-swift - EmitterKit - Implementation of event emitters and listeners. (Libs / Events)
README
# emitter-kit v5.2.2
![stable](https://img.shields.io/badge/stability-stable-4EBA0F.svg?style=flat)
[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/EmitterKit.svg?style=flat)](https://cocoapods.org/pods/EmitterKit)
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Platform](https://img.shields.io/cocoapods/p/EmitterKit.svg?style=flat)](http://cocoadocs.org/docsets/EmitterKit)A replacement for `NSNotificationCenter#addObserver` and `NSObject#addObserver` that is **type-safe** and **not verbose**.
```swift
import EmitterKit// A generic event emitter (but type-safe)!
var event = Event()// Any emitted data must be the correct type.
event.emit(data)// This listener will only be called once.
// You are *not* required to retain it.
event.once { data: T in
print(data)
}// This listener won't stop listening;
// unless you stop it manually,
// or its Event is deallocated.
// You *are* required to retain it.
var listener = event.on { data: T in
print(data)
}// Stop the listener manually.
listener.isListening = false// Restart the listener (if it was stopped).
listener.isListening = true
```
### Targeting
A **target** allows you to associate a specific `AnyObject` with an `emit` call. This is useful when emitting events associated with classes you can't add properties to (like `UIView`).
When calling `emit` with a target, you must also call `on` or `once` with the same target in order to receive the emitted event.
```Swift
let myView = UIView()
let didTouch = Event()didTouch.once(myView) { touch in
print(touch)
}didTouch.emit(myView, touch)
```
### NSNotification
The `Notifier` class helps when you are forced to use `NSNotificationCenter` (for example, if you want to know when the keyboard has appeared).
```swift
// You are **not** required to retain this after creating your listener.
var event = Notifier(UIKeyboardWillShowNotification)// Handle NSNotifications with style!
listener = event.on { (notif: Notification) in
print(notif.userInfo)
}
```
### Key-Value Observation (KVO)
```swift
// Any NSObject descendant will work.
var view = UIView()// "Make KVO great again!" - Donald Trump
listener = view.on("bounds") { (change: Change) in
print(change)
}
```
### Thread Safety
⚠️ None of the classes in EmitterKit are thread-safe!
The following actions must be done on the same thread, or you need manual locking:
- Emit an event
- Add/remove a listener
- Set the `isListening` property of a listener
### v5.2.2 changelog
- Fixed protocol casting (#60)
### v5.2.1 changelog
- Fix Carthage compatibility for non iOS platforms
### v5.2.0 changelog
- Added the `Event.getListeners` method
- Listeners are now always called in the order they were added
- Event.emit() can be called without an argument
- Carthage support has been improved### v5.1.0 changelog
- The `NotificationListener` class now takes a `Notification` instead of an `NSDictionary`.
- A `NotificationListener` without a target will now receive every `Notification` with its name, regardless of the value of `notif.object`.
### v5.0.0 changelog
- Swift 3.0 + Xcode 8.0 beta 6 support
- The `Signal` class was removed. (use `Event` instead)
- The `Emitter` abstract class was removed.
- The `EmitterListener` class was renamed `EventListener`.
- The `Event` class no longer has a superclass.
- The `Notification` class was renamed `Notifier` (to prevent collision with `Foundation.Notification`).
- The `on` and `once` methods of `Event` now return an `EventListener` (instead of just a `Listener`)
- The `on` and `once` methods of `Notifier` now return an `NotificationListener` (instead of just a `Listener`)
- The `on` and `once` methods of `NSObject` now return an `ChangeListener` (instead of just a `Listener`)
- The `keyPath`, `options`, and `object` properties of `ChangeListener` are now public.
- A `listenerCount: Int` computed property was added to the `Event` class.
- An `event: Event` property was added to the `EventListener` class.
The changelog for older versions can be [found here](https://github.com/aleclarson/emitter-kit/wiki/Changelog).