org.reactfx.SuspendWhenStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reactfx Show documentation
Show all versions of reactfx Show documentation
Reactive event streams for JavaFX
package org.reactfx;
import javafx.beans.value.ObservableValue;
class SuspendWhenStream extends EventStreamBase {
private final SuspendableEventStream source;
private final ObservableValue condition;
private Guard suspensionGuard = null;
public SuspendWhenStream(
SuspendableEventStream source,
ObservableValue condition) {
this.source = source;
this.condition = condition;
}
@Override
protected Subscription observeInputs() {
Subscription s1 = EventStreams.valuesOf(condition)
.subscribe(this::suspendSource);
Subscription s2 = source.subscribe(this::emit);
return s1.and(s2).and(this::resumeSource);
}
private void suspendSource(boolean suspend) {
if(suspend) {
suspendSource();
} else {
resumeSource();
}
}
private void suspendSource() {
if(suspensionGuard == null) {
suspensionGuard = source.suspend();
}
}
private void resumeSource() {
if(suspensionGuard != null) {
Guard toClose = suspensionGuard;
suspensionGuard = null;
toClose.close();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy