c# - Reactive Extensions (Rx) - sample with last known value when no value is present in interval -


i have observable stream produces values @ inconsistent intervals this:

------1---2------3----------------4--------------5--- 

and sample without empty samples once value has been produced:

------1---2------3----------------4--------------5-----  ----_----1----2----3----3----3----4----4----4----5----5 

i thought replay().refcount() used here provide last known value sample() doesn't re-subscribe source stream didn't work out.

any thoughts on how can this?

assuming source stream iobservable<int> xs , sampling interval timespan duration then:

xs.publish(ps =>      observable.interval(duration)         .zip(ps.mostrecent(0), (x,y) => y)         .skipuntil(ps)) 

for generic solution, replace 0 parameter mostrecent default(t) iobservable<t> source stream type.

the purpose of publish prevent subscription side effects since need subscribe source twice - once mostrecent , once skipuntil. purpose of latter prevent sampling values until source stream's first event.

you can simplify if don't care getting default values before source stream's first event:

observable.interval(duration)     .zip(xs.mostrecent(0), (x,y) => y) 

a related operator withlatestfrom might of interest; coming rx in next release. see here details.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -