Changeset 23882
- Timestamp:
- 03/30/2013 11:32:12 PM (11 years ago)
- Location:
- trunk/wp-includes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/general-template.php
r23801 r23882 2334 2334 */ 2335 2335 function wp_heartbeat_settings( $settings ) { 2336 if ( ! is_admin() ) 2337 $settings['ajaxurl'] = admin_url( 'admin-ajax.php', 'relative' ); 2338 2336 2339 if ( is_user_logged_in() ) 2337 2340 $settings['nonce'] = wp_create_nonce( 'heartbeat-nonce' ); -
trunk/wp-includes/js/heartbeat.js
r23481 r23882 13 13 nonce, 14 14 screenid = typeof pagenow != 'undefined' ? pagenow : '', 15 url = typeof ajaxurl != 'undefined' ? ajaxurl : '', 15 16 settings, 16 17 tick = 0, 17 18 queue = {}, 18 19 interval, 19 lastconnect = 0,20 20 connecting, 21 countdown, 21 countdown = 0, 22 errorcount = 0, 22 23 tempInterval, 23 24 hasFocus = true, … … 27 28 frameBlurTimeout = -1; 28 29 29 this.url = typeof ajaxurl != 'undefined' ? ajaxurl : 'wp-admin/admin-ajax.php';30 30 this.autostart = true; 31 this.connectionLost = false; 31 32 32 33 if ( typeof( window.heartbeatSettings != 'undefined' ) ) { 33 settings = $.extend( {}, window.heartbeatSettings ); 34 window.heartbeatSettings = null; 34 settings = window.heartbeatSettings; 35 35 36 36 // Add private vars 37 37 nonce = settings.nonce || ''; 38 38 delete settings.nonce; 39 40 url = settings.ajaxurl || url; 41 delete settings.ajaxurl; 39 42 40 43 interval = settings.interval || 15; // default interval … … 48 51 interval = interval * 1000; 49 52 50 // todo: needed?51 53 // 'screenid' can be added from settings on the front-end where the JS global 'pagenow' is not set 52 54 screenid = screenid || settings.screenid || 'site'; … … 73 75 } 74 76 75 // Set error state and fire an event if errors persist for over 2 min when the window has focus 76 // or 6 min when the window is in the background 77 function errorstate() { 78 var since; 79 80 if ( lastconnect ) { 81 since = time() - lastconnect, duration = hasFocus ? 120000 : 360000; 82 83 if ( since > duration ) { 77 // Set error state and fire an event if XHR errors or timeout 78 function errorstate( error ) { 79 var trigger; 80 81 if ( error ) { 82 switch ( error ) { 83 case 'abort': 84 // do nothing 85 break; 86 case 'timeout': 87 // no response for 30 sec. 88 trigger = true; 89 break; 90 case 'parsererror': 91 case 'error': 92 case 'empty': 93 case 'unknown': 94 errorcount++; 95 96 if ( errorcount > 2 ) 97 trigger = true; 98 99 break; 100 } 101 102 if ( trigger && ! self.connectionLost ) { 84 103 self.connectionLost = true; 85 $(document).trigger( 'heartbeat-connection-lost', parseInt(since / 1000) ); 86 } else if ( self.connectionLost ) { 87 self.connectionLost = false; 88 $(document).trigger( 'heartbeat-connection-restored' ); 104 $(document).trigger( 'heartbeat-connection-lost' ); 89 105 } 106 } else if ( self.connectionLost ) { 107 errorcount = 0; 108 self.connectionLost = false; 109 $(document).trigger( 'heartbeat-connection-restored' ); 90 110 } 91 111 } … … 96 116 97 117 data.data = $.extend( {}, queue ); 118 // Clear the data queue, anything added after this point will be send on the next tick 119 queue = {}; 120 98 121 $(document).trigger( 'heartbeat-send', [data.data] ); 99 122 … … 105 128 106 129 connecting = true; 107 self.xhr = $.post( self.url, data, 'json' ) 108 .done( function( data, textStatus, jqXHR ) { 109 var interval; 110 111 // Clear the data queue 112 queue = {}; 130 self.xhr = $.ajax({ 131 url: url, 132 type: 'post', 133 timeout: 30000, // throw an error of not completed after 30 sec. 134 data: data, 135 dataType: 'json' 136 }).done( function( data, textStatus, jqXHR ) { 137 var new_interval, timed; 138 139 if ( ! data ) 140 return errorstate( 'empty' ); 113 141 114 142 // Clear error state 115 lastconnect = time();116 143 if ( self.connectionLost ) 117 144 errorstate(); 118 145 119 146 // Change the interval from PHP 120 interval = data.heartbeat_interval;147 new_interval = data.heartbeat_interval; 121 148 delete data.heartbeat_interval; 122 149 123 150 self.tick( data, textStatus, jqXHR ); 124 151 125 // do this last, can trigger the next XHR 126 if ( interval )127 self.interval. apply( self, data.heartbeat_interval );128 }).always( function() {152 // do this last, can trigger the next XHR if connection time > 5 sec. and new_interval == 'fast' 153 if ( new_interval ) 154 self.interval.call( self, new_interval ); 155 }).always( function() { 129 156 connecting = false; 130 157 next(); 131 }).fail( function( jqXHR, textStatus, error ) {132 errorstate( );158 }).fail( function( jqXHR, textStatus, error ) { 159 errorstate( textStatus || 'unknown' ); 133 160 self.error( jqXHR, textStatus, error ); 134 161 }); … … 143 170 if ( !hasFocus ) { 144 171 t = 120000; // 2 min 145 } else if ( countdown && tempInterval ) {172 } else if ( countdown > 0 && tempInterval ) { 146 173 t = tempInterval; 147 174 countdown--; … … 215 242 } 216 243 217 $(window).on('blur.wp-heartbeat-focus', function(e) {244 $(window).on('blur.wp-heartbeat-focus', function(e) { 218 245 setFrameEvents(); 219 246 winBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 ); 220 }).on('focus.wp-heartbeat-focus', function() {247 }).on('focus.wp-heartbeat-focus', function() { 221 248 $('iframe').each( function(i, frame){ 222 249 if ( !isLocalFrame(frame) ) … … 262 289 if ( !userActiveEvents ) { 263 290 $(document).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); }); 291 264 292 $('iframe').each( function(i, frame){ 265 293 if ( !isLocalFrame(frame) ) … … 268 296 $(frame.contentWindow).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); }); 269 297 }); 298 270 299 userActiveEvents = true; 271 300 } … … 284 313 } 285 314 286 this. winHasFocus = function() {315 this.hasFocus = function() { 287 316 return hasFocus; 288 317 } … … 291 320 * Get/Set the interval 292 321 * 293 * When setting the interval to 'fast', the number of ticks is specified wiht the second argument, default 30. 294 * If the window doesn't have focus, the interval is overridden to 2 min. In this case setting the 'ticks' 295 * will start counting after the window gets focus. 322 * When setting to 'fast', the interval is 5 sec. for the next 30 ticks (for 2 min and 30 sec). 323 * If the window doesn't have focus, the interval slows down to 2 min. 296 324 * 297 325 * @param string speed Interval speed: 'fast' (5sec), 'standard' (15sec) default, 'slow' (60sec) 298 * @param int ticks Number of ticks for the changed interval, optional when setting 'standard' or 'slow'299 326 * @return int Current interval in seconds 300 327 */ 301 this.interval = function( speed, ticks) {328 this.interval = function( speed ) { 302 329 var reset, seconds; 303 330 … … 306 333 case 'fast': 307 334 seconds = 5; 308 countdown = parseInt(ticks) ||30;335 countdown = 30; 309 336 break; 310 337 case 'slow': 311 338 seconds = 60; 312 countdown = parseInt(ticks) ||0;339 countdown = 0; 313 340 break; 314 341 case 'long-polling': … … 325 352 reset = seconds * 1000 < interval; 326 353 327 if ( countdown ) {354 if ( countdown > 0 ) { 328 355 tempInterval = seconds * 1000; 329 356 } else { … … 362 389 363 390 /** 364 * Send datawith the next XHR391 * Enqueue data to send with the next XHR 365 392 * 366 393 * As the data is sent later, this function doesn't return the XHR response. … … 372 399 * Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle. 373 400 * 374 * $param string handle Unique handle for the data. The handle is used in PHP to receive the data 375 * $param mixed data The data to be sent376 * $param bool overwrite Whether to overwrite existing data in the queue377 * $return bool Whether the data was queued or not 401 * $param string handle Unique handle for the data. The handle is used in PHP to receive the data. 402 * $param mixed data The data to send. 403 * $param bool dont_overwrite Whether to overwrite existing data in the queue. 404 * $return bool Whether the data was queued or not. 378 405 */ 379 this. send = function(handle, data, overwrite) {406 this.enqueue = function( handle, data, dont_overwrite ) { 380 407 if ( handle ) { 381 if ( queue.hasOwnProperty(handle) && !overwrite )408 if ( queue.hasOwnProperty(handle) && dont_overwrite ) 382 409 return false; 383 410 … … 394 421 * $return mixed The data queued with that handle or null 395 422 */ 396 this.isQueued = function( handle) {423 this.isQueued = function( handle ) { 397 424 return queue[handle]; 398 425 } … … 400 427 401 428 $.extend( Heartbeat.prototype, { 402 tick: function( data, textStatus, jqXHR) {429 tick: function( data, textStatus, jqXHR ) { 403 430 $(document).trigger( 'heartbeat-tick', [data, textStatus, jqXHR] ); 404 431 }, 405 error: function( jqXHR, textStatus, error) {432 error: function( jqXHR, textStatus, error ) { 406 433 $(document).trigger( 'heartbeat-error', [jqXHR, textStatus, error] ); 407 434 }
Note: See TracChangeset
for help on using the changeset viewer.