rn-blockmonitor
Version:
block monitor
1 lines • 1.45 kB
JavaScript
var BMJSEventLoopWatchdog={getStats:function getStats(){return{stallCount:stallCount,totalStallTime:totalStallTime,longestStall:longestStall,acceptableBusyTime:acceptableBusyTime};},reset:function reset(){console.log('JSEventLoopWatchdog: reset');totalStallTime=0;stallCount=0;longestStall=0;lastInterval=new Date().getTime();},stop:function stop(){stopFlag=true;},addHandler:function addHandler(handler){handlers.push(handler);},install:function install(_ref){var thresholdMS=_ref.thresholdMS;acceptableBusyTime=thresholdMS;if(installed){return;}installed=true;lastInterval=new Date().getTime();function iteration(){var now=new Date().getTime();var busyTime=now-lastInterval-thresholdMS/2;if(busyTime>=thresholdMS){var stallTime=busyTime-thresholdMS;stallCount++;totalStallTime+=stallTime;longestStall=Math.max(longestStall,stallTime);var msg='JSEventLoopWatchdog: JS thread busy for '+busyTime+'ms. '+(totalStallTime+'ms in '+stallCount+' stalls so far. ');handlers.forEach(function(handler){msg+=handler.onStall({lastInterval:lastInterval,busyTime:busyTime,now:now})||'';});console.log(''+msg);}handlers.forEach(function(handler){handler.onIterate&&handler.onIterate();});lastInterval=now;if(!stopFlag){setTimeout(iteration,thresholdMS/2);}}iteration();}};var acceptableBusyTime=0;var installed=false;var totalStallTime=0;var stallCount=0;var longestStall=0;var lastInterval=0;var stopFlag=false;var handlers=[];module.exports=BMJSEventLoopWatchdog;