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

248
Views
Observable: obtener el último valor en intervalos hasta que finalice la fuente

Estoy buscando un selector observable con una firma similar a esta:

 static IObservable<T> TakeLatest(this IObservable<T> input, TimeSpan interval)

Que debería:

  1. Emite el primer elemento tan pronto como la entrada emite su primer elemento
  2. A partir de ese momento, en intervalos de tiempo fijos, emitir el elemento más reciente producido por la entrada
  3. Complete (o falle) siempre que la entrada se complete (o falle)

En términos de canicas, algo como lo siguiente, asumiendo intervalo = 2 unidades de tiempo:

Tiempo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Aporte UN B C D mi F (completa)
Producción UN B D D mi mi completo (F ya no se emite)

¿Hay alguna forma lista para usar de hacerlo, o un selector razonablemente fácil para producir estos resultados?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Esto probablemente debería hacer exactamente lo que quieres. Aunque no lo he probado.

 /// <summary>Samples the source observable sequence at each interval, /// allowing repeated emissions of the same element.</summary> public static IObservable<T> SampleWithDuplicates<T>(this IObservable<T> source, TimeSpan interval, IScheduler scheduler = null) { scheduler ??= DefaultScheduler.Instance; return source.Publish(published => Observable .Interval(interval, scheduler) .WithLatestFrom(published, (_, x) => x) .Merge(published.FirstAsync()) .TakeUntil(published.LastOrDefaultAsync())); }
over 4 years ago · Santiago Trujillo Report

0

He hecho lo siguiente ahora: creo que funciona, pero lo dejaré abierto en caso de que alguien pueda pensar en una forma más elegante (o puede pensar en un problema con mi implementación actual)

 static IObservable<T> TakeLatest(this IObservable<T> input, TimeSpan interval, IScheduler scheduler) => input .FirstAsync() .Select(_ => Observable.Interval(interval, scheduler).StartWith(0)) .Switch() .CombineLatest(input, (a,b) => (a,b)) .DistinctUntilChanged(x => xa) .Select(x => xb) .TakeUntil(input.LastAsync());
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!