Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: repeatWhen operator #396

Closed
smaspe opened this issue Jan 8, 2016 · 12 comments
Closed

Feature request: repeatWhen operator #396

smaspe opened this issue Jan 8, 2016 · 12 comments

Comments

@smaspe
Copy link

smaspe commented Jan 8, 2016

Other implementations of Rx have the repeatWhen operator, which allows the repetition to be controlled by another Observable. Very convenient.

It is quite different from repeat in that a/ you can control the flow of repetitions and b/ you can interrupt it.

The implementation would probably take from repeat but not entirely, as it requires to delay the scheduleRecursive call until a new event was received from the control Observable, and also to resubscribe to the original source instead of re-emitting the same event.

@thanegill
Copy link
Contributor

Does retryWhen not do the same thing?

@kzaher
Copy link
Member

kzaher commented Jan 10, 2016

@smaspe could you maybe explain more about the context for which you need repeatWhen. I would first like to understand how common your need is :)

@smaspe
Copy link
Author

smaspe commented Jan 11, 2016

@thanegill no, because retryWhen requires the source to error. (but similar)

@kzaher sure! It is used in a polling system. The control Observable is able to trigger a single repeat from the source Observable.

The control Observable can then be bound to either a timed interval, a user action, ...

@smaspe
Copy link
Author

smaspe commented Jan 11, 2016

@thanegill Actually transforming the Completed event into a specific Error event on the source Observable allows to use retryWhen. A bit of a hack, though.

@kzaher
Copy link
Member

kzaher commented Jan 17, 2016

Hi @smaspe,

I think we would need to refactor retryWhen to accommodate that new functionality. I'll try to find time to investigate RxJava to see how they've organized their code, find proper unit tests and see what can we learn from them.

That will probably take some time since I'll need to find time for investigations and those refactorings, but I'll see what we can do here in the long run.

@smaspe
Copy link
Author

smaspe commented Jan 18, 2016

@kzaher thanks for taking that into consideration! much appreciated!

@AquaGeek
Copy link

AquaGeek commented Feb 5, 2016

+1 to this feature request — I'm doing something very similar (polling system).

@kzaher
Copy link
Member

kzaher commented Feb 20, 2016

I think that right now you can just do something like:

struct InfiniteSequence<E> : SequenceType {
    typealias Element = E
    typealias Generator = AnyGenerator<E>

    private let _repeatedValue: E

    init(repeatedValue: E) {
        _repeatedValue = repeatedValue
    }

    func generate() -> Generator {
        let repeatedValue = _repeatedValue
        return anyGenerator {
            return repeatedValue
        }
    }
}

and then

InfiniteSequence(sourceSequence.takeWhile { x in x is correct }).concat()

@DanielAsher
Copy link

I believe this is worthy of inclusion in the core library.

@RamblinWreck77
Copy link

I would like this as well! Would be super useful

@JeromeCHA
Copy link

Me too! It would be awesome, but this is from 2016 so I don't think it will be done soon... :/

@freak4pc
Copy link
Member

freak4pc commented May 6, 2018

I'm closing this for now due to inactivity - If you have any more comments on this subject, feel free to comment or ping me directly and I'll re-open it :) Thanks !

@freak4pc freak4pc closed this as completed May 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants