UNPKG

caught-core

Version:
17 lines (14 loc) 8.35 kB
var t={};var r="__proto__"in{},e=function(t){return"boolean"==typeof t},n=function(t){return"object"===function(t){return Object.prototype.toString.call(t).slice(8,-1)}(t)},o=function(t){return"function"==typeof t},i=function(t){return Array.isArray(t)},a=function(t){return"function"==typeof t&&/native code/.test(t.toString())},c=function(t){var r=[];for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&r.push(t[e]);return r};function u(t,r){Object.defineProperties(t,{length:{writable:r},name:{writable:r}})}function s(t){var e=0,i=t.scheduler;return function(t,c){if(!o(t))return t;var s,f=n(c);if(f){var l=c.others;if(l)(s||(s={})).proxyCaught={others:l}}var h=e++,p=i.createCustomInsert("proxyError",f?c.flag:c,h),y=function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];try{return t.apply(this,r)}catch(a){if(n(c)&&c.withArgs){var o=s||(s={});(o.proxyCaught||(o.proxyCaught={})).args=r}var i={error:a,meta:s};throw p(i),a}};return function(t,e){if(t.prototype=e.prototype,r)t.__proto__=e.__proto__;else{var n=Object.getPrototypeOf(e);Object.getOwnPropertyNames(n).forEach((function(r){Object.defineProperty(t,r,{get:function(){return n[r]}})}))}}(y,t),function(t,r){function e(e){e.forEach((function(e){t[e]=r[e]}))}u(t,!0),e(Object.getOwnPropertyNames(r)),u(t,!1);try{a(Symbol)&&e(Object.getOwnPropertySymbols(r))}catch(n){}}(y,t),y}} /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var f=function(t,r){return f=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])},f(t,r)};function l(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function e(){this.constructor=t}f(t,r),t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)}var h=function(){return h=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var o in r=arguments[e])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t},h.apply(this,arguments)};function p(t,r,e){if(e||2===arguments.length)for(var n,o=0,i=r.length;o<i;o++)!n&&o in r||(n||(n=Array.prototype.slice.call(r,0,o)),n[o]=r[o]);return t.concat(n||Array.prototype.slice.call(r))}function y(t,r,e,n){var i;try{i=t.hooksMap[r];for(var a=0;a<i.length;a++){var c=i[a];if(o(c)){var u=c.apply(null,e);o(n)&&n(u)}}}catch(s){}}function g(t,r){var e,n=t.meta;null!==(e=r)&&"object"==typeof e&&(t.meta=n?h(h({},n),r):r)}var v,O=!0,m=[],d=function(){try{O=!0,m.forEach((function(t){return o(t)&&t()}))}catch(t){}finally{m.length=0}};!function(t){function r(){return null!==t&&t.apply(this,arguments)||this}l(r,t)}(Array),function(t){t.ADDHOOK="addInfo",t.SUCCESSHOOK="success",t.FAILHOOK="fail",t.SCHEDULABLEHOOK="schedulable",t.JSERRORHOOK="jsError",t.STATICERRORHOOK="staticError",t.PROMISERECTIONHOOK="promiseRejection"}(v||(v={})),function(t){function r(){return null!==t&&t.apply(this,arguments)||this}l(r,t)}(Array);var E=function(){function r(t){this.caught=t,this.queueIndex=-1,this.cachedFailMap=Object.create(null),this.failCount=0,this.tickStart=!1,this.stop=!1}return r.prototype.pendingInsertInfo=function(t,r){var e=this;if(!this.stop){y(this.caught,v.ADDHOOK,[t],(function(r){g(t,r)}));var n=this.createPendingQueue();if(!r){var o=this.getSameIndex(n,t);if(o>-1)this.combineInfo(n[o],t);else t.count=1,n.push(t)}this.tickStart||(this.tickStart=!0,function(t,r){if(m.push(t),r)return d();O&&(O=!1,a(Promise)?Promise.resolve().then(d):setTimeout(d))}((function(){try{if(e.tickStart=!1,e.queueCache=null,n.length){var t=e.createNotify(n,e.queueIndex);(0,e.caught.config.errorHandler)(n,t)}}catch(r){}}),this.caught.config.sync))}},r.prototype.createCustomInsert=function(r,e,n){e=function(r,e,n){r=String(r),e=String(e),n=n?String(n):"";var o="".concat(r,"-").concat(e),i=t[o];return i=i?i++:0,t[e]=i,"".concat(o,"-").concat(i).concat(n&&"-"+n)}(r,e,n);var o=this;return function(t){var n=h(h({},t),{type:r,flag:e,time:+new Date});o.pendingInsertInfo(n)}},r.prototype.retryHandler=function(){Object.keys(this.cachedFailMap).length&&this.pendingInsertInfo({flag:"",type:"",error:[],meta:[]},!0)},r.prototype.getSameIndex=function(t,r){return t.findIndex((function(t){return t.flag===r.flag}))},r.prototype.combineInfo=function(t,r){var e=t.count;e>=this.caught.config.sameLimit||(t.count=++e,t.error=I(t.error,r.error),t.meta=I(t.meta,r.meta))},r.prototype.createInitQueue=function(){var t=[];for(var r in this.cachedFailMap){for(var e=this.cachedFailMap[r],n=0;n<e.length;n++){var o=e[n],i=this.getSameIndex(t,o);if(i>-1)this.combineInfo(t[i],o);else t.push(o)}delete this.cachedFailMap[r]}return t},r.prototype.createPendingQueue=function(){if(this.tickStart)return this.queueCache||[];var t=p([],this.createInitQueue(),!0);return++this.queueIndex,this.queueCache=t},r.prototype.createNotify=function(t,r){return function(e){(e=!!e)?y(this.caught,v.SUCCESSHOOK,[t,r]):(this.cachedFailMap[r]=t,this.failCount++,this.failCount>=this.caught.config.failMaxNum&&(this.stop=!0,this.cachedFailMap=Object.create(null)),y(this.caught,v.FAILHOOK,[t,r]))}.bind(this)},r}();function I(t,r){if(!t)return r;if(!r)return t;var e=i(r);return i(t)?e?p(p([],t,!0),r,!0):t.push(r):e?r.unshift(t):[t,r]}function b(t){var r,n=e(r=t.config.listeners)?{jsError:r,staticError:r,promiseRection:r}:r,o=n.staticError,i=n.promiseRection;n.jsError&&function(t){var r=t.scheduler.createCustomInsert("jsError","emergency");window.addEventListener("error",(function(e){var n={error:e.error,message:e.message,filename:e.filename,lineno:e.lineno,colno:e.colno};y(t,v.JSERRORHOOK,[n,e],(function(t){g(n,t)})),r(n)}))}(t),o&&function(t){var r=t.scheduler.createCustomInsert("staticError","staticResource");window.addEventListener("error",(function(e){var n=e.target;if(n!==window){var o=n.href||n.src,i={error:"url: ".concat(o," is fail to load"),meta:{targetName:n.localName,resourceUrl:o}};y(t,v.STATICERRORHOOK,[i,e],(function(t){g(i,t)})),r(i)}}),!0)}(t),i&&function(t){var r=t.scheduler.createCustomInsert("promiseRection","uncaughtPromise");window.addEventListener("unhandledrejection",(function(e){var n={error:e.reason&&e.reason.msg||e.reason||""};y(t,v.PROMISERECTIONHOOK,[n,e],(function(t){g(n,t)})),r(n)}))}(t)}function C(t,r){return!("listeners"in r)||(n(t)?{jsError:S(t,"jsError",!0),staticError:S(t,"staticError",!0),promiseRection:S(t,"promiseRection",!0)}:e(t)?t:!!t)}function S(t,r,n){var o=t[r];return r in t?e(o)?o:!!o:n}function j(t,r){if(function(t){return!!o(t.errorHandler)}(r)){var e=function(t){var r=t.retryTime,e=t.failMaxNum,n=t.listeners,o=t.errorHandler,a=t.plugins,c=void 0===a?[]:a;return{sameLimit:Number(t.sameLimit)||10,retry:S(t,"retry",!0),retryTime:Number(r)||5e3,failMaxNum:Number(e)||5,listeners:C(n,t),errorHandler:o,plugins:i(c)?c:[],sync:S(t,"retry",!1)}}(r);return t.config=h({},e)}}function w(t){var r=t.hooksMap=function(){var t=Object.create(null);return c(v).forEach((function(r){t[r]=[]})),t}(),e=t.config.plugins;if(i(e)){var n=function(t){var r=Object.create(null);return c(v).forEach((function(e){r[e]=function(r,n){var o=t[e],i=o.pluginNameList||(o.pluginNameList=[]);-1===i.indexOf(r)&&(o.push(n),i.push(r))}})),r}(r);e.forEach((function(t){t.apply(n)}))}}function R(t){var r=Object.create(null);if(j(r,t),r.config)return w(r),function(t){var r=t.scheduler=new E(t),e=t.createCustomInsert=r.createCustomInsert.bind(r);y(t,v.SCHEDULABLEHOOK,[e])}(r),b(r),function(t){t.proxyCaught=s(t)}(r),{createCustomInsert:r.createCustomInsert,proxyCaught:r.proxyCaught}}export{R as default};