You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by sh...@apache.org on 2021/05/06 12:25:31 UTC

[echarts] branch enhance-visual-regression-test updated: test: loader fix dependencies

This is an automated email from the ASF dual-hosted git repository.

shenyi pushed a commit to branch enhance-visual-regression-test
in repository https://gitbox.apache.org/repos/asf/echarts.git


The following commit(s) were added to refs/heads/enhance-visual-regression-test by this push:
     new c2c59c2  test: loader fix dependencies
c2c59c2 is described below

commit c2c59c2ea5e713dd80089a61681ac03b53780bcd
Author: pissang <bm...@gmail.com>
AuthorDate: Thu May 6 20:24:19 2021 +0800

    test: loader fix dependencies
---
 test/lib/config.js        |   2 +-
 test/lib/simpleRequire.js | 170 ++++++++++++++++------------------------------
 2 files changed, 58 insertions(+), 114 deletions(-)

diff --git a/test/lib/config.js b/test/lib/config.js
index 73fc71a..a4a0f37 100644
--- a/test/lib/config.js
+++ b/test/lib/config.js
@@ -65,7 +65,7 @@
                 'theme': 'theme',
                 'data': 'data',
                 'map': 'data/map',
-                'extension': 'dist/extension',
+                'extension': '../dist/extension',
             }
         });
     }
diff --git a/test/lib/simpleRequire.js b/test/lib/simpleRequire.js
index 0c33cbc..c88b181 100644
--- a/test/lib/simpleRequire.js
+++ b/test/lib/simpleRequire.js
@@ -26,82 +26,53 @@
 // Limitations:
 //  1. Not support ancient browsers.
 //  2. Only `paths` can be configured
+//  3. Not support defined id.
 
 (function (global) {
 
     var requireCfg = { paths: {} }
 
-    var currentDefinedExports;
+    var currentDefinedFactory;
+    var currentDefinedDeps;
     var exportsPlaceholder = {};
 
-    // { loaded, content, callbacks: [] }
-    var loadingCache = {};
-    // { exports }
+    // Promise to get modules exports.
     var mods = {};
 
-    function checkLoadingCache(url, cb, onMiss) {
-        var cache = loadingCache[url];
-        if (cache) {
-            if (cache.loaded) {
-                setTimeout(function () {
-                    cb(cache.exports);
-                });
-            }
-            else {
-                cache.callbacks.push(cb);
-            }
-        }
-        else {
-            cache = loadingCache[url] = {
-                content: null,
-                loaded: null,
-                callbacks: [cb]
-            }
-            onMiss(function (content) {
-                cache.content = content;
-                cache.callbacks.forEach(function (cb) {
-                    cb(content);
-                });
-                cache.callbacks = [];
-            });
-        }
-    }
-
-    function loadText(url, cb) {
-        checkLoadingCache(url, cb, function (loaded) {
+    function loadText(url) {
+        new Promise(function (resolve, reject) {
             var xhr = new XMLHttpRequest();
             xhr.onload = function (e) {
                 if (xhr.status === 200) {
-                    loaded(xhr.responseText);
+                    resolve(xhr.responseText);
                 }
                 else {
-                    throw new Error('Error loading ' + url);
+                    reject('Error loading ' + url);
                 }
             }
             xhr.onerror = function (e) {
-                throw new Error('Error loading ' + url);
+                reject('Error loading ' + url);
             }
             xhr.open('GET', url);
             xhr.send();
         });
     }
 
-    function loadJSON(url, cb) {
-        loadText(url, function (text) {
-            cb(JSON.parse(text));
+    function loadJSON(url) {
+        return loadJSON(url).then(function (text) {
+            return JSON.parse(text);
         });
     }
 
-    function loadScript(url, cb) {
-        checkLoadingCache(url, cb, function (loaded) {
+    function loadScript(url) {
+        return new Promise(function (resolve, reject) {
             var script = document.createElement('script');
             script.async = true;
             script.onload = function () {
-                loaded();
+                resolve();
             }
             script.onerror = function () {
-                currentDefinedExports = null;
-                throw new Error('Error loading ' + url);
+                reject('Error loading ' + url);
             }
             script.src = url;
             document.head.appendChild(script);
@@ -133,9 +104,6 @@
         return str.split('.').pop();
     }
 
-    // var cachedResources = {};
-    // var pendingRequires = [];
-
     // Simplify the logic. don't support id.
     function define(modId, depsIds, factory) {
         if (factory == null) {
@@ -155,91 +123,67 @@
                 }
             }
         }
+        currentDefinedFactory = factory;
+        currentDefinedDeps = loadDeps(depsIds || []);
+    }
 
-        loadDeps(depsIds || [], function (deps) {
-            if (typeof factory === 'function') {
-                var modExports;
-                if (!modId) {
-                    modExports = currentDefinedExports = {};
+    function loadMod(modId) {
+        if (!mods[modId]) {
+            mods[modId] = {};
+
+            function loaded(exports) {
+                var ret;
+                if (currentDefinedDeps) {
+                    var factory = currentDefinedFactory;
+                    ret = currentDefinedDeps.then(function (deps) {
+                        var modExports = {};
+                        factory.apply(null, deps.map(function (dep) {
+                            return dep === exportsPlaceholder ? modExports : dep;
+                        }));
+                        return modExports;
+                    });
                 }
                 else {
-                    mods[modId] = { exports: {} };
-                    modExports = mods[modId].exports;
+                    ret = exports;
                 }
-                factory.apply(null, deps.map(function (dep) {
-                    return dep === exportsPlaceholder ? modExports : dep;
-                }));
+                // Clear.
+                currentDefinedFactory = null;
+                currentDefinedDeps = null;
+                return ret;
+            }
+
+            var url = resolvePath(modId);
+            var ext = getExt(url);
+            if (ext === 'js') {
+                mods[modId].exports = loadScript(url).then(loaded);
+            }
+            else if (ext === 'json' || ext === 'geojson') {
+                mods[modId].exports = loadJSON(url).then(loaded);
             }
             else {
-                // define(JSONObject)
-                currentDefinedExports = factory;
+                mods[modId].exports = loadText(url).then(loaded);
             }
-        });
-    }
-
-    function loadDep(modId, cb) {
-        if (mods[modId]) {
-            // pending async?
-            cb(mods[modId].exports);
-            return;
-        }
-
-        function loaded(exports) {
-            // Needs to use currentDefinedExports if loading scripts.
-            mods[modId] = {
-                exports: currentDefinedExports || exports
-            };
-            // Clear.
-            currentDefinedExports = null;
-            cb(mods[modId].exports);
-        }
-
-        var url = resolvePath(modId);
-        var ext = getExt(url);
-        if (ext === 'js') {
-            loadScript(url, loaded);
-        }
-        else if (ext === 'json' || ext === 'geojson') {
-            loadJSON(url, loaded);
-        }
-        else {
-            loadText(url, loaded);
         }
+        return mods[modId].exports;
     }
 
-    function loadDeps(depsIds, cb) {
-        var deps = [];
-        var count = depsIds.length;
-        depsIds.forEach(function (depId, idx) {
+    function loadDeps(depsIds) {
+        return Promise.all(depsIds.map(function (depId) {
             if (depId === 'exports') {
-                deps[idx] = exportsPlaceholder;
-                count--;
-                if (!count) {
-                    cb(deps);
-                }
+                return Promise.resolve(exportsPlaceholder);
             }
             else {
-                loadDep(depId, function (dep) {
-                    deps[idx] = dep;
-                    count--;
-                    if (!count) {
-                        cb(deps);
-                    }
-                });
+                return loadMod(depId);
             }
-        });
-        if (!count) {
-            cb(deps);
-        }
+        }));
     }
     function require(depsIds, cb) {
         if (typeof depsIds === 'string') {
             depsIds = [depsIds];
         }
-
-        // Batch multiple requires in one frame and callback theme in one frame.
+        // Batch multiple requires callback theme in one frame.
         // Ensure all instances are started at one time and avoid time difference in visual regression test
-        loadDeps(depsIds, function (deps) {
+        loadDeps(depsIds).then(function (deps) {
             cb && cb.apply(null, deps);
         });
     }

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org