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

org.reactfx.EmitBothOnEachStream Maven / Gradle / Ivy

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

import static org.reactfx.util.Tuples.*;

import org.reactfx.util.Tuple2;

/**
 * {@link EventStream#emitBothOnEach(EventStream)}
 */
class EmitBothOnEachStream extends EventStreamBase> {
    private final EventStream source;
    private final EventStream impulse;

    private boolean hasValue = false;
    private A a = null;

    public EmitBothOnEachStream(EventStream source, EventStream impulse) {
        this.source = source;
        this.impulse = impulse;
    }

    @Override
    protected Subscription observeInputs() {
        Subscription s1 = source.subscribe(a -> {
            hasValue = true;
            this.a = a;
        });

        Subscription s2 = impulse.subscribe(i -> {
            if(hasValue) {
                emit(t(a, i));
            }
        });

        return s1.and(s2);
    }
}