简体   繁体   中英

RxJava only emit item if another source will not item for some period

having two observable

Observable<Void> cancelButtonClick
            = RxView.clicks(btnCancel);
Observable<Void> actionButtonClick
            = RxView.clicks(btnSomeAction)

Problem

Trying to emit values from actionButtonClick stream, if cancelButtonClick stream will not emit value for 10 second

Tried :

final DateTime[] lastCancelDate = new DateTime[1];
cancelButtonClick
       .map(new Func1<Void, DateTime>() {
            @Override
            public DateTime call(Void aVoid) {
                return DateTime.now();
            }
        })
        .subscribe(new Action1<DateTime>() {
           @Override
            public void call(DateTime dateTime) {
                lastCancelDate[0] = dateTime;
            }
        });

actionButtonClick                
        .delay(10, TimeUnit.SECONDS)
        .filter(new Func1<Void, Boolean>() {
            @Override
            public Boolean call(Void aVoid) {
             return 
                lastCancelDate[0] == null 
                || (new Duration(lastCancelDate[0], DateTime.now()).getStandardSeconds()) 
                   > 10;
            }
        })
        .doOnNext(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                lastCancelDate[0] = null;
            }
        })
        .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Log.d("ACTIONII", "Got Action");
            }
        });

It works but there should be better way.

So you want to prevent actionButtonClick to emit if cancelButtonClick doesn't emit item in the coming 10 seconds ?

First you could improve a little your solution by using a BehaviorSubject :

BehaviorSubject<DateTime> lastCancelSubject = BehaviorSubject.create();

cancelButtonClick
        .map(new Func1<Void, DateTime>() {
            @Override
            public DateTime call(Void aVoid) {
                return DateTime.now();
            }
        })
        .subscribe(lastCancelSubject);

actionButtonClick
        .delay(10, TimeUnit.SECONDS)
        .filter(new Func1<Void, Boolean>() {
            @Override
            public Boolean call(Void aVoid) {
                return
                        lastCancelDate[0] == null
                                || (new Duration(lastCancelSubject.getValue(), DateTime.now()).getStandardSeconds())
                                > 10;
            }
        })
        .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Log.d("ACTIONII", "Got Action");
            }
        });

While rx-java offer severals way to achieve this, I would declare another Observable :

final Observable<Boolean> iscancelledObservable = cancelButtonClick
        .buffer(10, TimeUnit.SECONDS)
        .first()
        .map(new Func1<List<Void>, Boolean>() {
            @Override
            public Boolean call(List<Void> voids) {
                return voids.size() > 0;
            }
        });

actionButtonClick
        .flatMap(new Func1<Void, Observable<Boolean>>() {
            @Override
            public Observable<Boolean> call(Void aVoid) {
                return iscancelledObservable;
            }
        })
        .filter(new Func1<Boolean, Boolean>() {
            @Override
            public Boolean call(Boolean aBoolean) {
                return !aBoolean;
            }
        })
        .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                Log.d("ACTIONII", "Got Action");
            }
        });

This solution is more functional and doesn't rely on storing state.

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