Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

295
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda