diff options
author | Philip Chimento <philip.chimento@gmail.com> | 2023-02-21 06:11:46 +0000 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2023-02-21 06:11:46 +0000 |
commit | 7544371db1910c2b8525edf90cd25af2985d6e95 (patch) | |
tree | cc1d382e40602b7dc19c26b2de847f5dfaf0788e | |
parent | 0a28d991e53944e495b564133fc78a3a95d70085 (diff) | |
parent | 986e850e3121eddbf474e9c56ecbaa8699e1a1dd (diff) | |
download | gjs-7544371db1910c2b8525edf90cd25af2985d6e95.tar.gz |
Merge branch 'proxy-init-promise' into 'master'
Gio: Add support for initializing a DBus Proxy via a promise
Closes #494
See merge request GNOME/gjs!794
-rw-r--r-- | installed-tests/js/testGDBus.js | 22 | ||||
-rw-r--r-- | modules/core/overrides/Gio.js | 18 |
2 files changed, 39 insertions, 1 deletions
diff --git a/installed-tests/js/testGDBus.js b/installed-tests/js/testGDBus.js index fd1bb15b..25de96b6 100644 --- a/installed-tests/js/testGDBus.js +++ b/installed-tests/js/testGDBus.js @@ -342,6 +342,14 @@ describe('Exported DBus object', function () { expect(hello.deepUnpack()).toEqual('world'); }); + it('can initiate a proxy with promise and call a method with async/await', async function () { + const asyncProxy = await ProxyClass.newAsync(Gio.DBus.session, + 'org.gnome.gjs.Test', '/org/gnome/gjs/Test'); + expect(asyncProxy).toBeInstanceOf(Gio.DBusProxy); + const [{hello}] = await asyncProxy.frobateStuffAsync({}); + expect(hello.deepUnpack()).toEqual('world'); + }); + it('can call a remote method when not using makeProxyWrapper', function () { let info = Gio.DBusNodeInfo.new_for_xml(TestIface); let iface = info.interfaces[0]; @@ -918,6 +926,20 @@ describe('DBus Proxy wrapper', function () { expect(writerFunc).not.toHaveBeenCalled(); }); + it('can create a proxy from a promise', async function () { + const proxyPromise = ProxyClass.newAsync(Gio.DBus.session, 'org.gnome.gjs.Test', + '/org/gnome/gjs/Test'); + await expectAsync(proxyPromise).toBeResolved(); + }); + + it('can create fail a proxy from a promise', async function () { + const cancellable = new Gio.Cancellable(); + cancellable.cancel(); + const proxyPromise = ProxyClass.newAsync(Gio.DBus.session, 'org.gnome.gjs.Test', + '/org/gnome/gjs/Test', cancellable); + await expectAsync(proxyPromise).toBeRejected(); + }); + afterAll(function () { if (!wasPromise) { // Remove stuff added by Gio._promisify, this can be not needed in future diff --git a/modules/core/overrides/Gio.js b/modules/core/overrides/Gio.js index 66f36d96..85fcde98 100644 --- a/modules/core/overrides/Gio.js +++ b/modules/core/overrides/Gio.js @@ -233,7 +233,7 @@ function _addDBusConvenience() { function _makeProxyWrapper(interfaceXml) { var info = _newInterfaceInfo(interfaceXml); var iname = info.name; - return function (bus, name, object, asyncCallback, cancellable, + function wrapper(bus, name, object, asyncCallback, cancellable, flags = Gio.DBusProxyFlags.NONE) { var obj = new Gio.DBusProxy({ g_connection: bus, @@ -253,7 +253,23 @@ function _makeProxyWrapper(interfaceXml) { obj.init(cancellable); } return obj; + } + wrapper.newAsync = function newAsync(bus, name, object, cancellable, + flags = Gio.DBusProxyFlags.NONE) { + const obj = new Gio.DBusProxy({ + g_connection: bus, + g_interface_name: info.name, + g_interface_info: info, + g_name: name, + g_flags: flags, + g_object_path: object, + }); + + return new Promise((resolve, reject) => + obj.init_async(GLib.PRIORITY_DEFAULT, cancellable ?? null).then( + () => resolve(obj)).catch(reject)); }; + return wrapper; } |