Skip to content

Event bus framework supports sticky events and subscribers priority based on RxSwift

License

Notifications You must be signed in to change notification settings

portone-io/RxBus-Swift

This branch is 7 commits ahead of, 2 commits behind ridi/RxBus-Swift:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

2445842 · Sep 27, 2022
Jul 25, 2022
Feb 6, 2020
Jul 25, 2022
Jun 20, 2020
Jul 25, 2022
Aug 5, 2019
Mar 18, 2020
Apr 30, 2021
Apr 29, 2021
Sep 28, 2017
Apr 28, 2021
Sep 27, 2022
Jul 25, 2022
Jul 25, 2022

Repository files navigation

RxBus

Version License Platform

Event bus framework supports sticky events and subscribers priority based on RxSwift.

Requirements

  • Xcode 10.2+
  • Swift 5.0+
  • iOS 9.0+
  • macOS 10.10+
  • tvOS 9.0+

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/ridi/RxBus-swift.git", .upToNextMinor(from: "0.1.0"))
]

This library is distributed by CocoaPods.

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate RxBus into your Xcode project using CocoaPods, specify it in your 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

Then, run the following command:

$ pod install

## Usage

#### Imports

```swift
import RxBus
import RxSwift

Defining Events struct

// Defining Events struct
struct Events {
    struct LoggedIn: BusEvent {
        let userId: String
    }
    struct LoggedOut: BusEvent { }
    struct Purchased: BusEvent {
        let tid: Int
    }
}

Event subscription/posting

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

Sticky events

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

Subscription priority

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

System Notification subscription

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}]

Defining Custom Notification

extension Notification.Name {
    static let Custom = Notification.Name("CustomNotification")
}

Custom Notification subscription/posting

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~"]

Sticky Notifications

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]

About

Event bus framework supports sticky events and subscribers priority based on RxSwift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 94.0%
  • Ruby 6.0%