cc-haxe/src/kernel/ps/ProcessHandle.hx

83 lines
1.9 KiB
Haxe

package kernel.ps;
import kernel.ps.ProcessManager.PID;
import kernel.ui.WindowContext;
import kernel.ui.WindowManager;
import haxe.ds.ReadOnlyArray;
using tink.CoreApi;
typedef HandleConfig = {
?args: Array<String>,
?onWrite: Callback<String>,
?onExit: Callback<Bool>,
}
class ProcessHandle {
public var args(get,null): ReadOnlyArray<String>;
private final pid: PID;
private final config:HandleConfig;
private final closeFuture: Future<Bool>;
private var closeFutureResolev: Bool -> Void;
private final windowContexts: Array<WindowContext> = [];
@:allow(kernel.ps.ProcessManager)
private function new(config: HandleConfig,pid: PID) {
this.config = config;
this.pid = pid;
this.closeFuture = new Future<Bool>((trigger)->{
this.closeFutureResolev = trigger;
return null;
});
if (this.config.onExit != null) {
this.closeFuture.handle(this.config.onExit);
}
this.closeFuture.eager();
}
public function onExit(): Future<Bool> {
return this.closeFuture;
}
public function close(success: Bool = true): Void {
this.dispose();
this.closeFutureResolev(success);
ProcessManager.removeProcess(this.pid);
}
public function write(message: String): Void {
this.config.onWrite.invoke(message);
}
public function writeLine(message: String): Void {
this.write(message + "\n");
}
public function createBufferdWindowContext(): WindowContext {
var ctx = WindowManager.instance.createNewContext();
this.windowContexts.push(ctx);
return ctx;
}
public function createStatelessWindowContext(): {ctx:WindowContext, setRenderFunc: (() -> Void) -> Void, requestRender:() -> Void} {
var ctx = WindowManager.instance.createNewStatelessContext();
this.windowContexts.push(ctx.ctx);
return ctx;
}
public function getWindowContexts(): ReadOnlyArray<WindowContext> {
return this.windowContexts;
}
private function dispose() {
// TODO
}
function get_args():ReadOnlyArray<String> {
return this.config.args;
}
}