Skip to content

Commit 69403c1

Browse files
committed
refactor: add default Utils.getURL getModule processor
1 parent 50800df commit 69403c1

File tree

1 file changed

+64
-42
lines changed

1 file changed

+64
-42
lines changed

core/modules.js

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@
6666
}
6767
loadModules(code, callback);
6868
});
69+
70+
// Append the 'getModule' processor as the last (plugins get initialized after Espruino.Core modules)
71+
Espruino.Plugins.CoreModules = {
72+
init: function() {
73+
Espruino.addProcessor("getModule", function(data, callback) {
74+
if (data.moduleCode!==undefined) { // already provided be previous getModule processor
75+
return callback(data);
76+
}
77+
78+
fetchGetModule(data, callback);
79+
});
80+
}
81+
};
6982
}
7083

7184
function isBuiltIn(module) {
@@ -108,6 +121,50 @@
108121
return modules;
109122
};
110123

124+
/** Download modules from MODULE_URL/.. */
125+
function fetchGetModule(data, callback) {
126+
var fullModuleName = data.moduleName;
127+
128+
// try and load the module the old way...
129+
console.log("loadModule("+fullModuleName+")");
130+
131+
var urls = []; // Array of where to look for this module
132+
var modName; // Simple name of the module
133+
if(Espruino.Core.Utils.isURL(fullModuleName)) {
134+
modName = fullModuleName.substr(fullModuleName.lastIndexOf("/") + 1).split(".")[0];
135+
urls = [ fullModuleName ];
136+
} else {
137+
modName = fullModuleName;
138+
Espruino.Config.MODULE_URL.split("|").forEach(function (url) {
139+
url = url.trim();
140+
if (url.length!=0)
141+
Espruino.Config.MODULE_EXTENSIONS.split("|").forEach(function (extension) {
142+
urls.push(url + "/" + fullModuleName + extension);
143+
})
144+
});
145+
};
146+
147+
// Recursively go through all the urls
148+
(function download(urls) {
149+
if (urls.length==0) {
150+
return callback(data);
151+
}
152+
var dlUrl = urls[0];
153+
Espruino.Core.Utils.getURL(dlUrl, function (code) {
154+
if (code!==undefined) {
155+
// we got it!
156+
data.moduleCode = code;
157+
data.isMinified = dlUrl.substr(-7)==".min.js";
158+
return callback(data);
159+
} else {
160+
// else try next
161+
download(urls.slice(1));
162+
}
163+
});
164+
})(urls);
165+
}
166+
167+
111168
/** Called from loadModule when a module is loaded. Parse it for other modules it might use
112169
* and resolve dfd after all submodules have been loaded */
113170
function moduleLoaded(resolve, requires, modName, data, loadedModuleData, alreadyMinified){
@@ -147,51 +204,16 @@
147204
return new Promise(function(resolve, reject) {
148205
// First off, try and find this module using callProcessor
149206
Espruino.callProcessor("getModule",
150-
{ moduleName:fullModuleName, moduleCode:undefined },
207+
{ moduleName:fullModuleName, moduleCode:undefined, isMinified:false },
151208
function(data) {
152-
if (data.moduleCode!==undefined) {
153-
// great! it found something. Use it.
154-
moduleLoaded(resolve, requires, fullModuleName, data.moduleCode, loadedModuleData, false);
155-
} else {
156-
// otherwise try and load the module the old way...
157-
console.log("loadModule("+fullModuleName+")");
158-
159-
var urls = []; // Array of where to look for this module
160-
var modName; // Simple name of the module
161-
if(Espruino.Core.Utils.isURL(fullModuleName)) {
162-
modName = fullModuleName.substr(fullModuleName.lastIndexOf("/") + 1).split(".")[0];
163-
urls = [ fullModuleName ];
164-
} else {
165-
modName = fullModuleName;
166-
Espruino.Config.MODULE_URL.split("|").forEach(function (url) {
167-
url = url.trim();
168-
if (url.length!=0)
169-
Espruino.Config.MODULE_EXTENSIONS.split("|").forEach(function (extension) {
170-
urls.push(url + "/" + fullModuleName + extension);
171-
})
172-
});
173-
};
174-
175-
// Recursively go through all the urls
176-
(function download(urls) {
177-
if (urls.length==0) {
178-
Espruino.Core.Notifications.warning("Module "+fullModuleName+" not found");
179-
return resolve();
180-
}
181-
var dlUrl = urls[0];
182-
Espruino.Core.Utils.getURL(dlUrl, function (data) {
183-
if (data!==undefined) {
184-
// we got it!
185-
moduleLoaded(resolve, requires, fullModuleName, data, loadedModuleData, dlUrl.substr(-7)==".min.js");
186-
} else {
187-
// else try next
188-
download(urls.slice(1));
189-
}
190-
});
191-
})(urls);
209+
if (data.moduleCode===undefined) {
210+
Espruino.Core.Notifications.warning("Module "+fullModuleName+" not found");
211+
return resolve();
192212
}
193-
});
194213

214+
// great! it found something. Use it.
215+
moduleLoaded(resolve, requires, fullModuleName, data.moduleCode, loadedModuleData, data.isMinified);
216+
});
195217
});
196218
}
197219

0 commit comments

Comments
 (0)