Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidation factory for javafx.beans.Observable #47

Closed
protogenes opened this issue Feb 13, 2017 · 5 comments
Closed

Invalidation factory for javafx.beans.Observable #47

protogenes opened this issue Feb 13, 2017 · 5 comments

Comments

@protogenes
Copy link
Contributor

Add a factory for invalidations of any FX observable.
I think propagating the FX observable instead of some constant is of better value.

public static Observable<javafx.beans.Observable> invalidationsOf(final javafx.beans.Observable fxObservable) {
    return ObservableSource.fromInvalidations(fxObservable);
}
public class ObservableSource {
	private ObservableSource() {}

	public static Observable<javafx.beans.Observable> fromInvalidations(javafx.beans.Observable fxObservable) {
		return Observable.create((ObservableEmitter<javafx.beans.Observable> emitter) -> {
			final InvalidationListener listener = emitter::onNext;
			fxObservable.addListener(listener);
			emitter.setDisposable(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> fxObservable.removeListener(listener)));
		});
	}
}
@thomasnield
Copy link
Collaborator

Makes perfect sense. I'll implement this later in 1.0 as well as the 2.0 branch.

@thomasnield
Copy link
Collaborator

Wouldn't it make more sense to emit an ObservableValue<T> rather than just a JavaFX Observable? The JavaFX Observable (not Rx one) does not have the getValue() method, but the ObservableValue<T> does. I imagine that would be more useful.

    public static <T> Observable<ObservableValue<T>> fromInvalidations(final ObservableValue<T> fxObservable) {
        return Observable.create(subscriber -> {
            final InvalidationListener listener = s -> subscriber.onNext(fxObservable);
            fxObservable.addListener(listener);
            subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> fxObservable.removeListener(listener)));
        });
    }

Would it also make sense to overload a Property<T> version as well? That way we can have access to a setValue() method for each emitted Property<T>?

    public static <T> Observable<Property<T>> fromInvalidations(final Property<T> fxProperty) {
        return Observable.create(subscriber -> {
            final InvalidationListener listener = s -> subscriber.onNext(fxProperty);
            fxProperty.addListener(listener);
            subscriber.add(JavaFxSubscriptions.unsubscribeInEventDispatchThread(() -> fxProperty.removeListener(listener)));
        });
    }

@protogenes
Copy link
Contributor Author

The main difference is, that ObservableList<>, ObservableSet<>, ObservableMap<> and Property<> all implement the Observable interface and this could be used without knowing the specific type. If you know it you should use valuesOf, changesOf and so on.

@thomasnield
Copy link
Collaborator

@protogenes that makes sense. Okay I'll implement your way.

@thomasnield
Copy link
Collaborator

thomasnield commented Feb 23, 2017

@protogenes Implemented in 1.x. I'll do 2.x as soon as I merge the new Scheduler for that branch. Let me know if that's ready.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants