WordPress/wp-includes/js/heartbeat.min.js
Andrew Ozz 188fce6c9b Heartbeat: clean up code style, better naming, better code structure. Props carldanley, props evansolomon.
Changes:
- Add connectNow() public method to trigger a connection immediately.
- Remove the "skipping" when no data to send.
- Change the default interval to 60 sec.
- Fix resetting the next connection time when changing the interval.
See #25073.
Built from https://develop.svn.wordpress.org/trunk@26169


git-svn-id: http://core.svn.wordpress.org/trunk@26078 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-11-14 18:41:10 +00:00

1 line
4.7 KiB
JavaScript

!function(a,b,c){var d=function(){function d(){if("string"==typeof b.pagenow&&(A.screenId=b.pagenow),"string"==typeof b.ajaxurl&&(A.url=b.ajaxurl),"object"==typeof b.heartbeatSettings){var c=b.heartbeatSettings;!A.url&&c.ajaxurl&&(A.url=c.ajaxurl),c.interval&&(A.mainInterval=c.interval,A.mainInterval<15?A.mainInterval=15:A.mainInterval>60&&(A.mainInterval=60)),A.screenId||(A.screenId=c.screenId||"front")}A.mainInterval=1e3*A.mainInterval,A.originalInterval=A.mainInterval,a(b).on("blur.wp-heartbeat-focus",function(){m(),A.winBlurTimer=b.setTimeout(function(){k()},500)}).on("focus.wp-heartbeat-focus",function(){n(),l()}).on("unload.wp-heartbeat",function(){A.isRunning=!1}),b.setInterval(function(){q()},3e4),z.ready(function(){A.lastTick=e(),j()})}function e(){return(new Date).getTime()}function f(a){var c,d=a.src;if(d&&/^https?:\/\//.test(d)&&(c=b.location.origin?b.location.origin:b.location.protocol+"//"+b.location.host,0!==d.indexOf(c)))return!1;try{if(a.contentWindow.document)return!0}catch(e){}return!1}function g(b,c){var d;if(b){switch(b){case"abort":break;case"timeout":d=!0;break;case"error":if(503===c&&A.hasConnected){d=!0;break}case"parsererror":case"empty":case"unknown":A.errorcount++,A.errorcount>2&&A.hasConnected&&(d=!0)}d&&!s()&&(A.connectionError=!0,a(document).trigger("heartbeat-connection-lost",[b,c]))}}function h(){A.hasConnected=!0,s()&&(A.errorcount=0,A.connectionError=!1,z.trigger("heartbeat-connection-restored"))}function i(){var c,d;A.connecting||(A.lastTick=e(),d=a.extend({},A.queue),A.queue={},z.trigger("heartbeat-send",[d]),c={data:d,interval:A.tempInterval?A.tempInterval/1e3:A.mainInterval/1e3,_nonce:"object"==typeof b.heartbeatSettings?b.heartbeatSettings.nonce:"",action:"heartbeat",screen_id:A.screenId,has_focus:A.hasFocus},A.connecting=!0,A.xhr=a.ajax({url:A.url,type:"post",timeout:3e4,data:c,dataType:"json"}).always(function(){A.connecting=!1,j()}).done(function(a,b,c){var d;return a?(h(),a.nonces_expired?(z.trigger("heartbeat-nonces-expired"),void 0):(a.heartbeat_interval&&(d=a.heartbeat_interval,delete a.heartbeat_interval),z.trigger("heartbeat-tick",[a,b,c]),d&&u(d),void 0)):(g("empty"),void 0)}).fail(function(a,b,c){g(b||"unknown",a.status),z.trigger("heartbeat-error",[a,b,c])}))}function j(){var a=e()-A.lastTick,c=A.mainInterval;A.isRunning&&(A.hasFocus?A.countdown>0&&A.tempInterval&&(c=A.tempInterval,A.countdown--,A.countdown<1&&(A.tempInterval=0)):c=12e4,b.clearTimeout(A.beatTimer),c>a?A.beatTimer=b.setTimeout(function(){i()},c-a):i())}function k(){o(),A.hasFocus=!1}function l(){o(),A.userActivity=e(),A.hasFocus||(A.hasFocus=!0,j())}function m(){a("iframe").each(function(c,d){f(d)&&(a.data(d,"wp-heartbeat-focus")||(a.data(d,"wp-heartbeat-focus",1),a(d.contentWindow).on("focus.wp-heartbeat-focus",function(){l()}).on("blur.wp-heartbeat-focus",function(){m(),A.frameBlurTimer=b.setTimeout(function(){k()},500)})))})}function n(){a("iframe").each(function(b,c){f(c)&&(a.removeData(c,"wp-heartbeat-focus"),a(c.contentWindow).off(".wp-heartbeat-focus"))})}function o(){b.clearTimeout(A.winBlurTimer),b.clearTimeout(A.frameBlurTimer)}function p(){A.userActivityEvents=!1,z.off(".wp-heartbeat-active"),a("iframe").each(function(b,c){f(c)&&a(c.contentWindow).off(".wp-heartbeat-active")}),l()}function q(){var b=A.userActivity?e()-A.userActivity:0;b>3e5&&A.hasFocus&&k(),A.userActivityEvents||(z.on("mouseover.wp-heartbeat-active keyup.wp-heartbeat-active",function(){p()}),a("iframe").each(function(b,c){f(c)&&a(c.contentWindow).on("mouseover.wp-heartbeat-active keyup.wp-heartbeat-active",function(){p()})}),A.userActivityEvents=!0)}function r(){return A.hasFocus}function s(){return A.connectionError}function t(){A.lastTick=0,j()}function u(a,b){var c,d=A.tempInterval?A.tempInterval:A.mainInterval;if(a){switch(a){case"fast":case 5:c=5e3;break;case 15:c=15e3;break;case 30:c=3e4;break;case 60:c=6e4;break;case"long-polling":return A.mainInterval=0,0;default:c=A.originalInterval}5e3===c?(b=parseInt(b,10)||30,b=1>b||b>30?30:b,A.countdown=b,A.tempInterval=c):(A.countdown=0,A.tempInterval=0,A.mainInterval=c),c!==d&&j()}return A.tempInterval?A.tempInterval/1e3:A.mainInterval/1e3}function v(a,b,c){return a?c&&this.isQueued(a)?!1:(A.queue[a]=b,!0):!1}function w(a){return a?A.queue.hasOwnProperty(a):void 0}function x(a){a&&delete A.queue[a]}function y(a){return a?this.isQueued(a)?A.queue[a]:c:void 0}var z=a(document),A={isRunning:!0,screenId:"",url:"",lastTick:0,queue:{},mainInterval:60,tempInterval:0,originalInterval:0,countdown:0,connecting:!1,connectionError:!1,errorcount:0,hasConnected:!1,hasFocus:!0,userActivity:0,userActivityEvents:!1,beatTimer:0,winBlurTimer:0,frameBlurTimer:0};return d(),{hasFocus:r,connectNow:t,setInterval:u,hasConnectionError:s,enqueue:v,dequeue:x,isQueued:w,getQueuedItem:y}};b.wp=b.wp||{},b.wp.heartbeat=new d}(jQuery,window);