UNPKG

@cosmicmind/patternjs

Version:

A library of Design Patterns in TypeScript.

2 lines (1 loc) 3.11 kB
"use strict";var t,e,s,n=Object.defineProperty,r=t=>{throw TypeError(t)},i=(t,e)=>n(t,"name",{value:e,configurable:!0}),o=(t,e,s)=>((t,e,s)=>e in t?n(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s)(t,"symbol"!=typeof e?e+"":e,s),c=(t,e,s)=>e.has(t)||r("Cannot "+s),a=(t,e,s)=>(c(t,e,"read from private field"),s?s.call(t):e.get(t)),u=(t,e,s)=>e.has(t)?r("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(t):e.set(t,s),l=(t,e,s,n)=>(c(t,e,"write to private field"),n?n.call(t,s):e.set(t,s),s);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("@cosmicmind/foundationjs"),p=class{constructor(s){u(this,e),u(this,t),l(this,t,s)}set(e,s){return Object.defineProperty(a(this,t),e,{configurable:!0,enumerable:!0,writable:!1,value:s}),this}map(t){for(const[e,s]of Object.entries(t))this.set(e,s);return this}build(){const n=a(this,t);var r,i,o;return(r=this,i=e,o=s,c(r,i,"access private method"),o).call(this),n}};t=new WeakMap,e=new WeakSet,s=i((function(){l(this,t,{})}),"#clear"),i(p,"Builder");let f=p;const d=class{clone(){return h.assign(Object.create(Object.getPrototypeOf(this)??null),this)}};i(d,"Prototype");let b=d;const g=class{constructor(){o(this,"commands"),this.commands=[]}push(t){this.commands.push(t)}pop(){return this.commands.pop()}clear(){this.commands=[]}};i(g,"CommandHistory");let m=g;const x=class{constructor(){o(this,"topics"),this.topics={}}subscribe(t,...e){this.topics[t]||(this.topics[t]=new Set);const s=this.topics[t];if(h.guard(s))for(const n of e)null==s||s.add(n)}once(t,...e){const s=i((n=>{this.unsubscribe(t,s);for(const t of e)t(n)}),"cb");this.subscribe(t,s)}unsubscribe(t,...e){if(this.topics[t]){const s=this.topics[t];if(h.guard(s))for(const t of e)null==s||s.delete(t)}}publish(t,e){return h.timeout((()=>{const s=this.topics[t];if(h.guard(s))for(const t of s)t(e)}))}publishSync(t,e){const s=this.topics[t];if(h.guard(s))for(const n of s)n(e)}};i(x,"Observable");let v=x;const y=class{};i(y,"Plugin");let O=y;const P=class{constructor(){o(this,"plugins"),this.plugins=[]}register(...t){for(const e of t){if(-1===this.indexOf(e))return this.plugins.push(e),!0}return!1}deregister(...t){for(const e of t){const t=this.indexOf(e);if(-1<t)return this.plugins.splice(t,1),!0}return!1}execute(...t){for(const e of this.plugins)e.execute(...t)}indexOf(t){const e=this.plugins,s="string"==typeof t?t:t.name;for(let n=e.length-1;n>=0;--n)if(s===e[n].name)return n;return-1}};i(P,"PluginManager");let C=P;const j=class{constructor(){o(this,"_next")}get next(){return this._next}append(t){this._next=t}clear(){this._next=void 0}execute(...t){var e;return this.isProcessable(...t)?this.processor(...t):!!(null==(e=this.next)?void 0:e.execute(...t))}};i(j,"RequestChain");let w=j;function S(...t){if(1<t.length){let e=t[0];for(let s=1,n=t.length;s<n;++s){const n=t[s];e.append(n),e=n}}return t[0]}i(S,"generateRequestChain"),exports.Builder=f,exports.CommandHistory=m,exports.Observable=v,exports.Plugin=O,exports.PluginManager=C,exports.Prototype=b,exports.RequestChain=w,exports.generateRequestChain=S;