Cameron MacFarland
Cameron MacFarland

Reputation: 71856

Creating a long running observable from shorter observables?

I have a method that returns a short observable that returns 1 or 2 items, and then completes.

I would like to have an observable that continues running, and when I call a method the short observable is created and merged into the longer running observable.

Is there a way of doing this with observable operators? Or should I just use a Subject?

Upvotes: 1

Views: 332

Answers (1)

Gluck
Gluck

Reputation: 2962

As you present the problem, there's no way around the use of subjects, specifically because you need a method call to trigger events on an existing stream.

But you can restrict the use of subjects to a minimum, which is this method call:

Subject<Unit> trigger;

void RefreshMethod() { trigger.OnNext(Unit.Default); }

IObservable<Item> GetLongObservable() {
    return trigger.SelectMany(_ => GetShortObservable());
}

Ideally, depending where the RefreshMethod is called from, you can try to propagate further the use of Rx and replace the subject by the actual event behind it.

Upvotes: 3

Related Questions