scripting: Switch to standard async/await pattern

The original scripting framework was based on SpiderMonkey's
pre-standard generators, and was simply translated to the
corresponding standard syntax when updating it to work with
recent JS versions.

We can do even better by using the standard async/await pattern
instead of generators/yield.

https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1396
This commit is contained in:
Florian Müllner
2020-07-30 18:41:11 +02:00
committed by Georges Basile Stavracas Neto
parent 33ff3dc44f
commit a436226266
3 changed files with 52 additions and 53 deletions

View File

@ -21,16 +21,13 @@ const { loadInterfaceXML } = imports.misc.fileUtils;
// When scripting an automated test we want to make a series of calls
// in a linear fashion, but we also want to be able to let the main
// loop run so actions can finish. For this reason we write the script
// as a generator function that yields when it want to let the main
// as an async function that uses await when it wants to let the main
// loop run.
//
// yield Scripting.sleep(1000);
// await Scripting.sleep(1000);
// main.overview.show();
// yield Scripting.waitLeisure();
// await Scripting.waitLeisure();
//
// While it isn't important to the person writing the script, the actual
// yielded result is a function that the caller uses to provide the
// callback for resuming the script.
/**
* sleep:
@ -285,13 +282,11 @@ function _collect(scriptModule, outputFile) {
}
async function _runPerfScript(scriptModule, outputFile) {
for (let step of scriptModule.run()) {
try {
await step; // eslint-disable-line no-await-in-loop
} catch (err) {
log(`Script failed: ${err}\n${err.stack}`);
Meta.exit(Meta.ExitCode.ERROR);
}
try {
await scriptModule.run();
} catch (err) {
log(`Script failed: ${err}\n${err.stack}`);
Meta.exit(Meta.ExitCode.ERROR);
}
try {