Skip to content

Commit

Permalink
Core: use different ready code when Deferred is excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
timmywil committed Feb 1, 2016
1 parent 3267321 commit 54836a5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 44 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module.exports = function( grunt ) {
callbacks: [ "deferred" ],
css: [ "effects", "dimensions", "offset" ],
"css/showHide": [ "effects" ],
deferred: [ "ajax", "effects" ],
sizzle: [ "css/hiddenVisibleSelectors", "effects/animatedSelector" ]
}
}
Expand Down
116 changes: 116 additions & 0 deletions src/core/ready-no-deferred.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
define( [
"../core",
"../var/document"
], function( jQuery, document ) {

var readyCallbacks = [],
readyFiring = false,
whenReady = function( fn ) {
readyCallbacks.push( fn );
};

jQuery.fn.ready = function( fn ) {
whenReady( fn );
return this;
};

jQuery.extend( {

// Is the DOM ready to be used? Set to true once it occurs.
isReady: false,

// A counter to track how many items to wait for before
// the ready event fires. See #6781
readyWait: 1,

// Hold (or release) the ready event
holdReady: function( hold ) {
if ( hold ) {
jQuery.readyWait++;
} else {
jQuery.ready( true );
}
},

ready: function( wait ) {

// Abort if there are pending holds or we're already ready
if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
return;
}

// Remember that the DOM is ready
jQuery.isReady = true;

// If a normal DOM Ready event fired, decrement, and wait if need be
if ( wait !== true && --jQuery.readyWait > 0 ) {
return;
}

whenReady = function( fn ) {
readyCallbacks.push( fn );

if ( !readyFiring ) {
readyFiring = true;

// Prevent errors from freezing future callback execution (gh-1823)
try {
while ( readyCallbacks.length ) {
fn = readyCallbacks.shift();
if ( jQuery.isFunction( fn ) ) {

// For backwards compatibility,
// invoke synchronously and with document context
fn.call( document, jQuery );
}
}
} finally {
readyFiring = false;

// If there was an error in a ready callback,
// continue with the rest (gh-1823)
if ( readyCallbacks.length ) {

// Retry async to allow the error to propagate to console
window.setTimeout( whenReady );
}
}
}
};

whenReady();
}
} );

// Make jQuery.ready Promise consumable (gh-1778)
jQuery.ready.then = jQuery.fn.ready;

/**
* The ready event handler and self cleanup method
*/
function completed() {
document.removeEventListener( "DOMContentLoaded", completed );
window.removeEventListener( "load", completed );
jQuery.ready();
}

// Catch cases where $(document).ready() is called
// after the browser event has already occurred.
// Support: IE9-10 only
// Older IE sometimes signals "interactive" too soon
if ( document.readyState === "complete" ||
( document.readyState !== "loading" && !document.documentElement.doScroll ) ) {

// Handle it asynchronously to allow scripts the opportunity to delay ready
window.setTimeout( jQuery.ready );

} else {

// Use the handy event callback
document.addEventListener( "DOMContentLoaded", completed );

// A fallback to window.onload, that will always work
window.addEventListener( "load", completed );
}

} );
58 changes: 14 additions & 44 deletions src/core/ready.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
define( [
"../core",
"../var/document"
"../var/document",
"../deferred"
], function( jQuery, document ) {

var readyCallbacks = [],
readyFiring = false,
whenReady = function( fn ) {
readyCallbacks.push( fn );
};
// The deferred used on DOM ready
var readyList = jQuery.Deferred(),
readyPromise = readyList.promise();

jQuery.fn.ready = function( fn ) {
whenReady( fn );

// Add the callback
readyPromise.done( fn );

return this;
};

Expand All @@ -32,6 +34,7 @@ jQuery.extend( {
}
},

// Handle when the DOM is ready
ready: function( wait ) {

// Abort if there are pending holds or we're already ready
Expand All @@ -47,47 +50,14 @@ jQuery.extend( {
return;
}

whenReady = function( fn ) {
readyCallbacks.push( fn );

if ( !readyFiring ) {
readyFiring = true;

// Prevent errors from freezing future callback execution (gh-1823)
try {
while ( readyCallbacks.length ) {
fn = readyCallbacks.shift();
if ( jQuery.isFunction( fn ) ) {

// For backwards compatibility,
// invoke synchronously and with document context
fn.call( document, jQuery );
}
}
} finally {
readyFiring = false;

// If there was an error in a ready callback,
// continue with the rest (gh-1823)
if ( readyCallbacks.length ) {

// Retry async to allow the error to propagate to console
window.setTimeout( whenReady );
}
}
}
};

whenReady();
// If there are functions bound, to execute
readyList.resolveWith( document, [ jQuery ] );
}
} );

// Make jQuery.ready Promise consumable (gh-1778)
jQuery.ready.then = jQuery.fn.ready;
jQuery.ready.then = readyPromise.then;

/**
* The ready event handler and self cleanup method
*/
// The ready event handler and self cleanup method
function completed() {
document.removeEventListener( "DOMContentLoaded", completed );
window.removeEventListener( "load", completed );
Expand Down

0 comments on commit 54836a5

Please sign in to comment.