[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/ -> heartbeat.js (source)

   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   * @output wp-includes/js/heartbeat.js
  28   */
  29  
  30  ( function( $, window, undefined ) {
  31  
  32      /**
  33       * Constructs the Heartbeat API.
  34       *
  35       * @since 3.6.0
  36       *
  37       * @returns {Object} An instance of the Heartbeat class.
  38       * @constructor
  39       */
  40      var Heartbeat = function() {
  41          var $document = $(document),
  42              settings = {
  43                  // Suspend/resume.
  44                  suspend: false,
  45  
  46                  // Whether suspending is enabled.
  47                  suspendEnabled: true,
  48  
  49                  // Current screen id, defaults to the JS global 'pagenow' when present
  50                  // (in the admin) or 'front'.
  51                  screenId: '',
  52  
  53                  // XHR request URL, defaults to the JS global 'ajaxurl' when present.
  54                  url: '',
  55  
  56                  // Timestamp, start of the last connection request.
  57                  lastTick: 0,
  58  
  59                  // Container for the enqueued items.
  60                  queue: {},
  61  
  62                  // Connect interval (in seconds).
  63                  mainInterval: 60,
  64  
  65                  // Used when the interval is set to 5 sec. temporarily.
  66                  tempInterval: 0,
  67  
  68                  // Used when the interval is reset.
  69                  originalInterval: 0,
  70  
  71                  // Used to limit the number of AJAX requests.
  72                  minimalInterval: 0,
  73  
  74                  // Used together with tempInterval.
  75                  countdown: 0,
  76  
  77                  // Whether a connection is currently in progress.
  78                  connecting: false,
  79  
  80                  // Whether a connection error occurred.
  81                  connectionError: false,
  82  
  83                  // Used to track non-critical errors.
  84                  errorcount: 0,
  85  
  86                  // Whether at least one connection has been completed successfully.
  87                  hasConnected: false,
  88  
  89                  // Whether the current browser window is in focus and the user is active.
  90                  hasFocus: true,
  91  
  92                  // Timestamp, last time the user was active. Checked every 30 sec.
  93                  userActivity: 0,
  94  
  95                  // Flag whether events tracking user activity were set.
  96                  userActivityEvents: false,
  97  
  98                  // Timer that keeps track of how long a user has focus.
  99                  checkFocusTimer: 0,
 100  
 101                  // Timer that keeps track of how long needs to be waited before connecting to
 102                  // the server again.
 103                  beatTimer: 0
 104              };
 105  
 106          /**
 107           * Sets local variables and events, then starts the heartbeat.
 108           *
 109           * @access private
 110           *
 111           * @since 3.8.0
 112           *
 113           * @returns {void}
 114           */
 115  		function initialize() {
 116              var options, hidden, visibilityState, visibilitychange;
 117  
 118              if ( typeof window.pagenow === 'string' ) {
 119                  settings.screenId = window.pagenow;
 120              }
 121  
 122              if ( typeof window.ajaxurl === 'string' ) {
 123                  settings.url = window.ajaxurl;
 124              }
 125  
 126              // Pull in options passed from PHP.
 127              if ( typeof window.heartbeatSettings === 'object' ) {
 128                  options = window.heartbeatSettings;
 129  
 130                  // The XHR URL can be passed as option when window.ajaxurl is not set.
 131                  if ( ! settings.url && options.ajaxurl ) {
 132                      settings.url = options.ajaxurl;
 133                  }
 134  
 135                  /*
 136                   * The interval can be from 15 to 120 sec. and can be set temporarily to 5 sec.
 137                   * It can be set in the initial options or changed later through JS and/or
 138                   * through PHP.
 139                   */
 140                  if ( options.interval ) {
 141                      settings.mainInterval = options.interval;
 142  
 143                      if ( settings.mainInterval < 15 ) {
 144                          settings.mainInterval = 15;
 145                      } else if ( settings.mainInterval > 120 ) {
 146                          settings.mainInterval = 120;
 147                      }
 148                  }
 149  
 150                  /*
 151                   * Used to limit the number of AJAX requests. Overrides all other intervals if
 152                   * they are shorter. Needed for some hosts that cannot handle frequent requests
 153                   * and the user may exceed the allocated server CPU time, etc. The minimal
 154                   * interval can be up to 600 sec. however setting it to longer than 120 sec.
 155                   * will limit or disable some of the functionality (like post locks). Once set
 156                   * at initialization, minimalInterval cannot be changed/overridden.
 157                   */
 158                  if ( options.minimalInterval ) {
 159                      options.minimalInterval = parseInt( options.minimalInterval, 10 );
 160                      settings.minimalInterval = options.minimalInterval > 0 && options.minimalInterval <= 600 ? options.minimalInterval * 1000 : 0;
 161                  }
 162  
 163                  if ( settings.minimalInterval && settings.mainInterval < settings.minimalInterval ) {
 164                      settings.mainInterval = settings.minimalInterval;
 165                  }
 166  
 167                  // 'screenId' can be added from settings on the front end where the JS global
 168                  // 'pagenow' is not set.
 169                  if ( ! settings.screenId ) {
 170                      settings.screenId = options.screenId || 'front';
 171                  }
 172  
 173                  if ( options.suspension === 'disable' ) {
 174                      settings.suspendEnabled = false;
 175                  }
 176              }
 177  
 178              // Convert to milliseconds.
 179              settings.mainInterval = settings.mainInterval * 1000;
 180              settings.originalInterval = settings.mainInterval;
 181  
 182              /*
 183               * Switch the interval to 120 seconds by using the Page Visibility API.
 184               * If the browser doesn't support it (Safari < 7, Android < 4.4, IE < 10), the
 185               * interval will be increased to 120 seconds after 5 minutes of mouse and keyboard
 186               * inactivity.
 187               */
 188              if ( typeof document.hidden !== 'undefined' ) {
 189                  hidden = 'hidden';
 190                  visibilitychange = 'visibilitychange';
 191                  visibilityState = 'visibilityState';
 192              } else if ( typeof document.msHidden !== 'undefined' ) { // IE10
 193                  hidden = 'msHidden';
 194                  visibilitychange = 'msvisibilitychange';
 195                  visibilityState = 'msVisibilityState';
 196              } else if ( typeof document.webkitHidden !== 'undefined' ) { // Android
 197                  hidden = 'webkitHidden';
 198                  visibilitychange = 'webkitvisibilitychange';
 199                  visibilityState = 'webkitVisibilityState';
 200              }
 201  
 202              if ( hidden ) {
 203                  if ( document[hidden] ) {
 204                      settings.hasFocus = false;
 205                  }
 206  
 207                  $document.on( visibilitychange + '.wp-heartbeat', function() {
 208                      if ( document[visibilityState] === 'hidden' ) {
 209                          blurred();
 210                          window.clearInterval( settings.checkFocusTimer );
 211                      } else {
 212                          focused();
 213                          if ( document.hasFocus ) {
 214                              settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
 215                          }
 216                      }
 217                  });
 218              }
 219  
 220              // Use document.hasFocus() if available.
 221              if ( document.hasFocus ) {
 222                  settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
 223              }
 224  
 225              $(window).on( 'unload.wp-heartbeat', function() {
 226                  // Don't connect anymore.
 227                  settings.suspend = true;
 228  
 229                  // Abort the last request if not completed.
 230                  if ( settings.xhr && settings.xhr.readyState !== 4 ) {
 231                      settings.xhr.abort();
 232                  }
 233              });
 234  
 235              // Check for user activity every 30 seconds.
 236              window.setInterval( checkUserActivity, 30000 );
 237  
 238              // Start one tick after DOM ready.
 239              $document.ready( function() {
 240                  settings.lastTick = time();
 241                  scheduleNextTick();
 242              });
 243          }
 244  
 245          /**
 246           * Returns the current time according to the browser.
 247           *
 248           * @access private
 249           *
 250           * @since 3.6.0
 251           *
 252           * @returns {number} Returns the current time.
 253           */
 254  		function time() {
 255              return (new Date()).getTime();
 256          }
 257  
 258          /**
 259           * Checks if the iframe is from the same origin.
 260           *
 261           * @access private
 262           *
 263           * @since 3.6.0
 264           *
 265           * @returns {boolean} Returns whether or not the iframe is from the same origin.
 266           */
 267  		function isLocalFrame( frame ) {
 268              var origin, src = frame.src;
 269  
 270              /*
 271               * Need to compare strings as WebKit doesn't throw JS errors when iframes have
 272               * different origin. It throws uncatchable exceptions.
 273               */
 274              if ( src && /^https?:\/\//.test( src ) ) {
 275                  origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host;
 276  
 277                  if ( src.indexOf( origin ) !== 0 ) {
 278                      return false;
 279                  }
 280              }
 281  
 282              try {
 283                  if ( frame.contentWindow.document ) {
 284                      return true;
 285                  }
 286              } catch(e) {}
 287  
 288              return false;
 289          }
 290  
 291          /**
 292           * Checks if the document's focus has changed.
 293           *
 294           * @access private
 295           *
 296           * @since 4.1.0
 297           *
 298           * @returns {void}
 299           */
 300  		function checkFocus() {
 301              if ( settings.hasFocus && ! document.hasFocus() ) {
 302                  blurred();
 303              } else if ( ! settings.hasFocus && document.hasFocus() ) {
 304                  focused();
 305              }
 306          }
 307  
 308          /**
 309           * Sets error state and fires an event on XHR errors or timeout.
 310           *
 311           * @access private
 312           *
 313           * @since 3.8.0
 314           *
 315           * @param {string} error  The error type passed from the XHR.
 316           * @param {number} status The HTTP status code passed from jqXHR
 317           *                        (200, 404, 500, etc.).
 318           *
 319           * @returns {void}
 320           */
 321  		function setErrorState( error, status ) {
 322              var trigger;
 323  
 324              if ( error ) {
 325                  switch ( error ) {
 326                      case 'abort':
 327                          // Do nothing.
 328                          break;
 329                      case 'timeout':
 330                          // No response for 30 sec.
 331                          trigger = true;
 332                          break;
 333                      case 'error':
 334                          if ( 503 === status && settings.hasConnected ) {
 335                              trigger = true;
 336                              break;
 337                          }
 338                          /* falls through */
 339                      case 'parsererror':
 340                      case 'empty':
 341                      case 'unknown':
 342                          settings.errorcount++;
 343  
 344                          if ( settings.errorcount > 2 && settings.hasConnected ) {
 345                              trigger = true;
 346                          }
 347  
 348                          break;
 349                  }
 350  
 351                  if ( trigger && ! hasConnectionError() ) {
 352                      settings.connectionError = true;
 353                      $document.trigger( 'heartbeat-connection-lost', [error, status] );
 354                      wp.hooks.doAction( 'heartbeat.connection-lost', error, status );
 355                  }
 356              }
 357          }
 358  
 359          /**
 360           * Clears the error state and fires an event if there is a connection error.
 361           *
 362           * @access private
 363           *
 364           * @since 3.8.0
 365           *
 366           * @returns {void}
 367           */
 368  		function clearErrorState() {
 369              // Has connected successfully.
 370              settings.hasConnected = true;
 371  
 372              if ( hasConnectionError() ) {
 373                  settings.errorcount = 0;
 374                  settings.connectionError = false;
 375                  $document.trigger( 'heartbeat-connection-restored' );
 376                  wp.hooks.doAction( 'heartbeat.connection-restored' );
 377              }
 378          }
 379  
 380          /**
 381           * Gathers the data and connects to the server.
 382           *
 383           * @access private
 384           *
 385           * @since 3.6.0
 386           *
 387           * @returns {void}
 388           */
 389  		function connect() {
 390              var ajaxData, heartbeatData;
 391  
 392              // If the connection to the server is slower than the interval,
 393              // heartbeat connects as soon as the previous connection's response is received.
 394              if ( settings.connecting || settings.suspend ) {
 395                  return;
 396              }
 397  
 398              settings.lastTick = time();
 399  
 400              heartbeatData = $.extend( {}, settings.queue );
 401              // Clear the data queue. Anything added after this point will be sent on the next tick.
 402              settings.queue = {};
 403  
 404              $document.trigger( 'heartbeat-send', [ heartbeatData ] );
 405              wp.hooks.doAction( 'heartbeat.send', heartbeatData );
 406  
 407              ajaxData = {
 408                  data: heartbeatData,
 409                  interval: settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000,
 410                  _nonce: typeof window.heartbeatSettings === 'object' ? window.heartbeatSettings.nonce : '',
 411                  action: 'heartbeat',
 412                  screen_id: settings.screenId,
 413                  has_focus: settings.hasFocus
 414              };
 415  
 416              if ( 'customize' === settings.screenId  ) {
 417                  ajaxData.wp_customize = 'on';
 418              }
 419  
 420              settings.connecting = true;
 421              settings.xhr = $.ajax({
 422                  url: settings.url,
 423                  type: 'post',
 424                  timeout: 30000, // throw an error if not completed after 30 sec.
 425                  data: ajaxData,
 426                  dataType: 'json'
 427              }).always( function() {
 428                  settings.connecting = false;
 429                  scheduleNextTick();
 430              }).done( function( response, textStatus, jqXHR ) {
 431                  var newInterval;
 432  
 433                  if ( ! response ) {
 434                      setErrorState( 'empty' );
 435                      return;
 436                  }
 437  
 438                  clearErrorState();
 439  
 440                  if ( response.nonces_expired ) {
 441                      $document.trigger( 'heartbeat-nonces-expired' );
 442                      wp.hooks.doAction( 'heartbeat.nonces-expired' );
 443                  }
 444  
 445                  // Change the interval from PHP
 446                  if ( response.heartbeat_interval ) {
 447                      newInterval = response.heartbeat_interval;
 448                      delete response.heartbeat_interval;
 449                  }
 450  
 451                  // Update the heartbeat nonce if set.
 452                  if ( response.heartbeat_nonce && typeof window.heartbeatSettings === 'object' ) {
 453                      window.heartbeatSettings.nonce = response.heartbeat_nonce;
 454                      delete response.heartbeat_nonce;
 455                  }
 456  
 457                  // Update the Rest API nonce if set and wp-api loaded.
 458                  if ( response.rest_nonce && typeof window.wpApiSettings === 'object' ) {
 459                      window.wpApiSettings.nonce = response.rest_nonce;
 460                      // This nonce is required for api-fetch through heartbeat.tick.
 461                      // delete response.rest_nonce;
 462                  }
 463  
 464                  $document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] );
 465                  wp.hooks.doAction( 'heartbeat.tick', response, textStatus, jqXHR );
 466  
 467                  // Do this last. Can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'.
 468                  if ( newInterval ) {
 469                      interval( newInterval );
 470                  }
 471              }).fail( function( jqXHR, textStatus, error ) {
 472                  setErrorState( textStatus || 'unknown', jqXHR.status );
 473                  $document.trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
 474                  wp.hooks.doAction( 'heartbeat.error', jqXHR, textStatus, error );
 475              });
 476          }
 477  
 478          /**
 479           * Schedules the next connection.
 480           *
 481           * Fires immediately if the connection time is longer than the interval.
 482           *
 483           * @access private
 484           *
 485           * @since 3.8.0
 486           *
 487           * @returns {void}
 488           */
 489  		function scheduleNextTick() {
 490              var delta = time() - settings.lastTick,
 491                  interval = settings.mainInterval;
 492  
 493              if ( settings.suspend ) {
 494                  return;
 495              }
 496  
 497              if ( ! settings.hasFocus ) {
 498                  interval = 120000; // 120 sec. Post locks expire after 150 sec.
 499              } else if ( settings.countdown > 0 && settings.tempInterval ) {
 500                  interval = settings.tempInterval;
 501                  settings.countdown--;
 502  
 503                  if ( settings.countdown < 1 ) {
 504                      settings.tempInterval = 0;
 505                  }
 506              }
 507  
 508              if ( settings.minimalInterval && interval < settings.minimalInterval ) {
 509                  interval = settings.minimalInterval;
 510              }
 511  
 512              window.clearTimeout( settings.beatTimer );
 513  
 514              if ( delta < interval ) {
 515                  settings.beatTimer = window.setTimeout(
 516                      function() {
 517                          connect();
 518                      },
 519                      interval - delta
 520                  );
 521              } else {
 522                  connect();
 523              }
 524          }
 525  
 526          /**
 527           * Sets the internal state when the browser window becomes hidden or loses focus.
 528           *
 529           * @access private
 530           *
 531           * @since 3.6.0
 532           *
 533           * @returns {void}
 534           */
 535  		function blurred() {
 536              settings.hasFocus = false;
 537          }
 538  
 539          /**
 540           * Sets the internal state when the browser window becomes visible or is in focus.
 541           *
 542           * @access private
 543           *
 544           * @since 3.6.0
 545           *
 546           * @returns {void}
 547           */
 548  		function focused() {
 549              settings.userActivity = time();
 550  
 551              // Resume if suspended
 552              settings.suspend = false;
 553  
 554              if ( ! settings.hasFocus ) {
 555                  settings.hasFocus = true;
 556                  scheduleNextTick();
 557              }
 558          }
 559  
 560          /**
 561           * Runs when the user becomes active after a period of inactivity.
 562           *
 563           * @access private
 564           *
 565           * @since 3.6.0
 566           *
 567           * @returns {void}
 568           */
 569  		function userIsActive() {
 570              settings.userActivityEvents = false;
 571              $document.off( '.wp-heartbeat-active' );
 572  
 573              $('iframe').each( function( i, frame ) {
 574                  if ( isLocalFrame( frame ) ) {
 575                      $( frame.contentWindow ).off( '.wp-heartbeat-active' );
 576                  }
 577              });
 578  
 579              focused();
 580          }
 581  
 582          /**
 583           * Checks for user activity.
 584           *
 585           * Runs every 30 sec. Sets 'hasFocus = true' if user is active and the window is
 586           * in the background. Sets 'hasFocus = false' if the user has been inactive
 587           * (no mouse or keyboard activity) for 5 min. even when the window has focus.
 588           *
 589           * @access private
 590           *
 591           * @since 3.8.0
 592           *
 593           * @returns {void}
 594           */
 595  		function checkUserActivity() {
 596              var lastActive = settings.userActivity ? time() - settings.userActivity : 0;
 597  
 598              // Throttle down when no mouse or keyboard activity for 5 min.
 599              if ( lastActive > 300000 && settings.hasFocus ) {
 600                  blurred();
 601              }
 602  
 603              // Suspend after 10 min. of inactivity when suspending is enabled.
 604              // Always suspend after 60 min. of inactivity. This will release the post lock, etc.
 605              if ( ( settings.suspendEnabled && lastActive > 600000 ) || lastActive > 3600000 ) {
 606                  settings.suspend = true;
 607              }
 608  
 609              if ( ! settings.userActivityEvents ) {
 610                  $document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() {
 611                      userIsActive();
 612                  });
 613  
 614                  $('iframe').each( function( i, frame ) {
 615                      if ( isLocalFrame( frame ) ) {
 616                          $( frame.contentWindow ).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() {
 617                              userIsActive();
 618                          });
 619                      }
 620                  });
 621  
 622                  settings.userActivityEvents = true;
 623              }
 624          }
 625  
 626          // Public methods.
 627  
 628          /**
 629           * Checks whether the window (or any local iframe in it) has focus, or the user
 630           * is active.
 631           *
 632           * @since 3.6.0
 633           * @memberOf wp.heartbeat.prototype
 634           *
 635           * @returns {boolean} True if the window or the user is active.
 636           */
 637  		function hasFocus() {
 638              return settings.hasFocus;
 639          }
 640  
 641          /**
 642           * Checks whether there is a connection error.
 643           *
 644           * @since 3.6.0
 645           *
 646           * @memberOf wp.heartbeat.prototype
 647           *
 648           * @returns {boolean} True if a connection error was found.
 649           */
 650  		function hasConnectionError() {
 651              return settings.connectionError;
 652          }
 653  
 654          /**
 655           * Connects as soon as possible regardless of 'hasFocus' state.
 656           *
 657           * Will not open two concurrent connections. If a connection is in progress,
 658           * will connect again immediately after the current connection completes.
 659           *
 660           * @since 3.8.0
 661           *
 662           * @memberOf wp.heartbeat.prototype
 663           *
 664           * @returns {void}
 665           */
 666  		function connectNow() {
 667              settings.lastTick = 0;
 668              scheduleNextTick();
 669          }
 670  
 671          /**
 672           * Disables suspending.
 673           *
 674           * Should be used only when Heartbeat is performing critical tasks like
 675           * autosave, post-locking, etc. Using this on many screens may overload the
 676           * user's hosting account if several browser windows/tabs are left open for a
 677           * long time.
 678           *
 679           * @since 3.8.0
 680           *
 681           * @memberOf wp.heartbeat.prototype
 682           *
 683           * @returns {void}
 684           */
 685  		function disableSuspend() {
 686              settings.suspendEnabled = false;
 687          }
 688  
 689          /**
 690           * Gets/Sets the interval.
 691           *
 692           * When setting to 'fast' or 5, the interval is 5 seconds for the next 30 ticks
 693           * (for 2 minutes and 30 seconds) by default. In this case the number of 'ticks'
 694           * can be passed as second argument. If the window doesn't have focus, the
 695           * interval slows down to 2 min.
 696           *
 697           * @since 3.6.0
 698           *
 699           * @memberOf wp.heartbeat.prototype
 700           *
 701           * @param {string|number} speed Interval: 'fast' or 5, 15, 30, 60, 120. Fast
 702           *                              equals 5.
 703           * @param {string}        ticks Tells how many ticks before the interval reverts
 704           *                              back. Used with speed = 'fast' or 5.
 705           *
 706           * @returns {number} Current interval in seconds.
 707           */
 708  		function interval( speed, ticks ) {
 709              var newInterval,
 710                  oldInterval = settings.tempInterval ? settings.tempInterval : settings.mainInterval;
 711  
 712              if ( speed ) {
 713                  switch ( speed ) {
 714                      case 'fast':
 715                      case 5:
 716                          newInterval = 5000;
 717                          break;
 718                      case 15:
 719                          newInterval = 15000;
 720                          break;
 721                      case 30:
 722                          newInterval = 30000;
 723                          break;
 724                      case 60:
 725                          newInterval = 60000;
 726                          break;
 727                      case 120:
 728                          newInterval = 120000;
 729                          break;
 730                      case 'long-polling':
 731                          // Allow long polling, (experimental)
 732                          settings.mainInterval = 0;
 733                          return 0;
 734                      default:
 735                          newInterval = settings.originalInterval;
 736                  }
 737  
 738                  if ( settings.minimalInterval && newInterval < settings.minimalInterval ) {
 739                      newInterval = settings.minimalInterval;
 740                  }
 741  
 742                  if ( 5000 === newInterval ) {
 743                      ticks = parseInt( ticks, 10 ) || 30;
 744                      ticks = ticks < 1 || ticks > 30 ? 30 : ticks;
 745  
 746                      settings.countdown = ticks;
 747                      settings.tempInterval = newInterval;
 748                  } else {
 749                      settings.countdown = 0;
 750                      settings.tempInterval = 0;
 751                      settings.mainInterval = newInterval;
 752                  }
 753  
 754                  // Change the next connection time if new interval has been set.
 755                  // Will connect immediately if the time since the last connection
 756                  // is greater than the new interval.
 757                  if ( newInterval !== oldInterval ) {
 758                      scheduleNextTick();
 759                  }
 760              }
 761  
 762              return settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000;
 763          }
 764  
 765          /**
 766           * Enqueues data to send with the next XHR.
 767           *
 768           * As the data is send asynchronously, this function doesn't return the XHR
 769           * response. To see the response, use the custom jQuery event 'heartbeat-tick'
 770           * on the document, example:
 771           *        $(document).on( 'heartbeat-tick.myname', function( event, data, textStatus, jqXHR ) {
 772           *            // code
 773           *        });
 774           * If the same 'handle' is used more than once, the data is not overwritten when
 775           * the third argument is 'true'. Use `wp.heartbeat.isQueued('handle')` to see if
 776           * any data is already queued for that handle.
 777           *
 778           * @since 3.6.0
 779           *
 780           * @memberOf wp.heartbeat.prototype
 781           *
 782           * @param {string}  handle      Unique handle for the data, used in PHP to
 783           *                              receive the data.
 784           * @param {*}       data        The data to send.
 785           * @param {boolean} noOverwrite Whether to overwrite existing data in the queue.
 786           *
 787           * @returns {boolean} True if the data was queued.
 788           */
 789  		function enqueue( handle, data, noOverwrite ) {
 790              if ( handle ) {
 791                  if ( noOverwrite && this.isQueued( handle ) ) {
 792                      return false;
 793                  }
 794  
 795                  settings.queue[handle] = data;
 796                  return true;
 797              }
 798              return false;
 799          }
 800  
 801          /**
 802           * Checks if data with a particular handle is queued.
 803           *
 804           * @since 3.6.0
 805           *
 806           * @param {string} handle The handle for the data.
 807           *
 808           * @returns {boolean} True if the data is queued with this handle.
 809           */
 810  		function isQueued( handle ) {
 811              if ( handle ) {
 812                  return settings.queue.hasOwnProperty( handle );
 813              }
 814          }
 815  
 816          /**
 817           * Removes data with a particular handle from the queue.
 818           *
 819           * @since 3.7.0
 820           *
 821           * @memberOf wp.heartbeat.prototype
 822           *
 823           * @param {string} handle The handle for the data.
 824           *
 825           * @returns {void}
 826           */
 827  		function dequeue( handle ) {
 828              if ( handle ) {
 829                  delete settings.queue[handle];
 830              }
 831          }
 832  
 833          /**
 834           * Gets data that was enqueued with a particular handle.
 835           *
 836           * @since 3.7.0
 837           *
 838           * @memberOf wp.heartbeat.prototype
 839           *
 840           * @param {string} handle The handle for the data.
 841           *
 842           * @returns {*} The data or undefined.
 843           */
 844  		function getQueuedItem( handle ) {
 845              if ( handle ) {
 846                  return this.isQueued( handle ) ? settings.queue[handle] : undefined;
 847              }
 848          }
 849  
 850          initialize();
 851  
 852          // Expose public methods.
 853          return {
 854              hasFocus: hasFocus,
 855              connectNow: connectNow,
 856              disableSuspend: disableSuspend,
 857              interval: interval,
 858              hasConnectionError: hasConnectionError,
 859              enqueue: enqueue,
 860              dequeue: dequeue,
 861              isQueued: isQueued,
 862              getQueuedItem: getQueuedItem
 863          };
 864      };
 865  
 866      /**
 867       * Ensure the global `wp` object exists.
 868       *
 869       * @namespace wp
 870       */
 871      window.wp = window.wp || {};
 872  
 873      /**
 874       * Contains the Heartbeat API.
 875       *
 876       * @namespace wp.heartbeat
 877       * @type {Heartbeat}
 878       */
 879      window.wp.heartbeat = new Heartbeat();
 880  
 881  }( jQuery, window ));


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1