Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

294
Views
Take items from observable when other observable is true

How can I control which items are pushed through an observable a depending on the (boolean) values of another observable b?

I mean, items are in a are pushed only if last item emitted by b is true.

Observable sequence b should act like a pass filter.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

OK, I think I have at least a solution It can be done with a combination of WithLatestFrom and Where.

In my real code, I have this:

  • An observable of positions in a video
  • An observable of booleans that indicate if the user has started a 'seeking' operation (by dragging a thumb in the video player arbitrarily)
positionObs
   .WithLatestFrom(isSeekingObs, (pos, isSeeking) => new { pos, isSeeking })
   .Where(x => !x.isSeeking)
   .Select(x => x.pos);

It essential combine the two most recent values and applies a filter on the "signaler".

Anyway, I'd like to know if there's a cleaner solution.

over 4 years ago · Santiago Trujillo Report

0

My preferred way to do this kind of thing is with a combination of Publish and Switch.

Here's how:

Subject<bool> subject_b = new();
IObservable<bool> observable_b = subject_b.AsObservable();

IObservable<long> observable_a = Observable.Interval(TimeSpan.FromSeconds(1.0));

IObservable<long> observable_c =
    observable_a
        .Publish(published_a =>
            observable_b
                .Select(value_b =>
                    value_b
                        ? published_a
                        : Observable.Never<long>()))
        .Switch();

IDisposable subscription = observable_c.Subscribe(Console.WriteLine);
        
Thread.Sleep(TimeSpan.FromSeconds(2.5));
subject_b.OnNext(true);
Thread.Sleep(TimeSpan.FromSeconds(3));
subject_b.OnNext(false);
Thread.Sleep(TimeSpan.FromSeconds(4));
subject_b.OnNext(true);

That gives:

2
3
4
9
10
11
12
13
14
...

I prefer Publish/Switch over WithLatestFrom as there are more variety of queries that the former handles that the latter doesn't.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!