e371fe50702011b7021c8d79181e88184c23448bAdam Moore/**
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * Optional automatic rollup logic for reducing http connections
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * when not using a combo service.
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * @module loader
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * @submodule rollup
e371fe50702011b7021c8d79181e88184c23448bAdam Moore */
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore/**
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * Look for rollup packages to determine if all of the modules a
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * rollup supersedes are required. If so, include the rollup to
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * help reduce the total number of connections required. Called
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * by calculate(). This is an optional feature, and requires the
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * appropriate submodule to function.
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * @method _rollup
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * @for Loader
e371fe50702011b7021c8d79181e88184c23448bAdam Moore * @private
e371fe50702011b7021c8d79181e88184c23448bAdam Moore */
e371fe50702011b7021c8d79181e88184c23448bAdam MooreY.Loader.prototype._rollup = function() {
54119bda17c0eb663f4d2147df7363de8019647fAdam Moore var i, j, m, s, r = this.required, roll,
6eb4be1e0f37f895f72bc59d0d9efd610eb78100Adam Moore info = this.moduleInfo, rolled, c, smod;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // find and cache rollup modules
e371fe50702011b7021c8d79181e88184c23448bAdam Moore if (this.dirty || !this.rollups) {
54119bda17c0eb663f4d2147df7363de8019647fAdam Moore this.rollups = {};
e371fe50702011b7021c8d79181e88184c23448bAdam Moore for (i in info) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore if (info.hasOwnProperty(i)) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore m = this.getModule(i);
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // if (m && m.rollup && m.supersedes) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore if (m && m.rollup) {
54119bda17c0eb663f4d2147df7363de8019647fAdam Moore this.rollups[i] = m;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // make as many passes as needed to pick up rollup rollups
e371fe50702011b7021c8d79181e88184c23448bAdam Moore for (;;) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore rolled = false;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // go through the rollup candidates
54119bda17c0eb663f4d2147df7363de8019647fAdam Moore for (i in this.rollups) {
54119bda17c0eb663f4d2147df7363de8019647fAdam Moore if (this.rollups.hasOwnProperty(i)) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // there can be only one, unless forced
e371fe50702011b7021c8d79181e88184c23448bAdam Moore if (!r[i] && ((!this.loaded[i]) || this.forceMap[i])) {
531a5eaa1b3229b1e88802ccfeb1ece1ef004f53Adam Moore m = this.getModule(i);
531a5eaa1b3229b1e88802ccfeb1ece1ef004f53Adam Moore s = m.supersedes || [];
e371fe50702011b7021c8d79181e88184c23448bAdam Moore roll = false;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // @TODO remove continue
e371fe50702011b7021c8d79181e88184c23448bAdam Moore if (!m.rollup) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore continue;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore c = 0;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // check the threshold
531a5eaa1b3229b1e88802ccfeb1ece1ef004f53Adam Moore for (j = 0; j < s.length; j++) {
6eb4be1e0f37f895f72bc59d0d9efd610eb78100Adam Moore smod = info[s[j]];
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
531a5eaa1b3229b1e88802ccfeb1ece1ef004f53Adam Moore // if the superseded module is loaded, we can't
b39897a381c2203466da5568bfd2862a54a81311Adam Moore // load the rollup unless it has been forced.
e371fe50702011b7021c8d79181e88184c23448bAdam Moore if (this.loaded[s[j]] && !this.forceMap[s[j]]) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore roll = false;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore break;
531a5eaa1b3229b1e88802ccfeb1ece1ef004f53Adam Moore // increment the counter if this module is required.
531a5eaa1b3229b1e88802ccfeb1ece1ef004f53Adam Moore // if we are beyond the rollup threshold, we will
b39897a381c2203466da5568bfd2862a54a81311Adam Moore // use the rollup module
6eb4be1e0f37f895f72bc59d0d9efd610eb78100Adam Moore } else if (r[s[j]] && m.type == smod.type) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore c++;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // Y.log("adding to thresh: " + c + ", " + s[j]);
e371fe50702011b7021c8d79181e88184c23448bAdam Moore roll = (c >= m.rollup);
e371fe50702011b7021c8d79181e88184c23448bAdam Moore if (roll) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // Y.log("over thresh " + c + ", " + s[j]);
e371fe50702011b7021c8d79181e88184c23448bAdam Moore break;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore if (roll) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // Y.log("adding rollup: " + i);
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // add the rollup
e371fe50702011b7021c8d79181e88184c23448bAdam Moore r[i] = true;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore rolled = true;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // expand the rollup's dependencies
e371fe50702011b7021c8d79181e88184c23448bAdam Moore this.getRequires(m);
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore
e371fe50702011b7021c8d79181e88184c23448bAdam Moore // if we made it here w/o rolling up something, we are done
e371fe50702011b7021c8d79181e88184c23448bAdam Moore if (!rolled) {
e371fe50702011b7021c8d79181e88184c23448bAdam Moore break;
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore }
e371fe50702011b7021c8d79181e88184c23448bAdam Moore};