SwiftyEventBus

SwiftyEventBus is a publish/subscribe event bus for iOS and Swift.

  • simplifies the communication between components
  • make your code simple and elegant

Usage

SwiftyEventBus is very easy to use, you just need 3 steps:

1️⃣ Define

The stuff that you want to delivery need implement EventPresentable Protocol, most of foundation type already implemented, such as Int, Float, String, etc...

If you have custom type, then you should make it confirm EventPresentable Protocol.

2️⃣ Register

You can register in anywhere, it will always observe until the EventSubscription object been released.

class DemoViewController: UIViewController {

    var ob: EventSubscription<String>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        ob = EventBus.`default`.register { (x: String) in
            print(x)
        }
    }
}

3️⃣ Post

Finally, you just need to post any type that implement EventPresentable.

EventBus.default.post("Foo")

Rx-Extension

if you project using RxSwift, maybe you need this to bridge SwiftyEventBus to Rx.

pod 'SwiftyEventBus/Rx'

after that, you can use SwiftyEventBus in RxSwift world.?

var bag: DisposeBag? = DisposeBag()
EventBus.default.rx.register(String.self)
    .subscribe(onNext: { (x) in
        print(x) /// "foo"
    })
    .disposed(by: bag!)
EventBus.default.post("foo")

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

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

pod 'SwiftyEventBus'

GitHub