package speiger.src.coreengine.utils.tasks;

import java.util.concurrent.Callable;
import java.util.function.Consumer;

public class ListenableTask<T> implements Runnable
{
	Callable<T> call;
	Consumer<T> listener;
	boolean canceled = false;
	
	public ListenableTask(Callable<T> call, Consumer<T> listener) {
		this.call = call;
		this.listener = listener;
	}
	
	@Override
	public void run() {
		if(!canceled) {
			try {
				T value = call.call();
				if(value == null) return;
				listener.accept(value);
			}
			catch(Exception e) { }
		}
	}
	
	public void cancel() { canceled = true; }
}
