Make WordPress Core

Changeset 24268


Ignore:
Timestamp:
05/15/2013 11:17:51 PM (13 years ago)
Author:
azaozz
Message:

Heartbeat: don't connect when a response is not expected by any script (nothing to send), remove debug logging, clean up code formatting, see #23216

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/heartbeat.js

    r24139 r24268  
    112112
    113113                function connect() {
    114                         var data = {};
     114                        var send = {}, data, i, empty = true;
    115115                        tick = time();
    116116
    117                         data.data = $.extend( {}, queue );
     117                        data = $.extend( {}, queue );
    118118                        // Clear the data queue, anything added after this point will be send on the next tick
    119119                        queue = {};
    120120
    121                         $(document).trigger( 'heartbeat-send', [data.data] );
    122 
    123                         data.interval = interval / 1000;
    124                         data._nonce = nonce;
    125                         data.action = 'heartbeat';
    126                         data.screenid = screenid;
    127                         data.has_focus = hasFocus;
     121                        $(document).trigger( 'heartbeat-send', [data] );
     122
     123                        for ( i in data ) {
     124                                if ( data.hasOwnProperty( i ) ) {
     125                                        empty = false;
     126                                        break;
     127                                }
     128                        }
     129
     130                        // If nothing to send (nothing is expecting a response),
     131                        // schedule the next tick and bail
     132                        if ( empty ) {
     133                                connecting = false;
     134                                next();
     135                                return;
     136                        }
     137
     138                        send.data = data;
     139                        send.interval = interval / 1000;
     140                        send._nonce = nonce;
     141                        send.action = 'heartbeat';
     142                        send.screenid = screenid;
     143                        send.has_focus = hasFocus;
    128144
    129145                        connecting = true;
     
    132148                                type: 'post',
    133149                                timeout: 30000, // throw an error of not completed after 30 sec.
    134                                 data: data,
     150                                data: send,
    135151                                dataType: 'json'
    136                         }).done( function( data, textStatus, jqXHR ) {
     152                        }).done( function( response, textStatus, jqXHR ) {
    137153                                var new_interval, timed;
    138154
    139                                 if ( ! data )
     155                                if ( ! response )
    140156                                        return errorstate( 'empty' );
    141157
     
    145161
    146162                                // Change the interval from PHP
    147                                 new_interval = data.heartbeat_interval;
    148                                 delete data.heartbeat_interval;
    149 
    150                                 self.tick( data, textStatus, jqXHR );
     163                                if ( response.heartbeat_interval ) {
     164                                        new_interval = response.heartbeat_interval;
     165                                        delete response.heartbeat_interval;
     166                                }
     167
     168                                self.tick( response, textStatus, jqXHR );
    151169
    152170                                // do this last, can trigger the next XHR if connection time > 5 sec. and new_interval == 'fast'
     
    165183                        var delta = time() - tick, t = interval;
    166184
    167                         if ( !running )
     185                        if ( ! running )
    168186                                return;
    169187
    170                         if ( !hasFocus ) {
     188                        if ( ! hasFocus ) {
    171189                                t = 120000; // 2 min
    172190                        } else if ( countdown > 0 && tempInterval ) {
     
    196214
    197215                        hasFocus = false;
    198 
    199                         // temp debug
    200                         if ( self.debug )
    201                                 console.log('### blurred(), slow down...')
    202216                }
    203217
     
    215229                        window.clearTimeout(beat);
    216230
    217                         if ( !connecting )
     231                        if ( ! connecting )
    218232                                next();
    219 
    220                         // temp debug
    221                         if ( self.debug )
    222                                 console.log('### focused(), speed up... ')
    223233                }
    224234
    225235                function setFrameEvents() {
    226                         $('iframe').each( function(i, frame){
    227                                 if ( !isLocalFrame(frame) )
     236                        $('iframe').each( function( i, frame ){
     237                                if ( ! isLocalFrame( frame ) )
    228238                                        return;
    229239
    230                                 if ( $.data(frame, 'wp-heartbeat-focus') )
     240                                if ( $.data( frame, 'wp-heartbeat-focus' ) )
    231241                                        return;
    232242
    233                                 $.data(frame, 'wp-heartbeat-focus', 1);
    234 
    235                                 $(frame.contentWindow).on('focus.wp-heartbeat-focus', function(e){
     243                                $.data( frame, 'wp-heartbeat-focus', 1 );
     244
     245                                $( frame.contentWindow ).on( 'focus.wp-heartbeat-focus', function(e) {
    236246                                        focused();
    237                                 }).on('blur.wp-heartbeat-focus', function(e){
     247                                }).on('blur.wp-heartbeat-focus', function(e) {
    238248                                        setFrameEvents();
    239249                                        frameBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
     
    242252                }
    243253
    244                 $(window).on('blur.wp-heartbeat-focus', function(e) {
     254                $(window).on( 'blur.wp-heartbeat-focus', function(e) {
    245255                        setFrameEvents();
    246256                        winBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
    247                 }).on('focus.wp-heartbeat-focus', function() {
    248                         $('iframe').each( function(i, frame){
    249                                 if ( !isLocalFrame(frame) )
     257                }).on( 'focus.wp-heartbeat-focus', function() {
     258                        $('iframe').each( function( i, frame ) {
     259                                if ( !isLocalFrame( frame ) )
    250260                                        return;
    251261
    252                                 $.removeData(frame, 'wp-heartbeat-focus');
    253                                 $(frame.contentWindow).off('.wp-heartbeat-focus');
     262                                $.removeData( frame, 'wp-heartbeat-focus' );
     263                                $( frame.contentWindow ).off( '.wp-heartbeat-focus' );
    254264                        });
    255265
     
    259269                function userIsActive() {
    260270                        userActiveEvents = false;
    261                         $(document).off('.wp-heartbeat-active');
    262                         $('iframe').each( function(i, frame){
    263                                 if ( !isLocalFrame(frame) )
     271                        $(document).off( '.wp-heartbeat-active' );
     272                        $('iframe').each( function( i, frame ) {
     273                                if ( ! isLocalFrame( frame ) )
    264274                                        return;
    265275
    266                                 $(frame.contentWindow).off('.wp-heartbeat-active');
     276                                $( frame.contentWindow ).off( '.wp-heartbeat-active' );
    267277                        });
    268278
    269279                        focused();
    270 
    271                         // temp debug
    272                         if ( self.debug )
    273                                 console.log( 'userIsActive()' );
    274280                }
    275281
     
    279285                        var lastActive = isUserActive ? time() - isUserActive : 0;
    280286
    281                         // temp debug
    282                         if ( self.debug )
    283                                 console.log( 'checkUserActive(), lastActive = %s seconds ago', parseInt(lastActive / 1000) || 'null' );
    284 
    285287                        // Throttle down when no mouse or keyboard activity for 5 min
    286288                        if ( lastActive > 300000 && hasFocus )
    287289                                 blurred();
    288290
    289                         if ( !userActiveEvents ) {
    290                                 $(document).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });
    291 
    292                                 $('iframe').each( function(i, frame){
    293                                         if ( !isLocalFrame(frame) )
     291                        if ( ! userActiveEvents ) {
     292                                $(document).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } );
     293
     294                                $('iframe').each( function( i, frame ) {
     295                                        if ( ! isLocalFrame( frame ) )
    294296                                                return;
    295297
    296                                         $(frame.contentWindow).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });
     298                                        $( frame.contentWindow ).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } );
    297299                                });
    298300
     
    305307
    306308                if ( this.autostart ) {
    307                         $(document).ready( function(){
     309                        $(document).ready( function() {
    308310                                // Start one tick (15 sec) after DOM ready
    309311                                running = true;
     
    363365                        }
    364366
    365                         if ( !hasFocus )
     367                        if ( ! hasFocus )
    366368                                return 120;
    367369
     
    395397                 * As the data is sent later, this function doesn't return the XHR response.
    396398                 * To see the response, use the custom jQuery event 'heartbeat-tick' on the document, example:
    397                  *              $(document).on('heartbeat-tick.myname', function(data, textStatus, jqXHR) {
     399                 *              $(document).on( 'heartbeat-tick.myname', function( event, data, textStatus, jqXHR ) {
    398400                 *                      // code
    399401                 *              });
    400                  * If the same 'handle' is used more than once, the data is overwritten when the third argument is 'true'.
     402                 * If the same 'handle' is used more than once, the data is not overwritten when the third argument is 'true'.
    401403                 * Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle.
    402404                 *
     
    408410                this.enqueue = function( handle, data, dont_overwrite ) {
    409411                        if ( handle ) {
    410                                 if ( queue.hasOwnProperty(handle) && dont_overwrite )
     412                                if ( queue.hasOwnProperty( handle ) && dont_overwrite )
    411413                                        return false;
    412414
Note: See TracChangeset for help on using the changeset viewer.