added the concept of a process

This commit is contained in:
Djeeberjr 2023-04-14 00:18:21 +02:00
parent 655439461a
commit 747bde4aa6
3 changed files with 75 additions and 0 deletions

8
src/kernel/ps/Process.hx Normal file
View File

@ -0,0 +1,8 @@
package kernel.ps;
/**
Defines an independent process that can be run by the kernel.
**/
interface Process {
public function run(handle: ProcessHandle): Void;
}

View File

@ -0,0 +1,56 @@
package kernel.ps;
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 config:HandleConfig;
private final closeFuture: Future<Bool>;
private var closeFutureResolev: Bool -> Void;
@:allow(kernel.ps.ProcessManager)
private function new(config: HandleConfig) {
this.config = config;
this.closeFuture = new Future<Bool>((trigger)->{
this.closeFutureResolev = trigger;
return null;
});
if (this.config.onExit != null) {
this.closeFuture.handle(this.config.onExit);
}
}
public function onExit(): Future<Bool> {
return this.closeFuture;
}
public function close(success: Bool = true): Void {
this.dispose();
this.closeFutureResolev(success);
}
public function write(message: String): Void {
this.config.onWrite.invoke(message);
}
public function writeLine(message: String): Void {
this.write(message + "\n");
}
private function dispose() {
// TODO
}
function get_args():ReadOnlyArray<String> {
return this.config.args;
}
}

View File

@ -0,0 +1,11 @@
package kernel.ps;
import kernel.ps.ProcessHandle.HandleConfig;
class ProcessManager {
public static function run(process:Process, config: HandleConfig):ProcessHandle {
var handle = new ProcessHandle(config);
process.run(handle);
return handle;
}
}