-
-
Save timyates/fd6904dcca366d50729c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.scene.Scene; | |
import javafx.scene.control.SplitPane; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.web.WebEngine; | |
import javafx.scene.web.WebView; | |
import javafx.stage.Stage; | |
import rx.subjects.BehaviorSubject; | |
import static java.util.concurrent.TimeUnit.*; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
TextArea textArea = new TextArea(); | |
WebView webView = new WebView(); | |
WebEngine engine = webView.getEngine(); | |
BehaviorSubject<String> textSubject = BehaviorSubject.create( "" ) ; | |
textArea.textProperty().addListener( ( control, oldValue, newValue ) -> textSubject.onNext( newValue ) ) ; | |
textSubject.sample( 500, MILLISECONDS ) | |
.distinctUntilChanged() | |
.subscribe( ( s ) -> Platform.runLater( () -> engine.loadContent( s ) ) ) ; | |
SplitPane root = new SplitPane(); | |
root.getItems().addAll(textArea, webView); | |
Scene scene = new Scene(root); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
I have written a short blogpost about this topic: http://www.guigarage.com/2014/03/reactive-programming-javafx/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you tried this code? Is "Listener removed" printed on
textAreaSub.unsubscribe()
?Let me give names to the intermediate Observables for the sake of the text below:
textAreaSub.unsubscribe()
clearly unsubscribes fromdistinct
. Does this causedistinct
to unsubscribe fromdebounced
, provided thattextAreaSub
was the only subscriber todistinct
? Does that in turn causedebounced
to unsubscribe fromtextObserver
, causing thetextListener
to be removed? I don't know the answer, but this is what ReactFX would do.