org.reactfx.HookStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of richtextfx Show documentation
Show all versions of richtextfx Show documentation
FX-Text-Area for formatted text and other special effects.
package org.reactfx;
import java.util.function.Consumer;
class HookStream extends EventStreamBase {
private final EventStream source;
private final Consumer super T> sideEffect;
private boolean sideEffectInProgress = false;
public HookStream(EventStream source, Consumer super T> sideEffect) {
this.source = source;
this.sideEffect = sideEffect;
}
@Override
protected Subscription observeInputs() {
return source.subscribe(t -> {
if(sideEffectInProgress) {
throw new IllegalStateException("Side effect is not allowed to cause recursive event emission");
}
sideEffectInProgress = true;
try {
sideEffect.accept(t);
} finally {
sideEffectInProgress = false;
}
emit(t);
});
}
}