opt: use ksu.spawn for long script

This commit is contained in:
KOWX712
2025-05-05 18:57:24 +08:00
parent 59a74e8ee2
commit 8c4f7c0e5c
8 changed files with 248 additions and 206 deletions

View File

@@ -41,6 +41,27 @@ export function exec(command, options = {}) {
});
}
/**
* Standard I/O stream for a child process.
* @class
*/
class Stdio {
constructor() {
this.listeners = {};
}
on(event, listener) {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push(listener);
}
emit(event, ...args) {
if (this.listeners[event]) {
this.listeners[event].forEach(listener => listener(...args));
}
}
}
/**
* Spawn shell process with ksu.spawn
* @param {string} command - The command to execute
@@ -71,20 +92,6 @@ export function spawn(command, args = [], options = {}) {
}
}
};
function Stdio() {
this.listeners = {};
}
Stdio.prototype.on = function(event, listener) {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push(listener);
};
Stdio.prototype.emit = function(event, ...args) {
if (this.listeners[event]) {
this.listeners[event].forEach(listener => listener(...args));
}
};
const callbackName = getUniqueCallbackName("spawn");
window[callbackName] = child;
child.on("exit", () => delete window[callbackName]);