1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.gx.obe.component.rx;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Widget;
import com.gx.obe.bind.promise.Promise;
import com.gx.obe.bind.rx.RxBind;
import com.gx.obe.bind.rx.Tangent;
public class RxSwt<T> {
private final RxBind<T> rxBind;
private Tangent tangent = Tangent.TEMP;
private Supplier<Boolean> check = () -> false;
private Supplier<T> other = () -> null;
public static <T> RxSwt<T> run(Supplier<T> run) {
return new RxSwt<T>(run);
}
public RxSwt<T> setTangent(Tangent tangent) {
this.tangent = tangent;
return this;
}
private RxSwt(Supplier<T> run) {
rxBind = RxBind.run(run);
}
public RxSwt<T> checkWidget(Widget widget) {
if (widget != null) {
this.check = widget::isDisposed;
}
return this;
}
public RxSwt<T> other(Supplier<T> other) {
this.other = other;
return this;
}
public Promise<T> exe() {
return new Promise<>((s, f) -> exe(s));
}
public void exe(Consumer<T> consumer) {
rxBind.supplierSync(this::startThead).consumerSync(Display.getDefault()::asyncExec).tangent(tangent).check(check).other(other).exe(consumer);
}
private void startThead(Runnable runnable) {
new Thread(runnable).start();
}
}