Skip to content

mishimay/EventHub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventHub

Type-safe and handy observation system in Swift.

Quick Example

struct MessageEvent: EventType {
    let message: String
}

EventHub.addObserver(self) { (event: MessageEvent) in
    print(event.message) // -> 😜
}
EventHub.post(MessageEvent(message: "😜"))

Usage

  1. 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)
}
  1. 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)
    }
}
  1. Post events
EventHub.post(LoginEvent.success(id: id))

Requirements

Swift 3.0

Installation

EventHub is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "EventHub"

Author

Yuki Mishima, mishimaybe@gmail.com

License

EventHub is available under the MIT license. See the LICENSE file for more info.

About

Type-safe and handy observation system in Swift.

Resources

License

Stars

Watchers

Forks

Packages

No packages published