EventBus
- 快速
- 线程安全
- 类型安全
3 步使用 EventBus
定义任何类型的事件
struct DemoEvent: Event{
//Any property you want
}
订阅事件
DemoEvent.subscribe().next { (event) in }
发布事件
DemoEvent().publish()
要求
- iOS 8
- Swift 4
安装
pod EventBus.swift
特性
主题
默认情况下,所有回调处理程序都是在发布事件的线程上调用的,如果您希望在另一个线程上接收它
Event.subscribe().at(queue:yourqueue).next { (event) in }
生命周期跟踪器
大多数情况下,您希望在对象解构时取消订阅事件。您可以使用 dispose(with:)
来实现这一点。
//When self is dealloced, disposed() is called
Event.subscribe().dispose(with:self).next { (event) in }
如果您想手动处理解构
let token = Event.subscribe().next { (event) in }
token.dispose()
事件 ID
有时,您只对事件的特殊 ID 感兴趣。
1. 提供事件 ID
struct DownloadEvent: Event{
var eventId: String{
return itemId
}
let itemId: String
}
2. 使用 only
订阅
DownloadEvent.subscribe()
.dispose(with:self)
.only(id:"12345")
.next{ (event) in
}
3. 发布
DownloadEvent(itemId:"12345").publish()
作者
Leo, [email protected]
许可证
EventBus 在 MIT 许可证下可用。更多信息请参阅 LICENSE 文件。