+"use strict";class t{constructor(t=null,e=null){this._notifyMethod=t,this._notifyContext=e}notifyObserver(t){this._notifyMethod.call(this._notifyContext,t)}compareNotifyContext(t){return this._notifyContext===t}get notifyMethod(){return this._notifyMethod}set notifyMethod(t){this._notifyMethod=t}get notifyContext(){return this._notifyContext}set notifyContext(t){this._notifyContext=t}}class e{constructor(t){if(null!=e.instanceMap.get(t))throw new Error(e.MULTITON_MSG);this.multitonKey=t,e.instanceMap.set(this.multitonKey,this),this.mediatorMap=new Map,this.observerMap=new Map,this.initializeView()}initializeView(){}static getInstance(t,i){return null==e.instanceMap&&(e.instanceMap=new Map),null==e.instanceMap.get(t)&&e.instanceMap.set(t,i(t)),e.instanceMap.get(t)}registerObserver(t,e){if(null!=this.observerMap.get(t)){this.observerMap.get(t).push(e)}else this.observerMap.set(t,new Array(e))}notifyObservers(t){if(this.observerMap.has(t.name)){let e=this.observerMap.get(t.name).slice();for(let i=0;i<e.length;i++)e[i].notifyObserver(t)}}removeObserver(t,e){let i=this.observerMap.get(t);for(let t=0;t<i.length;t++)if(!0===i[t].compareNotifyContext(e)){i.splice(t,1);break}0===i.length&&this.observerMap.delete(t)}registerMediator(e){if(!1!==this.mediatorMap.has(e.mediatorName))return;e.initializeNotifier(this.multitonKey),this.mediatorMap.set(e.mediatorName,e);let i=e.listNotificationInterests();if(i.length>0){let n=new t(e.handleNotification.bind(e),e);for(let t=0;t<i.length;t++)this.registerObserver(i[t],n)}e.onRegister()}retrieveMediator(t){return this.mediatorMap.get(t)||null}removeMediator(t){let e=this.mediatorMap.get(t);if(e){let i=e.listNotificationInterests();for(let t=0;t<i.length;t++)this.removeObserver(i[t],e);this.mediatorMap.delete(t),e.onRemove()}return e}hasMediator(t){return this.mediatorMap.has(t)}static removeView(t){this.instanceMap.delete(t)}static get MULTITON_MSG(){return"View instance for this Multiton key already constructed!"}}class i{constructor(t){if(null!=i.instanceMap[t])throw new Error(i.MULTITON_MSG);this.multitonKey=t,i.instanceMap.set(this.multitonKey,this),this.commandMap=new Map,this.initializeController()}initializeController(){this.view=e.getInstance(this.multitonKey,(t=>new e(t)))}static getInstance(t,e){return null==i.instanceMap&&(i.instanceMap=new Map),null==i.instanceMap.get(t)&&i.instanceMap.set(t,e(t)),i.instanceMap.get(t)}executeCommand(t){let e=this.commandMap.get(t.name);if(null==e)return;let i=e();i.initializeNotifier(this.multitonKey),i.execute(t)}registerCommand(e,i){null==this.commandMap.get(e)&&this.view.registerObserver(e,new t(this.executeCommand,this)),this.commandMap.set(e,i)}hasCommand(t){return this.commandMap.has(t)}removeCommand(t){this.hasCommand(t)&&(this.view.removeObserver(t,this),this.commandMap.delete(t))}static removeController(t){i.instanceMap.delete(t)}static get MULTITON_MSG(){return"Controller instance for this Multiton key already constructed!"}}class n{constructor(t){if(null!=n.instanceMap.get(t))throw new Error(n.MULTITON_MSG);this.multitonKey=t,n.instanceMap.set(this.multitonKey,this),this.proxyMap=new Map,this.initializeModel()}initializeModel(){}static getInstance(t,e){return null==n.instanceMap&&(n.instanceMap=new Map),null==n.instanceMap.get(t)&&n.instanceMap.set(t,e(t)),n.instanceMap.get(t)}registerProxy(t){t.initializeNotifier(this.multitonKey),this.proxyMap.set(t.proxyName,t),t.onRegister()}retrieveProxy(t){return this.proxyMap.get(t)||null}hasProxy(t){return this.proxyMap.has(t)}removeProxy(t){let e=this.proxyMap.get(t);return null!=e&&(this.proxyMap.delete(t),e.onRemove()),e}static removeModel(t){n.instanceMap.delete(t)}static get MULTITON_MSG(){return"Model instance for this Multiton key already constructed!"}}class r{constructor(t,e=null,i=""){this._name=t,this._body=e,this._type=i}get name(){return this._name}get body(){return this._body}set body(t){this._body=t}get type(){return this._type}set type(t){this._type=t}toString(){let t="Notification Name: "+this.name;return t+="\nBody:"+(null==this.body?"null":this.body.toString()),t+="\nType:"+(null==this.type?"null":this.type),t}}class s{constructor(t){if(null!=s.instanceMap[t])throw new Error(s.MULTITON_MSG);this.initializeNotifier(t),s.instanceMap.set(this.multitonKey,this),this.initializeFacade()}initializeFacade(){this.initializeModel(),this.initializeController(),this.initializeView()}static getInstance(t,e){return null==s.instanceMap&&(s.instanceMap=new Map),null==s.instanceMap.get(t)&&s.instanceMap.set(t,e(t)),s.instanceMap.get(t)}initializeModel(){null==this.model&&(this.model=n.getInstance(this.multitonKey,(t=>new n(t))))}initializeController(){null==this.controller&&(this.controller=i.getInstance(this.multitonKey,(t=>new i(t))))}initializeView(){null==this.view&&(this.view=e.getInstance(this.multitonKey,(t=>new e(t))))}registerCommand(t,e){this.controller.registerCommand(t,e)}hasCommand(t){return this.controller.hasCommand(t)}removeCommand(t){this.controller.removeCommand(t)}registerProxy(t){this.model.registerProxy(t)}removeProxy(t){return this.model.removeProxy(t)}hasProxy(t){return this.model.hasProxy(t)}retrieveProxy(t){return this.model.retrieveProxy(t)}registerMediator(t){this.view.registerMediator(t)}removeMediator(t){return this.view.removeMediator(t)}hasMediator(t){return this.view.hasMediator(t)}retrieveMediator(t){return this.view.retrieveMediator(t)}sendNotification(t,e=null,i=""){this.notifyObservers(new r(t,e,i))}notifyObservers(t){this.view.notifyObservers(t)}initializeNotifier(t){this.multitonKey=t}static hasCore(t){return this.instanceMap.has(t)}static removeCore(t){null!=s.instanceMap.get(t)&&(n.removeModel(t),e.removeView(t),i.removeController(t),this.instanceMap.delete(t))}static get MULTITON_MSG(){return"Facade instance for this Multiton key already constructed!"}}class o{constructor(){}sendNotification(t,e=null,i=""){null!=this.facade&&this.facade.sendNotification(t,e,i)}initializeNotifier(t){this.multitonKey=t}get facade(){if(null==this.multitonKey)throw new Error(o.MULTITON_MSG);return s.getInstance(this.multitonKey,(t=>new s(t)))}static get MULTITON_MSG(){return"multitonKey for this Notifier not yet initialized!"}}class a extends o{constructor(){super()}execute(t){}}class l extends o{constructor(t=null,e=null){super(),this._mediatorName=t||l.NAME,this._viewComponent=e}onRegister(){}onRemove(){}listNotificationInterests(){return[]}handleNotification(t){}get mediatorName(){return this._mediatorName}get viewComponent(){return this._viewComponent}set viewComponent(t){this._viewComponent=t}static get NAME(){return"Mediator"}}class h extends o{constructor(t=null,e=null){super(),this._proxyName=t||h.NAME,this._data=e}onRegister(){}onRemove(){}get proxyName(){return this._proxyName}get data(){return this._data}set data(t){this._data=t}static get NAME(){return"Proxy"}}exports.Controller=i,exports.Facade=s,exports.MacroCommand=class extends a{constructor(){super(),this.subCommands=[],this.initializeMacroCommand()}initializeMacroCommand(){}addSubCommand(t){this.subCommands.push(t)}execute(t){for(;this.subCommands.length>0;){let e=this.subCommands.shift()();e.initializeNotifier(this.multitonKey),e.execute(t)}}},exports.Mediator=l,exports.Model=n,exports.Notification=r,exports.Notifier=o,exports.Observer=t,exports.Proxy=h,exports.SimpleCommand=a,exports.View=e;
0 commit comments