Make WordPress Core

Changeset 42389


Ignore:
Timestamp:
12/12/2017 02:32:33 PM (7 years ago)
Author:
atimmer
Message:

Docs: Improve JS Docs for wp-includes/js/heartbeat.js.

Most of the documentation was already present. This change achieves the following:

  • Fixes the formatting for most doc blocks.
  • Adds @since tags to all functions.
  • Makes sure the heartbeat public methods are correctly added to the wp.heartbeat namespace.

Props andizer, jjcomack, carolinegeven.
Fixes #42675.

File:
1 edited

Legend:

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

    r41839 r42389  
    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
     49                // (in the admin) or 'front'.
    4050                screenId: '',
    4151
    42                 // XHR request URL, defaults to the JS global 'ajaxurl' when present
     52                // XHR request URL, defaults to the JS global 'ajaxurl' when present.
    4353                url: '',
    4454
    45                 // Timestamp, start of the last connection request
     55                // Timestamp, start of the last connection request.
    4656                lastTick: 0,
    4757
    48                 // Container for the enqueued items
     58                // Container for the enqueued items.
    4959                queue: {},
    5060
    51                 // Connect interval (in seconds)
     61                // Connect interval (in seconds).
    5262                mainInterval: 60,
    5363
    54                 // Used when the interval is set to 5 sec. temporarily
     64                // Used when the interval is set to 5 sec. temporarily.
    5565                tempInterval: 0,
    5666
    57                 // Used when the interval is reset
     67                // Used when the interval is reset.
    5868                originalInterval: 0,
    5969
     
    6171                minimalInterval: 0,
    6272
    63                 // Used together with tempInterval
     73                // Used together with tempInterval.
    6474                countdown: 0,
    6575
    66                 // Whether a connection is currently in progress
     76                // Whether a connection is currently in progress.
    6777                connecting: false,
    6878
    69                 // Whether a connection error occurred
     79                // Whether a connection error occurred.
    7080                connectionError: false,
    7181
    72                 // Used to track non-critical errors
     82                // Used to track non-critical errors.
    7383                errorcount: 0,
    7484
    75                 // Whether at least one connection has completed successfully
     85                // Whether at least one connection has been completed successfully.
    7686                hasConnected: false,
    7787
    78                 // Whether the current browser window is in focus and the user is active
     88                // Whether the current browser window is in focus and the user is active.
    7989                hasFocus: true,
    8090
     
    8292                userActivity: 0,
    8393
    84                 // Flags whether events tracking user activity were set
     94                // Flag whether events tracking user activity were set.
    8595                userActivityEvents: false,
    8696
     97                // Timer that keeps track of how long a user has focus.
    8798                checkFocusTimer: 0,
     99
     100                // Timer that keeps track of how long needs to be waited before connecting to
     101                // the server again.
    88102                beatTimer: 0
    89103            };
    90104
    91105        /**
    92          * Set local vars and events, then start
    93          *
    94          * @access private
    95          *
    96          * @return void
     106         * Sets local variables and events, then starts the heartbeat.
     107         *
     108         * @access private
     109         *
     110         * @since 3.8.0
     111         *
     112         * @returns {void}
    97113         */
    98114        function initialize() {
     
    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
     137                 * through PHP.
     138                 */
    120139                if ( options.interval ) {
    121140                    settings.mainInterval = options.interval;
     
    128147                }
    129148
    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.
     149                /*
     150                 * Used to limit the number of AJAX requests. Overrides all other intervals if
     151                 * they are shorter. Needed for some hosts that cannot handle frequent requests
     152                 * and the user may exceed the allocated server CPU time, etc. The minimal
     153                 * interval can be up to 600 sec. however setting it to longer than 120 sec.
     154                 * will limit or disable some of the functionality (like post locks). Once set
     155                 * at initialization, minimalInterval cannot be changed/overridden.
     156                 */
    135157                if ( options.minimalInterval ) {
    136158                    options.minimalInterval = parseInt( options.minimalInterval, 10 );
     
    142164                }
    143165
    144                 // 'screenId' can be added from settings on the front end where the JS global 'pagenow' is not set
     166                // 'screenId' can be added from settings on the front end where the JS global
     167                // 'pagenow' is not set.
    145168                if ( ! settings.screenId ) {
    146169                    settings.screenId = options.screenId || 'front';
     
    152175            }
    153176
    154             // Convert to milliseconds
     177            // Convert to milliseconds.
    155178            settings.mainInterval = settings.mainInterval * 1000;
    156179            settings.originalInterval = settings.mainInterval;
    157180
    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.
     181            /*
     182             * Switch the interval to 120 seconds by using the Page Visibility API.
     183             * If the browser doesn't support it (Safari < 7, Android < 4.4, IE < 10), the
     184             * interval will be increased to 120 seconds after 5 minutes of mouse and keyboard
     185             * inactivity.
     186             */
    161187            if ( typeof document.hidden !== 'undefined' ) {
    162188                hidden = 'hidden';
     
    197223
    198224            $(window).on( 'unload.wp-heartbeat', function() {
    199                 // Don't connect any more
     225                // Don't connect anymore.
    200226                settings.suspend = true;
    201227
    202                 // Abort the last request if not completed
     228                // Abort the last request if not completed.
    203229                if ( settings.xhr && settings.xhr.readyState !== 4 ) {
    204230                    settings.xhr.abort();
     
    209235            window.setInterval( checkUserActivity, 30000 );
    210236
    211             // Start one tick after DOM ready
     237            // Start one tick after DOM ready.
    212238            $document.ready( function() {
    213239                settings.lastTick = time();
     
    217243
    218244        /**
    219          * Return the current time according to the browser
    220          *
    221          * @access private
    222          *
    223          * @return int
     245         * Returns the current time according to the browser.
     246         *
     247         * @access private
     248         *
     249         * @since 3.6.0
     250         *
     251         * @returns {number} Returns the current time.
    224252         */
    225253        function time() {
     
    228256
    229257        /**
    230          * Check if the iframe is from the same origin
    231          *
    232          * @access private
    233          *
    234          * @return bool
     258         * Checks if the iframe is from the same origin.
     259         *
     260         * @access private
     261         *
     262         * @since 3.6.0
     263         *
     264         * @returns {boolean} Returns whether or not the iframe is from the same origin.
    235265         */
    236266        function isLocalFrame( frame ) {
    237267            var origin, src = frame.src;
    238268
    239             // Need to compare strings as WebKit doesn't throw JS errors when iframes have different origin.
    240             // It throws uncatchable exceptions.
     269            /*
     270             * Need to compare strings as WebKit doesn't throw JS errors when iframes have
     271             * different origin. It throws uncatchable exceptions.
     272             */
    241273            if ( src && /^https?:\/\//.test( src ) ) {
    242274                origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host;
     
    257289
    258290        /**
    259          * Check if the document's focus has changed
    260          *
    261          * @access private
    262          *
    263          * @return void
     291         * Checks if the document's focus has changed.
     292         *
     293         * @access private
     294         *
     295         * @since 4.1.0
     296         *
     297         * @returns {void}
    264298         */
    265299        function checkFocus() {
     
    272306
    273307        /**
    274          * Set error state and fire an event on XHR errors or timeout
    275          *
    276          * @access private
    277          *
    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         * Sets error state and fires an event on XHR errors or timeout.
     309         *
     310         * @access private
     311         *
     312         * @since 3.8.0
     313         *
     314         * @param {string} error  The error type passed from the XHR.
     315         * @param {number} status The HTTP status code passed from jqXHR
     316         *                        (200, 404, 500, etc.).
     317         *
     318         * @returns {void}
    281319         */
    282320        function setErrorState( error, status ) {
     
    286324                switch ( error ) {
    287325                    case 'abort':
    288                         // do nothing
     326                        // Do nothing.
    289327                        break;
    290328                    case 'timeout':
    291                         // no response for 30 sec.
     329                        // No response for 30 sec.
    292330                        trigger = true;
    293331                        break;
     
    318356
    319357        /**
    320          * Clear the error state and fire an event
    321          *
    322          * @access private
    323          *
    324          * @return void
     358         * Clears the error state and fires an event if there is a connection error.
     359         *
     360         * @access private
     361         *
     362         * @since 3.8.0
     363         *
     364         * @returns {void}
    325365         */
    326366        function clearErrorState() {
    327             // Has connected successfully
     367            // Has connected successfully.
    328368            settings.hasConnected = true;
    329369
     
    336376
    337377        /**
    338          * Gather the data and connect to the server
    339          *
    340          * @access private
    341          *
    342          * @return void
     378         * Gathers the data and connects to the server.
     379         *
     380         * @access private
     381         *
     382         * @since 3.6.0
     383         *
     384         * @returns {void}
    343385         */
    344386        function connect() {
     
    354396
    355397            heartbeatData = $.extend( {}, settings.queue );
    356             // Clear the data queue, anything added after this point will be send on the next tick
     398            // Clear the data queue. Anything added after this point will be sent on the next tick.
    357399            settings.queue = {};
    358400
     
    404446                $document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] );
    405447
    406                 // Do this last, can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'
     448                // Do this last. Can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'.
    407449                if ( newInterval ) {
    408450                    interval( newInterval );
     
    415457
    416458        /**
    417          * Schedule the next connection
     459         * Schedules the next connection.
    418460         *
    419461         * Fires immediately if the connection time is longer than the interval.
     
    421463         * @access private
    422464         *
    423          * @return void
     465         * @since 3.8.0
     466         *
     467         * @returns {void}
    424468         */
    425469        function scheduleNextTick() {
     
    461505
    462506        /**
    463          * Set the internal state when the browser window becomes hidden or loses focus
    464          *
    465          * @access private
    466          *
    467          * @return void
     507         * Sets the internal state when the browser window becomes hidden or loses focus.
     508         *
     509         * @access private
     510         *
     511         * @since 3.6.0
     512         *
     513         * @returns {void}
    468514         */
    469515        function blurred() {
     
    472518
    473519        /**
    474          * Set the internal state when the browser window becomes visible or is in focus
    475          *
    476          * @access private
    477          *
    478          * @return void
     520         * Sets the internal state when the browser window becomes visible or is in focus.
     521         *
     522         * @access private
     523         *
     524         * @since 3.6.0
     525         *
     526         * @returns {void}
    479527         */
    480528        function focused() {
     
    491539
    492540        /**
    493          * Runs when the user becomes active after a period of inactivity
    494          *
    495          * @access private
    496          *
    497          * @return void
     541         * Runs when the user becomes active after a period of inactivity.
     542         *
     543         * @access private
     544         *
     545         * @since 3.6.0
     546         *
     547         * @returns {void}
    498548         */
    499549        function userIsActive() {
     
    511561
    512562        /**
    513          * Check for user activity
    514          *
    515          * Runs every 30 sec.
    516          * 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)
    518          * for 5 min. even when the window has focus.
    519          *
    520          * @access private
    521          *
    522          * @return void
     563         * Checks for user activity.
     564         *
     565         * Runs every 30 sec. Sets 'hasFocus = true' if user is active and the window is
     566         * in the background. Sets 'hasFocus = false' if the user has been inactive
     567         * (no mouse or keyboard activity) for 5 min. even when the window has focus.
     568         *
     569         * @access private
     570         *
     571         * @since 3.8.0
     572         *
     573         * @returns {void}
    523574         */
    524575        function checkUserActivity() {
     
    553604        }
    554605
    555         // Public methods
    556 
    557         /**
    558          * Whether the window (or any local iframe in it) has focus, or the user is active
    559          *
    560          * @return bool
     606        // Public methods.
     607
     608        /**
     609         * Checks whether the window (or any local iframe in it) has focus, or the user
     610         * is active.
     611         *
     612         * @since 3.6.0
     613         * @memberOf wp.heartbeat.prototype
     614         *
     615         * @returns {boolean} True if the window or the user is active.
    561616         */
    562617        function hasFocus() {
     
    565620
    566621        /**
    567          * Whether there is a connection error
    568          *
    569          * @return bool
     622         * Checks whether there is a connection error.
     623         *
     624         * @since 3.6.0
     625         *
     626         * @memberOf wp.heartbeat.prototype
     627         *
     628         * @returns {boolean} True if a connection error was found.
    570629         */
    571630        function hasConnectionError() {
     
    574633
    575634        /**
    576          * Connect asap regardless of 'hasFocus'
     635         * Connects as soon as possible regardless of 'hasFocus' state.
    577636         *
    578637         * Will not open two concurrent connections. If a connection is in progress,
    579638         * will connect again immediately after the current connection completes.
    580639         *
    581          * @return void
     640         * @since 3.8.0
     641         *
     642         * @memberOf wp.heartbeat.prototype
     643         *
     644         * @returns {void}
    582645         */
    583646        function connectNow() {
     
    587650
    588651        /**
    589          * Disable suspending
    590          *
    591          * Should be used only when Heartbeat is performing critical tasks like autosave, post-locking, etc.
    592          * Using this on many screens may overload the user's hosting account if several
    593          * browser windows/tabs are left open for a long time.
    594          *
    595          * @return void
     652         * Disables suspending.
     653         *
     654         * Should be used only when Heartbeat is performing critical tasks like
     655         * autosave, post-locking, etc. Using this on many screens may overload the
     656         * user's hosting account if several browser windows/tabs are left open for a
     657         * long time.
     658         *
     659         * @since 3.8.0
     660         *
     661         * @memberOf wp.heartbeat.prototype
     662         *
     663         * @returns {void}
    596664         */
    597665        function disableSuspend() {
     
    600668
    601669        /**
    602          * Get/Set the interval
    603          *
    604          * When setting to 'fast' or 5, by default interval is 5 sec. for the next 30 ticks (for 2 min and 30 sec).
    605          * In this case the number of 'ticks' can be passed as second argument.
    606          * If the window doesn't have focus, the interval slows down to 2 min.
    607          *
    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
     670         * Gets/Sets the interval.
     671         *
     672         * When setting to 'fast' or 5, the interval is 5 seconds for the next 30 ticks
     673         * (for 2 minutes and 30 seconds) by default. In this case the number of 'ticks'
     674         * can be passed as second argument. If the window doesn't have focus, the
     675         * interval slows down to 2 min.
     676         *
     677         * @since 3.6.0
     678         *
     679         * @memberOf wp.heartbeat.prototype
     680         *
     681         * @param {string|number} speed Interval: 'fast' or 5, 15, 30, 60, 120. Fast
     682         *                              equals 5.
     683         * @param {string}        ticks Tells how many ticks before the interval reverts
     684         *                              back. Used with speed = 'fast' or 5.
     685         *
     686         * @returns {number} Current interval in seconds.
    611687         */
    612688        function interval( speed, ticks ) {
     
    668744
    669745        /**
    670          * Enqueue data to send with the next XHR
    671          *
    672          * As the data is send asynchronously, this function doesn't return the XHR response.
    673          * To see the response, use the custom jQuery event 'heartbeat-tick' on the document, example:
     746         * Enqueues data to send with the next XHR.
     747         *
     748         * As the data is send asynchronously, this function doesn't return the XHR
     749         * response. To see the response, use the custom jQuery event 'heartbeat-tick'
     750         * on the document, example:
    674751         *      $(document).on( 'heartbeat-tick.myname', function( event, data, textStatus, jqXHR ) {
    675752         *          // code
    676753         *      });
    677          * If the same 'handle' is used more than once, the data is not overwritten when the third argument is 'true'.
    678          * Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle.
    679          *
    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.
     754         * If the same 'handle' is used more than once, the data is not overwritten when
     755         * the third argument is 'true'. Use `wp.heartbeat.isQueued('handle')` to see if
     756         * any data is already queued for that handle.
     757         *
     758         * @since 3.6.0
     759         *
     760         * @memberOf wp.heartbeat.prototype
     761         *
     762         * @param {string}  handle      Unique handle for the data, used in PHP to
     763         *                              receive the data.
     764         * @param {*}       data        The data to send.
     765         * @param {boolean} noOverwrite Whether to overwrite existing data in the queue.
     766         *
     767         * @returns {boolean} True if the data was queued.
    684768         */
    685769        function enqueue( handle, data, noOverwrite ) {
     
    696780
    697781        /**
    698          * Check if data with a particular handle is queued
    699          *
    700          * $param string handle The handle for the data
    701          * $return bool Whether some data is queued with this handle
     782         * Checks if data with a particular handle is queued.
     783         *
     784         * @since 3.6.0
     785         *
     786         * @param {string} handle The handle for the data.
     787         *
     788         * @returns {boolean} True if the data is queued with this handle.
    702789         */
    703790        function isQueued( handle ) {
     
    708795
    709796        /**
    710          * Remove data with a particular handle from the queue
    711          *
    712          * $param string handle The handle for the data
    713          * $return void
     797         * Removes data with a particular handle from the queue.
     798         *
     799         * @since 3.7.0
     800         *
     801         * @memberOf wp.heartbeat.prototype
     802         *
     803         * @param {string} handle The handle for the data.
     804         *
     805         * @returns {void}
    714806         */
    715807        function dequeue( handle ) {
     
    720812
    721813        /**
    722          * Get data that was enqueued with a particular handle
    723          *
    724          * $param string handle The handle for the data
    725          * $return mixed The data or undefined
     814         * Gets data that was enqueued with a particular handle.
     815         *
     816         * @since 3.7.0
     817         *
     818         * @memberOf wp.heartbeat.prototype
     819         *
     820         * @param {string} handle The handle for the data.
     821         *
     822         * @returns {*} The data or undefined.
    726823         */
    727824        function getQueuedItem( handle ) {
     
    733830        initialize();
    734831
    735         // Expose public methods
     832        // Expose public methods.
    736833        return {
    737834            hasFocus: hasFocus,
     
    753850     */
    754851    window.wp = window.wp || {};
     852
     853    /**
     854     * Contains the Heartbeat API.
     855     *
     856     * @namespace wp.heartbeat
     857     * @type {Heartbeat}
     858     */
    755859    window.wp.heartbeat = new Heartbeat();
    756860
Note: See TracChangeset for help on using the changeset viewer.