Make WordPress Core

Changeset 24749


Ignore:
Timestamp:
07/19/2013 01:39:48 AM (11 years ago)
Author:
nacin
Message:

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.

Location:
trunk/wp-includes/js
Files:
2 edited

Legend:

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

    r24747 r24749  
    247247function autosave_enable_buttons() {
    248248    jQuery(document).trigger('autosave-enable-buttons');
    249     if ( ! wp.heartbeat.connectionLost ) {
     249    if ( ! wp.heartbeat.hasConnectionError() ) {
    250250        // delay that a bit to avoid some rare collisions while the DOM is being updated.
    251251        setTimeout(function(){
  • trunk/wp-includes/js/heartbeat.js

    r24705 r24749  
    4141            userActiveEvents,
    4242            winBlurTimeout,
    43             frameBlurTimeout = -1;
    44 
    45         this.autostart = true;
    46         this.connectionLost = false;
     43            frameBlurTimeout = -1,
     44            hasConnectionError = false;
     45
     46        /**
     47         * Returns a boolean that's indicative of whether or not there is a connection error
     48         *
     49         * @returns boolean
     50         * @private
     51         */
     52        this.hasConnectionError = function() {
     53            return hasConnectionError;
     54        };
    4755
    4856        if ( typeof( window.heartbeatSettings ) == 'object' ) {
     
    122130                }
    123131
    124                 if ( trigger && ! self.connectionLost ) {
    125                     self.connectionLost = true;
     132                if ( trigger && ! self.hasConnectionError() ) {
     133                    hasConnectionError = true;
    126134                    $(document).trigger( 'heartbeat-connection-lost', [error] );
    127135                }
    128             } else if ( self.connectionLost ) {
     136            } else if ( self.hasConnectionError() ) {
    129137                errorcount = 0;
    130                 self.connectionLost = false;
     138                hasConnectionError = false;
    131139                $(document).trigger( 'heartbeat-connection-restored' );
    132140            }
     
    153161            // If nothing to send (nothing is expecting a response),
    154162            // schedule the next tick and bail
    155             if ( empty && ! self.connectionLost ) {
     163            if ( empty && ! self.hasConnectionError() ) {
    156164                connecting = false;
    157165                next();
     
    180188
    181189                // Clear error state
    182                 if ( self.connectionLost )
     190                if ( self.hasConnectionError() )
    183191                    errorstate();
    184192
     
    206214                self.error( jqXHR, textStatus, error );
    207215            });
    208         };
     216        }
    209217
    210218        function next() {
     
    333341        // Check for user activity every 30 seconds.
    334342        window.setInterval( function(){ checkUserActive(); }, 30000 );
    335 
    336         if ( this.autostart ) {
    337             $(document).ready( function() {
    338                 // Start one tick (15 sec) after DOM ready
    339                 running = true;
    340                 tick = time();
    341                 next();
    342             });
    343         }
     343        $(document).ready( function() {
     344            // Start one tick (15 sec) after DOM ready
     345            running = true;
     346            tick = time();
     347            next();
     348        });
    344349
    345350        this.hasFocus = function() {
    346351            return hasFocus;
    347         }
     352        };
    348353
    349354        /**
     
    401406            return tempInterval ? tempInterval / 1000 : interval / 1000;
    402407        };
    403 
    404         // Start. Has no effect if heartbeat is already running
    405         this.start = function() {
    406             if ( running )
    407                 return false;
    408 
    409             running = true;
    410             connect();
    411             return true;
    412         };
    413 
    414         // Stop. If a XHR is in progress, abort it
    415         this.stop = function() {
    416             if ( self.xhr && self.xhr.readyState != 4 )
    417                 self.xhr.abort();
    418 
    419             // Reset the error state
    420             errorstate();
    421             running = false;
    422             return true;
    423         }
    424408
    425409        /**
     
    448432            }
    449433            return false;
    450         }
     434        };
    451435
    452436        /**
     
    458442        this.isQueued = function( handle ) {
    459443            return queue[handle];
    460         }
    461     }
     444        };
     445    };
    462446
    463447    $.extend( Heartbeat.prototype, {
Note: See TracChangeset for help on using the changeset viewer.