diff options
author | Aylur <[email protected]> | 2024-08-22 02:14:36 +0200 |
---|---|---|
committer | Aylur <[email protected]> | 2024-08-22 02:14:50 +0200 |
commit | 24af00f6a7f624feda4b6aaceb4c5b0fa6f536d6 (patch) | |
tree | 11fdac0f3031a991b32b12ff76f5f96adaf4848d /src | |
parent | 6dd5200fa30c48e1cd9a2f820c62c54b0974c925 (diff) |
feat: async Process.exec
the exec_async function was not truly async but a signal based one
Diffstat (limited to 'src')
-rw-r--r-- | src/process.vala | 32 | ||||
-rw-r--r-- | src/variable.vala | 28 |
2 files changed, 30 insertions, 30 deletions
diff --git a/src/process.vala b/src/process.vala index 86e3ff6..073fe93 100644 --- a/src/process.vala +++ b/src/process.vala @@ -94,32 +94,26 @@ public class Astal.Process : Object { return Process.execv(argv); } - public Process.exec_asyncv(string[] cmd) throws Error { - Object(argv: cmd); - process = new Subprocess.newv(cmd, + public static async string exec_asyncv(string[] cmd) throws Error { + var process = new Subprocess.newv( + cmd, SubprocessFlags.STDERR_PIPE | SubprocessFlags.STDOUT_PIPE ); - process.communicate_utf8_async.begin(null, null, (_, res) => { - string err_str, out_str; - try { - process.communicate_utf8_async.end(res, out out_str, out err_str); - if (process.get_successful()) - stdout(out_str.strip()); - else - stderr(err_str.strip()); - } catch (Error err) { - printerr("%s\n", err.message); - } finally { - dispose(); - } - }); + string err_str, out_str; + yield process.communicate_utf8_async(null, null, out out_str, out err_str); + var success = process.get_successful(); + process.dispose(); + if (success) + return out_str.strip(); + else + throw new IOError.FAILED(err_str.strip()); } - public static Process exec_async(string cmd) throws Error { + public static async string exec_async(string cmd) throws Error { string[] argv; Shell.parse_argv(cmd, out argv); - return new Process.exec_asyncv(argv); + return yield exec_asyncv(argv); } } diff --git a/src/variable.vala b/src/variable.vala index e1d6414..c7edb16 100644 --- a/src/variable.vala +++ b/src/variable.vala @@ -141,20 +141,26 @@ public class Variable : VariableBase { }, Priority.DEFAULT); } if (poll_exec != null) { - var proc = new Process.exec_asyncv(poll_exec); - proc.stdout.connect((str) => set_closure(str, poll_transform)); - proc.stderr.connect((str) => this.error(str)); - poll_id = Timeout.add(poll_interval, () => { + Process.exec_asyncv.begin(poll_exec, (_, res) => { try { - proc = new Process.exec_asyncv(poll_exec); - proc.stdout.connect((str) => set_closure(str, poll_transform)); - proc.stderr.connect((str) => this.error(str)); - return Source.CONTINUE; + var str = Process.exec_asyncv.end(res); + set_closure(str, poll_transform); } catch (Error err) { - printerr("%s\n", err.message); - poll_id = 0; - return Source.REMOVE; + this.error(err.message); } + }); + poll_id = Timeout.add(poll_interval, () => { + Process.exec_asyncv.begin(poll_exec, (_, res) => { + try { + var str = Process.exec_asyncv.end(res); + set_closure(str, poll_transform); + } catch (Error err) { + this.error(err.message); + Source.remove(poll_id); + poll_id = 0; + } + }); + return Source.CONTINUE; }, Priority.DEFAULT); } } |