Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor running shell command async #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 14 additions & 13 deletions [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2157,19 +2157,7 @@ const Gpu = class SystemMonitor_Gpu extends ElementBase {
}
}
refresh() {
// Run asynchronously, to avoid shell freeze
try {
let path = this.extension.path;
let script = ['/bin/bash', path + '/gpu_usage.sh'];

// Create subprocess and capture STDOUT
let proc = new Gio.Subprocess({argv: script, flags: Gio.SubprocessFlags.STDOUT_PIPE});
proc.init(null);
// Asynchronously call the output handler when script output is ready
proc.communicate_utf8_async(null, null, this._handleOutput.bind(this));
} catch (err) {
console.error(err.message);
}
runCommandAndHandleOutputAsync(['/bin/bash', Me.dir.get_path() + '/gpu_usage.sh'], this._handleOutput.bind(this))
}
_handleOutput(proc, result) {
let [ok, output, ] = proc.communicate_utf8_finish(result);
Expand Down Expand Up @@ -2289,6 +2277,19 @@ const Gpu = class SystemMonitor_Gpu extends ElementBase {
}
}

// For running a command asynchronously in refresh() to avoid freezing Gnome Shell
const runCommandAndHandleOutputAsync = (commandAsArray, handleOutputFn) => {
try {
// Create subprocess and capture STDOUT
let proc = new Gio.Subprocess({argv: commandAsArray, flags: Gio.SubprocessFlags.STDOUT_PIPE});
proc.init(null);
// Asynchronously call the output handler when command output is ready
proc.communicate_utf8_async(null, null, Lang.bind(this, handleOutputFn));
} catch (err) {
global.logError(err.message);
}
}

const Icon = class SystemMonitor_Icon {
constructor(extension) {
this.extension = extension;
Expand Down