All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.reactfx.HookStream Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.reactfx;

import java.util.function.Consumer;

class HookStream extends EventStreamBase {
    private final EventStream source;
    private final Consumer sideEffect;
    private boolean sideEffectInProgress = false;

    public HookStream(EventStream source, Consumer 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);
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy