简体   繁体   中英

How to take last Observable sequence value after some time after sequence stopped produced elements in Reactive UI?

I need to disable WPF UI in my application when any of the 2 ReactiveCommands on the page is executed. Commands are called one by one. It means that they are invoked the following way:

Command1
   .Select(x => ...Prepare params for second...)
   .InvokeCommand(Command2)
   .DisposeWith(d);

I made an observable like:

IObservable<bool> CanInteractWithUserObservable => Command1.IsExecuting
                                        .CombineLatest(Command2.IsExecuting, (c1,c2)=>(c1,c2))
                                        .Select(tuple => tuple.c1 || tuple.c2)
                                        .Select(res => !res)
                                        .ObserveOn(RxApp.MainThread);

and bound it to the Window.IsEnabled property.

Generally, it works, but the problem that the observable returns value true when 1 command finished its job, but second did not start yet. So, the overall output is:

true; // Before start

false; // First command executing

true; // First command finished, second not started yet (It is problem)

false; // Second command executing

true; // Both commands finished

I need to listen to sequence and only after half-second or so from the last real event I should publish an update in mine CanInteractWithUserObservable to avoid UI blinking.

PS: Probably Timeout method may help me, but I didn't get how to use it.

If i understand correctly you want to wait both 2 event to be complete and then let your UI released.

Command1.IsExecuting
.CombineLatest(Command2.IsExecuting, (c1,c2)=>(c1,c2))
.Select(tuple => tuple.c1 && tuple.c2) // instead of or use and so only when both will be done value will be true
.SkipWhile(t=> t == false) // omit when both not completed 
.Delay(TimeSpan.FromSeconds(.5)) // i don't know you still want to wait 0.5 seconds more but Delay is the operator for that
.Select(res => !res)
.ObserveOn(RxApp.MainThread);

Timeout is used for to emitting error when an observable doesn't emit a value for amount of given value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM