RxSwt.java 1.27 KB
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();
	}
	
}