sob
Version:
Schedule on Browser through single rAF and rIC mechanism
2 lines • 2.41 kB
JavaScript
/*! (C) WebReflection Mit Style License */
var sob=function(e){"use strict";function b(){var e=O(),t=1e3/h.minFPS,n=!1,r=N(v,g);if(r){a(b),g=v.splice(0,r);while(g.length){x(g.shift()),n=O()-e>=t;if(h.isOverloaded=n)break}n&&h.debug&&console.warn("overloaded frame")}else p=!1,h.isOverloaded=p}function w(e){for(var t=this,n=[],r={id:{},fn:e,ar:n},i=1;i<arguments.length;i++)n[i-1]=arguments[i];return k(t,r)||(t.push(r),r.id)}function E(e){return function(t){return arguments[0]=function(){r?t.apply(null,arguments):i.indexOf(t)<0&&i.push(t,arguments)},e.apply(null,arguments)}}function S(e,t){var n=T(e,t),r=-1<n;return r&&e.splice(n,1),r}function x(e){e.fn.apply(null,e.ar)}function T(e,t){var n=e.length;while(n--&&e[n].id!==t);return n}function N(e,t){return t.length?e.unshift.apply(e,t):e.length}function C(e){var t=N(m,y),n=e.didTimeout;if(t){f(C,{timeout:h.maxIdle}),y=m.splice(0,n?1:t);while(y.length&&(n||e.timeRemaining()))x(y.shift())}else d=!1}function k(e,t){for(var n=0,r=e.length,i;n<r;n++){i=e[n];if(i.fn===t.fn&&A(i.ar,t.ar))return i.id}return null}function L(){r=!document[n];for(var e=i.length,t=i.splice(0,e),s=0;s<e;s+=2)t[s].apply(null,t[s+1])}function A(e,t){var n=e.length,r=t.length,i=n===r;if(i)while(n--)if(e[n]!==t[n])return!i;return i}function O(){return o.call(s)}var t,n,r=!0,i=[],s=e.performance||{now:Date.now},o=s.now||s.webkitNow||function(){return(new Date).getTime()},u=Math.max,a=e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||function(e){c(e,16)},f=e.requestIdleCallback||function(e,t){var n=1e3/h.minFPS,r=O(),i;c(function(){i=O(),c(function(){e({didTimeout:t.timeout<O()-r,timeRemaining:function(){return u(0,h.minFPS-(O()-i))}},1)})},n)},l=e.clearInterval,c=e.setTimeout,h={debug:!1,isOverloaded:!1,minFPS:60,maxIdle:2e3,clear:function(e){return typeof e=="number"?l(e):void (S(v,e)||S(m,e)||S(g,e)||S(y,e))},frame:function(){return p||(p=!0,a(b)),w.apply(v,arguments)},idle:function(){return d||(d=!0,f(C,{timeout:h.maxIdle})),w.apply(m,arguments)},interval:E(e.setInterval),timeout:E(c),now:o},p=!1,d=!1,v=[],m=[],g=[],y=[];h.raf=h.frame,h.ric=h.idle;switch(!0){case"hidden"in document:n="hidden",t="visibilitychange";break;case"msHidden"in document:n="msHidden",t="msvisibilitychange";break;case"webkitHidden"in document:n="webkitHidden",t="webkitvisibilitychange"}return n&&(document.addEventListener(t,L,!1),L()),h}(window);