-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyerf.min.js
9 lines (9 loc) · 4.42 KB
/
yerf.min.js
1
2
3
4
5
6
7
8
9
yerf=function(){var g=!1,h=!1,j=!1;window.performance&&("function"===typeof performance.now&&(g=!0),performance.timing&&(h=!0),"function"===typeof performance.getEntries&&(j=!0));var k=0;g&&(k=performance.now());var l=(new Date).getTime(),e={},f={},b={config:{},find:function(a){return e[a]},all:function(){return e},create:function(a){return new this.Sample(a)},start:function(a){return this.create(a).start()},on:function(a,c,b){if("string"!==typeof a)throw Error("You must specify a fullKey for yerf().on().");
if("string"!==typeof c)throw Error("You must specify an event for yerf().on().");if("function"!==typeof b)throw Error("You must specify a subscriber function for yerf().on().");var d=f[a];d||(d={},f[a]=d);a=d[c];a||(a=[],d[c]=a);a.push(b)},trigger:function(a,c,b){if("string"!==typeof a)throw Error("You must specify a fullKey for yerf().trigger().");if("string"!==typeof c)throw Error("You must specify an event for yerf().trigger().");(a=f[a])&&kivi._.each(a[c],function(a){a(b)})},hasNow:g,hasTiming:h,
hasEntries:j,unixEpoch:l,yerfEpoch:k,getTime:function(){return this.hasNow?performance.now():(new Date).getTime()-this.unixEpoch},unixToYerf:function(a){var c=0,c=this.hasNow?a-this.unixEpoch+this.yerfEpoch:a-this.unixEpoch;0>c&&(c=0);return c},getEntries:function(){var a=[];this.hasEntries&&(a=performance.getEntries());return a},clear:function(){e={};f={}},onError:function(a){kivi.log(a.message)}},d=function(a){if("string"!==typeof a)throw Error("You must specify a key for this Sample.");if(e[a])return b.onError(Error("Sample["+
a+"] already exists.")),e[a];this.key=a;this.waitingFor=this.children=this.parent=this.stoppedAt=this.startedAt=this.offset=this.delta=void 0;this.state="created";e[a]=this};d.prototype.fullChildKey=function(a){if(!a)throw Error("You must specify a childKey.");return this.key+"."+a};d.prototype.find=function(a){return b.find(this.fullChildKey(a))};d.prototype.on=function(a,c){if(!a)throw Error("You must specify an event for Sample.on().");b.on(this.key,a,c);return this};d.prototype.trigger=function(a){if(!a)throw Error("You must specify an event for Sample.trigger().");
b.trigger(this.key,a,this);return this};d.prototype.start=function(){if(0===arguments.length)if("created"===this.state)this.startedAt=b.getTime(),this.state="started",this.offset=this.startedAt,this.trigger("start");else b.onError(Error("Sample["+this.key+"] has already started."));else kivi._.each(arguments,function(a){(!this.waitingFor||!this.waitingFor[a])&&this.waterfall(a);var c=this.children[a];c||(c=new d(this.fullChildKey(a)));c.start()},this);return this};d.prototype.stop=function(){if(0===
arguments.length)if("created"===this.state)b.onError(Error("Sample["+this.key+"] has not been started."));else if("started"===this.state){if(this.waitingFor)return b.onError(Error("Sample["+this.key+"] is a waterfall and cannot be manually stopped.")),this;this._doStop()}else b.onError(Error("Sample["+this.key+"] has already stopped."));else kivi._.each(arguments,function(a){var c=this.children[a];if(!c)return b.onError(Error("Cannot stop a child["+a+"] that is not attached.")),this;c.stop()},this);
return this};d.prototype._doStop=function(a){this.stoppedAt=a||b.getTime();this.delta=this.stoppedAt-this.startedAt;this.state="stopped";"function"===typeof this.beforeReport&&this.beforeReport();this.parent||this._reportToKivi();this.trigger("stop")};d.prototype._reportToKivi=function(){kivi.set("yerf.delta."+this.key,Math.round(this.delta));kivi.set("yerf.offset."+this.key,Math.round(this.offset));kivi._.each(this.children,function(a){a._reportToKivi()})};d.prototype.waterfall=function(){var a=
this;if("created"===this.state)this.start();else if("started"!==this.state)return b.onError(Error("Sample["+this.key+"] has already stopped.")),this;0<arguments.length&&(this.waitingFor=this.waitingFor||{},this.children=this.children||{},kivi._.each(arguments,function(c){if("boolean"!==typeof this.waitingFor[c]){this.waitingFor[c]=!0;var d=this.fullChildKey(c),e=yerf(d);if(e&&"created"!==e.state)return b.onError(Error("Child["+d+"] has already started.")),this;b.on(d,"start",function(b){b.parent=
a;b.offset=b.startedAt-a.startedAt;a.children[c]=b});b.on(this.fullChildKey(c),"stop",function(b){a._checkStop(c,b.stoppedAt)})}},this));return this};d.prototype._checkStop=function(a,b){if(!this.waitingFor)return this;this.waitingFor[a]=!1;var d=!0;kivi._.each(this.waitingFor,function(a){!0===a&&(d=!1)});d&&this._doStop(b)};b.Sample=d;return function(a){return a?b.find(a):b}}();