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

org.reactfx.DefaultEventStream Maven / Gradle / Ivy

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

import java.util.function.Consumer;

class DefaultEventStream extends EventStreamBase {
    private final EventStream input;
    private final T initial;

    private T latestEvent = null;
    private boolean firstObserver = true;
    private boolean emitted = false;

    public DefaultEventStream(
            EventStream input,
            T initial) {
        this.input = input;
        this.initial = initial;
    }

    @Override
    protected void newObserver(Consumer observer) {
        if(firstObserver) {
            firstObserver = false;
            if(!emitted) {
                observer.accept(initial);
            }
        } else {
            observer.accept(latestEvent);
        }
    }

    @Override
    protected final Subscription observeInputs() {
        firstObserver = true;
        emitted = false;
        latestEvent = initial;
        return input.subscribe(x -> {
            latestEvent = x;
            emitted = true;
            emit(x);
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy