Make WordPress Core

Ticket #42675: heartbeat.diff

File heartbeat.diff, 18.6 KB (added by andizer, 7 years ago)
  • src/wp-includes/js/heartbeat.js

    diff --git src/wp-includes/js/heartbeat.js src/wp-includes/js/heartbeat.js
    index 9db2162ab..0f93ff36d 100644
     
    2727 */
    2828
    2929( function( $, window, undefined ) {
    30 
    31         /**
    32          * Constructs the Heartbeat API.
    33          *
    34          * @since 3.6.0
    35          *
    36          * @returns {Object} An instance of the Heartbeat class.
    37          * @constructor
    38          */
    3930        var Heartbeat = function() {
    4031                var $document = $(document),
    4132                        settings = {
    42                                 // Suspend/resume.
     33                                // Suspend/resume
    4334                                suspend: false,
    4435
    45                                 // Whether suspending is enabled.
     36                                // Whether suspending is enabled
    4637                                suspendEnabled: true,
    4738
    48                                 // Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front'.
     39                                // Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front'
    4940                                screenId: '',
    5041
    51                                 // XHR request URL, defaults to the JS global 'ajaxurl' when present.
     42                                // XHR request URL, defaults to the JS global 'ajaxurl' when present
    5243                                url: '',
    5344
    54                                 // Timestamp, start of the last connection request.
     45                                // Timestamp, start of the last connection request
    5546                                lastTick: 0,
    5647
    57                                 // Container for the enqueued items.
     48                                // Container for the enqueued items
    5849                                queue: {},
    5950
    60                                 // Connect interval (in seconds).
     51                                // Connect interval (in seconds)
    6152                                mainInterval: 60,
    6253
    63                                 // Used when the interval is set to 5 sec. temporarily.
     54                                // Used when the interval is set to 5 sec. temporarily
    6455                                tempInterval: 0,
    6556
    66                                 // Used when the interval is reset.
     57                                // Used when the interval is reset
    6758                                originalInterval: 0,
    6859
    6960                                // Used to limit the number of AJAX requests.
    7061                                minimalInterval: 0,
    7162
    72                                 // Used together with tempInterval.
     63                                // Used together with tempInterval
    7364                                countdown: 0,
    7465
    75                                 // Whether a connection is currently in progress.
     66                                // Whether a connection is currently in progress
    7667                                connecting: false,
    7768
    78                                 // Whether a connection error occurred.
     69                                // Whether a connection error occurred
    7970                                connectionError: false,
    8071
    81                                 // Used to track non-critical errors.
     72                                // Used to track non-critical errors
    8273                                errorcount: 0,
    8374
    84                                 // Whether at least one connection has been completed successfully.
     75                                // Whether at least one connection has completed successfully
    8576                                hasConnected: false,
    8677
    87                                 // Whether the current browser window is in focus and the user is active.
     78                                // Whether the current browser window is in focus and the user is active
    8879                                hasFocus: true,
    8980
    9081                                // Timestamp, last time the user was active. Checked every 30 sec.
    9182                                userActivity: 0,
    9283
    93                                 // Flag whether events tracking user activity were set.
     84                                // Flags whether events tracking user activity were set
    9485                                userActivityEvents: false,
    9586
    96                                 // Timer that keeps track of how long a user has focus.
    9787                                checkFocusTimer: 0,
    98 
    99                                 // Timer that keeps track of how long needs to be waited before connecting to the server again.
    10088                                beatTimer: 0
    10189                        };
    10290
    10391                /**
    104                  * Sets the necessary variables and events before starting the actual heartbeat.
    105                  *
    106                  * @summary Sets local variables and events, then starts the heartbeat.
     92                 * Set local vars and events, then start
    10793                 *
    10894                 * @access private
    10995                 *
    110                  * @since 3.8.0
    111                  *
    112                  * @returns { void }
     96                 * @return void
    11397                 */
    11498                function initialize() {
    11599                        var options, hidden, visibilityState, visibilitychange;
     
    122106                                settings.url = window.ajaxurl;
    123107                        }
    124108
    125                         // Pull in options passed from PHP.
     109                        // Pull in options passed from PHP
    126110                        if ( typeof window.heartbeatSettings === 'object' ) {
    127111                                options = window.heartbeatSettings;
    128112
    129                                 // The XHR URL can be passed as option when window.ajaxurl is not set.
     113                                // The XHR URL can be passed as option when window.ajaxurl is not set
    130114                                if ( ! settings.url && options.ajaxurl ) {
    131115                                        settings.url = options.ajaxurl;
    132116                                }
    133117
    134                                 /**
    135                                  * The interval can be from 15 to 120 sec. and can be set temporarily to 5 sec.
    136                                  * It can be set in the initial options or changed later through JS and/or through PHP.
    137                                  */
     118                                // The interval can be from 15 to 120 sec. and can be set temporarily to 5 sec.
     119                                // It can be set in the initial options or changed later from JS and/or from PHP.
    138120                                if ( options.interval ) {
    139121                                        settings.mainInterval = options.interval;
    140122
     
    145127                                        }
    146128                                }
    147129
    148                                 /**
    149                                  * Used to limit the number of AJAX requests. Overrides all other intervals if they are shorter.
    150                                  * Needed for some hosts that cannot handle frequent requests and the user may exceed the allocated server CPU time, etc.
    151                                  * The minimal interval can be up to 600 sec. however setting it to longer than 120 sec. will limit or disable
    152                                  * some of the functionality (like post locks).
    153                                  * Once set at initialization, minimalInterval cannot be changed/overridden.
    154                                  */
     130                                // Used to limit the number of AJAX requests. Overrides all other intervals if they are shorter.
     131                                // Needed for some hosts that cannot handle frequent requests and the user may exceed the allocated server CPU time, etc.
     132                                // The minimal interval can be up to 600 sec. however setting it to longer than 120 sec. will limit or disable
     133                                // some of the functionality (like post locks).
     134                                // Once set at initialization, minimalInterval cannot be changed/overridden.
    155135                                if ( options.minimalInterval ) {
    156136                                        options.minimalInterval = parseInt( options.minimalInterval, 10 );
    157137                                        settings.minimalInterval = options.minimalInterval > 0 && options.minimalInterval <= 600 ? options.minimalInterval * 1000 : 0;
     
    161141                                        settings.mainInterval = settings.minimalInterval;
    162142                                }
    163143
    164                                 // 'screenId' can be added from settings on the front end where the JS global 'pagenow' is not set.
     144                                // 'screenId' can be added from settings on the front end where the JS global 'pagenow' is not set
    165145                                if ( ! settings.screenId ) {
    166146                                        settings.screenId = options.screenId || 'front';
    167147                                }
     
    171151                                }
    172152                        }
    173153
    174                         // Convert to milliseconds.
     154                        // Convert to milliseconds
    175155                        settings.mainInterval = settings.mainInterval * 1000;
    176156                        settings.originalInterval = settings.mainInterval;
    177157
    178                         /**
    179                          * Switch the interval to 120 seconds by using the Page Visibility API.
    180                          * If the browser doesn't support it (Safari < 7, Android < 4.4, IE < 10), the interval
    181                          * will be increased to 120 seconds after 5 minutes of mouse and keyboard inactivity.
    182                          */
     158                        // Switch the interval to 120 sec. by using the Page Visibility API.
     159                        // If the browser doesn't support it (Safari < 7, Android < 4.4, IE < 10), the interval
     160                        // will be increased to 120 sec. after 5 min. of mouse and keyboard inactivity.
    183161                        if ( typeof document.hidden !== 'undefined' ) {
    184162                                hidden = 'hidden';
    185163                                visibilitychange = 'visibilitychange';
     
    218196                        }
    219197
    220198                        $(window).on( 'unload.wp-heartbeat', function() {
    221                                 // Don't connect anymore.
     199                                // Don't connect any more
    222200                                settings.suspend = true;
    223201
    224                                 // Abort the last request if not completed.
     202                                // Abort the last request if not completed
    225203                                if ( settings.xhr && settings.xhr.readyState !== 4 ) {
    226204                                        settings.xhr.abort();
    227205                                }
     
    230208                        // Check for user activity every 30 seconds.
    231209                        window.setInterval( checkUserActivity, 30000 );
    232210
    233                         // Start one tick after DOM ready.
     211                        // Start one tick after DOM ready
    234212                        $document.ready( function() {
    235213                                settings.lastTick = time();
    236214                                scheduleNextTick();
     
    238216                }
    239217
    240218                /**
    241                  * Returns the current time according to the browser.
     219                 * Return the current time according to the browser
    242220                 *
    243221                 * @access private
    244222                 *
    245                  * @since 3.6.0
    246                  *
    247                  * @returns int Returns the current time.
     223                 * @return int
    248224                 */
    249225                function time() {
    250226                        return (new Date()).getTime();
    251227                }
    252228
    253229                /**
    254                  * Checks if the iframe is from the same origin.
     230                 * Check if the iframe is from the same origin
    255231                 *
    256232                 * @access private
    257233                 *
    258                  * @since 3.6.0
    259                  *
    260                  * @returns boolean Returns whether or not the iframe is from the same origin.
     234                 * @return bool
    261235                 */
    262236                function isLocalFrame( frame ) {
    263237                        var origin, src = frame.src;
    264238
    265                         /**
    266                          * Need to compare strings as WebKit doesn't throw JS errors when iframes have different origin.
    267                          * It throws uncatchable exceptions.
    268                          */
     239                        // Need to compare strings as WebKit doesn't throw JS errors when iframes have different origin.
     240                        // It throws uncatchable exceptions.
    269241                        if ( src && /^https?:\/\//.test( src ) ) {
    270242                                origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host;
    271243
     
    284256                }
    285257
    286258                /**
    287                  * Checks if the document's focus has changed.
     259                 * Check if the document's focus has changed
    288260                 *
    289261                 * @access private
    290262                 *
    291                  * @since 4.1.0
    292                  *
    293                  * @returns { void }
     263                 * @return void
    294264                 */
    295265                function checkFocus() {
    296266                        if ( settings.hasFocus && ! document.hasFocus() ) {
     
    301271                }
    302272
    303273                /**
    304                  * Sets error state and fires an event on XHR errors or timeout.
     274                 * Set error state and fire an event on XHR errors or timeout
    305275                 *
    306276                 * @access private
    307277                 *
    308                  * @since 3.8.0
    309                  *
    310                  * @param string error The error type passed from the XHR.
    311                  * @param int status The HTTP status code passed from jqXHR (200, 404, 500, etc.).
    312                  *
    313                  * @returns { void }
     278                 * @param string error The error type passed from the XHR
     279                 * @param int status The HTTP status code passed from jqXHR (200, 404, 500, etc.)
     280                 * @return void
    314281                 */
    315282                function setErrorState( error, status ) {
    316283                        var trigger;
     
    318285                        if ( error ) {
    319286                                switch ( error ) {
    320287                                        case 'abort':
    321                                                 // Do nothing.
     288                                                // do nothing
    322289                                                break;
    323290                                        case 'timeout':
    324                                                 // No response for 30 sec.
     291                                                // no response for 30 sec.
    325292                                                trigger = true;
    326293                                                break;
    327294                                        case 'error':
     
    329296                                                        trigger = true;
    330297                                                        break;
    331298                                                }
    332                                                 // Falls through.
     299                                                /* falls through */
    333300                                        case 'parsererror':
    334301                                        case 'empty':
    335302                                        case 'unknown':
     
    350317                }
    351318
    352319                /**
    353                  * Clears the error state and fires an event if there is a connection error.
     320                 * Clear the error state and fire an event
    354321                 *
    355322                 * @access private
    356323                 *
    357                  * @since 3.8.0
    358                  *
    359                  * @returns { void }
     324                 * @return void
    360325                 */
    361326                function clearErrorState() {
    362                         // Has connected successfully.
     327                        // Has connected successfully
    363328                        settings.hasConnected = true;
    364329
    365330                        if ( hasConnectionError() ) {
     
    370335                }
    371336
    372337                /**
    373                  * Gathers the data and connects to the server.
     338                 * Gather the data and connect to the server
    374339                 *
    375340                 * @access private
    376341                 *
    377                  * @since 3.6.0
    378                  *
    379                  * @returns { void }
     342                 * @return void
    380343                 */
    381344                function connect() {
    382345                        var ajaxData, heartbeatData;
     
    390353                        settings.lastTick = time();
    391354
    392355                        heartbeatData = $.extend( {}, settings.queue );
    393                         // Clear the data queue. Anything added after this point will be sent on the next tick.
     356                        // Clear the data queue, anything added after this point will be send on the next tick
    394357                        settings.queue = {};
    395358
    396359                        $document.trigger( 'heartbeat-send', [ heartbeatData ] );
     
    440403
    441404                                $document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] );
    442405
    443                                 // Do this last. Can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'.
     406                                // Do this last, can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'
    444407                                if ( newInterval ) {
    445408                                        interval( newInterval );
    446409                                }
     
    451414                }
    452415
    453416                /**
    454                  * Schedules the next connection.
     417                 * Schedule the next connection
    455418                 *
    456419                 * Fires immediately if the connection time is longer than the interval.
    457420                 *
    458421                 * @access private
    459422                 *
    460                  * @since 3.8.0
    461                  *
    462                  * @returns { void }
     423                 * @return void
    463424                 */
    464425                function scheduleNextTick() {
    465426                        var delta = time() - settings.lastTick,
     
    499460                }
    500461
    501462                /**
    502                  * Sets the internal state when the browser window becomes hidden or loses focus.
     463                 * Set the internal state when the browser window becomes hidden or loses focus
    503464                 *
    504465                 * @access private
    505466                 *
    506                  * @since 3.6.0
    507                  *
    508                  * @returns { void }
     467                 * @return void
    509468                 */
    510469                function blurred() {
    511470                        settings.hasFocus = false;
    512471                }
    513472
    514473                /**
    515                  * Sets the internal state when the browser window becomes visible or is in focus.
     474                 * Set the internal state when the browser window becomes visible or is in focus
    516475                 *
    517476                 * @access private
    518477                 *
    519                  * @since 3.6.0
    520                  *
    521                  * @returns { void }
     478                 * @return void
    522479                 */
    523480                function focused() {
    524481                        settings.userActivity = time();
     
    533490                }
    534491
    535492                /**
    536                  * Runs when the user becomes active after a period of inactivity.
     493                 * Runs when the user becomes active after a period of inactivity
    537494                 *
    538495                 * @access private
    539496                 *
    540                  * @since 3.6.0
    541                  *
    542                  * @returns { void }
     497                 * @return void
    543498                 */
    544499                function userIsActive() {
    545500                        settings.userActivityEvents = false;
     
    555510                }
    556511
    557512                /**
    558                  * Checks for user activity.
     513                 * Check for user activity
    559514                 *
    560515                 * Runs every 30 sec.
    561516                 * Sets 'hasFocus = true' if user is active and the window is in the background.
    562                  * Sets 'hasFocus = false' if the user has been inactive (no mouse or keyboard activity)
     517                 * Set 'hasFocus = false' if the user has been inactive (no mouse or keyboard activity)
    563518                 * for 5 min. even when the window has focus.
    564519                 *
    565520                 * @access private
    566521                 *
    567                  * @since 3.8.0
    568                  *
    569                  * @returns { void }
     522                 * @return void
    570523                 */
    571524                function checkUserActivity() {
    572525                        var lastActive = settings.userActivity ? time() - settings.userActivity : 0;
     
    599552                        }
    600553                }
    601554
    602                 // Public methods.
     555                // Public methods
    603556
    604557                /**
    605                  * Checks whether the window (or any local iframe in it) has focus, or the user is active.
     558                 * Whether the window (or any local iframe in it) has focus, or the user is active
    606559                 *
    607                  * @since 3.6.0
    608                  *
    609                  * @returns boolean True if the window or the user is active.
     560                 * @return bool
    610561                 */
    611562                function hasFocus() {
    612563                        return settings.hasFocus;
    613564                }
    614565
    615566                /**
    616                  * Checks whether there is a connection error.
    617                  *
    618                  * @since 3.6.0
     567                 * Whether there is a connection error
    619568                 *
    620                  * @returns boolean True if a connection error was found.
     569                 * @return bool
    621570                 */
    622571                function hasConnectionError() {
    623572                        return settings.connectionError;
    624573                }
    625574
    626575                /**
    627                  * Connects as soon as possible regardless of 'hasFocus' state.
     576                 * Connect asap regardless of 'hasFocus'
    628577                 *
    629578                 * Will not open two concurrent connections. If a connection is in progress,
    630579                 * will connect again immediately after the current connection completes.
    631580                 *
    632                  * @since 3.8.0
    633                  *
    634                  * @returns { void }
     581                 * @return void
    635582                 */
    636583                function connectNow() {
    637584                        settings.lastTick = 0;
     
    639586                }
    640587
    641588                /**
    642                  * Disables suspending.
     589                 * Disable suspending
    643590                 *
    644591                 * Should be used only when Heartbeat is performing critical tasks like autosave, post-locking, etc.
    645592                 * Using this on many screens may overload the user's hosting account if several
    646593                 * browser windows/tabs are left open for a long time.
    647594                 *
    648                  * @since 3.8.0
    649                  *
    650                  * @returns { void }
     595                 * @return void
    651596                 */
    652597                function disableSuspend() {
    653598                        settings.suspendEnabled = false;
    654599                }
    655600
    656601                /**
    657                  * Gets/Sets the interval.
     602                 * Get/Set the interval
    658603                 *
    659                  * When setting to 'fast' or 5, the interval is 5 seconds for the next 30 ticks (for 2 minutes and 30 seconds) by default.
     604                 * When setting to 'fast' or 5, by default interval is 5 sec. for the next 30 ticks (for 2 min and 30 sec).
    660605                 * In this case the number of 'ticks' can be passed as second argument.
    661606                 * If the window doesn't have focus, the interval slows down to 2 min.
    662607                 *
    663                  * @since 3.6.0
    664                  *
    665                  * @param mixed speed Interval: 'fast' or 5, 15, 30, 60, 120.
    666                  * @param string ticks Used with speed = 'fast' or 5. Tells how many ticks before the interval reverts back.
    667                  *
    668                  * @returns int Current interval in seconds.
     608                 * @param mixed speed Interval: 'fast' or 5, 15, 30, 60, 120
     609                 * @param string ticks Used with speed = 'fast' or 5, how many ticks before the interval reverts back
     610                 * @return int Current interval in seconds
    669611                 */
    670612                function interval( speed, ticks ) {
    671613                        var newInterval,
     
    725667                }
    726668
    727669                /**
    728                  * Enqueues data to send with the next XHR.
     670                 * Enqueue data to send with the next XHR
    729671                 *
    730672                 * As the data is send asynchronously, this function doesn't return the XHR response.
    731673                 * To see the response, use the custom jQuery event 'heartbeat-tick' on the document, example:
     
    735677                 * If the same 'handle' is used more than once, the data is not overwritten when the third argument is 'true'.
    736678                 * Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle.
    737679                 *
    738                  * @since 3.6.0
    739                  *
    740                  * @param string handle Unique handle for the data, used in PHP to receive the data.
    741                  * @param mixed data The data to send.
    742                  * @param boolean noOverwrite Whether to overwrite existing data in the queue.
    743                  *
    744                  * @returns boolean True if the data was queued.
     680                 * $param string handle Unique handle for the data. The handle is used in PHP to receive the data.
     681                 * $param mixed data The data to send.
     682                 * $param bool noOverwrite Whether to overwrite existing data in the queue.
     683                 * $return bool Whether the data was queued or not.
    745684                 */
    746685                function enqueue( handle, data, noOverwrite ) {
    747686                        if ( handle ) {
     
    756695                }
    757696
    758697                /**
    759                  * Checks if data with a particular handle is queued.
    760                  *
    761                  * @since 3.6.0
    762                  *
    763                  * @param string handle The handle for the data.
     698                 * Check if data with a particular handle is queued
    764699                 *
    765                  * @returns boolean True if the data is queued with this handle.
     700                 * $param string handle The handle for the data
     701                 * $return bool Whether some data is queued with this handle
    766702                 */
    767703                function isQueued( handle ) {
    768704                        if ( handle ) {
     
    771707                }
    772708
    773709                /**
    774                  * Removes data with a particular handle from the queue.
     710                 * Remove data with a particular handle from the queue
    775711                 *
    776                  * @since 3.7.0
    777                  *
    778                  * @param string handle The handle for the data.
    779                  *
    780                  * @returns void
     712                 * $param string handle The handle for the data
     713                 * $return void
    781714                 */
    782715                function dequeue( handle ) {
    783716                        if ( handle ) {
     
    786719                }
    787720
    788721                /**
    789                  * Gets data that was enqueued with a particular handle.
    790                  *
    791                  * @since 3.7.0
    792                  *
    793                  * @param string handle The handle for the data.
     722                 * Get data that was enqueued with a particular handle
    794723                 *
    795                  * @returns mixed The data or undefined.
     724                 * $param string handle The handle for the data
     725                 * $return mixed The data or undefined
    796726                 */
    797727                function getQueuedItem( handle ) {
    798728                        if ( handle ) {
     
    802732
    803733                initialize();
    804734
    805                 // Expose public methods.
     735                // Expose public methods
    806736                return {
    807737                        hasFocus: hasFocus,
    808738                        connectNow: connectNow,