home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-includes / js / heartbeat.js < prev    next >
Encoding:
JavaScript  |  2017-10-11  |  19.7 KB  |  758 lines

  1. /**
  2.  * Heartbeat API
  3.  *
  4.  * Heartbeat is a simple server polling API that sends XHR requests to
  5.  * the server every 15 - 60 seconds and triggers events (or callbacks) upon
  6.  * receiving data. Currently these 'ticks' handle transports for post locking,
  7.  * login-expiration warnings, autosave, and related tasks while a user is logged in.
  8.  *
  9.  * Available PHP filters (in ajax-actions.php):
  10.  * - heartbeat_received
  11.  * - heartbeat_send
  12.  * - heartbeat_tick
  13.  * - heartbeat_nopriv_received
  14.  * - heartbeat_nopriv_send
  15.  * - heartbeat_nopriv_tick
  16.  * @see wp_ajax_nopriv_heartbeat(), wp_ajax_heartbeat()
  17.  *
  18.  * Custom jQuery events:
  19.  * - heartbeat-send
  20.  * - heartbeat-tick
  21.  * - heartbeat-error
  22.  * - heartbeat-connection-lost
  23.  * - heartbeat-connection-restored
  24.  * - heartbeat-nonces-expired
  25.  *
  26.  * @since 3.6.0
  27.  */
  28.  
  29. ( function( $, window, undefined ) {
  30.     var Heartbeat = function() {
  31.         var $document = $(document),
  32.             settings = {
  33.                 // Suspend/resume
  34.                 suspend: false,
  35.  
  36.                 // Whether suspending is enabled
  37.                 suspendEnabled: true,
  38.  
  39.                 // Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front'
  40.                 screenId: '',
  41.  
  42.                 // XHR request URL, defaults to the JS global 'ajaxurl' when present
  43.                 url: '',
  44.  
  45.                 // Timestamp, start of the last connection request
  46.                 lastTick: 0,
  47.  
  48.                 // Container for the enqueued items
  49.                 queue: {},
  50.  
  51.                 // Connect interval (in seconds)
  52.                 mainInterval: 60,
  53.  
  54.                 // Used when the interval is set to 5 sec. temporarily
  55.                 tempInterval: 0,
  56.  
  57.                 // Used when the interval is reset
  58.                 originalInterval: 0,
  59.  
  60.                 // Used to limit the number of AJAX requests.
  61.                 minimalInterval: 0,
  62.  
  63.                 // Used together with tempInterval
  64.                 countdown: 0,
  65.  
  66.                 // Whether a connection is currently in progress
  67.                 connecting: false,
  68.  
  69.                 // Whether a connection error occurred
  70.                 connectionError: false,
  71.  
  72.                 // Used to track non-critical errors
  73.                 errorcount: 0,
  74.  
  75.                 // Whether at least one connection has completed successfully
  76.                 hasConnected: false,
  77.  
  78.                 // Whether the current browser window is in focus and the user is active
  79.                 hasFocus: true,
  80.  
  81.                 // Timestamp, last time the user was active. Checked every 30 sec.
  82.                 userActivity: 0,
  83.  
  84.                 // Flags whether events tracking user activity were set
  85.                 userActivityEvents: false,
  86.  
  87.                 checkFocusTimer: 0,
  88.                 beatTimer: 0
  89.             };
  90.  
  91.         /**
  92.          * Set local vars and events, then start
  93.          *
  94.          * @access private
  95.          *
  96.          * @return void
  97.          */
  98.         function initialize() {
  99.             var options, hidden, visibilityState, visibilitychange;
  100.  
  101.             if ( typeof window.pagenow === 'string' ) {
  102.                 settings.screenId = window.pagenow;
  103.             }
  104.  
  105.             if ( typeof window.ajaxurl === 'string' ) {
  106.                 settings.url = window.ajaxurl;
  107.             }
  108.  
  109.             // Pull in options passed from PHP
  110.             if ( typeof window.heartbeatSettings === 'object' ) {
  111.                 options = window.heartbeatSettings;
  112.  
  113.                 // The XHR URL can be passed as option when window.ajaxurl is not set
  114.                 if ( ! settings.url && options.ajaxurl ) {
  115.                     settings.url = options.ajaxurl;
  116.                 }
  117.  
  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.
  120.                 if ( options.interval ) {
  121.                     settings.mainInterval = options.interval;
  122.  
  123.                     if ( settings.mainInterval < 15 ) {
  124.                         settings.mainInterval = 15;
  125.                     } else if ( settings.mainInterval > 120 ) {
  126.                         settings.mainInterval = 120;
  127.                     }
  128.                 }
  129.  
  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.
  135.                 if ( options.minimalInterval ) {
  136.                     options.minimalInterval = parseInt( options.minimalInterval, 10 );
  137.                     settings.minimalInterval = options.minimalInterval > 0 && options.minimalInterval <= 600 ? options.minimalInterval * 1000 : 0;
  138.                 }
  139.  
  140.                 if ( settings.minimalInterval && settings.mainInterval < settings.minimalInterval ) {
  141.                     settings.mainInterval = settings.minimalInterval;
  142.                 }
  143.  
  144.                 // 'screenId' can be added from settings on the front end where the JS global 'pagenow' is not set
  145.                 if ( ! settings.screenId ) {
  146.                     settings.screenId = options.screenId || 'front';
  147.                 }
  148.  
  149.                 if ( options.suspension === 'disable' ) {
  150.                     settings.suspendEnabled = false;
  151.                 }
  152.             }
  153.  
  154.             // Convert to milliseconds
  155.             settings.mainInterval = settings.mainInterval * 1000;
  156.             settings.originalInterval = settings.mainInterval;
  157.  
  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.
  161.             if ( typeof document.hidden !== 'undefined' ) {
  162.                 hidden = 'hidden';
  163.                 visibilitychange = 'visibilitychange';
  164.                 visibilityState = 'visibilityState';
  165.             } else if ( typeof document.msHidden !== 'undefined' ) { // IE10
  166.                 hidden = 'msHidden';
  167.                 visibilitychange = 'msvisibilitychange';
  168.                 visibilityState = 'msVisibilityState';
  169.             } else if ( typeof document.webkitHidden !== 'undefined' ) { // Android
  170.                 hidden = 'webkitHidden';
  171.                 visibilitychange = 'webkitvisibilitychange';
  172.                 visibilityState = 'webkitVisibilityState';
  173.             }
  174.  
  175.             if ( hidden ) {
  176.                 if ( document[hidden] ) {
  177.                     settings.hasFocus = false;
  178.                 }
  179.  
  180.                 $document.on( visibilitychange + '.wp-heartbeat', function() {
  181.                     if ( document[visibilityState] === 'hidden' ) {
  182.                         blurred();
  183.                         window.clearInterval( settings.checkFocusTimer );
  184.                     } else {
  185.                         focused();
  186.                         if ( document.hasFocus ) {
  187.                             settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
  188.                         }
  189.                     }
  190.                 });
  191.             }
  192.  
  193.             // Use document.hasFocus() if available.
  194.             if ( document.hasFocus ) {
  195.                 settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
  196.             }
  197.  
  198.             $(window).on( 'unload.wp-heartbeat', function() {
  199.                 // Don't connect any more
  200.                 settings.suspend = true;
  201.  
  202.                 // Abort the last request if not completed
  203.                 if ( settings.xhr && settings.xhr.readyState !== 4 ) {
  204.                     settings.xhr.abort();
  205.                 }
  206.             });
  207.  
  208.             // Check for user activity every 30 seconds.
  209.             window.setInterval( checkUserActivity, 30000 );
  210.  
  211.             // Start one tick after DOM ready
  212.             $document.ready( function() {
  213.                 settings.lastTick = time();
  214.                 scheduleNextTick();
  215.             });
  216.         }
  217.  
  218.         /**
  219.          * Return the current time according to the browser
  220.          *
  221.          * @access private
  222.          *
  223.          * @return int
  224.          */
  225.         function time() {
  226.             return (new Date()).getTime();
  227.         }
  228.  
  229.         /**
  230.          * Check if the iframe is from the same origin
  231.          *
  232.          * @access private
  233.          *
  234.          * @return bool
  235.          */
  236.         function isLocalFrame( frame ) {
  237.             var origin, src = frame.src;
  238.  
  239.             // Need to compare strings as WebKit doesn't throw JS errors when iframes have different origin.
  240.             // It throws uncatchable exceptions.
  241.             if ( src && /^https?:\/\//.test( src ) ) {
  242.                 origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host;
  243.  
  244.                 if ( src.indexOf( origin ) !== 0 ) {
  245.                     return false;
  246.                 }
  247.             }
  248.  
  249.             try {
  250.                 if ( frame.contentWindow.document ) {
  251.                     return true;
  252.                 }
  253.             } catch(e) {}
  254.  
  255.             return false;
  256.         }
  257.  
  258.         /**
  259.          * Check if the document's focus has changed
  260.          *
  261.          * @access private
  262.          *
  263.          * @return void
  264.          */
  265.         function checkFocus() {
  266.             if ( settings.hasFocus && ! document.hasFocus() ) {
  267.                 blurred();
  268.             } else if ( ! settings.hasFocus && document.hasFocus() ) {
  269.                 focused();
  270.             }
  271.         }
  272.  
  273.         /**
  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
  281.          */
  282.         function setErrorState( error, status ) {
  283.             var trigger;
  284.  
  285.             if ( error ) {
  286.                 switch ( error ) {
  287.                     case 'abort':
  288.                         // do nothing
  289.                         break;
  290.                     case 'timeout':
  291.                         // no response for 30 sec.
  292.                         trigger = true;
  293.                         break;
  294.                     case 'error':
  295.                         if ( 503 === status && settings.hasConnected ) {
  296.                             trigger = true;
  297.                             break;
  298.                         }
  299.                         /* falls through */
  300.                     case 'parsererror':
  301.                     case 'empty':
  302.                     case 'unknown':
  303.                         settings.errorcount++;
  304.  
  305.                         if ( settings.errorcount > 2 && settings.hasConnected ) {
  306.                             trigger = true;
  307.                         }
  308.  
  309.                         break;
  310.                 }
  311.  
  312.                 if ( trigger && ! hasConnectionError() ) {
  313.                     settings.connectionError = true;
  314.                     $document.trigger( 'heartbeat-connection-lost', [error, status] );
  315.                 }
  316.             }
  317.         }
  318.  
  319.         /**
  320.          * Clear the error state and fire an event
  321.          *
  322.          * @access private
  323.          *
  324.          * @return void
  325.          */
  326.         function clearErrorState() {
  327.             // Has connected successfully
  328.             settings.hasConnected = true;
  329.  
  330.             if ( hasConnectionError() ) {
  331.                 settings.errorcount = 0;
  332.                 settings.connectionError = false;
  333.                 $document.trigger( 'heartbeat-connection-restored' );
  334.             }
  335.         }
  336.  
  337.         /**
  338.          * Gather the data and connect to the server
  339.          *
  340.          * @access private
  341.          *
  342.          * @return void
  343.          */
  344.         function connect() {
  345.             var ajaxData, heartbeatData;
  346.  
  347.             // If the connection to the server is slower than the interval,
  348.             // heartbeat connects as soon as the previous connection's response is received.
  349.             if ( settings.connecting || settings.suspend ) {
  350.                 return;
  351.             }
  352.  
  353.             settings.lastTick = time();
  354.  
  355.             heartbeatData = $.extend( {}, settings.queue );
  356.             // Clear the data queue, anything added after this point will be send on the next tick
  357.             settings.queue = {};
  358.  
  359.             $document.trigger( 'heartbeat-send', [ heartbeatData ] );
  360.  
  361.             ajaxData = {
  362.                 data: heartbeatData,
  363.                 interval: settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000,
  364.                 _nonce: typeof window.heartbeatSettings === 'object' ? window.heartbeatSettings.nonce : '',
  365.                 action: 'heartbeat',
  366.                 screen_id: settings.screenId,
  367.                 has_focus: settings.hasFocus
  368.             };
  369.  
  370.             if ( 'customize' === settings.screenId  ) {
  371.                 ajaxData.wp_customize = 'on';
  372.             }
  373.  
  374.             settings.connecting = true;
  375.             settings.xhr = $.ajax({
  376.                 url: settings.url,
  377.                 type: 'post',
  378.                 timeout: 30000, // throw an error if not completed after 30 sec.
  379.                 data: ajaxData,
  380.                 dataType: 'json'
  381.             }).always( function() {
  382.                 settings.connecting = false;
  383.                 scheduleNextTick();
  384.             }).done( function( response, textStatus, jqXHR ) {
  385.                 var newInterval;
  386.  
  387.                 if ( ! response ) {
  388.                     setErrorState( 'empty' );
  389.                     return;
  390.                 }
  391.  
  392.                 clearErrorState();
  393.  
  394.                 if ( response.nonces_expired ) {
  395.                     $document.trigger( 'heartbeat-nonces-expired' );
  396.                 }
  397.  
  398.                 // Change the interval from PHP
  399.                 if ( response.heartbeat_interval ) {
  400.                     newInterval = response.heartbeat_interval;
  401.                     delete response.heartbeat_interval;
  402.                 }
  403.  
  404.                 $document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] );
  405.  
  406.                 // Do this last, can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'
  407.                 if ( newInterval ) {
  408.                     interval( newInterval );
  409.                 }
  410.             }).fail( function( jqXHR, textStatus, error ) {
  411.                 setErrorState( textStatus || 'unknown', jqXHR.status );
  412.                 $document.trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
  413.             });
  414.         }
  415.  
  416.         /**
  417.          * Schedule the next connection
  418.          *
  419.          * Fires immediately if the connection time is longer than the interval.
  420.          *
  421.          * @access private
  422.          *
  423.          * @return void
  424.          */
  425.         function scheduleNextTick() {
  426.             var delta = time() - settings.lastTick,
  427.                 interval = settings.mainInterval;
  428.  
  429.             if ( settings.suspend ) {
  430.                 return;
  431.             }
  432.  
  433.             if ( ! settings.hasFocus ) {
  434.                 interval = 120000; // 120 sec. Post locks expire after 150 sec.
  435.             } else if ( settings.countdown > 0 && settings.tempInterval ) {
  436.                 interval = settings.tempInterval;
  437.                 settings.countdown--;
  438.  
  439.                 if ( settings.countdown < 1 ) {
  440.                     settings.tempInterval = 0;
  441.                 }
  442.             }
  443.  
  444.             if ( settings.minimalInterval && interval < settings.minimalInterval ) {
  445.                 interval = settings.minimalInterval;
  446.             }
  447.  
  448.             window.clearTimeout( settings.beatTimer );
  449.  
  450.             if ( delta < interval ) {
  451.                 settings.beatTimer = window.setTimeout(
  452.                     function() {
  453.                         connect();
  454.                     },
  455.                     interval - delta
  456.                 );
  457.             } else {
  458.                 connect();
  459.             }
  460.         }
  461.  
  462.         /**
  463.          * Set the internal state when the browser window becomes hidden or loses focus
  464.          *
  465.          * @access private
  466.          *
  467.          * @return void
  468.          */
  469.         function blurred() {
  470.             settings.hasFocus = false;
  471.         }
  472.  
  473.         /**
  474.          * Set the internal state when the browser window becomes visible or is in focus
  475.          *
  476.          * @access private
  477.          *
  478.          * @return void
  479.          */
  480.         function focused() {
  481.             settings.userActivity = time();
  482.  
  483.             // Resume if suspended
  484.             settings.suspend = false;
  485.  
  486.             if ( ! settings.hasFocus ) {
  487.                 settings.hasFocus = true;
  488.                 scheduleNextTick();
  489.             }
  490.         }
  491.  
  492.         /**
  493.          * Runs when the user becomes active after a period of inactivity
  494.          *
  495.          * @access private
  496.          *
  497.          * @return void
  498.          */
  499.         function userIsActive() {
  500.             settings.userActivityEvents = false;
  501.             $document.off( '.wp-heartbeat-active' );
  502.  
  503.             $('iframe').each( function( i, frame ) {
  504.                 if ( isLocalFrame( frame ) ) {
  505.                     $( frame.contentWindow ).off( '.wp-heartbeat-active' );
  506.                 }
  507.             });
  508.  
  509.             focused();
  510.         }
  511.  
  512.         /**
  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
  523.          */
  524.         function checkUserActivity() {
  525.             var lastActive = settings.userActivity ? time() - settings.userActivity : 0;
  526.  
  527.             // Throttle down when no mouse or keyboard activity for 5 min.
  528.             if ( lastActive > 300000 && settings.hasFocus ) {
  529.                 blurred();
  530.             }
  531.  
  532.             // Suspend after 10 min. of inactivity when suspending is enabled.
  533.             // Always suspend after 60 min. of inactivity. This will release the post lock, etc.
  534.             if ( ( settings.suspendEnabled && lastActive > 600000 ) || lastActive > 3600000 ) {
  535.                 settings.suspend = true;
  536.             }
  537.  
  538.             if ( ! settings.userActivityEvents ) {
  539.                 $document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() {
  540.                     userIsActive();
  541.                 });
  542.  
  543.                 $('iframe').each( function( i, frame ) {
  544.                     if ( isLocalFrame( frame ) ) {
  545.                         $( frame.contentWindow ).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() {
  546.                             userIsActive();
  547.                         });
  548.                     }
  549.                 });
  550.  
  551.                 settings.userActivityEvents = true;
  552.             }
  553.         }
  554.  
  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
  561.          */
  562.         function hasFocus() {
  563.             return settings.hasFocus;
  564.         }
  565.  
  566.         /**
  567.          * Whether there is a connection error
  568.          *
  569.          * @return bool
  570.          */
  571.         function hasConnectionError() {
  572.             return settings.connectionError;
  573.         }
  574.  
  575.         /**
  576.          * Connect asap regardless of 'hasFocus'
  577.          *
  578.          * Will not open two concurrent connections. If a connection is in progress,
  579.          * will connect again immediately after the current connection completes.
  580.          *
  581.          * @return void
  582.          */
  583.         function connectNow() {
  584.             settings.lastTick = 0;
  585.             scheduleNextTick();
  586.         }
  587.  
  588.         /**
  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
  596.          */
  597.         function disableSuspend() {
  598.             settings.suspendEnabled = false;
  599.         }
  600.  
  601.         /**
  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
  611.          */
  612.         function interval( speed, ticks ) {
  613.             var newInterval,
  614.                 oldInterval = settings.tempInterval ? settings.tempInterval : settings.mainInterval;
  615.  
  616.             if ( speed ) {
  617.                 switch ( speed ) {
  618.                     case 'fast':
  619.                     case 5:
  620.                         newInterval = 5000;
  621.                         break;
  622.                     case 15:
  623.                         newInterval = 15000;
  624.                         break;
  625.                     case 30:
  626.                         newInterval = 30000;
  627.                         break;
  628.                     case 60:
  629.                         newInterval = 60000;
  630.                         break;
  631.                     case 120:
  632.                         newInterval = 120000;
  633.                         break;
  634.                     case 'long-polling':
  635.                         // Allow long polling, (experimental)
  636.                         settings.mainInterval = 0;
  637.                         return 0;
  638.                     default:
  639.                         newInterval = settings.originalInterval;
  640.                 }
  641.  
  642.                 if ( settings.minimalInterval && newInterval < settings.minimalInterval ) {
  643.                     newInterval = settings.minimalInterval;
  644.                 }
  645.  
  646.                 if ( 5000 === newInterval ) {
  647.                     ticks = parseInt( ticks, 10 ) || 30;
  648.                     ticks = ticks < 1 || ticks > 30 ? 30 : ticks;
  649.  
  650.                     settings.countdown = ticks;
  651.                     settings.tempInterval = newInterval;
  652.                 } else {
  653.                     settings.countdown = 0;
  654.                     settings.tempInterval = 0;
  655.                     settings.mainInterval = newInterval;
  656.                 }
  657.  
  658.                 // Change the next connection time if new interval has been set.
  659.                 // Will connect immediately if the time since the last connection
  660.                 // is greater than the new interval.
  661.                 if ( newInterval !== oldInterval ) {
  662.                     scheduleNextTick();
  663.                 }
  664.             }
  665.  
  666.             return settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000;
  667.         }
  668.  
  669.         /**
  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:
  674.          *        $(document).on( 'heartbeat-tick.myname', function( event, data, textStatus, jqXHR ) {
  675.          *            // code
  676.          *        });
  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.
  684.          */
  685.         function enqueue( handle, data, noOverwrite ) {
  686.             if ( handle ) {
  687.                 if ( noOverwrite && this.isQueued( handle ) ) {
  688.                     return false;
  689.                 }
  690.  
  691.                 settings.queue[handle] = data;
  692.                 return true;
  693.             }
  694.             return false;
  695.         }
  696.  
  697.         /**
  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
  702.          */
  703.         function isQueued( handle ) {
  704.             if ( handle ) {
  705.                 return settings.queue.hasOwnProperty( handle );
  706.             }
  707.         }
  708.  
  709.         /**
  710.          * Remove data with a particular handle from the queue
  711.          *
  712.          * $param string handle The handle for the data
  713.          * $return void
  714.          */
  715.         function dequeue( handle ) {
  716.             if ( handle ) {
  717.                 delete settings.queue[handle];
  718.             }
  719.         }
  720.  
  721.         /**
  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
  726.          */
  727.         function getQueuedItem( handle ) {
  728.             if ( handle ) {
  729.                 return this.isQueued( handle ) ? settings.queue[handle] : undefined;
  730.             }
  731.         }
  732.  
  733.         initialize();
  734.  
  735.         // Expose public methods
  736.         return {
  737.             hasFocus: hasFocus,
  738.             connectNow: connectNow,
  739.             disableSuspend: disableSuspend,
  740.             interval: interval,
  741.             hasConnectionError: hasConnectionError,
  742.             enqueue: enqueue,
  743.             dequeue: dequeue,
  744.             isQueued: isQueued,
  745.             getQueuedItem: getQueuedItem
  746.         };
  747.     };
  748.  
  749.     /**
  750.      * Ensure the global `wp` object exists.
  751.      *
  752.      * @namespace wp
  753.      */
  754.     window.wp = window.wp || {};
  755.     window.wp.heartbeat = new Heartbeat();
  756.  
  757. }( jQuery, window ));
  758.