[gnome-shell-extensions/wip/fmuellner/misc-cleanups: 6/12] placesDisplay: Promisify launching



commit a378e5fc3416a9a301dfa1d76c3cb4f584a2d71a
Author: Florian Müllner <fmuellner gnome org>
Date:   Sun Mar 3 01:34:44 2019 +0100

    placesDisplay: Promisify launching
    
    Use async/await to make nested async operations easier to read.
    
    https://gitlab.gnome.org/GNOME/gnome-shell-extensions/merge_requests/60

 extensions/places-menu/placeDisplay.js | 87 +++++++++++++++++++---------------
 1 file changed, 49 insertions(+), 38 deletions(-)
---
diff --git a/extensions/places-menu/placeDisplay.js b/extensions/places-menu/placeDisplay.js
index 991dae8..e97cef4 100644
--- a/extensions/places-menu/placeDisplay.js
+++ b/extensions/places-menu/placeDisplay.js
@@ -38,51 +38,36 @@ class PlaceInfo {
         return false;
     }
 
-    _createLaunchCallback(launchContext, tryMount) {
-        return (_ignored, result) => {
+    async _ensureMountAndLaunch(context, tryMount) {
+        try {
+            await this._launchDefaultForUri(this.file.get_uri(), context, null);
+        } catch (e) {
+            if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_MOUNTED)) {
+                Main.notifyError(_('Failed to launch “%s”').format(this.name), e.message);
+                return;
+            }
+
+            let source = {
+                get_icon: () => this.icon
+            };
+            let op = new ShellMountOperation.ShellMountOperation(source);
             try {
-                Gio.AppInfo.launch_default_for_uri_finish(result);
-            } catch (e) {
-                if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_MOUNTED)) {
-                    Main.notifyError(_('Failed to launch “%s”').format(this.name), e.message);
-                    return;
-                }
+                await this._mountEnclosingVolume(0, op.mountOp, null);
 
-                let source = {
-                    get_icon: () => this.icon
-                };
-                let op = new ShellMountOperation.ShellMountOperation(source);
-                this.file.mount_enclosing_volume(0, op.mountOp, null, (file, result) => {
-                    try {
-                        op.close();
-                        file.mount_enclosing_volume_finish(result);
-                    } catch (e) {
-                        if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.FAILED_HANDLED))
-                            // e.g. user canceled the password dialog
-                            return;
-                        Main.notifyError(_('Failed to mount volume for “%s”').format(this.name), e.message);
-                        return;
-                    }
-
-                    if (tryMount) {
-                        let callback = this._createLaunchCallback(launchContext, false);
-                        Gio.AppInfo.launch_default_for_uri_async(file.get_uri(),
-                                                                 launchContext,
-                                                                 null,
-                                                                 callback);
-                    }
-                });
+                if (tryMount)
+                    this._ensureMountAndLaunch(context, false);
+            } catch (e) {
+                if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.FAILED_HANDLED))
+                    Main.notifyError(_('Failed to mount volume for “%s”').format(this.name), e.message);
+            } finally {
+                op.close();
             }
-        };
+        }
     }
 
     launch(timestamp) {
         let launchContext = global.create_app_launch_context(timestamp, -1);
-        let callback = this._createLaunchCallback(launchContext, true);
-        Gio.AppInfo.launch_default_for_uri_async(this.file.get_uri(),
-                                                 launchContext,
-                                                 null,
-                                                 callback);
+        this._ensureMountAndLaunch(launchContext, true);
     }
 
     getIcon() {
@@ -126,6 +111,32 @@ class PlaceInfo {
             throw e;
         }
     }
+
+    _launchDefaultForUri(uri, context, cancel) {
+        return new Promise((resolve, reject) => {
+            Gio.AppInfo.launch_default_for_uri_async(uri, context, cancel, (o, res) => {
+                try {
+                    Gio.AppInfo.launch_default_for_uri_finish(res);
+                    resolve();
+                } catch (e) {
+                    reject(e);
+                }
+            });
+        });
+    }
+
+    _mountEnclosingVolume(flags, mountOp, cancel) {
+        return new Promise((resolve, reject) => {
+            this.file.mount_enclosing_volume(flags, mountOp, cancel, (o, res) => {
+                try {
+                    this.file.mount_enclosing_volume_finish(res);
+                    resolve();
+                } catch (e) {
+                    reject(e);
+                }
+            });
+        });
+    }
 }
 Signals.addSignalMethods(PlaceInfo.prototype);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]