File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed
test/java/rx/javafx/sources Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ package rx .javafx .sources ;
2+
3+ import javafx .animation .Animation ;
4+ import javafx .animation .KeyFrame ;
5+ import javafx .animation .Timeline ;
6+ import javafx .util .Duration ;
7+ import rx .Observable ;
8+ import rx .subscriptions .JavaFxSubscriptions ;
9+
10+ import java .util .concurrent .atomic .AtomicLong ;
11+
12+ public final class TimerSource {
13+ private TimerSource () {
14+ }
15+
16+
17+ public static <T > Observable <Long > interval (final Duration duration ) {
18+ return Observable .create (sub -> {
19+ final AtomicLong value = new AtomicLong (0 );
20+ Timeline timeline = new Timeline (new KeyFrame (duration , ae -> sub .onNext (value .getAndIncrement ())));
21+ timeline .setCycleCount (Animation .INDEFINITE );
22+ timeline .play ();
23+
24+ sub .add (JavaFxSubscriptions .unsubscribeInEventDispatchThread (timeline ::stop ));
25+ });
26+ }
27+ }
Original file line number Diff line number Diff line change 1616package rx .observables ;
1717
1818
19+ import javafx .animation .Animation ;
20+ import javafx .animation .KeyFrame ;
21+ import javafx .animation .Timeline ;
1922import javafx .beans .value .ObservableValue ;
2023import javafx .collections .ObservableList ;
2124import javafx .collections .ObservableMap ;
2932import javafx .scene .control .MenuItem ;
3033import javafx .stage .Window ;
3134import javafx .stage .WindowEvent ;
35+ import javafx .util .Duration ;
3236import rx .Observable ;
3337import rx .functions .Func1 ;
38+ import java .util .concurrent .atomic .AtomicLong ;
3439import rx .javafx .sources .*;
40+ import rx .subscriptions .JavaFxSubscriptions ;
3541
3642import java .util .Map ;
3743
@@ -281,4 +287,11 @@ public static <T> Observable<T> fromObservableSetRemovals(final ObservableSet<T>
281287 public static <T > Observable <SetChange <T >> fromObservableSetChanges (final ObservableSet <T > source ) {
282288 return ObservableSetSource .fromObservableSetChanges (source );
283289 }
290+
291+ /**
292+ * Returns an Observable that emits a 0L and ever increasing numbers after each duration of time thereafter
293+ */
294+ public static <T > Observable <Long > interval (final Duration duration ) {
295+ return TimerSource .interval (duration );
296+ }
284297}
Original file line number Diff line number Diff line change 2222import javafx .collections .FXCollections ;
2323import javafx .collections .ObservableList ;
2424import javafx .embed .swing .JFXPanel ;
25+ import javafx .util .Duration ;
2526import org .junit .Test ;
2627import rx .Observable ;
2728import rx .observables .JavaFxObservable ;
3536
3637public final class JavaFxObservableTest {
3738
39+ @ Test
40+ public void testIntervalSource () {
41+ new JFXPanel ();
42+
43+ final CountDownLatch latch = new CountDownLatch (5 );
44+
45+ JavaFxObservable .interval (Duration .millis (1000 )).take (5 )
46+ .subscribe (v -> latch .countDown ());
47+
48+ try {
49+ latch .await ();
50+ } catch (Exception e ) {
51+ throw new RuntimeException (e );
52+ }
53+ }
54+
3855 @ Test
3956 public void testRxObservableListAdds () {
4057 new JFXPanel ();
You can’t perform that action at this time.
0 commit comments