Last active
February 22, 2023 03:08
-
-
Save danielt1263/ec1032375498eb95aa260239b289d263 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// EmitWhile.swift | |
// | |
// Created by Daniel Tartaglia on 09/06/2018. | |
// Copyright © 2021 Daniel Tartaglia. MIT License. | |
// | |
import Foundation | |
import RxSwift | |
/** | |
Calls `producer` with `seed` then emits result and also passes it to `pred`. Will continue to call | |
`producer` with new values as long as `pred` returns values. | |
- parameter seed: The starting value needed for the first producer call. | |
- parameter pred: A closure that determines the next value pass into the producer or returns nil if no | |
more calls are necessary. | |
- parameter producer: A closure that returns a Single. | |
- returns: An observable that emits each producer's value. | |
*/ | |
func emitWhile<T, U>(seed: U, pred: @escaping (T) -> U?, producer: @escaping (U) -> Single<T>) -> Observable<T> { | |
producer(seed) | |
.asObservable() | |
.flatMap { (result) -> Observable<T> in | |
guard let value = pred(result) else { return .just(result) } | |
return emitWhile(seed: value, pred: pred, producer: producer) | |
.startWith(result) | |
} | |
} |
Many thanks to @cjspradling on the RxSwift slack for ideas on refinement!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool!