Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
commandLineUtil: refactor to use GSubprocess
This means doing all IO in a background thread, rather than popping
lines synchronously off a pipe.
  • Loading branch information
andyholmes committed Apr 26, 2021
commit e37b6c6c6e9809596f2ed0bcd4c9e575dd20cd40
49 changes: 15 additions & 34 deletions freon@UshakovVasilii_Github.yahoo.com/commandLineUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,23 @@ var CommandLineUtil = class {
execute(callback) {
try{
this._callback = callback;
let [exit, pid, stdinFd, stdoutFd, stderrFd] =
GLib.spawn_async_with_pipes(null, /* cwd */
this._argv, /* args */
null, /* env */
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null /* child_setup */);
let stdout = new Gio.UnixInputStream({fd: stdoutFd, close_fd: true});
let outReader = new Gio.DataInputStream({base_stream: stdout});

let stderr = new Gio.UnixInputStream({fd: stderrFd, close_fd: true});
let errReader = new Gio.DataInputStream({base_stream: stderr});

GLib.close(stdinFd);

let childWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, (pid, status, requestObj) => {
let output = [];
let error_output = [];
let [line, size] = [null, 0];

while (([line, size] = outReader.read_line(null)) != null && line != null) {
if(line)
output.push(ByteArray.toString(line));
let proc = Gio.Subprocess.new(this._argv,
Gio.SubprocessFlags.STDOUT_PIPE |
Gio.SubprocessFlags.STDERR_PIPE);

proc.communicate_utf8_async(null, null, (proc, result) => {
try {
let [, stdout, stderr] = proc.communicate_utf8_finish(result);

this._output = stdout ? stdout.split('\n') : [];
this._error_output = stderr ? stderr.split('\n') : [];
} catch (e) {
logError(e);
} finally {
this._updated = true;
callback();
}
stdout.close(null);

while (([line, size] = errReader.read_line(null)) != null && line != null) {
if(line)
error_output.push(ByteArray.toString(line));
}
stderr.close(null);

GLib.source_remove(childWatch);
this._output = output;
this._error_output = error_output;
this._updated = true;
callback();
});
} catch(e){
global.log(e.toString());
Expand Down