Permalink
Browse files

Bug 958675 - [email] Upgrade alameda.js to get latest prim

  • Loading branch information...
1 parent 4e359e8 commit d1ae1e8efa3f862dbf44beb06f9b892c1857889e @jrburke jrburke committed Jan 13, 2014
Showing with 398 additions and 299 deletions.
  1. +7 −1 data/lib/mailapi/allback.js
  2. +6 −1 data/lib/mailapi/cronsync.js
  3. +385 −297 deps/alameda.js
View
8 data/lib/mailapi/allback.js
@@ -99,9 +99,15 @@ exports.allbackMaker = function allbackMaker(names, allDoneCallback) {
*/
exports.latch = function() {
var ready = false;
- var deferred = prim();
+ var deferred = {};
var results = {};
var count = 0;
+
+ deferred.promise = prim(function (resolve, reject) {
+ deferred.resolve = resolve;
+ deferred.reject = reject;
+ });
+
function defer(name) {
count++;
var resolved = false;
View
7 data/lib/mailapi/cronsync.js
@@ -101,9 +101,14 @@ function makeSlice(storage, callback, parentLog) {
*/
function CronSync(universe, _logParent) {
this._universe = universe;
- this._universeDeferred = $prim();
+ this._universeDeferred = {};
this._isUniverseReady = false;
+ this._universeDeferred.promise = $prim(function (resolve, reject) {
+ this._universeDeferred.resolve = resolve;
+ this._universeDeferred.reject = reject;
+ }.bind(this));
+
this._LOG = LOGFAB.CronSync(this, null, _logParent);
this._activeSlices = [];
View
682 deps/alameda.js
@@ -1,16 +1,17 @@
/**
- * alameda 0.0.3+ Copyright (c) 2011-2012, The Dojo Foundation All Rights Reserved.
+ * alameda 0.2.0 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/requirejs/alameda for details
*/
//Going sloppy to avoid 'use strict' string cost, but strict practices should
//be followed.
/*jslint sloppy: true, nomen: true, regexp: true */
-/*global setTimeout, process, document, navigator, importScripts */
+/*global setTimeout, process, document, navigator, importScripts,
+ setImmediate */
var requirejs, require, define;
(function (global, undef) {
- var prim, topReq, dataMain,
+ var prim, topReq, dataMain, src, subPath,
bootstrapConfig = requirejs || require,
hasOwn = Object.prototype.hasOwnProperty,
contexts = {},
@@ -50,14 +51,17 @@ var requirejs, require, define;
}
/**
- * Mixes in properties from source into target,
+ * Simple function to mix in properties from source into target,
* but only if target does not already have a property of the same name.
*/
function mixin(target, source, force, deepStringMixin) {
if (source) {
eachProp(source, function (value, prop) {
if (force || !hasProp(target, prop)) {
- if (deepStringMixin && typeof value !== 'string') {
+ if (deepStringMixin && typeof value === 'object' && value &&
+ !Array.isArray(value) && typeof value !== 'function' &&
+ !(value instanceof RegExp)) {
+
if (!target[prop]) {
target[prop] = {};
}
@@ -84,32 +88,16 @@ var requirejs, require, define;
return g;
}
- //START prim
+ //START prim 0.0.6
/**
* Changes from baseline prim
- * - no hasProp or hasOwn (already defined in this file)
- * - no hideResolutionConflict, want early errors, trusted code.
- * - each() changed to Array.forEach
* - removed UMD registration
*/
- /**
- * prim 0.0.2 Copyright (c) 2012-2013, The Dojo Foundation All Rights Reserved.
- * Available via the MIT or new BSD license.
- * see: http://github.com/requirejs/prim for details
- */
-
- /*global setImmediate, process, setTimeout */
(function () {
'use strict';
- var waitingId,
- waiting = [];
- function check(p) {
- if (hasProp(p, 'e') || hasProp(p, 'v')) {
- throw new Error('nope');
- }
- return true;
- }
+ var waitingId, nextTick,
+ waiting = [];
function callWaiting() {
waitingId = 0;
@@ -127,6 +115,23 @@ var requirejs, require, define;
}
}
+ function syncTick(fn) {
+ fn();
+ }
+
+ function isFunObj(x) {
+ var type = typeof x;
+ return type === 'object' || type === 'function';
+ }
+
+ //Use setImmediate.bind() because attaching it (or setTimeout directly
+ //to prim will result in errors. Noticed first on IE10,
+ //issue requirejs/alameda#2)
+ nextTick = typeof setImmediate === 'function' ? setImmediate.bind() :
+ (typeof process !== 'undefined' && process.nextTick ?
+ process.nextTick : (typeof setTimeout !== 'undefined' ?
+ asyncTick : syncTick));
+
function notify(ary, value) {
prim.nextTick(function () {
ary.forEach(function (item) {
@@ -135,139 +140,173 @@ var requirejs, require, define;
});
}
- prim = function prim() {
- var p,
+ function callback(p, ok, yes) {
+ if (p.hasOwnProperty('v')) {
+ prim.nextTick(function () {
+ yes(p.v);
+ });
+ } else {
+ ok.push(yes);
+ }
+ }
+
+ function errback(p, fail, no) {
+ if (p.hasOwnProperty('e')) {
+ prim.nextTick(function () {
+ no(p.e);
+ });
+ } else {
+ fail.push(no);
+ }
+ }
+
+ prim = function prim(fn) {
+ var promise, f,
+ p = {},
ok = [],
fail = [];
- return (p = {
- callback: function (yes, no) {
- if (no) {
- p.errback(no);
- }
+ function makeFulfill() {
+ var f, f2,
+ called = false;
- if (hasProp(p, 'v')) {
- prim.nextTick(function () {
- yes(p.v);
- });
- } else {
- ok.push(yes);
+ function fulfill(v, prop, listeners) {
+ if (called) {
+ return;
}
- },
+ called = true;
- errback: function (no) {
- if (hasProp(p, 'e')) {
- prim.nextTick(function () {
- no(p.e);
- });
- } else {
- fail.push(no);
+ if (promise === v) {
+ called = false;
+ f.reject(new TypeError('value is same promise'));
+ return;
}
- },
-
- finished: function () {
- return hasProp(p, 'e') || hasProp(p, 'v');
- },
- rejected: function () {
- return hasProp(p, 'e');
- },
-
- resolve: function (v) {
- if (check(p)) {
- p.v = v;
- notify(ok, v);
+ try {
+ var then = v && v.then;
+ if (isFunObj(v) && typeof then === 'function') {
+ f2 = makeFulfill();
+ then.call(v, f2.resolve, f2.reject);
+ } else {
+ p[prop] = v;
+ notify(listeners, v);
+ }
+ } catch (e) {
+ called = false;
+ f.reject(e);
}
- return p;
- },
- reject: function (e) {
- if (check(p)) {
- p.e = e;
- notify(fail, e);
+ }
+
+ f = {
+ resolve: function (v) {
+ fulfill(v, 'v', ok);
+ },
+ reject: function(e) {
+ fulfill(e, 'e', fail);
}
- return p;
- },
+ };
+ return f;
+ }
- start: function (fn) {
- p.resolve();
- return p.promise.then(fn);
- },
+ f = makeFulfill();
- promise: {
- then: function (yes, no) {
- var next = prim();
+ promise = {
+ then: function (yes, no) {
+ var next = prim(function (nextResolve, nextReject) {
- p.callback(function (v) {
+ function finish(fn, nextFn, v) {
try {
- if (yes && typeof yes === 'function') {
- v = yes(v);
- }
-
- if (v && typeof v.then === 'function') {
- v.then(next.resolve, next.reject);
+ if (fn && typeof fn === 'function') {
+ v = fn(v);
+ nextResolve(v);
} else {
- next.resolve(v);
+ nextFn(v);
}
} catch (e) {
- next.reject(e);
+ nextReject(e);
}
- }, function (e) {
- var err;
+ }
- try {
- if (!no || typeof no !== 'function') {
- next.reject(e);
- } else {
- err = no(e);
+ callback(p, ok, finish.bind(undefined, yes, nextResolve));
+ errback(p, fail, finish.bind(undefined, no, nextReject));
- if (err && typeof err.then === 'function') {
- err.then(next.resolve, next.reject);
- } else {
- next.resolve(err);
- }
- }
- } catch (e2) {
- next.reject(e2);
- }
- });
+ });
+ return next;
+ },
- return next.promise;
- },
+ catch: function (no) {
+ return promise.then(null, no);
+ }
+ };
- fail: function (no) {
- return p.promise.then(null, no);
- },
+ try {
+ fn(f.resolve, f.reject);
+ } catch (e) {
+ f.reject(e);
+ }
- end: function () {
- p.errback(function (e) {
- throw e;
- });
- }
- }
+ return promise;
+ };
+
+ prim.resolve = function (value) {
+ return prim(function (yes) {
+ yes(value);
+ });
+ };
+
+ prim.reject = function (err) {
+ return prim(function (yes, no) {
+ no(err);
});
};
- prim.serial = function (ary) {
- var result = prim().resolve().promise;
- ary.forEach(function (item) {
- result = result.then(function () {
- return item();
+ prim.cast = function (x) {
+ // A bit of a weak check, want "then" to be a function,
+ // but also do not want to trigger a getter if accessing
+ // it. Good enough for now.
+ if (isFunObj(x) && 'then' in x) {
+ return x;
+ } else {
+ return prim(function (yes, no) {
+ if (x instanceof Error) {
+ no(x);
+ } else {
+ yes(x);
+ }
+ });
+ }
+ };
+
+ prim.all = function (ary) {
+ return prim(function (yes, no) {
+ var count = 0,
+ length = ary.length,
+ result = [];
+
+ function resolved(i, v) {
+ result[i] = v;
+ count += 1;
+ if (count === length) {
+ yes(result);
+ }
+ }
+
+ ary.forEach(function (item, i) {
+ prim.cast(item).then(function (v) {
+ resolved(i, v);
+ }, function (err) {
+ no(err);
+ });
});
});
- return result;
};
- prim.nextTick = typeof setImmediate === 'function' ? setImmediate :
- (typeof process !== 'undefined' && process.nextTick ?
- process.nextTick : (typeof setTimeout !== 'undefined' ?
- asyncTick : function (fn) {
- fn();
- }));
+ prim.nextTick = nextTick;
}());
//END prim
function newContext(contextName) {
- var req, main, makeMap, callDep, handlers, loadTimeId, load, context,
+ var req, main, makeMap, callDep, handlers, checkingLater, load, context,
defined = {},
waiting = {},
config = {
@@ -277,10 +316,12 @@ var requirejs, require, define;
waitSeconds: 7,
baseUrl: './',
paths: {},
+ bundles: {},
pkgs: {},
shim: {},
config: {}
},
+ mapCache = {},
requireDeferreds = [],
deferreds = {},
calledDefine = {},
@@ -289,7 +330,8 @@ var requirejs, require, define;
startTime = (new Date()).getTime(),
errCount = 0,
trackedErrors = {},
- urlFetched = {};
+ urlFetched = {},
+ bundlesMap = {};
/**
* Trims the . and .. from an array of path segments.
@@ -301,8 +343,8 @@ var requirejs, require, define;
* @param {Array} ary the array of path segments.
*/
function trimDots(ary) {
- var i, part;
- for (i = 0; ary[i]; i += 1) {
+ var i, part, length = ary.length;
+ for (i = 0; i < length; i++) {
part = ary[i];
if (part === '.') {
ary.splice(i, 1);
@@ -335,11 +377,11 @@ var requirejs, require, define;
* @returns {String} normalized name
*/
function normalize(name, baseName, applyMap) {
- var pkgName, pkgConfig, mapValue, nameParts, i, j, nameSegment,
+ var pkgMain, mapValue, nameParts, i, j, nameSegment, lastIndex,
foundMap, foundI, foundStarMap, starI,
baseParts = baseName && baseName.split('/'),
normalizedBaseParts = baseParts,
- map = applyMap && config.map,
+ map = config.map,
starMap = map && map['*'];
//Adjust any relative paths.
@@ -348,29 +390,26 @@ var requirejs, require, define;
//otherwise, assume it is a top-level require that will
//be relative to baseUrl in the end.
if (baseName) {
- if (getOwn(config.pkgs, baseName)) {
- //If the baseName is a package name, then just treat it as one
- //name to concat the name with.
- normalizedBaseParts = baseParts = [baseName];
- } else {
- //Convert baseName to array, and lop off the last part,
- //so that . matches that 'directory' and not name of the baseName's
- //module. For instance, baseName of 'one/two/three', maps to
- //'one/two/three.js', but we want the directory, 'one/two' for
- //this normalization.
- normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
+ //Convert baseName to array, and lop off the last part,
+ //so that . matches that 'directory' and not name of the baseName's
+ //module. For instance, baseName of 'one/two/three', maps to
+ //'one/two/three.js', but we want the directory, 'one/two' for
+ //this normalization.
+ normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
+ name = name.split('/');
+ lastIndex = name.length - 1;
+
+ // If wanting node ID compatibility, strip .js from end
+ // of IDs. Have to do this here, and not in nameToUrl
+ // because node allows either .js or non .js to map
+ // to same file.
+ if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
+ name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
}
- name = normalizedBaseParts.concat(name.split('/'));
+ name = normalizedBaseParts.concat(name);
trimDots(name);
-
- //Some use of packages may use a . path to reference the
- //'main' module name, so normalize for that.
- pkgConfig = getOwn(config.pkgs, (pkgName = name[0]));
name = name.join('/');
- if (pkgConfig && name === pkgName + '/' + pkgConfig.main) {
- name = pkgName;
- }
} else if (name.indexOf('./') === 0) {
// No baseName, so this is ID is resolved relative
// to baseUrl, pull off the leading dot.
@@ -379,10 +418,10 @@ var requirejs, require, define;
}
//Apply map config if available.
- if (applyMap && (baseParts || starMap) && map) {
+ if (applyMap && map && (baseParts || starMap)) {
nameParts = name.split('/');
- for (i = nameParts.length; i > 0; i -= 1) {
+ outerLoop: for (i = nameParts.length; i > 0; i -= 1) {
nameSegment = nameParts.slice(0, i).join('/');
if (baseParts) {
@@ -399,16 +438,12 @@ var requirejs, require, define;
//Match, update name to the new value.
foundMap = mapValue;
foundI = i;
- break;
+ break outerLoop;
}
}
}
}
- if (foundMap) {
- break;
- }
-
//Check for a star map match, but just hold on to it,
//if there is a shorter segment match later in a matching
//config, then favor over this star map.
@@ -429,9 +464,12 @@ var requirejs, require, define;
}
}
- return name;
- }
+ // If the name points to a package's name, use
+ // the package main instead.
+ pkgMain = getOwn(config.pkgs, name);
+ return pkgMain ? pkgMain : name;
+ }
function makeShimExports(value) {
function fn() {
@@ -535,9 +573,20 @@ var requirejs, require, define;
req.isBrowser = typeof document !== 'undefined' &&
typeof navigator !== 'undefined';
- req.nameToUrl = function (moduleName, ext) {
- var paths, pkgs, pkg, pkgPath, syms, i, parentModule, url,
- parentPath;
+ req.nameToUrl = function (moduleName, ext, skipExt) {
+ var paths, syms, i, parentModule, url,
+ parentPath, bundleId,
+ pkgMain = getOwn(config.pkgs, moduleName);
+
+ if (pkgMain) {
+ moduleName = pkgMain;
+ }
+
+ bundleId = getOwn(bundlesMap, moduleName);
+
+ if (bundleId) {
+ return req.nameToUrl(bundleId, ext, skipExt);
+ }
//If a colon is in the URL, it indicates a protocol is used and it is just
//an URL to a file, or if it starts with a slash, contains a query arg (i.e. ?)
@@ -551,15 +600,14 @@ var requirejs, require, define;
} else {
//A module that needs to be converted to a path.
paths = config.paths;
- pkgs = config.pkgs;
syms = moduleName.split('/');
//For each module name segment, see if there is a path
//registered for it. Start with most specific name
//and work up from it.
for (i = syms.length; i > 0; i -= 1) {
parentModule = syms.slice(0, i).join('/');
- pkg = getOwn(pkgs, parentModule);
+
parentPath = getOwn(paths, parentModule);
if (parentPath) {
//If an array, it means there are a few choices,
@@ -569,22 +617,12 @@ var requirejs, require, define;
}
syms.splice(0, i, parentPath);
break;
- } else if (pkg) {
- //If module name is just the package name, then looking
- //for the main module.
- if (moduleName === pkg.name) {
- pkgPath = pkg.location + '/' + pkg.main;
- } else {
- pkgPath = pkg.location;
- }
- syms.splice(0, i, pkgPath);
- break;
}
}
//Join the path parts together, then figure out if baseUrl is needed.
url = syms.join('/');
- url += (ext || (/\?/.test(url) ? '' : '.js'));
+ url += (ext || (/^data\:|\?/.test(url) || skipExt ? '' : '.js'));
url = (url.charAt(0) === '/' || url.match(/^[\w\+\.\-]+:/) ? '' : config.baseUrl) + url;
}
@@ -599,15 +637,19 @@ var requirejs, require, define;
* plain URLs like nameToUrl.
*/
req.toUrl = function (moduleNamePlusExt) {
- var index = moduleNamePlusExt.lastIndexOf('.'),
- ext = null;
-
- if (index !== -1) {
+ var ext,
+ index = moduleNamePlusExt.lastIndexOf('.'),
+ segment = moduleNamePlusExt.split('/')[0],
+ isRelative = segment === '.' || segment === '..';
+
+ //Have a file extension alias, and it is not the
+ //dots from a relative path.
+ if (index !== -1 && (!isRelative || index > 1)) {
ext = moduleNamePlusExt.substring(index, moduleNamePlusExt.length);
moduleNamePlusExt = moduleNamePlusExt.substring(0, index);
}
- return req.nameToUrl(normalize(moduleNamePlusExt, relName), ext);
+ return req.nameToUrl(normalize(moduleNamePlusExt, relName), ext, true);
};
req.defined = function (id) {
@@ -625,10 +667,20 @@ var requirejs, require, define;
function resolve(name, d, value) {
if (name) {
defined[name] = value;
+ if (requirejs.onResourceLoad) {
+ requirejs.onResourceLoad(context, d.map, d.deps);
+ }
}
+ d.finished = true;
d.resolve(value);
}
+ function reject(d, err) {
+ d.finished = true;
+ d.rejected = true;
+ d.reject(err);
+ }
+
function makeNormalize(relName) {
return function (name) {
return normalize(name, relName, true);
@@ -640,37 +692,44 @@ var requirejs, require, define;
ret = d.factory.apply(defined[name], d.values);
if (name) {
- //If setting exports via "module" is in play,
- //favor that over return value and exports.
- //After that, favor a non-undefined return
- //value over exports use.
- if (d.cjsModule && d.cjsModule.exports !== undef &&
- d.cjsModule.exports !== defined[name]) {
- ret = d.cjsModule.exports;
- } else if (ret === undef && d.usingExports) {
- ret = defined[name];
+ // Favor return value over exports. If node/cjs in play,
+ // then will not have a return value anyway. Favor
+ // module.exports assignment over exports object.
+ if (ret === undef) {
+ if (d.cjsModule) {
+ ret = d.cjsModule.exports;
+ } else if (d.usingExports) {
+ ret = defined[name];
+ }
}
- resolve(name, d, ret);
} else {
- d.resolve();
+ //Remove the require deferred from the list to
+ //make cycle searching faster. Do not need to track
+ //it anymore either.
+ requireDeferreds.splice(requireDeferreds.indexOf(d), 1);
}
+ resolve(name, d, ret);
}
//This method is attached to every module deferred,
//so the "this" in here is the module deferred object.
function depFinished(val, i) {
- if (!this.rejected() && !this.depDefined[i]) {
+ if (!this.rejected && !this.depDefined[i]) {
this.depDefined[i] = true;
this.depCount += 1;
this.values[i] = val;
- if (this.depCount === this.depMax) {
+ if (!this.depending && this.depCount === this.depMax) {
defineModule(this);
}
}
}
function makeDefer(name) {
- var d = prim();
+ var d = {};
+ d.promise = prim(function (resolve, reject) {
+ d.resolve = resolve;
+ d.reject = reject;
+ });
d.map = name ? makeMap(name, null, true) : {};
d.depCount = 0;
d.depMax = 0;
@@ -702,12 +761,12 @@ var requirejs, require, define;
function makeErrback(d, name) {
return function (err) {
- if (!d.rejected()) {
+ if (!d.rejected) {
if (!err.dynaId) {
err.dynaId = 'id' + (errCount += 1);
err.requireModules = [name];
}
- d.reject(err);
+ reject(d, err);
}
};
}
@@ -719,7 +778,7 @@ var requirejs, require, define;
//in the then callback execution.
callDep(depMap, relName).then(function (val) {
d.depFinished(val, i);
- }, makeErrback(d, depMap.id)).fail(makeErrback(d, d.map.id));
+ }, makeErrback(d, depMap.id)).catch(makeErrback(d, d.map.id));
}
function makeLoad(id) {
@@ -740,7 +799,7 @@ var requirejs, require, define;
/*jslint evil: true */
var d = getDefer(id),
map = makeMap(makeMap(id).n),
- plainId = map.id;
+ plainId = map.id;
fromTextCalled = true;
@@ -766,8 +825,8 @@ var requirejs, require, define;
try {
req.exec(text);
} catch (e) {
- throw new Error('fromText eval for ' + plainId +
- ' failed: ' + e);
+ reject(d, new Error('fromText eval for ' + plainId +
+ ' failed: ' + e));
}
//Execute any waiting define created by the plainId
@@ -831,7 +890,7 @@ var requirejs, require, define;
d.map = makeMap(id);
load(d.map);
} else {
- err = new Error('Load failed: ' + id + ': ' + script.url);
+ err = new Error('Load failed: ' + id + ': ' + script.src);
err.requireModules = [id];
getDefer(id).reject(err);
}
@@ -847,7 +906,7 @@ var requirejs, require, define;
}
callDep = function (map, relName) {
- var args,
+ var args, bundleId,
name = map.id,
shim = config.shim[name];
@@ -857,26 +916,33 @@ var requirejs, require, define;
main.apply(undef, args);
} else if (!hasProp(deferreds, name)) {
if (map.pr) {
- return callDep(makeMap(map.pr)).then(function (plugin) {
- //Redo map now that plugin is known to be loaded
- var newMap = makeMap(name, relName, true),
- newId = newMap.id,
- shim = getOwn(config.shim, newId);
-
- //Make sure to only call load once per resource. Many
- //calls could have been queued waiting for plugin to load.
- if (!hasProp(calledPlugin, newId)) {
- calledPlugin[newId] = true;
- if (shim && shim.deps) {
- req(shim.deps, function () {
+ //If a bundles config, then just load that file instead to
+ //resolve the plugin, as it is built into that bundle.
+ if ((bundleId = getOwn(bundlesMap, name))) {
+ map.url = req.nameToUrl(bundleId);
+ load(map);
+ } else {
+ return callDep(makeMap(map.pr)).then(function (plugin) {
+ //Redo map now that plugin is known to be loaded
+ var newMap = makeMap(name, relName, true),
+ newId = newMap.id,
+ shim = getOwn(config.shim, newId);
+
+ //Make sure to only call load once per resource. Many
+ //calls could have been queued waiting for plugin to load.
+ if (!hasProp(calledPlugin, newId)) {
+ calledPlugin[newId] = true;
+ if (shim && shim.deps) {
+ req(shim.deps, function () {
+ callPlugin(plugin, newMap, relName);
+ });
+ } else {
callPlugin(plugin, newMap, relName);
- });
- } else {
- callPlugin(plugin, newMap, relName);
+ }
}
- }
- return getDefer(newId).promise;
- });
+ return getDefer(newId).promise;
+ });
+ }
} else if (shim && shim.deps) {
req(shim.deps, function () {
load(map);
@@ -912,12 +978,17 @@ var requirejs, require, define;
return name;
}
- var plugin, url,
- parts = splitPrefix(name),
- prefix = parts[0];
+ var plugin, url, parts, prefix, result,
+ cacheKey = name + ' & ' + (relName || '') + ' & ' + !!applyMap;
+ parts = splitPrefix(name);
+ prefix = parts[0];
name = parts[1];
+ if (!prefix && hasProp(mapCache, cacheKey)) {
+ return mapCache[cacheKey];
+ }
+
if (prefix) {
prefix = normalize(prefix, relName, applyMap);
plugin = hasProp(defined, prefix) && defined[prefix];
@@ -940,19 +1011,19 @@ var requirejs, require, define;
}
//Using ridiculous property names for space reasons
- return {
+ result = {
id: prefix ? prefix + '!' + name : name, //fullName
n: name,
pr: prefix,
url: url
};
- };
- function makeConfig(name) {
- return function () {
- return config.config[name] || {};
- };
- }
+ if (!prefix) {
+ mapCache[cacheKey] = result;
+ }
+
+ return result;
+ };
handlers = {
require: function (name) {
@@ -970,8 +1041,10 @@ var requirejs, require, define;
return {
id: name,
uri: '',
- exports: defined[name],
- config: makeConfig(name)
+ exports: handlers.exports(name),
+ config: function () {
+ return getOwn(config.config, name) || {};
+ }
};
}
};
@@ -980,25 +1053,22 @@ var requirejs, require, define;
var id = d.map.id;
traced[id] = true;
- if (!d.finished() && d.deps) {
+ if (!d.finished && d.deps) {
d.deps.forEach(function (depMap) {
- var depIndex,
- depId = depMap.id,
+ var depId = depMap.id,
dep = !hasProp(handlers, depId) && getDefer(depId);
//Only force things that have not completed
//being defined, so still in the registry,
//and only if it has not been matched up
//in the module already.
- if (dep && !dep.finished() && !processed[depId]) {
+ if (dep && !dep.finished && !processed[depId]) {
if (hasProp(traced, depId)) {
- d.deps.some(function (depMap, i) {
+ d.deps.forEach(function (depMap, i) {
if (depMap.id === depId) {
- depIndex = i;
- return true;
+ d.depFinished(defined[depId], i);
}
});
- d.depFinished(defined[depId], depIndex);
} else {
breakCycle(dep, traced, processed);
}
@@ -1008,35 +1078,26 @@ var requirejs, require, define;
processed[id] = true;
}
- function check() {
- loadTimeId = 0;
-
+ function check(d) {
var err,
- reqDefs = [],
- noLoads = [],
+ notFinished = [],
waitInterval = config.waitSeconds * 1000,
//It is possible to disable the wait interval by using waitSeconds of 0.
expired = waitInterval && (startTime + waitInterval) < (new Date()).getTime();
if (loadCount === 0) {
- reqDefs = requireDeferreds.filter(function (d) {
- //Only want deferreds that have not finished.
- return !d.finished();
- });
-
- if (reqDefs.length) {
- //Something is not resolved. Dive into it.
- eachProp(deferreds, function (d) {
- if (!d.finished()) {
- noLoads.push(d);
- }
- });
-
- if (noLoads.length) {
- reqDefs.forEach(function (d) {
- breakCycle(d, {}, {});
- });
+ //If passed in a deferred, it is for a specific require call.
+ //Could be a sync case that needs resolution right away.
+ //Otherwise, if no deferred, means a nextTick and all
+ //waiting require deferreds should be checked.
+ if (d) {
+ if (!d.finished) {
+ breakCycle(d, {}, {});
}
+ } else if (requireDeferreds.length) {
+ requireDeferreds.forEach(function (d) {
+ breakCycle(d, {}, {});
+ });
}
}
@@ -1045,17 +1106,23 @@ var requirejs, require, define;
//scripts, then just try back later.
if (expired) {
//If wait time expired, throw error of unloaded modules.
- noLoads = noLoads.map(function (d) {
- return d.map.id;
+ eachProp(deferreds, function (d) {
+ if (!d.finished) {
+ notFinished.push(d.map.id);
+ }
});
- err = new Error('Timeout for modules: ' + noLoads);
- err.requireModules = noLoads;
+ err = new Error('Timeout for modules: ' + notFinished);
+ err.requireModules = notFinished;
req.onError(err);
- } else if (loadCount || reqDefs.length) {
+ } else if (loadCount || requireDeferreds.length) {
//Something is still waiting to load. Wait for it, but only
- //if a timeout is not already in effect.
- if (!loadTimeId) {
- loadTimeId = prim.nextTick(check);
+ //if a later check is not already scheduled.
+ if (!checkingLater) {
+ checkingLater = true;
+ prim.nextTick(function () {
+ checkingLater = false;
+ check();
+ });
}
}
}
@@ -1088,7 +1155,7 @@ var requirejs, require, define;
deps = [];
}
- d.promise.fail(errback || delayedError);
+ d.promise.catch(errback || delayedError);
//Use name if no relName
relName = relName || name;
@@ -1121,6 +1188,7 @@ var requirejs, require, define;
d.factory = factory;
d.deps = deps;
+ d.depending = true;
deps.forEach(function (depName, i) {
var depMap;
deps[i] = depMap = makeMap(depName, relName, true);
@@ -1136,10 +1204,13 @@ var requirejs, require, define;
} else if (depName === "module") {
//CommonJS module spec 1.1
d.values[i] = d.cjsModule = handlers.module(name);
+ } else if (depName === undefined) {
+ d.values[i] = undefined;
} else {
waitForDep(depMap, relName, d, i);
}
});
+ d.depending = false;
//Some modules just depend on the require, exports, modules, so
//trigger their definition here if so.
@@ -1155,7 +1226,7 @@ var requirejs, require, define;
startTime = (new Date()).getTime();
if (!name) {
- check();
+ check(d);
}
};
@@ -1170,6 +1241,9 @@ var requirejs, require, define;
return newContext(cfg.context).config(cfg);
}
+ //Since config changed, mapCache may not be valid any more.
+ mapCache = {};
+
//Make sure the baseUrl ends in a slash.
if (cfg.baseUrl) {
if (cfg.baseUrl.charAt(cfg.baseUrl.length - 1) !== '/') {
@@ -1180,29 +1254,36 @@ var requirejs, require, define;
//Save off the paths and packages since they require special processing,
//they are additive.
var primId,
- pkgs = config.pkgs,
shim = config.shim,
objs = {
paths: true,
+ bundles: true,
config: true,
map: true
};
eachProp(cfg, function (value, prop) {
if (objs[prop]) {
- if (prop === 'map') {
- if (!config.map) {
- config.map = {};
- }
- mixin(config[prop], value, true, true);
- } else {
- mixin(config[prop], value, true);
+ if (!config[prop]) {
+ config[prop] = {};
}
+ mixin(config[prop], value, true, true);
} else {
config[prop] = value;
}
});
+ //Reverse map the bundles
+ if (cfg.bundles) {
+ eachProp(cfg.bundles, function (value, prop) {
+ value.forEach(function (v) {
+ if (v !== prop) {
+ bundlesMap[v] = prop;
+ }
+ });
+ });
+ }
+
//Merge shim
if (cfg.shim) {
eachProp(cfg.shim, function (value, id) {
@@ -1223,29 +1304,25 @@ var requirejs, require, define;
//Adjust packages if necessary.
if (cfg.packages) {
cfg.packages.forEach(function (pkgObj) {
- var location;
+ var location, name;
pkgObj = typeof pkgObj === 'string' ? { name: pkgObj } : pkgObj;
+
+ name = pkgObj.name;
location = pkgObj.location;
+ if (location) {
+ config.paths[name] = pkgObj.location;
+ }
- //Create a brand new object on pkgs, since currentPackages can
- //be passed in again, and config.pkgs is the internal transformed
- //state for all package configs.
- pkgs[pkgObj.name] = {
- name: pkgObj.name,
- location: location || pkgObj.name,
- //Remove leading dot in main, so main paths are normalized,
- //and remove any trailing .js, since different package
- //envs have different conventions: some use a module name,
- //some use a file name.
- main: (pkgObj.main || 'main')
- .replace(currDirRegExp, '')
- .replace(jsSuffixRegExp, '')
- };
+ //Save pointer to main module ID for pkg name.
+ //Remove leading dot in main, so main paths are normalized,
+ //and remove any trailing .js, since different package
+ //envs have different conventions: some use a module name,
+ //some use a file name.
+ config.pkgs[name] = pkgObj.name + '/' + (pkgObj.main || 'main')
+ .replace(currDirRegExp, '')
+ .replace(jsSuffixRegExp, '');
});
-
- //Done with modifications, assing packages back to context config
- config.pkgs = pkgs;
}
//If want prim injected, inject it now.
@@ -1257,7 +1334,7 @@ var requirejs, require, define;
//If a deps array or a config callback is specified, then call
//require with those args. This is useful when require is defined as a
//config object before require.js is loaded.
- if (cfg.deps) {
+ if (cfg.deps || cfg.callback) {
req(cfg.deps, cfg.callback);
}
@@ -1313,13 +1390,24 @@ var requirejs, require, define;
}
//data-main support.
- if (topReq.isBrowser) {
+ if (topReq.isBrowser && !contexts._.config.skipDataMain) {
dataMain = document.querySelectorAll('script[data-main]')[0];
dataMain = dataMain && dataMain.getAttribute('data-main');
if (dataMain) {
//Strip off any trailing .js since dataMain is now
//like a module name.
dataMain = dataMain.replace(jsSuffixRegExp, '');
+
+ if (!bootstrapConfig || !bootstrapConfig.baseUrl) {
+ //Pull off the directory of data-main for use as the
+ //baseUrl.
+ src = dataMain.split('/');
+ dataMain = src.pop();
+ subPath = src.length ? src.join('/') + '/' : './';
+
+ topReq.config({baseUrl: subPath});
+ }
+
topReq([dataMain]);
}
}

0 comments on commit d1ae1e8

Please sign in to comment.