From 24af00f6a7f624feda4b6aaceb4c5b0fa6f536d6 Mon Sep 17 00:00:00 2001 From: Aylur Date: Thu, 22 Aug 2024 02:14:36 +0200 Subject: feat: async Process.exec the exec_async function was not truly async but a signal based one --- gjs/src/process.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'gjs/src') diff --git a/gjs/src/process.ts b/gjs/src/process.ts index 577ad99..c5329e2 100644 --- a/gjs/src/process.ts +++ b/gjs/src/process.ts @@ -1,4 +1,4 @@ -import { Astal, GLib } from "./imports.js" +import { Astal } from "./imports.js" type Args = { cmd: string | string[] @@ -24,7 +24,7 @@ export function subprocess( export function subprocess( argsOrCmd: Args | string | string[], onOut: (stdout: string) => void = print, - onErr: (stderr: string) => void = console.log, + onErr: (stderr: string) => void = printerr, ) { const { cmd, err, out } = args(argsOrCmd, onOut, onErr) const proc = Array.isArray(cmd) @@ -44,11 +44,26 @@ export function exec(cmd: string | string[]) { } export function execAsync(cmd: string | string[]): Promise { - const proc = Array.isArray(cmd) - ? Astal.Process.exec_asyncv(cmd) - : Astal.Process.exec_async(cmd) return new Promise((resolve, reject) => { - proc.connect("stdout", (_, out: string) => resolve(out)) - proc.connect("stderr", (_, err: string) => reject(err)) + if (Array.isArray(cmd)) { + Astal.Process.exec_asyncv(cmd, (_, res) => { + try { + resolve(Astal.Process.exec_asyncv_finish(res)) + } + catch (error) { + reject(error) + } + }) + } + else { + Astal.Process.exec_async(cmd, (_, res) => { + try { + resolve(Astal.Process.exec_finish(res)) + } + catch (error) { + reject(error) + } + }) + } }) } -- cgit v1.2.3