1 year ago

#76002

test-img

Dims

Mutual updates don't work in RxJava while were working in conventional listeners

I need to make two values be in sync, but to allow any of them to be changed. In plain EDT listeners I was able just to guard changes with boolean flag. Each change is done only if flug is down and while changing, flag is up. This was doing a job. But now I switched to reactive and everything was broken.

Below I am listenting yProperty and change offset according to and and also listening for offset and changing y property according to it.

    doubleValues(yProperty())
        .observeOn(SwingScheduler.INSTANCE)
        .subscribe(newValue -> {
        if(!changing) {
            changing = true;
            originNode.setOffset(originNode.getXOffset(), newValue);
            changing = false;
        }
    });

    offsetChanges(getOriginNode())
        .observeOn(SwingScheduler.INSTANCE)
        .subscribe(point -> {
        if(!changing) {
            changing = true;
            setY(point.getY());
            changing = false;
        }
    });

Since I have observeOn in a chain, each action is wrapped into Runnable and posted at the end of EDT queue and executed when changing flag is already off. This leads to infinite loop of mutual calls.

Are there any means in Rx to solve such cases?


changing is class variable, it is not final. Under the hood any call of first consumer triggers second one and it triggers first one and so on.

java

asynchronous

rx-java

rx-java2

event-dispatch-thread

0 Answers

Your Answer

Accepted video resources