`RxEventHub` 是一个基于 `RxSwift` 的消息总线
`RxEventHub` 是一个基于 `RxSwift` 的消息总线(类似于 NSNotificationCenter), 它使得广播事件变得简单、类型安全、尽可能少出错,很多情况下可以用于代替 `NSNotificationCenter`。
Swift 其它杂项
共35Star
详细介绍
RxEventHub
Overview
RxEventHub
is an event hub in RxSwift
world, it makes multicasting event easy, type-safe and error-free, it's intent to replace usage of NSNotificationCenter
in most cases.
To make multicasting event type-safe and error-free, this lib provides RxEventProvider
with generic type. For each type of event you want to send and observe, you create a subclass of RxEventProvider
, just like below:
class ExampleIntEventProvider: RxEventProvider<Int>{}
This class is used to provide type info and event name. You may override the typeKey
method to provide custom event name.
You can notify the hub when events occur by simply writing 1 line code like below:
RxEventHub.sharedHub.notify(ExampleIntEventProvider(), data: 2)
You can get the Observable
(Observable<Int>
in this example) when events occur by simply writing 1 line code like below:
RxEventHub.sharedHub.eventObservable(ExampleIntEventProvider())
One more thing, you can either use the default hub RxEventHub.sharedHub
, or create different hubs for different modules as you want.
Requirements
- Swift 3 (use 0.1.1 version for Swift 2)
Installation
CocoaPods
RxEventHub is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "RxEventHub"
Manual
Just copy RxEventHub.swift
into your project, and you're ready to go.
License
RxEventHub is available under the MIT license. See the LICENSE file for more info.