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

org.reactfx.SuspendWhenStream Maven / Gradle / Ivy

There is a newer version: 2.0-M5
Show newest version
package org.reactfx;

import javafx.beans.value.ObservableValue;

class SuspendWhenStream extends EventStreamBase {
    private final SuspendableEventStream source;
    private final ObservableValue condition;

    private Guard suspensionGuard = null;

    public SuspendWhenStream(
            SuspendableEventStream source,
            ObservableValue condition) {
        this.source = source;
        this.condition = condition;
    }

    @Override
    protected Subscription observeInputs() {
        Subscription s1 = EventStreams.valuesOf(condition)
                .subscribe(this::suspendSource);
        Subscription s2 = source.subscribe(this::emit);
        return s1.and(s2).and(this::resumeSource);
    }

    private void suspendSource(boolean suspend) {
        if(suspend) {
            suspendSource();
        } else {
            resumeSource();
        }
    }

    private void suspendSource() {
        if(suspensionGuard == null) {
            suspensionGuard = source.suspend();
        }
    }

    private void resumeSource() {
        if(suspensionGuard != null) {
            Guard toClose = suspensionGuard;
            suspensionGuard = null;
            toClose.close();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy