Code Monkey home page Code Monkey logo

swiftyeventbus's Introduction

SwiftyEventBus

CI Status Carthage compatible codecov Version License Platform

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

  • simplifies the communication between components
  • make your code simple and elegant
  • type safe
  • more advance feature: safety event, sticky event, etc...

In addition, if you want to read document, please click here.

Usage

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

1.Register

You can register in anywhere with any type, 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)
        }
    }
}

2.Post

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

EventBus.default.post("Foo")

Advance

Safe Post

Sometime, you maybe post a message that no one obseving, this is unsafety behaviour, then you can use this:

EventBus.default.safePost("Foo")

If there is no observer subscribe this kind of message, EventBus.default.safePost("Foo") will raise EventBusPostError.useless Exception, you can catch it and handle this.

/// handle EventBusPostError excetion
do {
    try EventBus.default.safePost("foo")
} catch {
    // do something
}

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

CocoaPods

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

pod 'SwiftyEventBus'

SwiftyEventBus is also available on Carthage, please add this to Cartfile:

github "Maru-zhang/SwiftyEventBus"

Author

Maru-zhang, [email protected]

License

SwiftyEventBus is available under the MIT license. See the LICENSE file for more info.

swiftyeventbus's People

Contributors

maru-zhang avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.