Heartbeat: introduce "suspend" functionality and enable it after 20 min. of inactivity, see #25073.

Built from https://develop.svn.wordpress.org/trunk@26428


git-svn-id: http://core.svn.wordpress.org/trunk@26328 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz
2013-11-27 01:56:10 +00:00
parent 29af7b55f3
commit 162037903d
3 changed files with 56 additions and 6 deletions

View File

@@ -34,8 +34,11 @@
var Heartbeat = function() {
var $document = $(document),
settings = {
// Used to stop the "beat"
isRunning: true,
// Suspend/resume
suspend: false,
// Whether suspending is enabled
suspendEnabled: true,
// Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front'
screenId: '',
@@ -128,6 +131,10 @@
if ( ! settings.screenId ) {
settings.screenId = options.screenId || 'front';
}
if ( options.suspend === 'disable' ) {
settings.suspendEnabled = false;
}
}
// Convert to milliseconds
@@ -145,7 +152,12 @@
focused();
}).on( 'unload.wp-heartbeat', function() {
// Don't connect any more
settings.isRunning = false;
settings.suspend = true;
// Abort the last request if not completed
if ( settings.xhr && settings.xhr.readyState !== 4 ) {
settings.xhr.abort();
}
});
// Check for user activity every 30 seconds.
@@ -274,7 +286,7 @@
// If the connection to the server is slower than the interval,
// heartbeat connects as soon as the previous connection's response is received.
if ( settings.connecting ) {
if ( settings.connecting || settings.suspend ) {
return;
}
@@ -351,7 +363,7 @@
var delta = time() - settings.lastTick,
interval = settings.mainInterval;
if ( ! settings.isRunning ) {
if ( settings.suspend ) {
return;
}
@@ -403,6 +415,9 @@
clearFocusTimers();
settings.userActivity = time();
// Resume if suspended
settings.suspend = false;
if ( ! settings.hasFocus ) {
settings.hasFocus = true;
scheduleNextTick();
@@ -513,6 +528,11 @@
blurred();
}
if ( settings.suspendEnabled && lastActive > 1200000 ) {
// Suspend after 20 min. of inactivity
settings.suspend = true;
}
if ( ! settings.userActivityEvents ) {
$document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } );
@@ -561,6 +581,19 @@
scheduleNextTick();
}
/**
* Disable suspending
*
* Should be used only when Heartbeat is performing critical tasks like autosave, post-locking, etc.
* Using this on many screens may overload the user's hosting account if several
* browser windows/tabs are left open for a long time.
*
* @return void
*/
function disableSuspend() {
settings.suspendEnabled = false;
}
/**
* Get/Set the interval
*
@@ -691,6 +724,7 @@
return {
hasFocus: hasFocus,
connectNow: connectNow,
disableSuspend: disableSuspend,
setInterval: setInterval,
hasConnectionError: hasConnectionError,
enqueue: enqueue,

File diff suppressed because one or more lines are too long