RxBus
支持粘性事件和基于 RxSwift 的订阅者优先级的事件总线框架。
要求
- Xcode 10.2+
- Swift 5.0+
- iOS 8+
- macOS 10.10+
- tvOS 9.0+
安装
此库由 CocoaPods 分发。
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 RxBus 集成到您的 Xcode 项目中,请在其 Podfile 中指定
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
# platform :osx, '10.10'
# platform :tvos, '9.0'
use_frameworks!
target '<Target name in your project>' do
pod 'RxBus'
end
然后,运行以下命令
$ pod install
uso
导入
import RxBus
import RxSwift
定义事件结构体
// Defining Events struct
struct Events {
struct LoggedIn: BusEvent {
let userId: String
}
struct LoggedOut: BusEvent { }
struct Purchased: BusEvent {
let tid: Int
}
}
事件订阅/发布
RxBus.shared.asObservable(event: Events.LoggedIn.self).subscribe { event in
print("LoggedIn, userId = \(event.element!.userId)")
}.disposed(by: disposeBag)
RxBus.shared.post(event: Events.LoggedIn(userId: "davin.ahn"))
LoggedIn, userId = davin.ahn
粘性事件
RxBus.shared.post(event: Events.LoggedOut(), sticky: true)
RxBus.shared.asObservable(event: Events.LoggedOut.self, sticky: true).subscribe { _ in
print("LoggedOut")
}.disposed(by: disposeBag)
LoggedOut
订阅优先级
RxBus.shared.asObservable(event: Events.Purchased.self, sticky: false, priority: -1).subscribe { event in
print("Purchased(priority: -1), tid = \(event.element!.tid)")
}.disposed(by: disposeBag)
RxBus.shared.asObservable(event: Events.Purchased.self, sticky: false, priority: 1).subscribe { event in
print("Purchased(priority: 1), tid = \(event.element!.tid)")
}.disposed(by: disposeBag)
RxBus.shared.asObservable(event: Events.Purchased.self).subscribe { event in
print("Purchased(priority: 0 = default), tid = \(event.element!.tid)")
}.disposed(by: disposeBag)
RxBus.shared.post(event: Events.Purchased(tid: 1001))
Purchased(priority: 1), tid = 1001
Purchased(priority: 0 = default), tid = 1001
Purchased(priority: -1), tid = 1001
系统通知订阅
RxBus.shared.asObservable(notificationName: UIResponder.keyboardWillShowNotification).subscribe { event in
print("\(event.element!.name.rawValue), userInfo: \(event.element!.userInfo)")
}.disposed(by: disposeBag)
textField.becomeFirstResponder()
UIKeyboardWillShowNotification, userInfo: [AnyHashable("UIKeyboardAnimationCurveUserInfoKey"): 7, AnyHashable("UIKeyboardCenterEndUserInfoKey"): NSPoint: {207, 745.5}, AnyHashable("UIKeyboardFrameBeginUserInfoKey"): NSRect: {{0, 896}, {414, 54}}, AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 595}, {414, 301}}, AnyHashable("UIKeyboardBoundsUserInfoKey"): NSRect: {{0, 0}, {414, 301}}, AnyHashable("UIKeyboardIsLocalUserInfoKey"): 1, AnyHashable("UIKeyboardAnimationDurationUserInfoKey"): 0.25, AnyHashable("UIKeyboardCenterBeginUserInfoKey"): NSPoint: {207, 923}]
自定义通知定义
extension Notification.Name {
static let Custom = Notification.Name("CustomNotification")
}
自定义通知订阅/发布
RxBus.shared.asObservable(notificationName: .Custom).subscribe { event in
print("\(event.element!.name.rawValue), userInfo: \(event.element!.userInfo)")
}.disposed(by: disposeBag)
RxBus.shared.post(notificationName: .Custom, userInfo: ["message": "Hi~"])
Custom, userInfo: [AnyHashable("message"): "Hi~"]
粘性通知
RxBus.shared.post(notificationName: .Custom, userInfo: ["value": 5], sticky: true)
RxBus.shared.asObservable(notificationName: .Custom, sticky: true).subscribe { event in
print("\(event.element!.name.rawValue), userInfo: \(event.element!.userInfo)")
}.disposed(by: disposeBag)
Custom, userInfo: [AnyHashable("value"): 5]