Simplify heartbeat API.

* Move to a method to check connection errors, a better framework for future changes.
 * Remove start(), stop(), autostart.

props carldanley.
fixes #23216.



git-svn-id: http://core.svn.wordpress.org/trunk@24749 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2013-07-19 01:39:48 +00:00
parent 5e1ecc90a6
commit 30344d57b8
2 changed files with 29 additions and 45 deletions

View File

@ -246,7 +246,7 @@ function autosave_loading() {
function autosave_enable_buttons() {
jQuery(document).trigger('autosave-enable-buttons');
if ( ! wp.heartbeat.connectionLost ) {
if ( ! wp.heartbeat.hasConnectionError() ) {
// delay that a bit to avoid some rare collisions while the DOM is being updated.
setTimeout(function(){
var parent = jQuery('#submitpost');

View File

@ -40,10 +40,18 @@ window.wp = window.wp || {};
isUserActive,
userActiveEvents,
winBlurTimeout,
frameBlurTimeout = -1;
frameBlurTimeout = -1,
hasConnectionError = false;
this.autostart = true;
this.connectionLost = false;
/**
* Returns a boolean that's indicative of whether or not there is a connection error
*
* @returns boolean
* @private
*/
this.hasConnectionError = function() {
return hasConnectionError;
};
if ( typeof( window.heartbeatSettings ) == 'object' ) {
settings = $.extend( {}, window.heartbeatSettings );
@ -121,13 +129,13 @@ window.wp = window.wp || {};
break;
}
if ( trigger && ! self.connectionLost ) {
self.connectionLost = true;
if ( trigger && ! self.hasConnectionError() ) {
hasConnectionError = true;
$(document).trigger( 'heartbeat-connection-lost', [error] );
}
} else if ( self.connectionLost ) {
} else if ( self.hasConnectionError() ) {
errorcount = 0;
self.connectionLost = false;
hasConnectionError = false;
$(document).trigger( 'heartbeat-connection-restored' );
}
}
@ -152,7 +160,7 @@ window.wp = window.wp || {};
// If nothing to send (nothing is expecting a response),
// schedule the next tick and bail
if ( empty && ! self.connectionLost ) {
if ( empty && ! self.hasConnectionError() ) {
connecting = false;
next();
return;
@ -179,7 +187,7 @@ window.wp = window.wp || {};
return errorstate( 'empty' );
// Clear error state
if ( self.connectionLost )
if ( self.hasConnectionError() )
errorstate();
if ( response.nonces_expired ) {
@ -205,7 +213,7 @@ window.wp = window.wp || {};
errorstate( textStatus || 'unknown' );
self.error( jqXHR, textStatus, error );
});
};
}
function next() {
var delta = time() - tick, t = interval;
@ -332,19 +340,16 @@ window.wp = window.wp || {};
// Check for user activity every 30 seconds.
window.setInterval( function(){ checkUserActive(); }, 30000 );
if ( this.autostart ) {
$(document).ready( function() {
// Start one tick (15 sec) after DOM ready
running = true;
tick = time();
next();
});
}
$(document).ready( function() {
// Start one tick (15 sec) after DOM ready
running = true;
tick = time();
next();
});
this.hasFocus = function() {
return hasFocus;
}
};
/**
* Get/Set the interval
@ -401,27 +406,6 @@ window.wp = window.wp || {};
return tempInterval ? tempInterval / 1000 : interval / 1000;
};
// Start. Has no effect if heartbeat is already running
this.start = function() {
if ( running )
return false;
running = true;
connect();
return true;
};
// Stop. If a XHR is in progress, abort it
this.stop = function() {
if ( self.xhr && self.xhr.readyState != 4 )
self.xhr.abort();
// Reset the error state
errorstate();
running = false;
return true;
}
/**
* Enqueue data to send with the next XHR
*
@ -447,7 +431,7 @@ window.wp = window.wp || {};
return true;
}
return false;
}
};
/**
* Check if data with a particular handle is queued
@ -457,8 +441,8 @@ window.wp = window.wp || {};
*/
this.isQueued = function( handle ) {
return queue[handle];
}
}
};
};
$.extend( Heartbeat.prototype, {
tick: function( data, textStatus, jqXHR ) {