diff options
author | Marco Trevisan (TreviƱo) <mail@3v1n0.net> | 2022-08-10 20:09:43 +0200 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2022-10-29 15:53:13 -0700 |
commit | 3eb2bf8e628141caabd3f05b119038cb4bf69662 (patch) | |
tree | ca89eb07a91d8b4984c3ec426360ee1ccc250051 | |
parent | 904050e98ceaff0d1807ab852d223952f964a0c9 (diff) | |
download | gjs-3eb2bf8e628141caabd3f05b119038cb4bf69662.tar.gz |
Gio: Promisify GDBusProxy.init_async by default
Some code may promisify it after that it has been overridden, this means
that when called we may not use the original method correctly as the
function won't have the arguments set anymore and thus its length will
be 0, causing the assumption included in commit 58840261 not to be true
anymore.
Since the function is already a wrapper, we can safely override it to be
a promise and then override it again. Allowing code simplifications and
being more future-proof, when all the async functions will be promises
by default.
Fixes: #494
-rw-r--r-- | installed-tests/js/testGDBus.js | 51 | ||||
-rw-r--r-- | modules/core/overrides/Gio.js | 1 |
2 files changed, 52 insertions, 0 deletions
diff --git a/installed-tests/js/testGDBus.js b/installed-tests/js/testGDBus.js index 70418e44..dbbefd9f 100644 --- a/installed-tests/js/testGDBus.js +++ b/installed-tests/js/testGDBus.js @@ -837,3 +837,54 @@ describe('Exported DBus object', function () { expect(invalidatedProps).toContain('PropReadOnly'); }); }); + + +describe('DBus Proxy wrapper', function () { + let loop; + let wasPromise; + let writerFunc; + + beforeAll(function () { + loop = new GLib.MainLoop(null, false); + + wasPromise = Gio.DBusProxy.prototype._original_init_async instanceof Function; + Gio._promisify(Gio.DBusProxy.prototype, 'init_async'); + + writerFunc = jasmine.createSpy( + 'log writer func', (level, fields) => { + const decoder = new TextDecoder('utf-8'); + const domain = decoder.decode(fields?.GLIB_DOMAIN); + const message = `${decoder.decode(fields?.MESSAGE)}\n`; + level |= GLib.LogLevelFlags.FLAG_RECURSION; + GLib.log_default_handler(domain, level, message, null); + return GLib.LogWriterOutput.HANDLED; + }); + + writerFunc.and.callThrough(); + GLib.log_set_writer_func(writerFunc); + }); + + beforeEach(function () { + writerFunc.calls.reset(); + }); + + it('can init a proxy asynchronously when promisified', function () { + new ProxyClass(Gio.DBus.session, 'org.gnome.gjs.Test', + '/org/gnome/gjs/Test', + () => loop.quit(), + Gio.DBusProxyFlags.NONE); + loop.run(); + + expect(writerFunc).not.toHaveBeenCalled(); + }); + + afterAll(function () { + if (!wasPromise) { + // Remove stuff added by Gio._promisify, this can be not needed in future + // nor should break other tests, but we didn't want to depend on those. + expect(Gio.DBusProxy.prototype._original_init_async).toBeInstanceOf(Function); + Gio.DBusProxy.prototype.init_async = Gio.DBusProxy.prototype._original_init_async; + delete Gio.DBusProxy.prototype._original_init_async; + } + }); +}); diff --git a/modules/core/overrides/Gio.js b/modules/core/overrides/Gio.js index 37df69d9..1fab4796 100644 --- a/modules/core/overrides/Gio.js +++ b/modules/core/overrides/Gio.js @@ -517,6 +517,7 @@ function _init() { }; _injectToMethod(Gio.DBusProxy.prototype, 'init', _addDBusConvenience); + _promisify(Gio.DBusProxy.prototype, 'init_async'); _injectToMethod(Gio.DBusProxy.prototype, 'init_async', _addDBusConvenience); _injectToStaticMethod(Gio.DBusProxy, 'new_sync', _addDBusConvenience); _injectToStaticMethod(Gio.DBusProxy, 'new_finish', _addDBusConvenience); |