Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playing around with script load firing #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions require.intellisense.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

!function (window) {
var defines = [],
moduleUrls = [],
moduleMap = {},
loadTimeId = 0,
oldDefine = window.define,
oldRequire = window.require,
oldLoad = requirejs.load;
Expand Down Expand Up @@ -42,42 +43,46 @@
});

requirejs.load = function (context, moduleName, url) {
moduleUrls.push(url);
intellisense.logMessage('requirejs.load called: ' + moduleName + ': ' + url);
moduleMap[url] = moduleName;
oldLoad.call(requirejs, context, moduleName, url);
}

window.define = function (name, deps, callback) {
defines.push([name, deps, callback]);
}

window.define.amd = {
multiversion: true,
plugins: true,
jQuery: true
};

window.require = function (deps, callback) {
setTimeout(function () {
// #1. Call the original require
oldRequire.call(window, deps, callback);

defines.forEach(function (define, index) {
oldDefine.apply(window, define);

oldDefine.apply(window, arguments);
intellisense.logMessage('In define: ' + name);
if (!loadTimeId) {
//Wait for current script to finish executing, then
//call script load event handler for anything that has
//been waiting.
loadTimeId = setTimeout(function () {
loadTimeId = 0;
var scriptElements = document.getElementsByTagName("script");

intellisense.logMessage('in loadTimeout');
for (var i = 0; i < scriptElements.length; i++) {
var script = scriptElements[i];
if (script.src == moduleUrls[index]) {
var moduleName = moduleMap[script.src];
intellisense.logMessage('SCRIPT: ' + script.src + ': ' + moduleName);
if (moduleName) {
intellisense.logMessage('herelTI');
intellisense.logMessage(moduleName + '->' + script.src + ' has state: ' + script.readyState.toString());
delete moduleMap[script.src];
loadEvent.currentTarget = script;
requirejs.onScriptLoad(loadEvent);
//force the define to be active
requirejs([moduleName]);
}
}
});
}, 0);
}, 0);
}
}

window.define.amd = {
jQuery: true
};

// Redirect all of the patched methods back to their originals
// so Intellisense will use the previously defined annotations
intellisense.redirectDefinition(requirejs.load, oldLoad);
intellisense.redirectDefinition(window.define, oldDefine);
intellisense.redirectDefinition(window.require, oldRequire);
Expand Down