UNPKG

@cosmicmind/patternjs

Version:

A library of Design Patterns in TypeScript.

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