summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Chimento <philip.chimento@gmail.com>2018-08-19 21:51:12 -0400
committerPhilip Chimento <philip.chimento@gmail.com>2018-08-19 21:51:12 -0400
commit3276e0c1b4cfce44d3d69dc02dd4285e9a9bd733 (patch)
tree7bebbf640efee99fae7f120036ba34f97ba604c7
parent06aae445ef4c538c1fd2515a78a506a668d45135 (diff)
parentdecae6d1c98c0b073eed23fe4b7b0481a127d9b2 (diff)
downloadgjs-3276e0c1b4cfce44d3d69dc02dd4285e9a9bd733.tar.gz
Merge branch 'llzes/gjs-wip/overrides-gio-promisify'
-rw-r--r--modules/overrides/Gio.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/modules/overrides/Gio.js b/modules/overrides/Gio.js
index 0092b99b..7e0a9ce2 100644
--- a/modules/overrides/Gio.js
+++ b/modules/overrides/Gio.js
@@ -343,6 +343,31 @@ function* _listModelIterator() {
}
}
+function _promisify(proto, asyncFunc, finishFunc) {
+ proto[`_original_${asyncFunc}`] = proto[asyncFunc];
+ proto[asyncFunc] = function(...args) {
+ if (!args.every(arg => typeof arg !== 'function'))
+ return this[`_original_${asyncFunc}`](...args);
+ return new Promise((resolve, reject) => {
+ const callStack = new Error().stack.split('\n').filter(line => !line.match(/promisify/)).join('\n');
+ this[`_original_${asyncFunc}`](...args, function(source, res) {
+ try {
+ const result = source[finishFunc](res);
+ if (Array.isArray(result) && result.length > 1 && result[0] === true)
+ result.shift();
+ resolve(result);
+ } catch (error) {
+ if (error.stack)
+ error.stack += `### Promise created here: ###\n${callStack}`;
+ else
+ error.stack = callStack;
+ reject(error);
+ }
+ });
+ });
+ };
+}
+
function _init() {
Gio = this;
@@ -402,6 +427,9 @@ function _init() {
// ListStore
Gio.ListStore.prototype[Symbol.iterator] = _listModelIterator;
+ // Promisify
+ Gio._promisify = _promisify;
+
// Temporary Gio.File.prototype fix
Gio._LocalFilePrototype = Gio.File.new_for_path('').constructor.prototype;
}