Changeset 24268
- Timestamp:
- 05/15/2013 11:17:51 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/js/heartbeat.js
r24139 r24268 112 112 113 113 function connect() { 114 var data = {};114 var send = {}, data, i, empty = true; 115 115 tick = time(); 116 116 117 data .data= $.extend( {}, queue );117 data = $.extend( {}, queue ); 118 118 // Clear the data queue, anything added after this point will be send on the next tick 119 119 queue = {}; 120 120 121 $(document).trigger( 'heartbeat-send', [data.data] ); 122 123 data.interval = interval / 1000; 124 data._nonce = nonce; 125 data.action = 'heartbeat'; 126 data.screenid = screenid; 127 data.has_focus = hasFocus; 121 $(document).trigger( 'heartbeat-send', [data] ); 122 123 for ( i in data ) { 124 if ( data.hasOwnProperty( i ) ) { 125 empty = false; 126 break; 127 } 128 } 129 130 // If nothing to send (nothing is expecting a response), 131 // schedule the next tick and bail 132 if ( empty ) { 133 connecting = false; 134 next(); 135 return; 136 } 137 138 send.data = data; 139 send.interval = interval / 1000; 140 send._nonce = nonce; 141 send.action = 'heartbeat'; 142 send.screenid = screenid; 143 send.has_focus = hasFocus; 128 144 129 145 connecting = true; … … 132 148 type: 'post', 133 149 timeout: 30000, // throw an error of not completed after 30 sec. 134 data: data,150 data: send, 135 151 dataType: 'json' 136 }).done( function( data, textStatus, jqXHR ) {152 }).done( function( response, textStatus, jqXHR ) { 137 153 var new_interval, timed; 138 154 139 if ( ! data)155 if ( ! response ) 140 156 return errorstate( 'empty' ); 141 157 … … 145 161 146 162 // Change the interval from PHP 147 new_interval = data.heartbeat_interval; 148 delete data.heartbeat_interval; 149 150 self.tick( data, textStatus, jqXHR ); 163 if ( response.heartbeat_interval ) { 164 new_interval = response.heartbeat_interval; 165 delete response.heartbeat_interval; 166 } 167 168 self.tick( response, textStatus, jqXHR ); 151 169 152 170 // do this last, can trigger the next XHR if connection time > 5 sec. and new_interval == 'fast' … … 165 183 var delta = time() - tick, t = interval; 166 184 167 if ( ! running )185 if ( ! running ) 168 186 return; 169 187 170 if ( ! hasFocus ) {188 if ( ! hasFocus ) { 171 189 t = 120000; // 2 min 172 190 } else if ( countdown > 0 && tempInterval ) { … … 196 214 197 215 hasFocus = false; 198 199 // temp debug200 if ( self.debug )201 console.log('### blurred(), slow down...')202 216 } 203 217 … … 215 229 window.clearTimeout(beat); 216 230 217 if ( ! connecting )231 if ( ! connecting ) 218 232 next(); 219 220 // temp debug221 if ( self.debug )222 console.log('### focused(), speed up... ')223 233 } 224 234 225 235 function setFrameEvents() { 226 $('iframe').each( function( i, frame){227 if ( ! isLocalFrame(frame) )236 $('iframe').each( function( i, frame ){ 237 if ( ! isLocalFrame( frame ) ) 228 238 return; 229 239 230 if ( $.data( frame, 'wp-heartbeat-focus') )240 if ( $.data( frame, 'wp-heartbeat-focus' ) ) 231 241 return; 232 242 233 $.data( frame, 'wp-heartbeat-focus', 1);234 235 $( frame.contentWindow).on('focus.wp-heartbeat-focus', function(e){243 $.data( frame, 'wp-heartbeat-focus', 1 ); 244 245 $( frame.contentWindow ).on( 'focus.wp-heartbeat-focus', function(e) { 236 246 focused(); 237 }).on('blur.wp-heartbeat-focus', function(e) {247 }).on('blur.wp-heartbeat-focus', function(e) { 238 248 setFrameEvents(); 239 249 frameBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 ); … … 242 252 } 243 253 244 $(window).on( 'blur.wp-heartbeat-focus', function(e) {254 $(window).on( 'blur.wp-heartbeat-focus', function(e) { 245 255 setFrameEvents(); 246 256 winBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 ); 247 }).on( 'focus.wp-heartbeat-focus', function() {248 $('iframe').each( function( i, frame){249 if ( !isLocalFrame( frame) )257 }).on( 'focus.wp-heartbeat-focus', function() { 258 $('iframe').each( function( i, frame ) { 259 if ( !isLocalFrame( frame ) ) 250 260 return; 251 261 252 $.removeData( frame, 'wp-heartbeat-focus');253 $( frame.contentWindow).off('.wp-heartbeat-focus');262 $.removeData( frame, 'wp-heartbeat-focus' ); 263 $( frame.contentWindow ).off( '.wp-heartbeat-focus' ); 254 264 }); 255 265 … … 259 269 function userIsActive() { 260 270 userActiveEvents = false; 261 $(document).off( '.wp-heartbeat-active');262 $('iframe').each( function( i, frame){263 if ( ! isLocalFrame(frame) )271 $(document).off( '.wp-heartbeat-active' ); 272 $('iframe').each( function( i, frame ) { 273 if ( ! isLocalFrame( frame ) ) 264 274 return; 265 275 266 $( frame.contentWindow).off('.wp-heartbeat-active');276 $( frame.contentWindow ).off( '.wp-heartbeat-active' ); 267 277 }); 268 278 269 279 focused(); 270 271 // temp debug272 if ( self.debug )273 console.log( 'userIsActive()' );274 280 } 275 281 … … 279 285 var lastActive = isUserActive ? time() - isUserActive : 0; 280 286 281 // temp debug282 if ( self.debug )283 console.log( 'checkUserActive(), lastActive = %s seconds ago', parseInt(lastActive / 1000) || 'null' );284 285 287 // Throttle down when no mouse or keyboard activity for 5 min 286 288 if ( lastActive > 300000 && hasFocus ) 287 289 blurred(); 288 290 289 if ( ! userActiveEvents ) {290 $(document).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });291 292 $('iframe').each( function( i, frame){293 if ( ! isLocalFrame(frame) )291 if ( ! userActiveEvents ) { 292 $(document).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } ); 293 294 $('iframe').each( function( i, frame ) { 295 if ( ! isLocalFrame( frame ) ) 294 296 return; 295 297 296 $( frame.contentWindow).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });298 $( frame.contentWindow ).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } ); 297 299 }); 298 300 … … 305 307 306 308 if ( this.autostart ) { 307 $(document).ready( function() {309 $(document).ready( function() { 308 310 // Start one tick (15 sec) after DOM ready 309 311 running = true; … … 363 365 } 364 366 365 if ( ! hasFocus )367 if ( ! hasFocus ) 366 368 return 120; 367 369 … … 395 397 * As the data is sent later, this function doesn't return the XHR response. 396 398 * To see the response, use the custom jQuery event 'heartbeat-tick' on the document, example: 397 * $(document).on( 'heartbeat-tick.myname', function(data, textStatus, jqXHR) {399 * $(document).on( 'heartbeat-tick.myname', function( event, data, textStatus, jqXHR ) { 398 400 * // code 399 401 * }); 400 * If the same 'handle' is used more than once, the data is overwritten when the third argument is 'true'.402 * If the same 'handle' is used more than once, the data is not overwritten when the third argument is 'true'. 401 403 * Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle. 402 404 * … … 408 410 this.enqueue = function( handle, data, dont_overwrite ) { 409 411 if ( handle ) { 410 if ( queue.hasOwnProperty( handle) && dont_overwrite )412 if ( queue.hasOwnProperty( handle ) && dont_overwrite ) 411 413 return false; 412 414
Note: See TracChangeset
for help on using the changeset viewer.