EventBus
- Fast
- Thread Safe
- Type Safe
EventBus in 3 steps
Define events with any type you want
struct DemoEvent: Event{
//Any property you want
}
Subscribe event
DemoEvent.subscribe().next { (event) in }
Publish Event
DemoEvent().publish()
Require
- iOS 8
- Swift 4
Install
pod EventBus.swift
Features
Thread
By deault, all callback hander is invoked on thread that publish the vent, if you want to receive it on another thread:
Event.subscribe().at(queue:yourqueue).next { (event) in }
Life Circle Tracker
Most times, you want to unsubscribe an event when a object is dealloced. You can using dispose(with:)
to achive this.
//When self is dealloced, disposed() is called
Event.subscribe().dispose(with:self).next { (event) in }
If you want to manual handle dispose
let token = Event.subscribe().next { (event) in }
token.dispose()
Event Id
Sometimes, you only interested in a special id of event.
1.Provide eventId
struct DownloadEvent: Event{
var eventId: String{
return itemId
}
let itemId: String
}
2.Use only
to subscribe
DownloadEvent.subscribe()
.dispose(with:self)
.only(id:"12345")
.next{ (event) in
}
3.Publish
DownloadEvent(itemId:"12345").publish()
Author
Leo, [email protected]
License
EventBus is available under the MIT license. See the LICENSE file for more info.