RxSwiftExt

repository·main·Indexed 23 days ago

https://github.com/rxswiftcommunity/rxswiftext

A collection of convenience operators and Reactive Extensions for RxSwift designed to provide additional functionality omitted from the RxSwift core. It includes operators such as unwrap, ignore, once, distinct, pairwise, nwise, retry, and filterMap, as well as specialized extensions for UIViewPropertyAnimator and UIScrollView.

Tokens
2.5K
Snippets
15
Records
22
Agent score
30%

What's inside RxSwiftExt

  1. Overview of RxSwiftExt operators

    main

    RxSwiftExt provides additional convenience operators and Reactive Extensions for RxSwift to extend its core functionality.

    Available operators include:

    • unwrap
    • ignore
    • ignoreWhen
    • Observable.once
    • distinct
    • map
    • not
    • and
    • Observable.cascade
    • pairwise
    • nwise
    • retry
    • repeatWithBehavior
    • catchErrorJustComplete
    • pausable
    • pausableBuffered
    • apply
    • filterMap
    • Observable.fromAsync
    • Observable.zip(with:)
    • Observable.merge(with:)
    • count
    • partition
    • bufferWithTrigger

    Additionally, there are operators available for materialize()'d sequences:

    • errors
    • elements
  2. Install RxSwiftExt via CocoaPods

    main

    To install RxSwiftExt using CocoaPods, add the following to your Podfile.

    For Swift 5.x and RxSwift 5.0.0 or later, use the standard pod. This installs both RxSwift and RxCocoa extensions. If you only need the RxSwift extensions, use the RxSwiftExt/Core subspec.

    If you are still using Swift 4, use version ~> 3.

  3. Collect elements with bufferWithTrigger()

    main

    The bufferWithTrigger(trigger) operator collects elements from the source observable and emits them as an array whenever the trigger observable emits.

    let observable = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
    let trigger = Observable.of(signalAtThreeSeconds, signalAtFiveSeconds).merge()
    let buffered = observable.bufferWithTrigger(trigger)
    
    // prints next([0, 1, 2]) @ 3s, next([3, 4]) @ 5s
    buffered.subscribe { print($0) }
  4. Ignore specific elements or elements matching a predicate

    main

    RxSwiftExt provides two ways to ignore elements:

    1. ignore(element): Ignores elements that are equal to the provided value.
    2. ignoreWhen { closure }: Ignores elements that satisfy the provided predicate closure.
      // Ignore specific value
      Observable.from(["One","Two","Three"])
        .ignore("Two")
        .subscribe { print($0) }
    
      // Ignore based on condition
      Observable<Int>
        .of(1,2,3,4,5,6)
        .ignoreWhen { $0 > 2 && $0 < 6 }
        .subscribe { print($0) }
  5. Send an element exactly once with once()

    main

    The once() operator ensures that only the first subscriber receives the next element. Subsequent subscribers will receive an empty sequence.

      let obs = Observable.once("Hello world")
      print("First")
      obs.subscribe { print($0) }
      print("Second")
      obs.subscribe { print($0) }
  6. Filter and map in one step with filterMap()

    main

    The filterMap operator combines filtering and mapping. For each element, you provide a closure that returns either .ignore (to filter out) or .map(value) (to transform and pass through).

    // keep only even numbers and double them
    Observable.of(1,2,3,4,5,6)
        .filterMap { number in
            (number % 2 == 0) ? .ignore : .map(number * 2)
        }
  7. Animate UIViewPropertyAnimator with rx.animate()

    main

    The animate(afterDelay:) operator provides a Completable that triggers the animation upon subscription and completes when the animation ends.

    button.rx.tap
        .flatMap {
            animator1.rx.animate()
                .andThen(animator2.rx.animate(afterDelay: 0.15))
                .andThen(animator3.rx.animate(afterDelay: 0.1))
        }
  8. Detect when UIScrollView reaches the bottom

    main

    The reachedBottom(offset:) operator provides a sequence that emits every time the UIScrollView is scrolled to the bottom, allowing for an optional offset.

    tableView.rx.reachedBottom(offset: 40)
                .subscribe { print("Reached bottom") }