[polari] thumbnailer: Promisify snapshot creation



commit f1237d660ae5538a30d1c66b54bc3707dd8f5f6c
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Dec 12 05:51:18 2019 +0100

    thumbnailer: Promisify snapshot creation
    
    We are about to add more complexity there, using promises will help
    with keeping the code readable.
    
    https://gitlab.gnome.org/GNOME/polari/merge_requests/140

 src/thumbnailer.js | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
---
diff --git a/src/thumbnailer.js b/src/thumbnailer.js
index 6cce789..2af5ac9 100644
--- a/src/thumbnailer.js
+++ b/src/thumbnailer.js
@@ -1,8 +1,10 @@
 imports.gi.versions.Gtk = '3.0';
 
-const { GLib, GObject, Gtk, WebKit2 } = imports.gi;
+const { Gio, GLib, GObject, Gtk, WebKit2 } = imports.gi;
 const Cairo = imports.cairo;
 
+Gio._promisify(WebKit2.WebView.prototype, 'get_snapshot', 'get_snapshot_finish');
+
 const PREVIEW_WIDTH = 120;
 const PREVIEW_HEIGHT = 90;
 
@@ -57,21 +59,21 @@ let PreviewWindow = GObject.registerClass({
         });
     }
 
-    _createSnapshot() {
-        this._view.get_snapshot(
+    async _createSnapshot() {
+        let snapshotOp = this._view.get_snapshot(
             WebKit2.SnapshotRegion.VISIBLE,
-            WebKit2.SnapshotOptions.TRANSPARENT_BACKGROUND,
-            null,
-            (o, res) => {
-                try {
-                    this._snapshot = this._view.get_snapshot_finish(res);
-                } catch (e) {
-                    log(`Creating snapshot failed: ${e}`);
-                    this.emit('snapshot-failed');
-                    return;
-                }
-                this.emit('snapshot-ready');
-            });
+            WebKit2.SnapshotOptions.NONE,
+            null);
+
+        try {
+            this._snapshot = await snapshotOp;
+        } catch (e) {
+            log(`Creating snapshot failed: ${e}`);
+            this.emit('snapshot-failed');
+            return;
+        }
+
+        this.emit('snapshot-ready');
     }
 
     getSnapshot() {


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