2017-10-03 15:35:31 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-10-10 21:11:05 +02:00
|
|
|
const require = function(){
|
2017-10-03 15:35:31 +02:00
|
|
|
"use strict";
|
2017-10-10 21:11:05 +02:00
|
|
|
window.scope = {};
|
|
|
|
const scope = window.scope;
|
2017-11-07 00:36:44 +01:00
|
|
|
|
|
|
|
function getScopeName(module){
|
|
|
|
var scopeName = module.substr(2).replace(/\..+/, "");
|
|
|
|
return scopeName;
|
|
|
|
}
|
|
|
|
|
2017-10-10 21:11:05 +02:00
|
|
|
function require(module){
|
|
|
|
if (module.startsWith("./")){
|
2017-11-07 00:36:44 +01:00
|
|
|
var scopeName = getScopeName(module);
|
2017-10-10 21:11:05 +02:00
|
|
|
return scope[scopeName];
|
|
|
|
}
|
|
|
|
else if (module === "sdk/getWrapped"){
|
|
|
|
return function getWrapped(obj){
|
|
|
|
if (!obj){
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
var wrapped;
|
|
|
|
try {
|
|
|
|
wrapped = obj.wrappedJSObject || obj;
|
|
|
|
}
|
|
|
|
catch (e){
|
|
|
|
require("./logging").error("getWrapped failed for", obj, ":", e);
|
|
|
|
wrapped = obj;
|
|
|
|
}
|
|
|
|
return wrapped;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
throw new ReferenceError("Unable to get non relative module " + module + "!");
|
2017-06-25 22:22:17 +02:00
|
|
|
}
|
2017-09-23 23:47:44 +02:00
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
var events = {};
|
|
|
|
require.on = function(module, callback){
|
|
|
|
var scopeName = getScopeName(module);
|
|
|
|
if (scope.hasOwnProperty(scopeName)){
|
|
|
|
callback(scope[scopeName]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!events.hasOwnProperty(scopeName)){
|
|
|
|
events[scopeName] = [];
|
|
|
|
}
|
|
|
|
events[scopeName].push(callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
require.emit = function(module){
|
|
|
|
var scopeName = getScopeName(module);
|
|
|
|
if (events[scopeName]){
|
|
|
|
events[scopeName].forEach(function(callback){
|
|
|
|
callback(scope[scopeName]);
|
|
|
|
});
|
|
|
|
events[scopeName] = [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-10 21:11:05 +02:00
|
|
|
return require;
|
|
|
|
}();
|