TestsTested | β |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Mar 2016 |
SPMSupports SPM | β |
Maintained by Yuki Mishima.
Type-safe and handy observation system in Swift.
struct MessageEvent: EventType {
let message: String
}
EventHub.addObserver(self) { (event: MessageEvent) in
print(event.message) // -> π
}
EventHub.post(MessageEvent(message: "π"))
Define events which adopt EventType
protocol
The event can be a class, structure, or enumeration.
enum LoginEvent: EventType {
case Success(id: String)
case Failure(error: ErrorType)
}
Add observers and blocks
Call addObserver(observer:thread:block:)
.
observer
: An observer object. If the observer object is destroyed, the observation will be removed automatically and the block will never be called.thread
: Optional (default is nil). It determines that which thread executes the block. If itβs nil, the block is run synchronously on the posting thread.block
: A callback closure. The block receives the defined event.EventHub.addObserver(self, thread: .Main) { (event: LoginEvent) in
switch event {
case .Success(let id):
print(id)
case .Failure(let error):
print(error)
}
}
Post events
EventHub.post(LoginEvent.Success(id: id))
Swift 2.1
EventHub is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "EventHub"
Yuki Mishima, [email protected]
EventHub is available under the MIT license. See the LICENSE file for more info.