Comparing version 0.20.2 to 0.20.3
@@ -1,1 +0,1 @@ | ||
function call(t,e){t.thisArg?t.cb.call(t.thisArg,e.data,e):t.cb(e.data,e)}class Emitter{constructor(t,e){this.log=t,this.eventNames=new Set,this.strictEvents=!1,this.destroyed=!1,e&&this.exposeEvent(e,!1)}exposeEvent(t,e){if(this.destroyed)throw new Error("Cannot call exposeEvent in a destroyed transmitter");for(let e of t)this.eventNames.add(e);e&&(this.strictEvents=!0)}emit(t){if(this.destroyed||!this.listeners)return;if(this.strictEvents&&!this.eventNames.has(t.eventName))throw new Error(`Unexposed event: ${t.eventName}`);let e=this.listeners.get(t.eventName);if(e)for(let n of e)try{call(n,t)}catch(t){this.log.errorHandler(t)}}on(t,e,n){if(!this.destroyed){this.listeners||(this.listeners=new Map);for(let i of t){let t=this.listeners.get(i);t||this.listeners.set(i,t=[]),t.push({cb:e,thisArg:n})}}}off(t,e,n){if(!this.destroyed&&this.listeners)for(let[i,r]of this.listeners)if(!t||t.has(i))for(let t=0;t<r.length;++t){let i=r[t];i.cb===e&&i.thisArg===n&&(r.splice(t,1),--t)}}destroy(){this.listeners=void 0,this.destroyed=!0}}class Subscriber{listenTo(t,e,n,i=null){t.on(e,n,i),this.map||(this.map=new Map);let r=this.map.get(n);r||this.map.set(n,r=new Map);let s=r.get(i);s||r.set(i,s=new Map);let o=s.get(t);o||s.set(t,o=new Set);for(let t of e)o.add(t)}stopListeningEverywhere(t,e=null){if(!this.map)return;let n=this.map.get(t);if(!n)return;let i=n.get(e);if(i){for(let[n,r]of i)n.off(r,t,e);n.delete(e),0===n.size&&this.map.delete(t)}}stopListening(t,e,n,i=null){if(!this.map)return;let r=this.map.get(n);if(!r)return;let s=r.get(i);if(!s)return;let o=s.get(t);if(!o)return;let a=new Set;for(let t of e)o.delete(t)&&a.add(t);t.off(a,n,i),0===o.size&&(s.delete(t),0===s.size&&(r.delete(i),0===r.size&&this.map.delete(n)))}destroy(){if(this.map){for(let[t,e]of this.map)for(let[n,i]of e)for(let[e,r]of i)e.off(r,t,n);this.map=void 0}}}class Container{constructor(t,e){this.app=t,this.componentId=e,this.subscriber=new Subscriber,this.emitter=new Emitter(t,["destroy"]),this.pub=makePublicDash(this),this.dash=makeDash(this,this.pub)}makeInstance(t,e){return this.inst?this.inst:(this.setInstance(new t(this.dash,...e)),this.inst)}setInstance(t){if(this.inst)return this.inst;if(!this.pub)throw new Error("Destroyed component");this.inst=t,this.app.setInstanceOf(this.componentId,this.inst)}getInstance(){if(!this.inst){if(this.pub)throw new Error("The component instance is still not initialized");throw new Error("Destroyed component")}return this.inst}destroy(){this.emit("destroy",void 0,{sync:!0}),this.app.removeComponent(this,this.inst),this.childGroups&&this.childGroups.clear(),this.emitter.destroy(),this.subscriber.destroy(),this.pub=void 0,this.dash=void 0,this.inst=void 0}forgetChild(t){if(this.childGroups)for(let e of this.childGroups.values())e.delete(t)}createChild(t){return this.app.createComponent(t,this)}addToGroup(t,e){let n=this.app.getContainerByInst(t).componentId;if(this!==this.app.getParentOf(n))throw new Error(`The component ${n} is not a child of ${this.componentId}`);this.childGroups||(this.childGroups=new Map);for(let t of e){let e=this.childGroups.get(t);e||this.childGroups.set(t,e=new Set),e.add(n)}}inGroup(t,e){if(!this.childGroups)return!1;let n=this.app.getContainerByInst(t).componentId;for(let t of e){let e=this.childGroups.get(t);if(e&&e.has(n))return!0}return!1}broadcast(t,e){e&&e.sync?this.emitter.emit(t):this.app.asyncCall(()=>this.emitter.emit(t))}emit(t,e,n){n&&n.sync?this.emitSync(this.createEvent(t,e)):this.app.asyncCall(()=>this.emitSync(this.createEvent(t,e)))}createEvent(t,e){let n=this,i=!0;return Object.freeze({eventName:t,get source(){return n.getInstance()},data:e,stopPropagation:()=>{i=!1},[Container.canPropagateSymb]:()=>i})}emitSync(t){this.emitter.emit(t);let e=this.app.getParentOf(this.componentId);e&&e.bubbleUpEvent(t)}bubbleUpEvent(t){if(t[Container.canPropagateSymb]&&!t[Container.canPropagateSymb]())return;this.emitter.emit(t);let e=this.app.getParentOf(this.componentId);e&&e.bubbleUpEvent(t)}getParent(t){let e=this;for(;e=this.app.getParentOf(e.componentId);)if(!t||t(e))return e}getParents(t){let e=this,n=[];for(;e=this.app.getParentOf(e.componentId);)t&&!t(e)||n.push(e);return n}getChildren(t){let e=this.getChildContainers(t.group),n=[];for(let i of e)!i.inst||t.filter&&!t.filter(i)||n.push(i.getInstance());return n}getChild(t){let e=this.getChildren(t);if(1!==e.length)throw new Error(`Cannot find single ${JSON.stringify(t)} in component ${this.componentId}`);return e[0]}countChildren(t){return this.getChildren(t).length}hasChildren(t){return this.countChildren(t)>0}isChild(t){let e=this.app.getContainerByInst(t).componentId;return this===this.app.getParentOf(e)}destroyChildren(t){let e=this.getChildContainers(t.group);for(let n of e)t.filter&&!t.filter(n)||n.destroy()}getChildContainers(t){if(!t)return this.app.getChildrenOf(this.componentId);if(!this.childGroups)return[];let e="string"==typeof t?[t]:t,n=new Set;for(let t of e){let e=this.childGroups.get(t);if(e)for(let t of e.values())n.add(t)}let i=[];for(let t of n.values())i.push(this.app.getContainer(t));return i}}function makePublicDash(t){let e=Object.freeze({unattendedEvents:Object.freeze({on:(e,n,i)=>{t.emitter.on(arr(e),n,i)},off:(e,n,i)=>{t.emitter.off(new Set(arr(e)),n,i)}}),get instance(){return t.getInstance()},get parent(){return e.getParent()},getParent:function(e){let n=t.getParent(e);return n?n.getInstance():void 0},getParents:e=>t.getParents(e).map(t=>t.getInstance()),destroy:()=>t.destroy(),children:(e={})=>t.getChildren(e),getChild:(e={})=>t.getChild(e),countChildren:(e={})=>t.countChildren(e),hasChildren:(e={})=>t.hasChildren(e),isChild:e=>t.isChild(e),isComponent:e=>t.app.isComponent(e),getPublicDashOf:e=>t.app.getContainerByInst(e).pub,log:t.app.log});return e}function makeDash(t,e){let n={setInstance:e=>(t.setInstance(e),i),exposeEvent:function(...e){return t.emitter.exposeEvent(flatten(e),!0),i},create:(e,...n)=>t.createChild({asObj:!1,Class:e,args:n}).getInstance(),toComponent:e=>t.createChild({asObj:!0,obj:e}).dash,addToGroup:(e,...n)=>(t.addToGroup(e,flatten(n)),i),inGroup:(e,...n)=>t.inGroup(e,flatten(n)),emit:function(e,n,r){let s=Array.isArray(e)?e:[e];for(let e of s)t.emit(e,n,r);return i},broadcast:function(e,n){return t.broadcast(e,n),i},listenTo:(...e)=>{let n,r,s,o;if(2===e.length||"string"==typeof e[0]||Array.isArray(e[0]))[r,s,o]=e,n=t;else{let i;[i,r,s,o]=e,n=t.app.getContainerByInst(i)}return t.subscriber.listenTo(n.emitter,arr(r),s,o),i},stopListening:(...e)=>{let n,r,s,o,a=e.length;if("string"==typeof e[0]||Array.isArray(e[0]))[r,s,o]=e,n=t;else{if(1===a||2===a)return[s,o]=e,void t.subscriber.stopListeningEverywhere(s,o);{let i;[i,r,s,o]=e,n=t.app.getContainerByInst(i)}}return t.subscriber.stopListening(n.emitter,arr(r),s,o),i},destroyChildren:(e={})=>(t.destroyChildren(e),i),publicDash:e},i=Object.assign(Object.create(e),n);return t.app.root&&t.app.root!==t?(Object.defineProperties(i,{app:{get:()=>t.app.root.getInstance()}}),Object.assign(i,...t.app.augmentList.map(t=>t(i)))):i.registerDashAugmentation=(e=>{t.app.augmentList.push(e)}),Object.freeze(i),i}function arr(t){return"string"==typeof t?[t]:t}function flatten(t){return 1===t.length&&Array.isArray(t[0])?t[0]:t}Container.canPropagateSymb=Symbol("canPropagate");class ApplicationContainer{constructor(t,e,n){this.compCount=0,this.nodesByInst=new WeakMap,this.nodes=new Map,this.insideRmComp=!1,this.augmentList=[];let i=["error","warn","info","debug","trace"],r=this.newId();this.log=this.createLog(i),this.root=new Container(this,r);let s={container:this.root};this.nodes.set(r,s),this.root.emitter.exposeEvent(["log",...i,"addComponent","removeComponent","changeComponent"],!1),e?this.root.setInstance(t):this.root.makeInstance(t,n||[])}setInstanceOf(t,e){let n=this.nodes.get(t);if(!n)throw new Error("Destroyed component");this.nodesByInst.set(e,n)}getParentOf(t){let e=this.findNode(t);return e.parent?e.parent.container:void 0}getChildrenOf(t){let e=[],n=this.findNode(t).children;if(n)for(let t of Array.from(n.values()))e.push(t.container);return e}getContainer(t){return this.findNode(t).container}getContainerByInst(t){return this.findNodeByInst(t).container}isComponent(t){return!!this.nodesByInst.get(t)}createComponent(t,e){if(!this.root.dash)throw new Error("Destroyed root component");let n=this.newId(),i=new Container(this,n),r=this.findNode(e.componentId),s={container:i,parent:r};return this.nodes.set(n,s),r.children||(r.children=new Map),r.children.set(n,s),t.asObj?i.setInstance(t.obj):i.makeInstance(t.Class,t.args),this.root.dash.emit("addComponent",{component:i.getInstance()}),this.root.dash.emit("changeComponent",{component:i.getInstance(),type:"add"}),i}removeComponent(t,e){if(!this.root.dash)throw new Error("Destroyed root component");let n=!this.insideRmComp;try{n&&(this.insideRmComp=!0,this.root.dash.emit("removeComponent",{component:t.getInstance()},{sync:!0}),this.root.dash.emit("changeComponent",{component:t.getInstance(),type:"remove"},{sync:!0}));let i=t.componentId,r=this.findNode(i);if(r.children){for(let t of Array.from(r.children.values()))t.parent=void 0,t.container.destroy();r.children.clear()}r.parent&&(r.parent.container.forgetChild(i),r.parent.children.delete(i)),this.nodes.delete(i),e&&this.nodesByInst.delete(e)}finally{n&&(this.insideRmComp=!1)}}errorHandler(t){if(!this.root.dash)throw new Error("Destroyed root component");this.root.dash.emit("log",{type:"error",messages:[t]},{sync:!0})}asyncCall(t){this.tickList?this.tickList.push(t):(this.tickList=[t],setTimeout(()=>{if(this.tickList){for(let t of this.tickList)try{t()}catch(t){this.errorHandler(t)}this.tickList=void 0}},0))}findNode(t){let e=this.nodes.get(t);if(!e)throw new Error(`Missing node of component "${t}"`);return e}findNodeByInst(t){let e=this.nodesByInst.get(t);if(!e)throw new Error(`Cannot find a component for the instance: ${t}`);return e}newId(){return this.compCount++}createLog(t){let e={};for(let n of t)e[n]=((...t)=>{if(!this.root.dash)throw new Error("Destroyed root component");this.root.dash.emit("log",{type:n,messages:t},{sync:!0})});return Object.freeze(e)}}function createApplication(t,...e){return new ApplicationContainer(t,!1,e).root.getInstance()}function asApplication(t){return new ApplicationContainer(t,!0).root.dash}export{createApplication,asApplication}; | ||
function call(t,e){t.thisArg?t.cb.call(t.thisArg,e.data,e):t.cb(e.data,e)}class Emitter{constructor(t,e){this.log=t,this.eventNames=new Set,this.strictEvents=!1,this.destroyed=!1,e&&this.exposeEvent(e,!1)}exposeEvent(t,e){if(this.destroyed)throw new Error("Cannot call exposeEvent in a destroyed transmitter");for(let e of t)this.eventNames.add(e);e&&(this.strictEvents=!0)}emit(t){if(this.destroyed||!this.listeners)return;if(this.strictEvents&&!this.eventNames.has(t.eventName))throw new Error(`Unexposed event: ${t.eventName}`);let e=this.listeners.get(t.eventName);if(e)for(let n of e)try{call(n,t)}catch(t){this.log.errorHandler(t)}}on(t,e,n){if(!this.destroyed){this.listeners||(this.listeners=new Map);for(let r of t){let t=this.listeners.get(r);t||this.listeners.set(r,t=[]),t.push({cb:e,thisArg:n})}}}off(t,e,n){if(!this.destroyed&&this.listeners)for(let[r,i]of this.listeners)if(!t||t.has(r))for(let t=0;t<i.length;++t){let r=i[t];r.cb===e&&r.thisArg===n&&(i.splice(t,1),--t)}}destroy(){this.listeners=void 0,this.destroyed=!0}}class Subscriber{listenTo(t,e,n,r=null){t.on(e,n,r),this.map||(this.map=new Map);let i=this.map.get(n);i||this.map.set(n,i=new Map);let o=i.get(r);o||i.set(r,o=new Map);let s=o.get(t);s||o.set(t,s=new Set);for(let t of e)s.add(t)}stopListeningEverywhere(t,e=null){if(!this.map)return;let n=this.map.get(t);if(!n)return;let r=n.get(e);if(r){for(let[n,i]of r)n.off(i,t,e);n.delete(e),0===n.size&&this.map.delete(t)}}stopListening(t,e,n,r=null){if(!this.map)return;let i=this.map.get(n);if(!i)return;let o=i.get(r);if(!o)return;let s=o.get(t);if(!s)return;let a=new Set;for(let t of e)s.delete(t)&&a.add(t);t.off(a,n,r),0===s.size&&(o.delete(t),0===o.size&&(i.delete(r),0===i.size&&this.map.delete(n)))}destroy(){if(this.map){for(let[t,e]of this.map)for(let[n,r]of e)for(let[e,i]of r)e.off(i,t,n);this.map=void 0}}}class Container{constructor(t,e){this.app=t,this.componentId=e,this.subscriber=new Subscriber,this.emitter=new Emitter(t,["destroy"]),this.pub=makePublicDash(this),this.dash=makeDash(this,this.pub)}makeInstance(t,e){if(this.inst)return;let n;try{n=new t(this.dash,...e)}catch(t){throw this.destroy(),t}this.setInstance(n)}setInstance(t){if(this.inst)return this.inst;if(!this.pub)throw new Error("Destroyed component");this.inst=t,this.app.setInstanceOf(this.componentId,this.inst)}getInstance(){if(!this.inst){if(this.pub)throw new Error("The component instance is still not initialized");throw new Error("Destroyed component")}return this.inst}destroy(){this.emit("destroy",void 0,{sync:!0}),this.app.removeComponent(this,this.inst),this.childGroups&&this.childGroups.clear(),this.emitter.destroy(),this.subscriber.destroy(),this.pub=void 0,this.dash=void 0,this.inst=void 0}forgetChild(t){if(this.childGroups)for(let e of this.childGroups.values())e.delete(t)}createChild(t){return this.app.createComponent(t,this)}addToGroup(t,e){let n=this.app.getContainerByInst(t).componentId;if(this!==this.app.getParentOf(n))throw new Error(`The component ${n} is not a child of ${this.componentId}`);this.childGroups||(this.childGroups=new Map);for(let t of e){let e=this.childGroups.get(t);e||this.childGroups.set(t,e=new Set),e.add(n)}}inGroup(t,e){if(!this.childGroups)return!1;let n=this.app.getContainerByInst(t).componentId;for(let t of e){let e=this.childGroups.get(t);if(e&&e.has(n))return!0}return!1}broadcast(t,e){e&&e.sync?this.emitter.emit(t):this.app.asyncCall(()=>this.emitter.emit(t))}emit(t,e,n){n&&n.sync?this.emitSync(this.createEvent(t,e)):this.app.asyncCall(()=>this.emitSync(this.createEvent(t,e)))}createEvent(t,e){let n=this,r=!0;return Object.freeze({eventName:t,get source(){return n.getInstance()},data:e,stopPropagation:()=>{r=!1},[Container.canPropagateSymb]:()=>r})}emitSync(t){this.emitter.emit(t);let e=this.app.getParentOf(this.componentId);e&&e.bubbleUpEvent(t)}bubbleUpEvent(t){if(t[Container.canPropagateSymb]&&!t[Container.canPropagateSymb]())return;this.emitter.emit(t);let e=this.app.getParentOf(this.componentId);e&&e.bubbleUpEvent(t)}getParent(t){let e=this;for(;e=this.app.getParentOf(e.componentId);)if(!t||t(e))return e}getParents(t){let e=this,n=[];for(;e=this.app.getParentOf(e.componentId);)t&&!t(e)||n.push(e);return n}getChildren(t){let e=this.getChildContainers(t.group),n=[];for(let r of e)t.filter&&!t.filter(r)||r.inst||console.log("=======> Missing instance for component",r.componentId),!r.inst||t.filter&&!t.filter(r)||n.push(r.getInstance());return n}getChild(t){let e=this.getChildren(t);if(1!==e.length)throw new Error(`Cannot find single ${JSON.stringify(t)} in component ${this.componentId}`);return e[0]}countChildren(t){return this.getChildren(t).length}hasChildren(t){return this.countChildren(t)>0}isChild(t){let e=this.app.getContainerByInst(t).componentId;return this===this.app.getParentOf(e)}destroyChildren(t){let e=this.getChildContainers(t.group);for(let n of e)t.filter&&!t.filter(n)||n.destroy()}getChildContainers(t){if(!t)return this.app.getChildrenOf(this.componentId);if(!this.childGroups)return[];let e="string"==typeof t?[t]:t,n=new Set;for(let t of e){let e=this.childGroups.get(t);if(e)for(let t of e.values())n.add(t)}let r=[];for(let t of n.values())r.push(this.app.getContainer(t));return r}}function makePublicDash(t){let e=Object.freeze({unattendedEvents:Object.freeze({on:(e,n,r)=>{t.emitter.on(arr(e),n,r)},off:(e,n,r)=>{t.emitter.off(new Set(arr(e)),n,r)}}),get instance(){return t.getInstance()},get parent(){return e.getParent()},getParent:function(e){let n=t.getParent(e);return n?n.getInstance():void 0},getParents:e=>t.getParents(e).map(t=>t.getInstance()),destroy:()=>t.destroy(),children:(e={})=>t.getChildren(e),getChild:(e={})=>t.getChild(e),countChildren:(e={})=>t.countChildren(e),hasChildren:(e={})=>t.hasChildren(e),isChild:e=>t.isChild(e),isComponent:e=>t.app.isComponent(e),getPublicDashOf:e=>t.app.getContainerByInst(e).pub,log:t.app.log});return e}function makeDash(t,e){let n={setInstance:e=>(t.setInstance(e),r),exposeEvent:function(...e){return t.emitter.exposeEvent(flatten(e),!0),r},create:(e,...n)=>t.createChild({asObj:!1,Class:e,args:n}).getInstance(),toComponent:e=>t.createChild({asObj:!0,obj:e}).dash,addToGroup:(e,...n)=>(t.addToGroup(e,flatten(n)),r),inGroup:(e,...n)=>t.inGroup(e,flatten(n)),emit:function(e,n,i){let o=Array.isArray(e)?e:[e];for(let e of o)t.emit(e,n,i);return r},broadcast:function(e,n){return t.broadcast(e,n),r},listenTo:(...e)=>{let n,i,o,s;if(2===e.length||"string"==typeof e[0]||Array.isArray(e[0]))[i,o,s]=e,n=t;else{let r;[r,i,o,s]=e,n=t.app.getContainerByInst(r)}return t.subscriber.listenTo(n.emitter,arr(i),o,s),r},stopListening:(...e)=>{let n,i,o,s,a=e.length;if("string"==typeof e[0]||Array.isArray(e[0]))[i,o,s]=e,n=t;else{if(1===a||2===a)return[o,s]=e,void t.subscriber.stopListeningEverywhere(o,s);{let r;[r,i,o,s]=e,n=t.app.getContainerByInst(r)}}return t.subscriber.stopListening(n.emitter,arr(i),o,s),r},destroyChildren:(e={})=>(t.destroyChildren(e),r),publicDash:e},r=Object.assign(Object.create(e),n);return t.app.root&&t.app.root!==t?(Object.defineProperties(r,{app:{get:()=>t.app.root.getInstance()}}),Object.assign(r,...t.app.augmentList.map(t=>t(r)))):r.registerDashAugmentation=(e=>{t.app.augmentList.push(e)}),Object.freeze(r),r}function arr(t){return"string"==typeof t?[t]:t}function flatten(t){return 1===t.length&&Array.isArray(t[0])?t[0]:t}Container.canPropagateSymb=Symbol("canPropagate");class ApplicationContainer{constructor(t,e,n){this.compCount=0,this.nodesByInst=new WeakMap,this.nodes=new Map,this.insideRmComp=!1,this.augmentList=[];let r=["error","warn","info","debug","trace"],i=this.newId();this.log=this.createLog(r),this.root=new Container(this,i);let o={container:this.root};this.nodes.set(i,o),this.root.emitter.exposeEvent(["log",...r,"addComponent","removeComponent","changeComponent"],!1),e?this.root.setInstance(t):this.root.makeInstance(t,n||[]),o.created=!0,console.log("===> [DEBUG] init",i,"\n",publicNodesToString(this.nodes.get(this.root.componentId)))}setInstanceOf(t,e){let n=this.nodes.get(t);if(!n)throw new Error("Destroyed component");this.nodesByInst.set(e,n)}getParentOf(t){let e=this.findNode(t);return e.parent?e.parent.container:void 0}getChildrenOf(t){let e=[],n=this.findNode(t).children;if(n)for(let t of Array.from(n.values()))e.push(t.container);return e}getContainer(t){return this.findNode(t).container}getContainerByInst(t){return this.findNodeByInst(t).container}isComponent(t){return!!this.nodesByInst.get(t)}createComponent(t,e){if(!this.root.dash)throw new Error("Destroyed root component");let n=this.newId(),r=new Container(this,n);console.log("===> [DEBUG] create (before)",n,"\n",publicNodesToString(this.nodes.get(this.root.componentId)));let i=this.findNode(e.componentId),o={container:r,parent:i};this.nodes.set(n,o),i.children||(i.children=new Map),i.children.set(n,o),t.asObj?r.setInstance(t.obj):r.makeInstance(t.Class,t.args);let s={component:r.getInstance(),type:"add"};return this.root.dash.emit(["addComponent","changeComponent"],s),console.log("===> [DEBUG] create (after)",n,"\n",publicNodesToString(this.nodes.get(this.root.componentId))),o.created=!0,r}removeComponent(t,e){if(!this.root.dash)throw new Error("Destroyed root component");console.log("===> [DEBUG] remove (before)",t.componentId,e||"-no inst-","\n",publicNodesToString(this.nodes.get(this.root.componentId)));let n=!this.insideRmComp;try{let r=t.componentId,i=this.findNode(r);if(n&&(this.insideRmComp=!0,e&&i.created)){let t={component:e,type:"remove"};this.root.dash.emit(["removeComponent","changeComponent"],t,{sync:!0})}if(i.children){for(let t of Array.from(i.children.values()))t.parent=void 0,t.container.destroy();i.children.clear()}i.parent&&(i.parent.container.forgetChild(r),i.parent.children.delete(r)),this.nodes.delete(r),e&&this.nodesByInst.delete(e)}finally{n&&(this.insideRmComp=!1)}console.log("===> [DEBUG] remove (after)",t.componentId,"\n",publicNodesToString(this.nodes.get(this.root.componentId)))}errorHandler(t){if(!this.root.dash)throw new Error("Destroyed root component");this.root.dash.emit("log",{type:"error",messages:[t]},{sync:!0})}asyncCall(t){this.tickList?this.tickList.push(t):(this.tickList=[t],setTimeout(()=>{if(this.tickList){for(let t of this.tickList)try{t()}catch(t){this.errorHandler(t)}this.tickList=void 0}},0))}findNode(t){let e=this.nodes.get(t);if(!e)throw new Error(`Missing node of component "${t}"`);return e}findNodeByInst(t){let e=this.nodesByInst.get(t);if(!e)throw new Error(`Cannot find a component for the instance: ${t}`);return e}newId(){return this.compCount++}createLog(t){let e={};for(let n of t)e[n]=((...t)=>{if(!this.root.dash)throw new Error("Destroyed root component");this.root.dash.emit("log",{type:n,messages:t},{sync:!0})});return Object.freeze(e)}}function publicNodesToString(t,e=""){let n=`${e}- ${t.container.componentId} ${getComponentName(t.container.inst)} (${t.children?Array.from(t.children.values()).length:"-no children-"})`;if(t.children)for(let r of t.children.values())n+="\n"+publicNodesToString(r,`${e} `);return n}function getComponentName(t){if(!t)return"-no-inst-";if(Symbol&&Symbol.toStringTag&&t[Symbol.toStringTag])return t[Symbol.toStringTag];if(t.constructor&&t.constructor.name)return t.constructor.name;let e=/function (.+)\(/.exec(t.toString());return e&&e.length>1?e[1]:"Function"}function createApplication(t,...e){return new ApplicationContainer(t,!1,e).root.getInstance()}function asApplication(t){return new ApplicationContainer(t,!0).root.dash}export{createApplication,asApplication}; |
{ | ||
"name": "bkb", | ||
"version": "0.20.2", | ||
"version": "0.20.3", | ||
"description": "A JavaScript Framework for Front-end Applications", | ||
@@ -5,0 +5,0 @@ "main": "bkb.min.js", |
import { ComponentEvent, EmitOptions, PublicDash, Dash, ApplicationDash, ComponentFilter, FindChildFilter, EventName, EventCallback, UnattendedEvents, DashAugmentation } from "./exported-definitions" | ||
import { ApplicationContainer } from "./Application" | ||
import { ApplicationContainer } from "./ApplicationContainer" | ||
import { Emitter } from "./Emitter" | ||
@@ -12,3 +12,3 @@ import { Subscriber } from "./Subscriber"; | ||
private inst: object | undefined | ||
public inst: object | undefined | ||
private childGroups?: Map<string, Set<number>> | ||
@@ -28,7 +28,13 @@ | ||
public makeInstance(Cl, args: any[]): object { | ||
public makeInstance(Cl, args: any[]) { | ||
if (this.inst) | ||
return this.inst | ||
this.setInstance(new Cl(this.dash, ...args)) | ||
return this.inst! | ||
return | ||
let inst | ||
try { | ||
inst = new Cl(this.dash, ...args) | ||
} catch (err) { | ||
this.destroy() | ||
throw err | ||
} | ||
this.setInstance(inst) | ||
} | ||
@@ -183,2 +189,4 @@ | ||
for (let child of containers) { | ||
// if ((!filter.filter || filter.filter(child)) &&!child.inst) | ||
// console.log("=======> Missing instance for component", child.componentId) | ||
if (child.inst && (!filter.filter || filter.filter(child))) | ||
@@ -185,0 +193,0 @@ result.push(child.getInstance()) |
import { ComponentEvent, EventCallback } from "./exported-definitions" | ||
import { InternalLog } from "./Application" | ||
import { Container } from "./Container" | ||
export interface EmitterLog { | ||
errorHandler(err: any): void | ||
} | ||
export interface Listener { | ||
@@ -23,3 +26,3 @@ cb: EventCallback | ||
constructor(private log: InternalLog, eventNames?: string[]) { | ||
constructor(private log: EmitterLog, eventNames?: string[]) { | ||
if (eventNames) | ||
@@ -26,0 +29,0 @@ this.exposeEvent(eventNames, false) |
import { ApplicationDash } from "./exported-definitions"; | ||
import { ApplicationContainer } from "./Application"; | ||
import { ApplicationContainer } from "./ApplicationContainer"; | ||
@@ -4,0 +4,0 @@ export function createApplication<A>(Class: { new(dash: ApplicationDash<A>, ...args: any[]): A }, ...args: any[]): A { |
58242
1094