io.reactivex.rxjavafx.observers.BindingObserver Maven / Gradle / Ivy
/**
* Copyright 2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.reactivex.rxjavafx.observers;
import com.sun.javafx.binding.ExpressionHelper;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observables.ConnectableObservable;
import javafx.beans.InvalidationListener;
import javafx.beans.binding.Binding;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
final class BindingObserver implements Observer, ObservableValue, Binding {
private final Function unmaskingFunction;
private final Consumer onError;
private final ConnectableObservable obs;
private boolean connected = false;
private Disposable disposable;
private ExpressionHelper helper;
private S value;
BindingObserver(Function unmaskingFunction, Consumer onError) {
this.unmaskingFunction = unmaskingFunction;
this.onError = onError;
this.obs = null;
}
BindingObserver(Function unmaskingFunction, ConnectableObservable obs, Consumer onError) {
this.unmaskingFunction = unmaskingFunction;
this.onError = onError;
this.obs = obs;
}
@Override
public void onSubscribe(Disposable d) {
this.disposable = d;
}
@Override
public void onComplete() {
//do nothing
}
@Override
public void onError(Throwable e) {
try {
onError.accept(e);
} catch (Throwable e1) {
e1.printStackTrace();
}
}
@Override
public void onNext(T t) {
try {
value = unmaskingFunction.apply(t);
fireValueChangedEvent();
} catch (Exception e) {
onError(e);
}
}
@Override
public S getValue() {
if (!connected && obs != null) {
obs.connect();
connected = true;
}
return value;
}
@Override
public boolean isValid() {
return true;
}
@Override
public void invalidate() {
//does nothing
}
@Override
public ObservableList> getDependencies() {
throw new UnsupportedOperationException();
}
@Override
public void dispose() {
if (disposable != null) {
disposable.dispose();
}
}
/**
* {@inheritDoc}
*/
@Override
public void addListener(InvalidationListener listener) {
helper = ExpressionHelper.addListener(helper, this, listener);
}
/**
* {@inheritDoc}
*/
@Override
public void addListener(ChangeListener super S> listener) {
helper = ExpressionHelper.addListener(helper, this, listener);
}
/**
* {@inheritDoc}
*/
@Override
public void removeListener(InvalidationListener listener) {
helper = ExpressionHelper.removeListener(helper, listener);
}
/**
* {@inheritDoc}
*/
@Override
public void removeListener(ChangeListener super S> listener) {
helper = ExpressionHelper.removeListener(helper, listener);
}
/**
* Notify the currently registered observers of a value change.
*
* This implementation will ignore all adds and removes of observers that
* are done while a notification is processed. The changes take effect in
* the following call to fireValueChangedEvent.
*/
protected void fireValueChangedEvent() {
ExpressionHelper.fireValueChangedEvent(helper);
}
}