@adapt-arch/utiliti-es 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/bundle/utiliti-es.cjs +1 -1
- package/dist/bundle/utiliti-es.iife.js +1 -1
- package/dist/bundle/utiliti-es.js +258 -172
- package/dist/bundle/utiliti-es.umd.cjs +1 -1
- package/dist/pubsub/index.d.ts +2 -1
- package/dist/pubsub/index.js +2 -1
- package/dist/pubsub/plugins/broadcastChannelPlugin.d.ts +29 -0
- package/dist/pubsub/plugins/broadcastChannelPlugin.js +55 -0
- package/dist/pubsub/plugins/index.d.ts +2 -0
- package/dist/pubsub/plugins/index.js +2 -0
- package/dist/pubsub/plugins/loggerPlugin.d.ts +18 -0
- package/dist/pubsub/plugins/loggerPlugin.js +25 -0
- package/dist/pubsub/pubsub.d.ts +45 -0
- package/dist/pubsub/pubsub.js +45 -5
- package/package.json +14 -11
- package/dist/bundle/mockServiceWorker.js +0 -284
- package/dist/mocks/logReporterHandlers.d.ts +0 -1
- package/dist/mocks/logReporterHandlers.js +0 -27
package/README.md
CHANGED
|
@@ -4,3 +4,4 @@ Ecma Script common utilities.
|
|
|
4
4
|
## Status
|
|
5
5
|
[](https://sonarcloud.io/dashboard?id=adaptive-architecture_utiliti-es)
|
|
6
6
|
[](https://sonarcloud.io/summary/new_code?id=adaptive-architecture_utiliti-es)
|
|
7
|
+
[](https://www.npmjs.com/package/@adapt-arch/utiliti-es)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var h=Object.defineProperty;var g=(i,e,s)=>e in i?h(i,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[e]=s;var o=(i,e,s)=>(g(i,typeof e!="symbol"?e+"":e,s),s);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class p{constructor(e){o(this,"_subscriptions",new Map);o(this,"_options");var s;if(this._options=e,(s=this._options)!=null&&s.plugins)for(const t of this._options.plugins)t.init&&t.init(this)}publish(e,s){var n;const t={topic:e,message:s};if((n=this._options)!=null&&n.plugins)for(const l of this._options.plugins)l.onPublish&&l.onPublish(t);if(!t.topic)throw new Error("Invalid topic.");if(!t.message)throw new Error("Invalid message.");const r=this._subscriptions.get(t.topic);if(r)for(const l of r.values()){const c=structuredClone(t.topic),u=structuredClone(t.message);setTimeout(()=>l(c,u),0)}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let t=this._subscriptions.get(e);t||(t=new Map,this._subscriptions.set(structuredClone(e),t));const r=`sub-${Date.now()}`;return t.set(r,s),r}unsubscribe(e){if(e){for(const s of this._subscriptions.values())if(s.delete(e))return}}[Symbol.dispose](){var e;if(this._subscriptions.clear(),(e=this._options)!=null&&e.plugins)for(const s of this._options.plugins){const t=s[Symbol.dispose];t&&t.call(s)}return Promise.resolve()}}class _{constructor(e){o(this,"_options");o(this,"_channel");o(this,"_eventListeners",null);o(this,"_broadcastEnabled",!0);this._options=e,this._channel=new BroadcastChannel(this._options.channelName)}init(e){this._eventListeners||(this._eventListeners=s=>{const t=s.data;if(!(!t.topic||!t.message))try{this._broadcastEnabled=!1,e.publish(t.topic,t.message)}finally{this._broadcastEnabled=!0}},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!this._broadcastEnabled||!e.topic||!e.message)return;const s={topic:e.topic,message:e.message};this._channel.postMessage(s)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class m{constructor(e,s){o(this,"_values");o(this,"_overrideExisting");this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const t in this._values)s.indexOf(t)!==-1&&!this._overrideExisting||(e.extraParams[t]=this._values[t])}}class f{constructor(e,s){o(this,"_valuesFn");o(this,"_overrideExisting");this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const t=Object.keys(e.extraParams);for(const r in s)t.indexOf(r)!==-1&&!this._overrideExisting||(e.extraParams[r]=s[r])}}exports.LogLevel=void 0;(function(i){i[i.Trace=0]="Trace",i[i.Debug=1]="Debug",i[i.Information=2]="Information",i[i.Warning=3]="Warning",i[i.Error=4]="Error",i[i.Critical=5]="Critical",i[i.None=6]="None"})(exports.LogLevel||(exports.LogLevel={}));class a{constructor(){o(this,"timestamp",new Date().getTime());o(this,"level",exports.LogLevel.None);o(this,"name","");o(this,"message","");o(this,"errorMessage");o(this,"stackTrace");o(this,"extraParams")}}class d{constructor(e){o(this,"_console");this._console=e}register(e){let s;if(this._console)switch(e.level){case exports.LogLevel.Trace:s=this._console.trace||this._console.log;break;case exports.LogLevel.Debug:s=this._console.debug||this._console.log;break;case exports.LogLevel.Information:s=this._console.info||this._console.log;break;case exports.LogLevel.Warning:s=this._console.warn||this._console.log;break;case exports.LogLevel.Error:s=this._console.error||this._console.log;break;case exports.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class v{constructor(){o(this,"endpoint","");o(this,"verb","POST");o(this,"batchSize",20);o(this,"interval",2e3);o(this,"requestTransform")}}class b{constructor(e){o(this,"_messageQueue");o(this,"_options");o(this,"_reportActionTimeoutRef");o(this,"_reportActionPromise");o(this,"_disposed");if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){if(this._disposed)return Promise.resolve();await(this._reportActionPromise??this._processMessages()),this._disposed=!0}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const t=()=>{s(!1)},r=new XMLHttpRequest;r.open(this._options.verb,this._options.endpoint),r.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(r),r.onload=function(){s(this.status>=200&&this.status<300)},r.onerror=t,r.onabort=t,r.send(JSON.stringify(e))})}}class L{constructor(){o(this,"_messages",[])}get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class y{constructor(e){o(this,"_reporters");this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=new Array;for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class P{constructor(e){o(this,"_options");this._options=e}logMessageCore(e){var s;e.name=this._options.name;for(const t of this._options.enrichers)t.enrich(e);(s=this._options.reporter)==null||s.register(e)}async[Symbol.asyncDispose](){var e;await((e=this._options.reporter)==null?void 0:e[Symbol.asyncDispose]())}isEnabled(e){return e!==exports.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new a;s.level=exports.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new a;s.level=exports.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new a;s.level=exports.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new a;s.level=exports.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new a;s.level=exports.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new a;s.level=exports.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,t,r){const n=new a;n.level=e,n.message=s,n.errorMessage=t==null?void 0:t.message,n.stackTrace=t==null?void 0:t.stack,n.extraParams=r,this.logMessage(n)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}}class w{constructor(){o(this,"name","");o(this,"reporter",null);o(this,"minimumLevel",exports.LogLevel.Warning);o(this,"enrichers",[])}static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return exports.LogLevel.Trace;case"DEBUG":return exports.LogLevel.Debug;case"INFORMATION":return exports.LogLevel.Information;case"WARNING":return exports.LogLevel.Warning;case"ERROR":return exports.LogLevel.Error;case"CRITICAL":return exports.LogLevel.Critical;case"NONE":return exports.LogLevel.None;default:return exports.LogLevel.None}}}class E{constructor(e,s=exports.LogLevel.Information){o(this,"_logger");o(this,"_logLevel");this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}function M(i=1,e){return new Promise((s,t)=>{setTimeout(()=>{e?t(e):s()},i)})}exports.BroadcastChannelPlugin=_;exports.ConsoleReporter=d;exports.DynamicValuesEnricher=f;exports.InMemoryReporter=L;exports.LogMessage=a;exports.Logger=P;exports.LoggerOptions=w;exports.LoggerPlugin=E;exports.MultipleReporter=y;exports.PubSubHub=p;exports.ValuesEnricher=m;exports.XhrReporter=b;exports.XhrReporterOptions=v;exports.delay=M;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var adaptArch_utilitiEs=function(t){"use strict";var
|
|
1
|
+
var adaptArch_utilitiEs=function(t){"use strict";var M=Object.defineProperty;var T=(t,a,u)=>a in t?M(t,a,{enumerable:!0,configurable:!0,writable:!0,value:u}):t[a]=u;var n=(t,a,u)=>(T(t,typeof a!="symbol"?a+"":a,u),u);class a{constructor(e){n(this,"_subscriptions",new Map);n(this,"_options");var s;if(this._options=e,(s=this._options)!=null&&s.plugins)for(const i of this._options.plugins)i.init&&i.init(this)}publish(e,s){var l;const i={topic:e,message:s};if((l=this._options)!=null&&l.plugins)for(const h of this._options.plugins)h.onPublish&&h.onPublish(i);if(!i.topic)throw new Error("Invalid topic.");if(!i.message)throw new Error("Invalid message.");const r=this._subscriptions.get(i.topic);if(r)for(const h of r.values()){const w=structuredClone(i.topic),E=structuredClone(i.message);setTimeout(()=>h(w,E),0)}}subscribe(e,s){if(!e)throw new Error("Invalid topic.");if(!s)throw new Error("Invalid handler.");let i=this._subscriptions.get(e);i||(i=new Map,this._subscriptions.set(structuredClone(e),i));const r=`sub-${Date.now()}`;return i.set(r,s),r}unsubscribe(e){if(e){for(const s of this._subscriptions.values())if(s.delete(e))return}}[Symbol.dispose](){var e;if(this._subscriptions.clear(),(e=this._options)!=null&&e.plugins)for(const s of this._options.plugins){const i=s[Symbol.dispose];i&&i.call(s)}return Promise.resolve()}}class u{constructor(e){n(this,"_options");n(this,"_channel");n(this,"_eventListeners",null);n(this,"_broadcastEnabled",!0);this._options=e,this._channel=new BroadcastChannel(this._options.channelName)}init(e){this._eventListeners||(this._eventListeners=s=>{const i=s.data;if(!(!i.topic||!i.message))try{this._broadcastEnabled=!1,e.publish(i.topic,i.message)}finally{this._broadcastEnabled=!0}},this._channel.addEventListener("message",this._eventListeners))}onPublish(e){if(!this._broadcastEnabled||!e.topic||!e.message)return;const s={topic:e.topic,message:e.message};this._channel.postMessage(s)}[Symbol.dispose](){this._eventListeners&&this._channel.removeEventListener("message",this._eventListeners),this._channel.close()}}class g{constructor(e,s){n(this,"_values");n(this,"_overrideExisting");this._values=e,this._overrideExisting=s}enrich(e){if(!this._values)return;e.extraParams=e.extraParams||{};const s=Object.keys(e.extraParams);for(const i in this._values)s.indexOf(i)!==-1&&!this._overrideExisting||(e.extraParams[i]=this._values[i])}}class _{constructor(e,s){n(this,"_valuesFn");n(this,"_overrideExisting");this._valuesFn=e,this._overrideExisting=s}enrich(e){const s=typeof this._valuesFn=="function"?this._valuesFn():void 0;if(!s)return;e.extraParams=e.extraParams||{};const i=Object.keys(e.extraParams);for(const r in s)i.indexOf(r)!==-1&&!this._overrideExisting||(e.extraParams[r]=s[r])}}t.LogLevel=void 0,function(o){o[o.Trace=0]="Trace",o[o.Debug=1]="Debug",o[o.Information=2]="Information",o[o.Warning=3]="Warning",o[o.Error=4]="Error",o[o.Critical=5]="Critical",o[o.None=6]="None"}(t.LogLevel||(t.LogLevel={}));class c{constructor(){n(this,"timestamp",new Date().getTime());n(this,"level",t.LogLevel.None);n(this,"name","");n(this,"message","");n(this,"errorMessage");n(this,"stackTrace");n(this,"extraParams")}}class m{constructor(e){n(this,"_console");this._console=e}register(e){let s;if(this._console)switch(e.level){case t.LogLevel.Trace:s=this._console.trace||this._console.log;break;case t.LogLevel.Debug:s=this._console.debug||this._console.log;break;case t.LogLevel.Information:s=this._console.info||this._console.log;break;case t.LogLevel.Warning:s=this._console.warn||this._console.log;break;case t.LogLevel.Error:s=this._console.error||this._console.log;break;case t.LogLevel.Critical:s=this._console.error||this._console.log;break;default:s=null;break}typeof s=="function"&&s.call(this._console,e.message,e)}[Symbol.asyncDispose](){return Promise.resolve()}}class f{constructor(){n(this,"endpoint","");n(this,"verb","POST");n(this,"batchSize",20);n(this,"interval",2e3);n(this,"requestTransform")}}class p{constructor(e){n(this,"_messageQueue");n(this,"_options");n(this,"_reportActionTimeoutRef");n(this,"_reportActionPromise");n(this,"_disposed");if(!e)throw new Error('Argument "options" is required');this._messageQueue=[],this._options=e,this._reportActionTimeoutRef=void 0,this._reportActionPromise=null,this._disposed=!1}register(e){this._disposed||(this._messageQueue.push(e),this._scheduleNextProcessAction())}async[Symbol.asyncDispose](){if(this._disposed)return Promise.resolve();await(this._reportActionPromise??this._processMessages()),this._disposed=!0}_scheduleNextProcessAction(){if(this._reportActionTimeoutRef)return;const e=this._messageQueue.length>=this._options.batchSize?0:this._options.interval;this._reportActionTimeoutRef=setTimeout(()=>{this._reportActionPromise=this._processMessages().then(()=>{const s=this._reportActionTimeoutRef;this._reportActionTimeoutRef=void 0,clearTimeout(s),this._reportActionPromise=null,this._scheduleNextProcessAction()})},e)}async _processMessages(){let e,s;for(;this._messageQueue.length>0;)if(e=this._messageQueue.splice(0,Math.min(this._messageQueue.length,this._options.batchSize)),s=await this._sendMessagesBatch(e),!s){this._messageQueue.unshift(...e);return}}_sendMessagesBatch(e){return new Promise(s=>{const i=()=>{s(!1)},r=new XMLHttpRequest;r.open(this._options.verb,this._options.endpoint),r.setRequestHeader("Content-Type","application/json;charset=UTF-8"),this._options.requestTransform&&this._options.requestTransform(r),r.onload=function(){s(this.status>=200&&this.status<300)},r.onerror=i,r.onabort=i,r.send(JSON.stringify(e))})}}class d{constructor(){n(this,"_messages",[])}get messages(){return this._messages.slice()}register(e){this._messages.push(e)}[Symbol.asyncDispose](){return Promise.resolve()}}class v{constructor(e){n(this,"_reporters");this._reporters=e||[]}register(e){for(const s of this._reporters)s.register(e)}async[Symbol.asyncDispose](){const e=new Array;for(const s of this._reporters)e.push(s[Symbol.asyncDispose]());e.length&&await Promise.all(e)}}class b{constructor(e){n(this,"_options");this._options=e}logMessageCore(e){var s;e.name=this._options.name;for(const i of this._options.enrichers)i.enrich(e);(s=this._options.reporter)==null||s.register(e)}async[Symbol.asyncDispose](){var e;await((e=this._options.reporter)==null?void 0:e[Symbol.asyncDispose]())}isEnabled(e){return e!==t.LogLevel.None&&e>=this._options.minimumLevel}trace(e){const s=new c;s.level=t.LogLevel.Trace,s.message=e,this.logMessage(s)}debug(e){const s=new c;s.level=t.LogLevel.Debug,s.message=e,this.logMessage(s)}info(e){const s=new c;s.level=t.LogLevel.Information,s.message=e,this.logMessage(s)}warn(e){const s=new c;s.level=t.LogLevel.Warning,s.message=e,this.logMessage(s)}error(e){const s=new c;s.level=t.LogLevel.Error,s.message=e,this.logMessage(s)}crit(e){const s=new c;s.level=t.LogLevel.Critical,s.message=e,this.logMessage(s)}log(e,s,i,r){const l=new c;l.level=e,l.message=s,l.errorMessage=i==null?void 0:i.message,l.stackTrace=i==null?void 0:i.stack,l.extraParams=r,this.logMessage(l)}logMessage(e){this.isEnabled(e.level)&&setTimeout(()=>{this.logMessageCore(e)},1)}}class L{constructor(){n(this,"name","");n(this,"reporter",null);n(this,"minimumLevel",t.LogLevel.Warning);n(this,"enrichers",[])}static getLevel(e){switch((e||"").toUpperCase()){case"TRACE":return t.LogLevel.Trace;case"DEBUG":return t.LogLevel.Debug;case"INFORMATION":return t.LogLevel.Information;case"WARNING":return t.LogLevel.Warning;case"ERROR":return t.LogLevel.Error;case"CRITICAL":return t.LogLevel.Critical;case"NONE":return t.LogLevel.None;default:return t.LogLevel.None}}}class y{constructor(e,s=t.LogLevel.Information){n(this,"_logger");n(this,"_logLevel");this._logger=e,this._logLevel=s}onPublish(e){this._logger.log(this._logLevel,`Publishing message to topic: ${e.topic}`,void 0,{topic:e.topic??null,message:e.message??null})}}function P(o=1,e){return new Promise((s,i)=>{setTimeout(()=>{e?i(e):s()},o)})}return t.BroadcastChannelPlugin=u,t.ConsoleReporter=m,t.DynamicValuesEnricher=_,t.InMemoryReporter=d,t.LogMessage=c,t.Logger=b,t.LoggerOptions=L,t.LoggerPlugin=y,t.MultipleReporter=v,t.PubSubHub=a,t.ValuesEnricher=g,t.XhrReporter=p,t.XhrReporterOptions=f,t.delay=P,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),t}({});
|