added the concept of a process
This commit is contained in:
parent
655439461a
commit
747bde4aa6
8
src/kernel/ps/Process.hx
Normal file
8
src/kernel/ps/Process.hx
Normal 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;
|
||||
}
|
56
src/kernel/ps/ProcessHandle.hx
Normal file
56
src/kernel/ps/ProcessHandle.hx
Normal 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;
|
||||
}
|
||||
}
|
11
src/kernel/ps/ProcessManager.hx
Normal file
11
src/kernel/ps/ProcessManager.hx
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user