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