From 7eafc248cd272637d9dc1c19061af0301a639fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Wed, 12 Jul 2023 21:40:05 +0200 Subject: [PATCH] lookingGlass: Evaluate command asynchronously This allows using await in the command (or the header we add to it), for example when handling Promises or importing a module dynamically. The latter will be crucial when porting to ESM. Part-of: --- js/ui/lookingGlass.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/js/ui/lookingGlass.js b/js/ui/lookingGlass.js index ac618200c..7feb10391 100644 --- a/js/ui/lookingGlass.js +++ b/js/ui/lookingGlass.js @@ -28,7 +28,7 @@ const CHEVRON = '>>> '; /* Imports...feel free to add here as needed */ const commandHeader = ` const {Clutter, Gio, GLib, GObject, Meta, Shell, St} = imports.gi; - const Main = imports.ui.main; + const Main = await imports.ui.main; /* Utility functions...we should probably be able to use these * in the shell core code too. */ @@ -39,6 +39,7 @@ const commandHeader = ` const it = Main.lookingGlass.getIt(); const r = Main.lookingGlass.getResult.bind(Main.lookingGlass); `; +const AsyncFunction = async function () {}.constructor; const HISTORY_KEY = 'looking-glass-history'; // Time between tabs for them to count as a double-tab event @@ -1412,7 +1413,7 @@ class LookingGlass extends St.BoxLayout { // Ensure we don't get newlines in the command; the history file is // newline-separated. text = text.replace('\n', ' '); - this._evaluate(text); + this._evaluate(text).catch(logError); return true; }); @@ -1528,7 +1529,7 @@ class LookingGlass extends St.BoxLayout { } } - _evaluate(command) { + async _evaluate(command) { command = this._history.addItem(command); // trims command if (!command) return; @@ -1540,7 +1541,7 @@ class LookingGlass extends St.BoxLayout { let resultObj; try { - resultObj = Function(fullCmd)(); + resultObj = await AsyncFunction(fullCmd)(); } catch (e) { resultObj = ``; }