Skip to content

Instantly share code, notes, and snippets.

@bsorrentino
Forked from bduisenov/Rx.java
Created July 1, 2016 05:26
Show Gist options
  • Save bsorrentino/f678d415432a6f63503bbf8268c00cd8 to your computer and use it in GitHub Desktop.
Save bsorrentino/f678d415432a6f63503bbf8268c00cd8 to your computer and use it in GitHub Desktop.
gwt jsinterop for Rx.js
import jsinterop.annotations.JsFunction;
import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsType;
/**
* Created by bduisenov on 15/12/15.
*/
public class Rx {
@FunctionalInterface
@JsFunction
public interface onNextFunc<T> {
void apply(T val);
}
@FunctionalInterface
@JsFunction
public interface onErrorFunc<E> {
void apply(E error);
}
@FunctionalInterface
@JsFunction
public interface onCompletedFunc {
void apply();
}
@FunctionalInterface
@JsFunction
public interface Predicate<T> {
boolean apply(T input);
}
@FunctionalInterface
@JsFunction
public interface Function0<R> {
R apply();
}
@FunctionalInterface
@JsFunction
public interface Function<T, R> {
R apply(T val);
}
@FunctionalInterface
@JsFunction
public interface Function2<X, Y, R> {
R apply(X x, Y y);
}
@JsType(name = "Disposable", namespace = "Rx", isNative = true)
public interface Disposable {
// Performs the task of cleaning up resources
void dispose();
}
@JsType(name = "Observer", namespace = "Rx", isNative = true)
public interface Observer<T> {
Observer notifyOn(Object scheduler);
void onCompleted(); // Notifies the observer of the end of the sequence.
void onError(Object error); // Notifies the observer that an exception has occurred.
void onNext(T val); // Notifies the observer of a new element in the sequence.
// void subscribe(Object observer);
}
@JsType(name = "Observable", namespace = "Rx", isNative = true)
public interface Observable<T> {
@JsMethod(name = "return")
Observable<T> returnFunc();
Disposable subscribe(); // Subscribes an observer to the observable sequence.
Disposable subscribe(Observer observer);
Disposable subscribe(onNextFunc<T> onNext);
Disposable subscribe(onNextFunc<T> onNext, onErrorFunc<Object> onError);
Disposable subscribe(onNextFunc<T> onNext, onErrorFunc<Object> onError, onCompletedFunc onCompleted);
Observable<T> timeInterval();
Observable<T> take(int count);
Observable<T> filter(Predicate<T> func);
// combine items emitted by two Observables whenever an item from one Observable
// is emitted during a time window defined according to an item emitted by the other Observable.
<P, S> Observable<P> join(Observable<S> ys, Function0 func1, Function0 func2, Function2<T, S, P> func3);
// when an item is emitted by either of two Observables, combine the latest item
// emitted by each Observable via a specified function and emit items based on the results of this function
<P, S> Observable<P> combineLatest(Observable<S> ys, Function2<T, S, P> func3);
}
public native static Observable ObservableInterval(int interval) /*-{
return $wnd.Rx.Observable.interval(interval);
}-*/;
public native static Observable ObservableOf(int val) /*-{
return $wnd.Rx.Observable.of(val);
}-*/;
public native static Observable ObservableTimer(int val) /*-{
return $wnd.Rx.Observable.timer(val);
}-*/;
@JsType(name = "Subject", namespace = "Rx", isNative = true)
public interface Subject<T> extends Observer<T>, Observable<T> {
@Override
Observer notifyOn(Object scheduler);
@Override
void onCompleted();
@Override
void onError(Object error);
@Override
void onNext(T val);
}
public native static Subject Subject() /*-{
return new $wnd.Rx.Subject();
}-*/;
@JsType(name = "BehaviorSubject", namespace = "Rx", isNative = true)
public interface BehaviorSubject<T> extends Subject<T> {
T getValue();
}
public native static <T> BehaviorSubject<T> BehaviorSubject(T initialValue) /*-{
return new $wnd.Rx.BehaviorSubject(initialValue);
}-*/;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment