[gjs] Gio: Make _promisify to guess the finish function by default



commit f985fca54cd210063a196bc1a28f88601e3be583
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Thu Nov 11 15:54:54 2021 +0100

    Gio: Make _promisify to guess the finish function by default
    
    Most of the async finish functions have guessable names, so we can
    try to guess it without having to specify it manually.
    
    So make the finish argument optional.

 installed-tests/js/testGio.js | 5 +++++
 modules/core/overrides/Gio.js | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)
---
diff --git a/installed-tests/js/testGio.js b/installed-tests/js/testGio.js
index 4ad134c8..aa57e4f1 100644
--- a/installed-tests/js/testGio.js
+++ b/installed-tests/js/testGio.js
@@ -71,6 +71,11 @@ describe('Promisify function', function () {
     it("doesn't crash when finish function is not defined", function () {
         expect(() => Gio._promisify(Gio.Subprocess.prototype, 'communicate_utf8_async', 
'commuicate_utf8_finish')).toThrowError(/commuicate_utf8_finish/);
     });
+
+    it('can guess the finish function', function () {
+        expect(() => Gio._promisify(Gio._LocalFilePrototype, 'read_async')).not.toThrow();
+        expect(() => Gio._promisify(Gio.DBus, 'get')).not.toThrow();
+    });
 });
 
 describe('Gio.Settings overrides', function () {
diff --git a/modules/core/overrides/Gio.js b/modules/core/overrides/Gio.js
index 90b62777..be2e5247 100644
--- a/modules/core/overrides/Gio.js
+++ b/modules/core/overrides/Gio.js
@@ -403,7 +403,8 @@ function* _listModelIterator() {
         yield this.get_item(_index++);
 }
 
-function _promisify(proto, asyncFunc, finishFunc) {
+function _promisify(proto, asyncFunc,
+    finishFunc = `${asyncFunc.replace(/_(begin|async)$/, '')}_finish`) {
     if (proto[asyncFunc] === undefined)
         throw new Error(`${proto} has no method named ${asyncFunc}`);
 


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