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

org.reactfx.ConnectableEventSource Maven / Gradle / Ivy

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

import org.reactfx.util.MapHelper;


public final class ConnectableEventSource
extends EventStreamBase
implements ConnectableEventStream, ConnectableEventSink {

    private MapHelper, Subscription> subscriptions = null;

    @Override
    public final void push(T value) {
        emit(value);
    }

    @Override
    public Subscription connectTo(EventStream input) {

        if(MapHelper.containsKey(subscriptions, input)) {
            throw new IllegalStateException("Already connected to event stream " + input);
        }

        Subscription sub = isObservingInputs() ? subscribeToInput(input) : null;
        subscriptions = MapHelper.put(subscriptions, input, sub);

        return () -> {
            Subscription s = MapHelper.get(subscriptions, input);
            subscriptions = MapHelper.remove(subscriptions, input);
            if(s != null) {
                s.unsubscribe();
            }
        };
    }

    @Override
    protected final Subscription observeInputs() {
        MapHelper.replaceAll(subscriptions, (input, sub) -> subscribeToInput(input));
        return () -> MapHelper.replaceAll(subscriptions, (input, sub) -> {
            sub.unsubscribe();
            return null;
        });
    }

    private final Subscription subscribeToInput(EventStream input) {
        return input.subscribe(this::push);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy