author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Heartbeat API |
|
3 |
* |
|
5 | 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. |
|
0 | 8 |
* |
5 | 9 |
* Available PHP filters (in ajax-actions.php): |
0 | 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 |
* |
|
5 | 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 |
* |
|
0 | 26 |
* @since 3.6.0 |
27 |
*/ |
|
28 |
||
5 | 29 |
( function( $, window, undefined ) { |
0 | 30 |
var Heartbeat = function() { |
5 | 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 |
}; |
|
0 | 90 |
|
91 |
/** |
|
5 | 92 |
* Set local vars and events, then start |
0 | 93 |
* |
5 | 94 |
* @access private |
95 |
* |
|
96 |
* @return void |
|
0 | 97 |
*/ |
5 | 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; |
|
0 | 122 |
|
5 | 123 |
if ( settings.mainInterval < 15 ) { |
124 |
settings.mainInterval = 15; |
|
125 |
} else if ( settings.mainInterval > 120 ) { |
|
126 |
settings.mainInterval = 120; |
|
127 |
} |
|
128 |
} |
|
0 | 129 |
|
5 | 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). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
// Once set at initialization, minimalInterval cannot be changed/overridden. |
5 | 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 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
// 'screenId' can be added from settings on the front end where the JS global 'pagenow' is not set |
5 | 145 |
if ( ! settings.screenId ) { |
146 |
settings.screenId = options.screenId || 'front'; |
|
147 |
} |
|
148 |
||
149 |
if ( options.suspension === 'disable' ) { |
|
150 |
settings.suspendEnabled = false; |
|
151 |
} |
|
152 |
} |
|
0 | 153 |
|
5 | 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 |
} |
|
0 | 179 |
|
5 | 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 |
} |
|
0 | 197 |
|
5 | 198 |
$(window).on( 'unload.wp-heartbeat', function() { |
199 |
// Don't connect any more |
|
200 |
settings.suspend = true; |
|
0 | 201 |
|
5 | 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 |
}); |
|
0 | 216 |
} |
217 |
||
5 | 218 |
/** |
219 |
* Return the current time according to the browser |
|
220 |
* |
|
221 |
* @access private |
|
222 |
* |
|
223 |
* @return int |
|
224 |
*/ |
|
225 |
function time() { |
|
0 | 226 |
return (new Date()).getTime(); |
227 |
} |
|
228 |
||
5 | 229 |
/** |
230 |
* Check if the iframe is from the same origin |
|
231 |
* |
|
232 |
* @access private |
|
233 |
* |
|
234 |
* @return bool |
|
235 |
*/ |
|
0 | 236 |
function isLocalFrame( frame ) { |
237 |
var origin, src = frame.src; |
|
238 |
||
5 | 239 |
// Need to compare strings as WebKit doesn't throw JS errors when iframes have different origin. |
240 |
// It throws uncatchable exceptions. |
|
0 | 241 |
if ( src && /^https?:\/\//.test( src ) ) { |
242 |
origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host; |
|
243 |
||
5 | 244 |
if ( src.indexOf( origin ) !== 0 ) { |
0 | 245 |
return false; |
5 | 246 |
} |
0 | 247 |
} |
248 |
||
249 |
try { |
|
5 | 250 |
if ( frame.contentWindow.document ) { |
0 | 251 |
return true; |
5 | 252 |
} |
0 | 253 |
} catch(e) {} |
254 |
||
255 |
return false; |
|
256 |
} |
|
257 |
||
5 | 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 ) { |
|
0 | 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; |
|
5 | 294 |
case 'error': |
295 |
if ( 503 === status && settings.hasConnected ) { |
|
296 |
trigger = true; |
|
297 |
break; |
|
298 |
} |
|
299 |
/* falls through */ |
|
0 | 300 |
case 'parsererror': |
301 |
case 'empty': |
|
302 |
case 'unknown': |
|
5 | 303 |
settings.errorcount++; |
0 | 304 |
|
5 | 305 |
if ( settings.errorcount > 2 && settings.hasConnected ) { |
0 | 306 |
trigger = true; |
5 | 307 |
} |
0 | 308 |
|
309 |
break; |
|
310 |
} |
|
311 |
||
5 | 312 |
if ( trigger && ! hasConnectionError() ) { |
313 |
settings.connectionError = true; |
|
314 |
$document.trigger( 'heartbeat-connection-lost', [error, status] ); |
|
0 | 315 |
} |
5 | 316 |
} |
317 |
} |
|
0 | 318 |
|
5 | 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' ); |
|
0 | 334 |
} |
335 |
} |
|
336 |
||
5 | 337 |
/** |
338 |
* Gather the data and connect to the server |
|
339 |
* |
|
340 |
* @access private |
|
341 |
* |
|
342 |
* @return void |
|
343 |
*/ |
|
0 | 344 |
function connect() { |
5 | 345 |
var ajaxData, heartbeatData; |
0 | 346 |
|
5 | 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 ) { |
|
0 | 350 |
return; |
351 |
} |
|
352 |
||
5 | 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 ] ); |
|
0 | 360 |
|
5 | 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 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
if ( 'customize' === settings.screenId ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
ajaxData.wp_customize = 'on'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
|
5 | 374 |
settings.connecting = true; |
375 |
settings.xhr = $.ajax({ |
|
376 |
url: settings.url, |
|
0 | 377 |
type: 'post', |
378 |
timeout: 30000, // throw an error if not completed after 30 sec. |
|
5 | 379 |
data: ajaxData, |
0 | 380 |
dataType: 'json' |
5 | 381 |
}).always( function() { |
382 |
settings.connecting = false; |
|
383 |
scheduleNextTick(); |
|
0 | 384 |
}).done( function( response, textStatus, jqXHR ) { |
5 | 385 |
var newInterval; |
0 | 386 |
|
5 | 387 |
if ( ! response ) { |
388 |
setErrorState( 'empty' ); |
|
389 |
return; |
|
390 |
} |
|
0 | 391 |
|
5 | 392 |
clearErrorState(); |
0 | 393 |
|
394 |
if ( response.nonces_expired ) { |
|
5 | 395 |
$document.trigger( 'heartbeat-nonces-expired' ); |
0 | 396 |
} |
397 |
||
398 |
// Change the interval from PHP |
|
399 |
if ( response.heartbeat_interval ) { |
|
5 | 400 |
newInterval = response.heartbeat_interval; |
0 | 401 |
delete response.heartbeat_interval; |
402 |
} |
|
403 |
||
5 | 404 |
$document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] ); |
0 | 405 |
|
5 | 406 |
// Do this last, can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast' |
407 |
if ( newInterval ) { |
|
408 |
interval( newInterval ); |
|
409 |
} |
|
0 | 410 |
}).fail( function( jqXHR, textStatus, error ) { |
5 | 411 |
setErrorState( textStatus || 'unknown', jqXHR.status ); |
412 |
$document.trigger( 'heartbeat-error', [jqXHR, textStatus, error] ); |
|
0 | 413 |
}); |
414 |
} |
|
415 |
||
5 | 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; |
|
0 | 428 |
|
5 | 429 |
if ( settings.suspend ) { |
430 |
return; |
|
0 | 431 |
} |
432 |
||
5 | 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 |
} |
|
0 | 443 |
|
5 | 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(); |
|
0 | 454 |
}, |
5 | 455 |
interval - delta |
0 | 456 |
); |
457 |
} else { |
|
458 |
connect(); |
|
459 |
} |
|
460 |
} |
|
461 |
||
5 | 462 |
/** |
463 |
* Set the internal state when the browser window becomes hidden or loses focus |
|
464 |
* |
|
465 |
* @access private |
|
466 |
* |
|
467 |
* @return void |
|
468 |
*/ |
|
0 | 469 |
function blurred() { |
5 | 470 |
settings.hasFocus = false; |
0 | 471 |
} |
472 |
||
5 | 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(); |
|
0 | 482 |
|
5 | 483 |
// Resume if suspended |
484 |
settings.suspend = false; |
|
0 | 485 |
|
5 | 486 |
if ( ! settings.hasFocus ) { |
487 |
settings.hasFocus = true; |
|
488 |
scheduleNextTick(); |
|
489 |
} |
|
0 | 490 |
} |
491 |
||
5 | 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' ); |
|
0 | 502 |
|
503 |
$('iframe').each( function( i, frame ) { |
|
5 | 504 |
if ( isLocalFrame( frame ) ) { |
505 |
$( frame.contentWindow ).off( '.wp-heartbeat-active' ); |
|
506 |
} |
|
0 | 507 |
}); |
508 |
||
509 |
focused(); |
|
510 |
} |
|
511 |
||
5 | 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; |
|
0 | 526 |
|
5 | 527 |
// Throttle down when no mouse or keyboard activity for 5 min. |
528 |
if ( lastActive > 300000 && settings.hasFocus ) { |
|
529 |
blurred(); |
|
530 |
} |
|
0 | 531 |
|
5 | 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 |
}); |
|
0 | 542 |
|
543 |
$('iframe').each( function( i, frame ) { |
|
5 | 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 |
} |
|
0 | 549 |
}); |
550 |
||
5 | 551 |
settings.userActivityEvents = true; |
0 | 552 |
} |
553 |
} |
|
554 |
||
5 | 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 |
} |
|
0 | 574 |
|
5 | 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 |
} |
|
0 | 600 |
|
601 |
/** |
|
602 |
* Get/Set the interval |
|
603 |
* |
|
5 | 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. |
|
0 | 606 |
* If the window doesn't have focus, the interval slows down to 2 min. |
607 |
* |
|
5 | 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 |
|
0 | 610 |
* @return int Current interval in seconds |
611 |
*/ |
|
5 | 612 |
function interval( speed, ticks ) { |
613 |
var newInterval, |
|
614 |
oldInterval = settings.tempInterval ? settings.tempInterval : settings.mainInterval; |
|
0 | 615 |
|
616 |
if ( speed ) { |
|
617 |
switch ( speed ) { |
|
618 |
case 'fast': |
|
5 | 619 |
case 5: |
620 |
newInterval = 5000; |
|
621 |
break; |
|
622 |
case 15: |
|
623 |
newInterval = 15000; |
|
0 | 624 |
break; |
5 | 625 |
case 30: |
626 |
newInterval = 30000; |
|
627 |
break; |
|
628 |
case 60: |
|
629 |
newInterval = 60000; |
|
630 |
break; |
|
631 |
case 120: |
|
632 |
newInterval = 120000; |
|
0 | 633 |
break; |
634 |
case 'long-polling': |
|
635 |
// Allow long polling, (experimental) |
|
5 | 636 |
settings.mainInterval = 0; |
0 | 637 |
return 0; |
638 |
default: |
|
5 | 639 |
newInterval = settings.originalInterval; |
640 |
} |
|
641 |
||
642 |
if ( settings.minimalInterval && newInterval < settings.minimalInterval ) { |
|
643 |
newInterval = settings.minimalInterval; |
|
0 | 644 |
} |
645 |
||
5 | 646 |
if ( 5000 === newInterval ) { |
647 |
ticks = parseInt( ticks, 10 ) || 30; |
|
648 |
ticks = ticks < 1 || ticks > 30 ? 30 : ticks; |
|
0 | 649 |
|
5 | 650 |
settings.countdown = ticks; |
651 |
settings.tempInterval = newInterval; |
|
0 | 652 |
} else { |
5 | 653 |
settings.countdown = 0; |
654 |
settings.tempInterval = 0; |
|
655 |
settings.mainInterval = newInterval; |
|
0 | 656 |
} |
657 |
||
5 | 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 |
} |
|
0 | 664 |
} |
665 |
||
5 | 666 |
return settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000; |
667 |
} |
|
0 | 668 |
|
669 |
/** |
|
670 |
* Enqueue data to send with the next XHR |
|
671 |
* |
|
5 | 672 |
* As the data is send asynchronously, this function doesn't return the XHR response. |
0 | 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. |
|
5 | 682 |
* $param bool noOverwrite Whether to overwrite existing data in the queue. |
0 | 683 |
* $return bool Whether the data was queued or not. |
684 |
*/ |
|
5 | 685 |
function enqueue( handle, data, noOverwrite ) { |
0 | 686 |
if ( handle ) { |
5 | 687 |
if ( noOverwrite && this.isQueued( handle ) ) { |
0 | 688 |
return false; |
5 | 689 |
} |
0 | 690 |
|
5 | 691 |
settings.queue[handle] = data; |
0 | 692 |
return true; |
693 |
} |
|
694 |
return false; |
|
5 | 695 |
} |
0 | 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 |
*/ |
|
5 | 703 |
function isQueued( handle ) { |
704 |
if ( handle ) { |
|
705 |
return settings.queue.hasOwnProperty( handle ); |
|
706 |
} |
|
707 |
} |
|
0 | 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 |
*/ |
|
5 | 715 |
function dequeue( handle ) { |
716 |
if ( handle ) { |
|
717 |
delete settings.queue[handle]; |
|
718 |
} |
|
719 |
} |
|
0 | 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 |
*/ |
|
5 | 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 |
|
0 | 746 |
}; |
747 |
}; |
|
748 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
749 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
* Ensure the global `wp` object exists. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
751 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
752 |
* @namespace wp |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
753 |
*/ |
5 | 754 |
window.wp = window.wp || {}; |
755 |
window.wp.heartbeat = new Heartbeat(); |
|
0 | 756 |
|
5 | 757 |
}( jQuery, window )); |