<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[23882] trunk/wp-includes: Heartbeat: improve setting the errorstate, add ajaxurl to the settings object when loading on the front-end, some code cleanup, see #23216</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/23882">23882</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2013-03-30 23:32:12 +0000 (Sat, 30 Mar 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Heartbeat: improve setting the errorstate, add ajaxurl to the settings object when loading on the front-end, some code cleanup, see <a href="http://core.trac.wordpress.org/ticket/23216">#23216</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesgeneraltemplatephp">trunk/wp-includes/general-template.php</a></li>
<li><a href="#trunkwpincludesjsheartbeatjs">trunk/wp-includes/js/heartbeat.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesgeneraltemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/general-template.php (23881 => 23882)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/general-template.php        2013-03-30 19:17:09 UTC (rev 23881)
+++ trunk/wp-includes/general-template.php        2013-03-30 23:32:12 UTC (rev 23882)
</span><span class="lines">@@ -2333,6 +2333,9 @@
</span><span class="cx">  * @return array $settings
</span><span class="cx">  */
</span><span class="cx"> function wp_heartbeat_settings( $settings ) {
</span><ins>+        if ( ! is_admin() )
+                $settings['ajaxurl'] = admin_url( 'admin-ajax.php', 'relative' );
+
</ins><span class="cx">         if ( is_user_logged_in() )
</span><span class="cx">                 $settings['nonce'] = wp_create_nonce( 'heartbeat-nonce' );
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesjsheartbeatjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/heartbeat.js (23881 => 23882)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/heartbeat.js        2013-03-30 19:17:09 UTC (rev 23881)
+++ trunk/wp-includes/js/heartbeat.js        2013-03-30 23:32:12 UTC (rev 23882)
</span><span class="lines">@@ -12,13 +12,14 @@
</span><span class="cx">                         beat,
</span><span class="cx">                         nonce,
</span><span class="cx">                         screenid = typeof pagenow != 'undefined' ? pagenow : '',
</span><ins>+                        url = typeof ajaxurl != 'undefined' ? ajaxurl : '',
</ins><span class="cx">                         settings,
</span><span class="cx">                         tick = 0,
</span><span class="cx">                         queue = {},
</span><span class="cx">                         interval,
</span><del>-                        lastconnect = 0,
</del><span class="cx">                         connecting,
</span><del>-                        countdown,
</del><ins>+                        countdown = 0,
+                        errorcount = 0,
</ins><span class="cx">                         tempInterval,
</span><span class="cx">                         hasFocus = true,
</span><span class="cx">                         isUserActive,
</span><span class="lines">@@ -26,17 +27,19 @@
</span><span class="cx">                         winBlurTimeout,
</span><span class="cx">                         frameBlurTimeout = -1;
</span><span class="cx"> 
</span><del>-                this.url = typeof ajaxurl != 'undefined' ? ajaxurl : 'wp-admin/admin-ajax.php';
</del><span class="cx">                 this.autostart = true;
</span><ins>+                this.connectionLost = false;
</ins><span class="cx"> 
</span><span class="cx">                 if ( typeof( window.heartbeatSettings != 'undefined' ) ) {
</span><del>-                        settings = $.extend( {}, window.heartbeatSettings );
-                        window.heartbeatSettings = null;
</del><ins>+                        settings = window.heartbeatSettings;
</ins><span class="cx"> 
</span><span class="cx">                         // Add private vars
</span><span class="cx">                         nonce = settings.nonce || '';
</span><span class="cx">                         delete settings.nonce;
</span><span class="cx"> 
</span><ins>+                        url = settings.ajaxurl || url;
+                        delete settings.ajaxurl;
+
</ins><span class="cx">                         interval = settings.interval || 15; // default interval
</span><span class="cx">                         delete settings.interval;
</span><span class="cx">                         // The interval can be from 5 to 60 sec.
</span><span class="lines">@@ -47,7 +50,6 @@
</span><span class="cx"> 
</span><span class="cx">                         interval = interval * 1000;
</span><span class="cx"> 
</span><del>-                        // todo: needed?
</del><span class="cx">                         // 'screenid' can be added from settings on the front-end where the JS global 'pagenow' is not set
</span><span class="cx">                         screenid = screenid || settings.screenid || 'site';
</span><span class="cx">                         delete settings.screenid;
</span><span class="lines">@@ -72,21 +74,39 @@
</span><span class="cx">                         return false;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                // Set error state and fire an event if errors persist for over 2 min when the window has focus
-                // or 6 min when the window is in the background
-                function errorstate() {
-                        var since;
</del><ins>+                // Set error state and fire an event if XHR errors or timeout
+                function errorstate( error ) {
+                        var trigger;
</ins><span class="cx"> 
</span><del>-                        if ( lastconnect ) {
-                                since = time() - lastconnect, duration = hasFocus ? 120000 : 360000;
</del><ins>+                        if ( error ) {
+                                switch ( error ) {
+                                        case 'abort':
+                                                // do nothing
+                                                break;
+                                        case 'timeout':
+                                                // no response for 30 sec.
+                                                trigger = true;
+                                                break;
+                                        case 'parsererror':
+                                        case 'error':
+                                        case 'empty':
+                                        case 'unknown':
+                                                errorcount++;
</ins><span class="cx"> 
</span><del>-                                if ( since &gt; duration ) {
</del><ins>+                                                if ( errorcount &gt; 2 )
+                                                        trigger = true;
+
+                                                break;
+                                }
+
+                                if ( trigger &amp;&amp; ! self.connectionLost ) {
</ins><span class="cx">                                         self.connectionLost = true;
</span><del>-                                        $(document).trigger( 'heartbeat-connection-lost', parseInt(since / 1000) );
-                                } else if ( self.connectionLost ) {
-                                        self.connectionLost = false;
-                                        $(document).trigger( 'heartbeat-connection-restored' );
</del><ins>+                                        $(document).trigger( 'heartbeat-connection-lost' );
</ins><span class="cx">                                 }
</span><ins>+                        } else if ( self.connectionLost ) {
+                                errorcount = 0;
+                                self.connectionLost = false;
+                                $(document).trigger( 'heartbeat-connection-restored' );
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="lines">@@ -95,6 +115,9 @@
</span><span class="cx">                         tick = time();
</span><span class="cx"> 
</span><span class="cx">                         data.data = $.extend( {}, queue );
</span><ins>+                        // Clear the data queue, anything added after this point will be send on the next tick
+                        queue = {};
+
</ins><span class="cx">                         $(document).trigger( 'heartbeat-send', [data.data] );
</span><span class="cx"> 
</span><span class="cx">                         data.interval = interval / 1000;
</span><span class="lines">@@ -104,32 +127,36 @@
</span><span class="cx">                         data.has_focus = hasFocus;
</span><span class="cx"> 
</span><span class="cx">                         connecting = true;
</span><del>-                        self.xhr = $.post( self.url, data, 'json' )
-                        .done( function( data, textStatus, jqXHR ) {
-                                var interval;
</del><ins>+                        self.xhr = $.ajax({
+                                url: url,
+                                type: 'post',
+                                timeout: 30000, // throw an error of not completed after 30 sec.
+                                data: data,
+                                dataType: 'json'
+                        }).done( function( data, textStatus, jqXHR ) {
+                                var new_interval, timed;
</ins><span class="cx"> 
</span><del>-                                // Clear the data queue
-                                queue = {};
</del><ins>+                                if ( ! data )
+                                        return errorstate( 'empty' );
</ins><span class="cx"> 
</span><span class="cx">                                 // Clear error state
</span><del>-                                lastconnect = time();
</del><span class="cx">                                 if ( self.connectionLost )
</span><span class="cx">                                         errorstate();
</span><span class="cx"> 
</span><span class="cx">                                 // Change the interval from PHP
</span><del>-                                interval = data.heartbeat_interval;
</del><ins>+                                new_interval = data.heartbeat_interval;
</ins><span class="cx">                                 delete data.heartbeat_interval;
</span><span class="cx"> 
</span><span class="cx">                                 self.tick( data, textStatus, jqXHR );
</span><span class="cx"> 
</span><del>-                                // do this last, can trigger the next XHR
-                                if ( interval )
-                                        self.interval.apply( self, data.heartbeat_interval );
-                        }).always( function(){
</del><ins>+                                // do this last, can trigger the next XHR if connection time &gt; 5 sec. and new_interval == 'fast'
+                                if ( new_interval )
+                                        self.interval.call( self, new_interval );
+                        }).always( function() {
</ins><span class="cx">                                 connecting = false;
</span><span class="cx">                                 next();
</span><del>-                        }).fail( function( jqXHR, textStatus, error ){
-                                errorstate();
</del><ins>+                        }).fail( function( jqXHR, textStatus, error ) {
+                                errorstate( textStatus || 'unknown' );
</ins><span class="cx">                                 self.error( jqXHR, textStatus, error );
</span><span class="cx">                         });
</span><span class="cx">                 };
</span><span class="lines">@@ -142,7 +169,7 @@
</span><span class="cx"> 
</span><span class="cx">                         if ( !hasFocus ) {
</span><span class="cx">                                 t = 120000; // 2 min
</span><del>-                        } else if ( countdown &amp;&amp; tempInterval ) {
</del><ins>+                        } else if ( countdown &gt; 0 &amp;&amp; tempInterval ) {
</ins><span class="cx">                                 t = tempInterval;
</span><span class="cx">                                 countdown--;
</span><span class="cx">                         }
</span><span class="lines">@@ -214,10 +241,10 @@
</span><span class="cx">                         });
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                $(window).on('blur.wp-heartbeat-focus', function(e){
</del><ins>+                $(window).on('blur.wp-heartbeat-focus', function(e) {
</ins><span class="cx">                         setFrameEvents();
</span><span class="cx">                         winBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
</span><del>-                }).on('focus.wp-heartbeat-focus', function(){
</del><ins>+                }).on('focus.wp-heartbeat-focus', function() {
</ins><span class="cx">                         $('iframe').each( function(i, frame){
</span><span class="cx">                                 if ( !isLocalFrame(frame) )
</span><span class="cx">                                         return;
</span><span class="lines">@@ -261,12 +288,14 @@
</span><span class="cx"> 
</span><span class="cx">                         if ( !userActiveEvents ) {
</span><span class="cx">                                 $(document).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });
</span><ins>+
</ins><span class="cx">                                 $('iframe').each( function(i, frame){
</span><span class="cx">                                         if ( !isLocalFrame(frame) )
</span><span class="cx">                                                 return;
</span><span class="cx"> 
</span><span class="cx">                                         $(frame.contentWindow).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });
</span><span class="cx">                                 });
</span><ins>+
</ins><span class="cx">                                 userActiveEvents = true;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="lines">@@ -283,33 +312,31 @@
</span><span class="cx">                         });
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                this.winHasFocus = function() {
</del><ins>+                this.hasFocus = function() {
</ins><span class="cx">                         return hasFocus;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /**
</span><span class="cx">                  * Get/Set the interval
</span><span class="cx">                  *
</span><del>-                 * When setting the interval to 'fast', the number of ticks is specified wiht the second argument, default 30.
-                 * If the window doesn't have focus, the interval is overridden to 2 min. In this case setting the 'ticks'
-                 * will start counting after the window gets focus.
</del><ins>+                 * When setting to 'fast', the interval is 5 sec. for the next 30 ticks (for 2 min and 30 sec).
+                 * If the window doesn't have focus, the interval slows down to 2 min.
</ins><span class="cx">                  *
</span><span class="cx">                  * @param string speed Interval speed: 'fast' (5sec), 'standard' (15sec) default, 'slow' (60sec)
</span><del>-                 * @param int ticks Number of ticks for the changed interval, optional when setting 'standard' or 'slow'
</del><span class="cx">                  * @return int Current interval in seconds
</span><span class="cx">                  */
</span><del>-                this.interval = function(speed, ticks) {
</del><ins>+                this.interval = function( speed ) {
</ins><span class="cx">                         var reset, seconds;
</span><span class="cx"> 
</span><span class="cx">                         if ( speed ) {
</span><span class="cx">                                 switch ( speed ) {
</span><span class="cx">                                         case 'fast':
</span><span class="cx">                                                 seconds = 5;
</span><del>-                                                countdown = parseInt(ticks) || 30;
</del><ins>+                                                countdown = 30;
</ins><span class="cx">                                                 break;
</span><span class="cx">                                         case 'slow':
</span><span class="cx">                                                 seconds = 60;
</span><del>-                                                countdown = parseInt(ticks) || 0;
</del><ins>+                                                countdown = 0;
</ins><span class="cx">                                                 break;
</span><span class="cx">                                         case 'long-polling':
</span><span class="cx">                                                 // Allow long polling, (experimental)
</span><span class="lines">@@ -324,7 +351,7 @@
</span><span class="cx">                                 // Reset when the new interval value is lower than the current one
</span><span class="cx">                                 reset = seconds * 1000 &lt; interval;
</span><span class="cx"> 
</span><del>-                                if ( countdown ) {
</del><ins>+                                if ( countdown &gt; 0 ) {
</ins><span class="cx">                                         tempInterval = seconds * 1000;
</span><span class="cx">                                 } else {
</span><span class="cx">                                         interval = seconds * 1000;
</span><span class="lines">@@ -361,7 +388,7 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 /**
</span><del>-                 * Send data with the next XHR
</del><ins>+                 * Enqueue data to send with the next XHR
</ins><span class="cx">                  *
</span><span class="cx">                  * As the data is sent later, this function doesn't return the XHR response.
</span><span class="cx">                  * To see the response, use the custom jQuery event 'heartbeat-tick' on the document, example:
</span><span class="lines">@@ -371,14 +398,14 @@
</span><span class="cx">                  * If the same 'handle' is used more than once, the data is overwritten when the third argument is 'true'.
</span><span class="cx">                  * Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle.
</span><span class="cx">                  *
</span><del>-                 * $param string handle Unique handle for the data. The handle is used in PHP to receive the data
-                 * $param mixed data The data to be sent
-                 * $param bool overwrite Whether to overwrite existing data in the queue
-                 * $return bool Whether the data was queued or not
</del><ins>+                 * $param string handle Unique handle for the data. The handle is used in PHP to receive the data.
+                 * $param mixed data The data to send.
+                 * $param bool dont_overwrite Whether to overwrite existing data in the queue.
+                 * $return bool Whether the data was queued or not.
</ins><span class="cx">                  */
</span><del>-                this.send = function(handle, data, overwrite) {
</del><ins>+                this.enqueue = function( handle, data, dont_overwrite ) {
</ins><span class="cx">                         if ( handle ) {
</span><del>-                                if ( queue.hasOwnProperty(handle) &amp;&amp; !overwrite )
</del><ins>+                                if ( queue.hasOwnProperty(handle) &amp;&amp; dont_overwrite )
</ins><span class="cx">                                         return false;
</span><span class="cx"> 
</span><span class="cx">                                 queue[handle] = data;
</span><span class="lines">@@ -393,16 +420,16 @@
</span><span class="cx">                  * $param string handle The handle for the data
</span><span class="cx">                  * $return mixed The data queued with that handle or null
</span><span class="cx">                  */
</span><del>-                this.isQueued = function(handle) {
</del><ins>+                this.isQueued = function( handle ) {
</ins><span class="cx">                         return queue[handle];
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         $.extend( Heartbeat.prototype, {
</span><del>-                tick: function(data, textStatus, jqXHR) {
</del><ins>+                tick: function( data, textStatus, jqXHR ) {
</ins><span class="cx">                         $(document).trigger( 'heartbeat-tick', [data, textStatus, jqXHR] );
</span><span class="cx">                 },
</span><del>-                error: function(jqXHR, textStatus, error) {
</del><ins>+                error: function( jqXHR, textStatus, error ) {
</ins><span class="cx">                         $(document).trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
</span><span class="cx">                 }
</span><span class="cx">         });
</span></span></pre>
</div>
</div>

</body>
</html>