Make WordPress Core

Ticket #42675: heartbeat-2.diff

File heartbeat-2.diff, 18.6 KB (added by andizer, 7 years ago)

This time the diff has been done correctly.

  • src/wp-includes/js/heartbeat.js

    diff --git src/wp-includes/js/heartbeat.js src/wp-includes/js/heartbeat.js
    index 0f93ff36d..9db2162ab 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         */
    3039        var Heartbeat = function() {
    3140                var $document = $(document),
    3241                        settings = {
    33                                 // Suspend/resume
     42                                // Suspend/resume.
    3443                                suspend: false,
    3544
    36                                 // Whether suspending is enabled
     45                                // Whether suspending is enabled.
    3746                                suspendEnabled: true,
    3847
    39                                 // Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front'
     48                                // Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front'.
    4049                                screenId: '',
    4150
    42                                 // XHR request URL, defaults to the JS global 'ajaxurl' when present
     51                                // XHR request URL, defaults to the JS global 'ajaxurl' when present.
    4352                                url: '',
    4453
    45                                 // Timestamp, start of the last connection request
     54                                // Timestamp, start of the last connection request.
    4655                                lastTick: 0,
    4756
    48                                 // Container for the enqueued items
     57                                // Container for the enqueued items.
    4958                                queue: {},
    5059
    51                                 // Connect interval (in seconds)
     60                                // Connect interval (in seconds).
    5261                                mainInterval: 60,
    5362
    54                                 // Used when the interval is set to 5 sec. temporarily
     63                                // Used when the interval is set to 5 sec. temporarily.
    5564                                tempInterval: 0,
    5665
    57                                 // Used when the interval is reset
     66                                // Used when the interval is reset.
    5867                                originalInterval: 0,
    5968
    6069                                // Used to limit the number of AJAX requests.
    6170                                minimalInterval: 0,
    6271
    63                                 // Used together with tempInterval
     72                                // Used together with tempInterval.
    6473                                countdown: 0,
    6574
    66                                 // Whether a connection is currently in progress
     75                                // Whether a connection is currently in progress.
    6776                                connecting: false,
    6877
    69                                 // Whether a connection error occurred
     78                                // Whether a connection error occurred.
    7079                                connectionError: false,
    7180
    72                                 // Used to track non-critical errors
     81                                // Used to track non-critical errors.
    7382                                errorcount: 0,
    7483
    75                                 // Whether at least one connection has completed successfully
     84                                // Whether at least one connection has been completed successfully.
    7685                                hasConnected: false,
    7786
    78                                 // Whether the current browser window is in focus and the user is active
     87                                // Whether the current browser window is in focus and the user is active.
    7988                                hasFocus: true,
    8089
    8190                                // Timestamp, last time the user was active. Checked every 30 sec.
    8291                                userActivity: 0,
    8392
    84                                 // Flags whether events tracking user activity were set
     93                                // Flag whether events tracking user activity were set.
    8594                                userActivityEvents: false,
    8695
     96                                // Timer that keeps track of how long a user has focus.
    8797                                checkFocusTimer: 0,
     98
     99                                // Timer that keeps track of how long needs to be waited before connecting to the server again.
    88100                                beatTimer: 0
    89101                        };
    90102
    91103                /**
    92                  * Set local vars and events, then start
     104                 * Sets the necessary variables and events before starting the actual heartbeat.
     105                 *
     106                 * @summary Sets local variables and events, then starts the heartbeat.
    93107                 *
    94108                 * @access private
    95109                 *
    96                  * @return void
     110                 * @since 3.8.0
     111                 *
     112                 * @returns { void }
    97113                 */
    98114                function initialize() {
    99115                        var options, hidden, visibilityState, visibilitychange;
     
    106122                                settings.url = window.ajaxurl;
    107123                        }
    108124
    109                         // Pull in options passed from PHP
     125                        // Pull in options passed from PHP.
    110126                        if ( typeof window.heartbeatSettings === 'object' ) {
    111127                                options = window.heartbeatSettings;
    112128
    113                                 // The XHR URL can be passed as option when window.ajaxurl is not set
     129                                // The XHR URL can be passed as option when window.ajaxurl is not set.
    114130                                if ( ! settings.url && options.ajaxurl ) {
    115131                                        settings.url = options.ajaxurl;
    116132                                }
    117133
    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.
     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                                 */
    120138                                if ( options.interval ) {
    121139                                        settings.mainInterval = options.interval;
    122140
     
    127145                                        }
    128146                                }
    129147
    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.
     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                                 */
    135155                                if ( options.minimalInterval ) {
    136156                                        options.minimalInterval = parseInt( options.minimalInterval, 10 );
    137157                                        settings.minimalInterval = options.minimalInterval > 0 && options.minimalInterval <= 600 ? options.minimalInterval * 1000 : 0;
     
    141161                                        settings.mainInterval = settings.minimalInterval;
    142162                                }
    143163
    144                                 // 'screenId' can be added from settings on the front end where the JS global 'pagenow' is not set
     164                                // 'screenId' can be added from settings on the front end where the JS global 'pagenow' is not set.
    145165                                if ( ! settings.screenId ) {
    146166                                        settings.screenId = options.screenId || 'front';
    147167                                }
     
    151171                                }
    152172                        }
    153173
    154                         // Convert to milliseconds
     174                        // Convert to milliseconds.
    155175                        settings.mainInterval = settings.mainInterval * 1000;
    156176                        settings.originalInterval = settings.mainInterval;
    157177
    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.
     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                         */
    161183                        if ( typeof document.hidden !== 'undefined' ) {
    162184                                hidden = 'hidden';
    163185                                visibilitychange = 'visibilitychange';
     
    196218                        }
    197219
    198220                        $(window).on( 'unload.wp-heartbeat', function() {
    199                                 // Don't connect any more
     221                                // Don't connect anymore.
    200222                                settings.suspend = true;
    201223
    202                                 // Abort the last request if not completed
     224                                // Abort the last request if not completed.
    203225                                if ( settings.xhr && settings.xhr.readyState !== 4 ) {
    204226                                        settings.xhr.abort();
    205227                                }
     
    208230                        // Check for user activity every 30 seconds.
    209231                        window.setInterval( checkUserActivity, 30000 );
    210232
    211                         // Start one tick after DOM ready
     233                        // Start one tick after DOM ready.
    212234                        $document.ready( function() {
    213235                                settings.lastTick = time();
    214236                                scheduleNextTick();
     
    216238                }
    217239
    218240                /**
    219                  * Return the current time according to the browser
     241                 * Returns the current time according to the browser.
    220242                 *
    221243                 * @access private
    222244                 *
    223                  * @return int
     245                 * @since 3.6.0
     246                 *
     247                 * @returns int Returns the current time.
    224248                 */
    225249                function time() {
    226250                        return (new Date()).getTime();
    227251                }
    228252
    229253                /**
    230                  * Check if the iframe is from the same origin
     254                 * Checks if the iframe is from the same origin.
    231255                 *
    232256                 * @access private
    233257                 *
    234                  * @return bool
     258                 * @since 3.6.0
     259                 *
     260                 * @returns boolean Returns whether or not the iframe is from the same origin.
    235261                 */
    236262                function isLocalFrame( frame ) {
    237263                        var origin, src = frame.src;
    238264
    239                         // Need to compare strings as WebKit doesn't throw JS errors when iframes have different origin.
    240                         // It throws uncatchable exceptions.
     265                        /**
     266                         * Need to compare strings as WebKit doesn't throw JS errors when iframes have different origin.
     267                         * It throws uncatchable exceptions.
     268                         */
    241269                        if ( src && /^https?:\/\//.test( src ) ) {
    242270                                origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host;
    243271
     
    256284                }
    257285
    258286                /**
    259                  * Check if the document's focus has changed
     287                 * Checks if the document's focus has changed.
    260288                 *
    261289                 * @access private
    262290                 *
    263                  * @return void
     291                 * @since 4.1.0
     292                 *
     293                 * @returns { void }
    264294                 */
    265295                function checkFocus() {
    266296                        if ( settings.hasFocus && ! document.hasFocus() ) {
     
    271301                }
    272302
    273303                /**
    274                  * Set error state and fire an event on XHR errors or timeout
     304                 * Sets error state and fires an event on XHR errors or timeout.
    275305                 *
    276306                 * @access private
    277307                 *
    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
     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 }
    281314                 */
    282315                function setErrorState( error, status ) {
    283316                        var trigger;
     
    285318                        if ( error ) {
    286319                                switch ( error ) {
    287320                                        case 'abort':
    288                                                 // do nothing
     321                                                // Do nothing.
    289322                                                break;
    290323                                        case 'timeout':
    291                                                 // no response for 30 sec.
     324                                                // No response for 30 sec.
    292325                                                trigger = true;
    293326                                                break;
    294327                                        case 'error':
     
    296329                                                        trigger = true;
    297330                                                        break;
    298331                                                }
    299                                                 /* falls through */
     332                                                // Falls through.
    300333                                        case 'parsererror':
    301334                                        case 'empty':
    302335                                        case 'unknown':
     
    317350                }
    318351
    319352                /**
    320                  * Clear the error state and fire an event
     353                 * Clears the error state and fires an event if there is a connection error.
    321354                 *
    322355                 * @access private
    323356                 *
    324                  * @return void
     357                 * @since 3.8.0
     358                 *
     359                 * @returns { void }
    325360                 */
    326361                function clearErrorState() {
    327                         // Has connected successfully
     362                        // Has connected successfully.
    328363                        settings.hasConnected = true;
    329364
    330365                        if ( hasConnectionError() ) {
     
    335370                }
    336371
    337372                /**
    338                  * Gather the data and connect to the server
     373                 * Gathers the data and connects to the server.
    339374                 *
    340375                 * @access private
    341376                 *
    342                  * @return void
     377                 * @since 3.6.0
     378                 *
     379                 * @returns { void }
    343380                 */
    344381                function connect() {
    345382                        var ajaxData, heartbeatData;
     
    353390                        settings.lastTick = time();
    354391
    355392                        heartbeatData = $.extend( {}, settings.queue );
    356                         // Clear the data queue, anything added after this point will be send on the next tick
     393                        // Clear the data queue. Anything added after this point will be sent on the next tick.
    357394                        settings.queue = {};
    358395
    359396                        $document.trigger( 'heartbeat-send', [ heartbeatData ] );
     
    403440
    404441                                $document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] );
    405442
    406                                 // Do this last, can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'
     443                                // Do this last. Can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'.
    407444                                if ( newInterval ) {
    408445                                        interval( newInterval );
    409446                                }
     
    414451                }
    415452
    416453                /**
    417                  * Schedule the next connection
     454                 * Schedules the next connection.
    418455                 *
    419456                 * Fires immediately if the connection time is longer than the interval.
    420457                 *
    421458                 * @access private
    422459                 *
    423                  * @return void
     460                 * @since 3.8.0
     461                 *
     462                 * @returns { void }
    424463                 */
    425464                function scheduleNextTick() {
    426465                        var delta = time() - settings.lastTick,
     
    460499                }
    461500
    462501                /**
    463                  * Set the internal state when the browser window becomes hidden or loses focus
     502                 * Sets the internal state when the browser window becomes hidden or loses focus.
    464503                 *
    465504                 * @access private
    466505                 *
    467                  * @return void
     506                 * @since 3.6.0
     507                 *
     508                 * @returns { void }
    468509                 */
    469510                function blurred() {
    470511                        settings.hasFocus = false;
    471512                }
    472513
    473514                /**
    474                  * Set the internal state when the browser window becomes visible or is in focus
     515                 * Sets the internal state when the browser window becomes visible or is in focus.
    475516                 *
    476517                 * @access private
    477518                 *
    478                  * @return void
     519                 * @since 3.6.0
     520                 *
     521                 * @returns { void }
    479522                 */
    480523                function focused() {
    481524                        settings.userActivity = time();
     
    490533                }
    491534
    492535                /**
    493                  * Runs when the user becomes active after a period of inactivity
     536                 * Runs when the user becomes active after a period of inactivity.
    494537                 *
    495538                 * @access private
    496539                 *
    497                  * @return void
     540                 * @since 3.6.0
     541                 *
     542                 * @returns { void }
    498543                 */
    499544                function userIsActive() {
    500545                        settings.userActivityEvents = false;
     
    510555                }
    511556
    512557                /**
    513                  * Check for user activity
     558                 * Checks for user activity.
    514559                 *
    515560                 * Runs every 30 sec.
    516561                 * Sets 'hasFocus = true' if user is active and the window is in the background.
    517                  * Set 'hasFocus = false' if the user has been inactive (no mouse or keyboard activity)
     562                 * Sets 'hasFocus = false' if the user has been inactive (no mouse or keyboard activity)
    518563                 * for 5 min. even when the window has focus.
    519564                 *
    520565                 * @access private
    521566                 *
    522                  * @return void
     567                 * @since 3.8.0
     568                 *
     569                 * @returns { void }
    523570                 */
    524571                function checkUserActivity() {
    525572                        var lastActive = settings.userActivity ? time() - settings.userActivity : 0;
     
    552599                        }
    553600                }
    554601
    555                 // Public methods
     602                // Public methods.
    556603
    557604                /**
    558                  * Whether the window (or any local iframe in it) has focus, or the user is active
     605                 * Checks whether the window (or any local iframe in it) has focus, or the user is active.
    559606                 *
    560                  * @return bool
     607                 * @since 3.6.0
     608                 *
     609                 * @returns boolean True if the window or the user is active.
    561610                 */
    562611                function hasFocus() {
    563612                        return settings.hasFocus;
    564613                }
    565614
    566615                /**
    567                  * Whether there is a connection error
     616                 * Checks whether there is a connection error.
     617                 *
     618                 * @since 3.6.0
    568619                 *
    569                  * @return bool
     620                 * @returns boolean True if a connection error was found.
    570621                 */
    571622                function hasConnectionError() {
    572623                        return settings.connectionError;
    573624                }
    574625
    575626                /**
    576                  * Connect asap regardless of 'hasFocus'
     627                 * Connects as soon as possible regardless of 'hasFocus' state.
    577628                 *
    578629                 * Will not open two concurrent connections. If a connection is in progress,
    579630                 * will connect again immediately after the current connection completes.
    580631                 *
    581                  * @return void
     632                 * @since 3.8.0
     633                 *
     634                 * @returns { void }
    582635                 */
    583636                function connectNow() {
    584637                        settings.lastTick = 0;
     
    586639                }
    587640
    588641                /**
    589                  * Disable suspending
     642                 * Disables suspending.
    590643                 *
    591644                 * Should be used only when Heartbeat is performing critical tasks like autosave, post-locking, etc.
    592645                 * Using this on many screens may overload the user's hosting account if several
    593646                 * browser windows/tabs are left open for a long time.
    594647                 *
    595                  * @return void
     648                 * @since 3.8.0
     649                 *
     650                 * @returns { void }
    596651                 */
    597652                function disableSuspend() {
    598653                        settings.suspendEnabled = false;
    599654                }
    600655
    601656                /**
    602                  * Get/Set the interval
     657                 * Gets/Sets the interval.
    603658                 *
    604                  * When setting to 'fast' or 5, by default interval is 5 sec. for the next 30 ticks (for 2 min and 30 sec).
     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.
    605660                 * In this case the number of 'ticks' can be passed as second argument.
    606661                 * If the window doesn't have focus, the interval slows down to 2 min.
    607662                 *
    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
     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.
    611669                 */
    612670                function interval( speed, ticks ) {
    613671                        var newInterval,
     
    667725                }
    668726
    669727                /**
    670                  * Enqueue data to send with the next XHR
     728                 * Enqueues data to send with the next XHR.
    671729                 *
    672730                 * As the data is send asynchronously, this function doesn't return the XHR response.
    673731                 * To see the response, use the custom jQuery event 'heartbeat-tick' on the document, example:
     
    677735                 * If the same 'handle' is used more than once, the data is not overwritten when the third argument is 'true'.
    678736                 * Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle.
    679737                 *
    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.
     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.
    684745                 */
    685746                function enqueue( handle, data, noOverwrite ) {
    686747                        if ( handle ) {
     
    695756                }
    696757
    697758                /**
    698                  * Check if data with a particular handle is queued
     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.
    699764                 *
    700                  * $param string handle The handle for the data
    701                  * $return bool Whether some data is queued with this handle
     765                 * @returns boolean True if the data is queued with this handle.
    702766                 */
    703767                function isQueued( handle ) {
    704768                        if ( handle ) {
     
    707771                }
    708772
    709773                /**
    710                  * Remove data with a particular handle from the queue
     774                 * Removes data with a particular handle from the queue.
    711775                 *
    712                  * $param string handle The handle for the data
    713                  * $return void
     776                 * @since 3.7.0
     777                 *
     778                 * @param string handle The handle for the data.
     779                 *
     780                 * @returns void
    714781                 */
    715782                function dequeue( handle ) {
    716783                        if ( handle ) {
     
    719786                }
    720787
    721788                /**
    722                  * Get data that was enqueued with a particular handle
     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.
    723794                 *
    724                  * $param string handle The handle for the data
    725                  * $return mixed The data or undefined
     795                 * @returns mixed The data or undefined.
    726796                 */
    727797                function getQueuedItem( handle ) {
    728798                        if ( handle ) {
     
    732802
    733803                initialize();
    734804
    735                 // Expose public methods
     805                // Expose public methods.
    736806                return {
    737807                        hasFocus: hasFocus,
    738808                        connectNow: connectNow,