@tracing/core
Version:
8 lines (7 loc) • 4.3 kB
JavaScript
/*!
* @tracing/core v0.0.3
* (c) 2024 wermdany <https://github.com/wermdany>
* license ISC
* hash f6fbb5bcb01c00afbd6fb6f5826e891202a721b1
*/
const n=window.performance;function t(n,t,e){let o=Object.assign({},t);return{name:n,get:()=>function(n){const t=Object.assign({},n);for(const e in t)"function"==typeof t[e]&&(t[e]=t[e].call());return t}(o),set(n,t){o[n]=t},getOrigin:()=>Object.assign({},o),remove(n){delete o[n]},clear(){o={}}}}function e(n,t){const{isLogger:e=!0}=t||{},r=()=>n?"[".concat("tracing","/",n,"]"):"[".concat("tracing","]");return{warn(...n){e&&console.warn(r(),...n)},info(...n){e&&console.info(r(),...n)},error(...n){e&&console.error(r(),...n)},throwError(t){throw t instanceof Error||(t=new Error(t),Object.defineProperty(t,"name",{value:"".concat(o("tracing"),o(n),"Error")})),t}}}function o(n){return n.charAt(0).toUpperCase()+n.slice(1)}function r(n,t,e,o){return new(e||(e=Promise))((function(r,i){function c(n){try{u(o.next(n))}catch(t){i(t)}}function s(n){try{u(o.throw(n))}catch(t){i(t)}}function u(n){var t;n.done?r(n.value):(t=n.value,t instanceof e?t:new e((function(n){n(t)}))).then(c,s)}u((o=o.apply(n,t||[])).next())}))}function i(n,t,e){const o=new Map,i=new Map(n.map((n=>[n,c(n,t)])));function u(n,t,o){return r(this,void 0,void 0,(function*(){const r=o[n],c="object"==typeof r?r.handler:r,s=i.get(o);return Promise.resolve().then((()=>{const n=c.apply(s,t);return n})).catch((n=>e.throwError(n)))}))}function a(n,t,o){const r=o[n],c="object"==typeof r?r.handler:r,s=i.get(o);try{return c.apply(s,t)}catch(u){return e.throwError(u)}}function l(n,t,e=f){const o=[],r=[],i=[];for(const c of t){const t=c[n];if(t)if("object"==typeof t){if(e(t.handler,n,c),"pre"==t.order){o.push(c);continue}if("post"==t.order){i.push(c);continue}r.push(c)}else e(t,n,c),r.push(c)}return o.concat(r,i)}function f(n,t,o){"function"!=typeof n&&e.throwError(`Error running plugin hook "${t}" for plugin "${o.name}", expected a function hook or an object with a "handler" function.`)}function h(t){return function(n,t,e){const o=n.get(t);if(o)return o;const r=e();return n.set(t,r),r}(o,t,(()=>l(t,n)))}return{runHook:u,runHookSync:a,getSortedValidatePlugins:l,destroyPluginDriver:function(){o.clear(),i.clear()},hookParallel(n,t){return r(this,void 0,void 0,(function*(){const e=[];for(const o of h(n))e.push(u(n,t,o));yield Promise.all(e)}))},hookSequentialSync(n,t){const e=[];for(const o of h(n)){const r=a(n,t,o);r&&"function"==typeof r&&e.push(r)}return e},hookBail(n,t){for(const e of h(n)){if(!1===a(n,t,e))return!1}return!0},hookChainMergeSync(n,t,e){let o=e;for(const r of h(n)){o=s(o,a(n,t,r))}return o}}}function c(n,t){return{logger:e(n.name,t),meta:{version:"0.0.3"}}}function s(n,t){const e=Object.assign({},n);for(const o in t){if(!Object.prototype.hasOwnProperty.call(t,o))continue;const n=t[o];if(null==n)continue;const r=e[o];null!=r?e[o]=Array.isArray(r)||Array.isArray(n)?[...u(null!=r?r:[]),...u(null!=n?n:[])]:a(r)&&a(n)?s(r,n):n:e[o]=n}return e}function u(n){return Array.isArray(n)?n:[n]}function a(n){return"[object Object]"===Object.prototype.toString.call(n)}!function(){if(n)Date.now(),n.now()}();const l={isLogger:!1,sendLog:!1,plugins:[]};class f{constructor(n){var t;this.initialized=!1,this.destroyed=!1,this.effects=[],this.config=(t=n,Object.assign(Object.assign({},l),t)),this.logger=e("core",this.config),this.pluginDriver=i(this.config.plugins.flat(),this.config,this.logger),this.pluginDriver.hookSequentialSync("setup",[this.config])}init(){this.initialized||(this.header=t("header",{}),this.initialized=!0,this.effects=this.pluginDriver.hookSequentialSync("init",[this]))}report(n,t,e){this.initialized&&(this.destroyed||this.build(n,t,e))}build(n,t,e){const o=this.pluginDriver.hookChainMergeSync("build",[n,t,e],{});this.send(n,o)}send(n,t){!1!==this.pluginDriver.hookBail("beforeSend",[n,t])&&this.pluginDriver.hookBail("send",[n,t])}destroy(){return r(this,void 0,void 0,(function*(){yield Promise.all(this.effects.map((n=>n()))),this.effects.length=0,yield this.pluginDriver.hookParallel("beforeDestroy",[this]),this.pluginDriver.hookSequentialSync("destroy",[this]),this.header.clear(),this.pluginDriver.destroyPluginDriver(),this.destroyed=!0}))}}const h="0.0.3";export{f as TracingCore,e as createLogger,t as createStore,h as version};