Skip to content

Commit

Permalink
Merge pull request #1727 from cb1kenobi/timob-8071
Browse files Browse the repository at this point in the history
[TIMOB-8071] Fixed error handling in require().
  • Loading branch information
nebrius committed Mar 18, 2012
2 parents 664fb88 + 7cf10d9 commit 731bb5c
Showing 1 changed file with 51 additions and 33 deletions.
84 changes: 51 additions & 33 deletions mobileweb/src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,36 +475,40 @@

var s,
x,
disconnector,
scriptTagLoadEvent,
scriptTagErrorEvent,
_t = this,
cached = defCache[_t.name],
fireCallbacks = function() {
each(_t.callbacks, function(c) { c(_t); });
_t.callbacks = [];
},
onLoad = function(rawDef) {
_t.loaded = 1;
if (_t.rawDef = rawDef) {
if (is(rawDef, "String")) {
// if rawDef is a string, then it's either a cached string or xhr response
if (/\.js$/.test(_t.url)) {
rawDef = evaluate(rawDef, _t.cjs);
_t.def = _t.rawDef = !isEmpty(rawDef.exports) ? rawDef.exports : (rawDef.module && !isEmpty(rawDef.module.exports) ? rawDef.module.exports : null);
_t.def === null && (_t.rawDef = rawDef);
} else {
_t.def = rawDef;
_t.executed = 1;
}
} else if (is(rawDef, "Function")) {
// if rawDef is a function, then it's a cached module definition
waiting[_t.name] = _t;
rawDef();
name = _t.name,
cached = defCache[name];

function fireCallbacks() {
each(_t.callbacks, function(c) { c(_t); });
_t.callbacks = [];
}

function onLoad(rawDef) {
_t.loaded = 1;
if (_t.rawDef = rawDef) {
if (is(rawDef, "String")) {
// if rawDef is a string, then it's either a cached string or xhr response
if (/\.js$/.test(_t.url)) {
rawDef = evaluate(rawDef, _t.cjs);
_t.def = _t.rawDef = !isEmpty(rawDef.exports) ? rawDef.exports : (rawDef.module && !isEmpty(rawDef.module.exports) ? rawDef.module.exports : null);
_t.def === null && (_t.rawDef = rawDef);
} else {
_t.def = rawDef;
_t.executed = 1;
}
} else if (is(rawDef, "Function")) {
// if rawDef is a function, then it's a cached module definition
waiting[name] = _t;
rawDef();
}
processDefQ(_t);
fireCallbacks();
return 1;
};
}
processDefQ(_t);
fireCallbacks();
return 1;
}

_t.sync = sync;
callback && _t.callbacks.push(callback);
Expand All @@ -517,19 +521,30 @@
}

// if we're already waiting, then we can just return and our callback will be fired
if (waiting[_t.name]) {
if (waiting[name]) {
return;
}

// if we're already loaded or the definition has been cached, then just return now
if (_t.loaded || cached) {
delete defCache[_t.name];
delete defCache[name];
return onLoad(cached);
}

// mark this module as waiting to be loaded so that anonymous modules can be
// identified
waiting[_t.name] = _t;
waiting[name] = _t;

function disconnect() {
scriptTagLoadEvent && scriptTagLoadEvent();
scriptTagErrorEvent && scriptTagErrorEvent();
}

function failed() {
modules[name] = 0;
delete waiting[name];
disconnect();
}

if (sync) {
x = new XMLHttpRequest();
Expand All @@ -539,7 +554,8 @@
if (x.status === 200) {
return onLoad(x.responseText);
} else {
throw new Error("Failed to load module \"" + _t.name + "\": " + x.status);
failed();
throw new Error("Failed to load module \"" + name + "\": " + x.status);
}
} else {
// insert the script tag, attach onload, wait
Expand All @@ -548,15 +564,17 @@
x.charset = "utf-8";
x.async = true;

disconnector = on(x, "load", function(e) {
scriptTagLoadEvent = on(x, "load", function(e) {
e = e || global.event;
var node = e.target || e.srcElement;
if (e.type === "load" || /complete|loaded/.test(node.readyState)) {
disconnector();
disconnect();
onLoad();
}
});

scriptTagErrorEvent = on(x, "error", failed);

// set the source url last
x.src = _t.url;

Expand Down

0 comments on commit 731bb5c

Please sign in to comment.