[gjs: 2/4] esm/gi: Use __version__ property to check for conflicts




commit 66b4e36210759a9a438f8b676bbea1caa26a98b7
Author: Florian Müllner <fmuellner gnome org>
Date:   Tue Aug 3 23:38:21 2021 +0200

    esm/gi: Use __version__ property to check for conflicts
    
    Currently the gi.versions object is not only used for specifying
    which version to load, but also to catch repeated require() calls
    with different versions.
    
    The latter prevents falling back to a different version after the
    original require() call fails, i.e. a pattern like the following:
    
    ```
    let Soup;
    try {
        Soup = gi.require('Soup', '3.0');
    } catch (e) {
        Soup = gi.require('Soup', '2.4');
    }
    ```
    
    Address this by using the newly added __version__ property for
    the version check, which reflects the actually loaded version
    instead of the argument of a previous require() call.
    
    It is also more reliable, as it works regardless of whether the
    version was specified with the successful require() call:
    
    ```
    const Gtk4 = gi.require('Gtk');
    const Gtk3 = gi.require('Gtk', '3.0');
    ``
    
    now throws an exception, while before both Gtk4 and Gtk3 were
    resolved to the Gtk namespace (version 4.0).

 installed-tests/js/testESModules.js |  4 ++++
 modules/esm/gi.js                   | 15 ++++++++-------
 2 files changed, 12 insertions(+), 7 deletions(-)
---
diff --git a/installed-tests/js/testESModules.js b/installed-tests/js/testESModules.js
index 5f435154..30db17e4 100644
--- a/installed-tests/js/testESModules.js
+++ b/installed-tests/js/testESModules.js
@@ -37,6 +37,10 @@ describe('ES module imports', function () {
         expect(() => gi.require('Gtk', '1.75')).toThrow();
     });
 
+    it('import with another version after a failed import', function () {
+        expect(gi.require('Gtk', '3.0').toString()).toEqual('[object GIRepositoryNamespace]');
+    });
+
     it('import nonexistent module', function () {
         expect(() => gi.require('PLib')).toThrow();
     });
diff --git a/modules/esm/gi.js b/modules/esm/gi.js
index 3b2a5c4c..126ce7e7 100644
--- a/modules/esm/gi.js
+++ b/modules/esm/gi.js
@@ -8,16 +8,17 @@ const Gi = {
         if (namespace === 'versions')
             throw new Error('Cannot import namespace "versions", use the version parameter of Gi.require to 
specify versions.');
 
-        if (version !== undefined) {
-            const alreadyLoadedVersion = gi.versions[namespace];
-            if (alreadyLoadedVersion !== undefined && version !== alreadyLoadedVersion) {
-                throw new Error(`Version ${alreadyLoadedVersion} of GI module ${
-                    namespace} already loaded, cannot load version ${version}`);
-            }
+        if (version !== undefined)
             gi.versions[namespace] = version;
+
+        const module = gi[namespace];
+
+        if (version !== undefined && version !== module.__version__) {
+            throw new Error(`Version ${module.__version__} of GI module ${
+                namespace} already loaded, cannot load version ${version}`);
         }
 
-        return gi[namespace];
+        return module;
     },
 };
 Object.freeze(Gi);


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