New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bkb

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bkb - npm Package Compare versions

Comparing version 0.17.4 to 0.18.0

94

bkb.d.ts

@@ -1,17 +0,12 @@

declare function createApplication<A>(Cl: { new (dash: ApplicationDash<A>, ...args: any[]): A }, ...args: any[]): Application<A>
declare function toApplication<A>(obj: A): ApplicationDash<A>
declare function createApplication<A>(Class: { new (dash: ApplicationDash<A>, ...args: any[]): A }, ...args: any[]): A
declare function asApplication<A>(obj: A): ApplicationDash<A>
type Component<T> = T & {
readonly bkb: Bkb
}
type Application<T> = T & {
readonly bkb: ApplicationBkb
}
interface ComponentEvent<D> {
interface ComponentEvent<D = any> {
readonly eventName: string
readonly sourceName: string
readonly sourceId: number
readonly source: Component<Object>
/**
* The component source
*/
readonly source: object
readonly data?: D

@@ -21,7 +16,5 @@ stopPropagation(): void

interface Transmitter<D> {
call(callback: (ev: ComponentEvent<D>) => void, thisArg?: any): this
call(mode: "eventOnly", callback: (ev: ComponentEvent<D>) => void, thisArg?: any): this
call(mode: "dataFirst", callback: (data: D, ev: ComponentEvent<D>) => void, thisArg?: any): this
call(mode: "arguments", callback: (...args: any[]) => void, thisArg?: any): this
interface Transmitter<D = any> {
onData(callback: (data: D, ev: ComponentEvent<D>) => void, thisArg?: any): this
onEvent(callback: (ev: ComponentEvent<D>) => void, thisArg?: any): this
disable(): void

@@ -44,11 +37,15 @@ isDisabled(): boolean

interface NewComponentProperties {
interface CreateComponentProperties<A = any, C = any> {
Class: { new(dash: Dash<A>, ...args: any[]): C },
arguments?: any[]
argument?: any
group?: string | string[]
componentName?: string
/**
* NB: Used only with instance components. Ignored for object components.
*/
args?: any[]
}
interface AsComponentProperties {
group?: string | string[]
componentName?: string
}
interface EmitterOptions {

@@ -62,15 +59,14 @@ /**

interface Bkb {
on<D>(eventName: string, callback: (ev: ComponentEvent<D>) => void, thisArg?: any): this
on<D>(eventName: string, mode: "eventOnly", callback: (ev: ComponentEvent<D>) => void, thisArg?: any): this
on<D>(eventName: string, mode: "dataFirst", callback: (data: D, ev: ComponentEvent<D>) => void, thisArg?: any): this
on(eventName: string, mode: "arguments", callback: (...args: any[]) => void, thisArg?: any): this
listen<D>(eventName: string): Transmitter<D>
onEvent<D = any>(eventName: string, callback: (ev: ComponentEvent<D>) => void, thisArg?: any): this
onData<D = any>(eventName: string, callback: (data: D, ev: ComponentEvent<D>) => void, thisArg?: any): this
listen<D = any>(eventName: string): Transmitter<D>
/**
* Find children
*/
find<E>(filter?: ChildFilter): E[]
find<C = any>(filter?: ChildFilter): C[]
/**
* Find a single child (an Error is thrown if there isn't one result)
*/
findSingle<E>(filter?: ChildFilter): E
findSingle<C = any>(filter?: ChildFilter): C
/**

@@ -91,11 +87,11 @@ * @returns The count of children that validate the filter

*/
readonly instance: Component<Object>
readonly instance: object
/**
* This property is available only when the parent instance is defined: after the initialisation, or after a call of `setInstance()` from its dash.
*/
readonly parent: Component<Object> | undefined
readonly parent: object | undefined
/**
* This method is available only when the targeted parent instance is defined: after the initialisation, or after a call of `setInstance()` from its dash.
*/
getParent(filter?: ParentFilter): Component<Object> | undefined
getParent(filter?: ParentFilter): object | undefined
}

@@ -108,3 +104,3 @@

interface BasicDash<A> extends Bkb {
interface BasicDash<A = any> extends Bkb {
/**

@@ -118,6 +114,6 @@ * Call this method if the instance must be available during the execution of the constructor

create<C>(Cl: { new (dash: Dash<A>, ...args: any[]): C }, properties?: NewComponentProperties): Component<C>
create<C>(Class: { new(dash: Dash<A>, ...args: any[]): C }, ...args: any[]): C
customCreate<C>(properties: CreateComponentProperties<A, C>): C
asComponent(obj: object, properties?: AsComponentProperties): Dash<A>
toComponent(obj: any, properties?: NewComponentProperties): Dash<A>
/**

@@ -133,3 +129,3 @@ * If the option `sync` is activated, the method is allowed only when the component instance is defined: after the initialisation, or after a call of `setInstance()`.

*/
broadcast(ev: ComponentEvent<any>, options?: EmitterOptions): this
broadcast(ev: ComponentEvent, options?: EmitterOptions): this

@@ -143,3 +139,3 @@ /**

*/
listenToParent<D>(eventName: string, filter?: ParentFilter): Transmitter<D>
listenToParent<D = any>(eventName: string, filter?: ParentFilter): Transmitter<D>

@@ -150,13 +146,16 @@ /**

*/
listenToChildren<D>(eventName: string, filter?: ChildFilter): Transmitter<D>
listenToChildren<D = any>(eventName: string, filter?: ChildFilter): Transmitter<D>
listenTo<D>(component: Component<Object>, eventName: string): Transmitter<D>
listenTo<D = any>(component: object, eventName: string): Transmitter<D>
readonly log: Log
getBkbOf(component: object): Bkb
}
interface Dash<A> extends BasicDash<A> {
readonly app: Application<A>
interface Dash<A = any> extends BasicDash<A> {
readonly app: A
readonly bkb: Bkb
}
interface ApplicationDash<A> extends BasicDash<A>, ApplicationBkb {
interface ApplicationDash<A = any> extends BasicDash<A>, ApplicationBkb {
readonly bkb: ApplicationBkb

@@ -183,4 +182,3 @@ }

createApplication,
toApplication,
Component,
asApplication,
ComponentEvent,

@@ -190,3 +188,4 @@ Transmitter,

ChildFilter,
NewComponentProperties,
CreateComponentProperties,
AsComponentProperties,
EmitterOptions,

@@ -198,4 +197,3 @@ Dash,

ApplicationBkb,
ApplicationDash,
Application
ApplicationDash
}

@@ -1,1 +0,1 @@

"use strict";function createApplication(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var r=new ApplicationContainer(t,!1,e);return r.root.getInstance()}function toApplication(t){var e=new ApplicationContainer(t,!0);return e.root.dash}function makeBkb(t,e){var n={get instance(){return t.getInstance()},get parent(){return this.getParent()},getParent:function(e){var n=t.getParent(e);return n?n.getInstance():void 0},on:function(e,n,r,o){return t.emitter.listen(e).call(n,r,o),this},listen:function(e){return t.emitter.listen(e)},destroy:function(){return t.destroy()},componentName:t.componentName,componentId:t.componentId,find:function(e){return void 0===e&&(e={}),t.find(e)},findSingle:function(e){return void 0===e&&(e={}),t.findSingle(e)},count:function(e){return void 0===e&&(e={}),t.count(e)},has:function(e){return void 0===e&&(e={}),t.has(e)}};return e&&Object.assign(n,e),Object.freeze(n),n}function makeDash(t,e){var n=Object.assign(Object.create(e),{setInstance:function(e){return t.setInstance(e)},exposeEvents:function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var r=1===e.length&&Array.isArray(e[0])?e[0]:e;return t.emitter.exposeEvents(r,!0),this},create:function(e,n){return void 0===n&&(n={}),t.createComponent(e,n,!1).getInstance()},toComponent:function(e,n){return void 0===n&&(n={}),t.createComponent(e,n,!0).dash},emit:function(e,n,r){for(var o=Array.isArray(e)?e:[e],i=0,a=o;i<a.length;i++){var s=a[i];t.emit(s,n,r)}return this},broadcast:function(e,n){return t.broadcast(e,n),this},listenToParent:function(e,n){return void 0===n&&(n={}),t.listenToParent(e,n)},listenToChildren:function(e,n){return t.childEmitter.listen(e,n)},listenTo:function(e,n){return t.listenTo(e,n)},bkb:e});return t.app.root&&t.app.root!==t&&Object.defineProperty(n,"app",{get:function(){return t.app.root.getInstance()}}),Object.freeze(n),n}function call(t,e){switch(t.mode){case"dataFirst":t.thisArg?t.callback.call(t.thisArg,e.data,e):t.callback(e.data,e);break;case"arguments":t.thisArg?t.callback.apply(t.thisArg,e.data):t.callback.apply(t,e.data);break;case"eventOnly":default:t.thisArg?t.callback.call(t.thisArg,e):t.callback(e)}}exports.createApplication=createApplication,exports.toApplication=toApplication;var ApplicationContainer=function(){function t(t,e,n){var r=this;this.compCount=0,this.nodes=new Map,this.tickList=null,this.insideRmComp=!1;var o=["error","warn","info","debug","trace"],i=this.newId();this.root=new Container(this,"root",i,{nextTick:function(t){return r.nextTick(t)},log:this.createLog(o)}),this.nodes.set(i,{container:this.root}),this.root.emitter.exposeEvents(["log"].concat(o,["addComponent","removeComponent","changeComponent"]),!1),e?this.root.setInstance(t):this.root.makeInstance(t,n||[])}return t.prototype.getParentOf=function(t){var e=this.findNode(t);return e.parent?e.parent.container:void 0},t.prototype.getChildrenOf=function(t){var e=[],n=this.findNode(t).children;if(n)for(var r=0,o=Array.from(n.values());r<o.length;r++){var i=o[r];e.push(i.container)}return e},t.prototype.getContainer=function(t){return this.findNode(t).container},t.prototype.createComponent=function(e,n,r,o){if(!this.root.dash)throw new Error("Destroyed root component");var i=o.componentName||t.getComponentName(e),a=this.newId(),s=new Container(this,i,a),c=this.findNode(n.componentId),h={container:s,parent:c};return this.nodes.set(a,h),c.children||(c.children=new Map),c.children.set(a,h),r?s.setInstance(e):s.makeInstance(e,o.args||[]),this.root.dash.emit("addComponent",{component:s.getInstance()}),this.root.dash.emit("changeComponent",{component:s.getInstance(),type:"add"}),s},t.prototype.removeComponent=function(t){if(!this.root.dash)throw new Error("Destroyed root component");var e=!this.insideRmComp;try{e&&(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}));var n=t.componentId,r=this.findNode(n);if(r.children){for(var o=0,i=Array.from(r.children.values());o<i.length;o++){var a=i[o];a.parent=null,a.container.destroy()}r.children.clear()}r.parent&&(r.parent.container.forgetChild(n),r.parent.children.delete(n)),this.nodes.delete(n)}finally{e&&(this.insideRmComp=!1)}},t.prototype.errorHandler=function(t){if(!this.root.dash)throw new Error("Destroyed root component");this.root.dash.emit("log",{type:"error",messages:[t]},{sync:!0})},t.prototype.nextTick=function(t){var e=this;this.tickList?this.tickList.push(t):(this.tickList=[t],setTimeout(function(){if(e.tickList){for(var t=0,n=e.tickList;t<n.length;t++){var r=n[t];try{r()}catch(t){e.errorHandler(t)}}e.tickList=null}},0))},t.prototype.findNode=function(t){var e=this.nodes.get(t);if(!e)throw new Error("Missing node of component "+t);return e},t.prototype.newId=function(){return this.compCount++},t.prototype.createLog=function(t){for(var e=this,n={},r=function(t){n[t]=function(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];if(!e.root.dash)throw new Error("Destroyed root component");e.root.dash.emit("log",{type:t,messages:n},{sync:!0})}},o=0,i=t;o<i.length;o++){var a=i[o];r(a)}return Object.freeze(n)},t.getComponentName=function(t){if("string"==typeof t.componentName)return t.componentName;if(t.constructor&&t.constructor.name)return t.constructor.name;var e=/function (.+)\(/.exec(t.toString());return e&&e.length>1?e[1]:"Function"},t}(),ChildEmitter=function(){function t(t){this.app=t,this.destroyed=!1}return t.prototype.emit=function(e,n,r){if(this.callbacks){var o=this.callbacks.get(e.eventName);if(o){for(var i=[],a=0,s=o;a<s.length;a++){var c=s[a];c.filter.componentName&&c.filter.componentName===e.sourceName||!c.filter.deep&&n||c.filter.group&&!t.hasGroup(c.filter.group,r)||i.push(c)}this.callCbList(i,e)}}},t.prototype.listen=function(t,e){var n=this;if(void 0===e&&(e={}),this.destroyed)throw new Error("Cannot call listen in a destroyed child-emitter");this.callbacks||(this.callbacks=new Map);var r=[],o=function(){return n.destroyed||!r},i={call:function(o,a,s){if(n.destroyed||!r||!n.callbacks)return i;var c=n.callbacks.get(t);c||n.callbacks.set(t,c=[]);var h=c.length;return r.push(h),"string"==typeof o?c[h]={mode:o,callback:a,thisArg:s,filter:e}:c[h]={mode:"eventOnly",callback:o,thisArg:a,filter:e},i},disable:function(){if(!o()&&n.callbacks){var e=n.callbacks.get(t);if(e)for(var i=0,a=r;i<a.length;i++){var s=a[i];delete e[s]}r=null}},isDisabled:o};return i},t.prototype.destroy=function(){this.callbacks=null,this.destroyed=!0},t.hasGroup=function(t,e){for(var n="string"==typeof t?[t]:t,r=0,o=n;r<o.length;r++){var i=o[r];if(e.has(i))return!0}return!1},t.prototype.callCbList=function(t,e){for(var n=0,r=t;n<r.length;n++){var o=r[n];try{call(o,e)}catch(t){this.app.errorHandler(t)}}},t}(),Container=function(){function t(t,e,n,r){this.app=t,this.componentName=e,this.componentId=n,this.emitter=new Emitter(t,["destroy"]),this.childEmitter=new ChildEmitter(t),this.bkb=makeBkb(this,r),this.dash=makeDash(this,this.bkb)}return t.prototype.makeInstance=function(t,e){this.setInstance(new(t.bind.apply(t,[void 0,this.dash].concat(e))))},t.prototype.setInstance=function(t){var e=this;if(!this.inst){if(!this.bkb)throw new Error("Destroyed component");if(t.bkb)throw new Error('A component cannot have a member "bkb"');Object.defineProperty(t,"bkb",{get:function(){return e.bkb}}),this.inst=t}},t.prototype.getInstance=function(){if(!this.inst){if(this.bkb)throw new Error("The component instance is still not initialized");throw new Error("Destroyed component")}return this.inst},t.prototype.destroy=function(){this.emit("destroy",void 0,{sync:!0}),this.app.removeComponent(this),this.childGroups&&this.childGroups.clear(),this.emitter.destroy(),this.childEmitter.destroy(),this.bkb=null,this.dash=null,this.inst=null},t.prototype.forgetChild=function(t){if(this.childGroups)for(var e=0,n=Array.from(this.childGroups.values());e<n.length;e++){var r=n[e];r.delete(t)}},t.prototype.createComponent=function(t,e,n){var r=this.app.createComponent(t,this,n,e);if(e.group){this.childGroups||(this.childGroups=new Map);for(var o="string"==typeof e.group?[e.group]:e.group,i=0,a=o;i<a.length;i++){var s=a[i],c=this.childGroups.get(s);c||this.childGroups.set(s,c=new Set),c.add(r.componentId)}}return r},t.prototype.broadcast=function(t,e){var n=this;e&&e.sync?this.emitter.emit(t):this.app.nextTick(function(){return n.emitter.emit(t)})},t.prototype.emit=function(t,e,n){var r=this;n&&n.sync?this.emitSync(this.createEvent(t,e)):this.app.nextTick(function(){return r.emitSync(r.createEvent(t,e))})},t.prototype.createEvent=function(e,n){var r=!0;return Object.freeze((o={eventName:e,sourceName:this.componentName,sourceId:this.componentId,source:this.getInstance(),data:n,stopPropagation:function(){r=!1}},o[t.canPropagateSymb]=function(){return r},o));var o},t.prototype.emitSync=function(t){this.emitter.emit(t);var e=this.app.getParentOf(this.componentId);e&&e.bubbleUpEvent(t,!1,this.componentId)},t.prototype.bubbleUpEvent=function(e,n,r){if(!e[t.canPropagateSymb]||e[t.canPropagateSymb]()){this.childEmitter.emit(e,n,this.getGroupsOf(r));var o=this.app.getParentOf(this.componentId);o&&o.bubbleUpEvent(e,!0,this.componentId)}},t.prototype.listenToParent=function(t,e){var n=this.getParent(e);if(n)return n.emitter.listen(t,this);if(e.componentName)throw new Error("Unknown parent "+e.componentName);return Emitter.empty()},t.prototype.listenTo=function(t,e){return this.app.getContainer(t.bkb.componentId).emitter.listen(e,this)},t.prototype.getGroupsOf=function(t){var e=new Set;if(this.childGroups)for(var n=0,r=Array.from(this.childGroups.entries());n<r.length;n++){var o=r[n],i=o[0],a=o[1];a.has(t)&&e.add(i)}return e},t.prototype.getParent=function(t){for(var e=this;e=this.app.getParentOf(e.componentId);)if(!t||!t.componentName||t.componentName===e.componentName)return e},t.prototype.find=function(t){if(t.deep)throw new Error('Cannot call "find" with filter deep');for(var e=this.getChildContainers(t.group),n=[],r=0,o=e;r<o.length;r++){var i=o[r];t.componentName&&t.componentName!==i.componentName||n.push(i.getInstance())}return n},t.prototype.findSingle=function(t){var e=this.find(t);if(1!==e.length)throw new Error("Cannot find single "+JSON.stringify(t)+" in "+this.componentName+" "+this.componentId);return e[0]},t.prototype.count=function(t){return this.find(t).length},t.prototype.has=function(t){return this.count(t)>0},t.prototype.getChildContainers=function(t){if(!t)return this.app.getChildrenOf(this.componentId);if(!this.childGroups)return[];for(var e="string"==typeof t?[t]:t,n=new Set,r=0,o=e;r<o.length;r++){var i=o[r],a=this.childGroups.get(i);if(a)for(var s=0,c=Array.from(a.values());s<c.length;s++){var h=c[s];n.add(h)}}for(var l=[],p=0,u=Array.from(n.values());p<u.length;p++){var h=u[p];l.push(this.app.getContainer(h))}return l},t}();Container.canPropagateSymb=Symbol("canPropagate");var Emitter=function(){function t(t,e){this.app=t,this.strictEvents=!1,this.destroyed=!1,this.fromEolCancelers=[],e&&this.exposeEvents(e,!1)}return t.empty=function(){var t={call:function(){return t},disable:function(){},isDisabled:function(){return!1}};return t},t.prototype.exposeEvents=function(t,e){if(this.destroyed)throw new Error("Cannot call exposeEvents in a destroyed transmitter");this.eventNames||(this.eventNames=new Set);for(var n=0,r=t;n<r.length;n++){var o=r[n];this.eventNames.add(o)}e&&(this.strictEvents=!0)},t.prototype.emit=function(t){if(this.callbacks){if(this.strictEvents&&this.eventNames&&!this.eventNames.has(t.eventName))throw new Error("Unexposed event: "+t.eventName);var e=this.callbacks.get(t.eventName);e&&this.callCbList(e,t)}},t.prototype.listen=function(t,e){var n=this;if(this.destroyed||!this.fromEolCancelers)throw new Error("Cannot call listen in a destroyed emitter");if(e&&!e.bkb)throw new Error("Cannot call listen from a destroyed component");this.callbacks||(this.callbacks=new Map);var r,o=[],i=function(){return n.destroyed||!o},a={call:function(e,r,i){if(n.destroyed||!o||!n.callbacks)return a;var s=n.callbacks.get(t);s||n.callbacks.set(t,s=[]);var c=s.length;return o.push(c),"string"==typeof e?s[c]={mode:e,callback:r,thisArg:i}:s[c]={mode:"eventOnly",callback:e,thisArg:r},a},disable:function(){if(!i()&&n.callbacks){var e=n.callbacks.get(t);if(e)for(var a=0,s=o;a<s.length;a++){var c=s[a];delete e[c]}o=null,r&&(r(),r=null)}},isDisabled:i};if(e){var s=e.bkb.listen("destroy").call(function(){return a.disable()}),c=this.fromEolCancelers.length;this.fromEolCancelers[c]=s,r=function(){s.disable(),n.fromEolCancelers&&delete n.fromEolCancelers[c]}}else r=null;return a},t.prototype.destroy=function(){if(this.fromEolCancelers)for(var t in this.fromEolCancelers)this.fromEolCancelers.hasOwnProperty(t)&&this.fromEolCancelers[t].disable();this.fromEolCancelers=null,this.callbacks=null,this.eventNames=null,this.destroyed=!0},t.prototype.callCbList=function(t,e){for(var n in t)if(t.hasOwnProperty(n))try{call(t[n],e)}catch(t){this.app.errorHandler(t)}},t}();
function createApplication(t,...e){return new ApplicationContainer(t,!1,e).root.getInstance()}function asApplication(t){return new ApplicationContainer(t,!0).root.dash}function makeBkb(t,e){let n={get instance(){return t.getInstance()},get parent(){return this.getParent()},getParent:function(e){let n=t.getParent(e);return n?n.getInstance():void 0},onData:function(e,n,s){return t.emitter.listen(e).onData(n,s),this},onEvent:function(e,n,s){return t.emitter.listen(e).onEvent(n,s),this},listen:e=>t.emitter.listen(e),destroy:()=>t.destroy(),componentName:t.componentName,componentId:t.componentId,find:(e={})=>t.find(e),findSingle:(e={})=>t.findSingle(e),count:(e={})=>t.count(e),has:(e={})=>t.has(e)};return e&&Object.assign(n,e),Object.freeze(n),n}function makeDash(t,e){let n={setInstance:e=>t.setInstance(e),exposeEvents:function(...e){let n=1===e.length&&Array.isArray(e[0])?e[0]:e;return t.emitter.exposeEvents(n,!0),this},create:(e,...n)=>t.createChild({asObj:!1,props:{Class:e,arguments:n}}).getInstance(),customCreate:e=>t.createChild({asObj:!1,props:e}).getInstance(),asComponent:(e,n={})=>t.createChild({asObj:!0,props:n,obj:e}).dash,emit:function(e,n,s){let i=Array.isArray(e)?e:[e];for(let e of i)t.emit(e,n,s);return this},broadcast:function(e,n){return t.broadcast(e,n),this},listenToParent:(e,n={})=>t.listenToParent(e,n),listenToChildren:(e,n)=>t.childEmitter.listen(e,n),listenTo:(e,n)=>t.listenTo(e,n),getBkbOf:e=>t.app.getContainerByInst(e).bkb,bkb:e};e.log||(n.log=t.app.log);let s=Object.assign(Object.create(e),n);return t.app.root&&t.app.root!==t&&Object.defineProperties(s,{app:{get:()=>t.app.root.getInstance()}}),Object.freeze(s),s}function call(t,e){switch(t.mode){case"data":t.thisArg?t.cb.call(t.thisArg,e.data,e):t.cb(e.data,e);break;case"event":default:t.thisArg?t.cb.call(t.thisArg,e):t.cb(e)}}class ApplicationContainer{constructor(t,e,n){this.compCount=0,this.nodesByInst=new WeakMap,this.nodes=new Map,this.tickList=null,this.insideRmComp=!1;let s=["error","warn","info","debug","trace"],i=this.newId();this.log=this.createLog(s),this.root=new Container(this,"root",i,{nextTick:t=>this.nextTick(t),log:this.log});let r={container:this.root};this.nodes.set(i,r),this.root.emitter.exposeEvents(["log",...s,"addComponent","removeComponent","changeComponent"],!1);let o=e?this.root.setInstance(t):this.root.makeInstance(t,n||[]);this.nodesByInst.set(o,r)}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}createComponent(t,e){if(!this.root.dash)throw new Error("Destroyed root component");let n=t.props.componentName||ApplicationContainer.getComponentName(t.asObj?t.obj:t.props.Class),s=this.newId(),i=new Container(this,n,s),r=this.findNode(e.componentId),o={container:i,parent:r};this.nodes.set(s,o),r.children||(r.children=new Map),r.children.set(s,o);let a;if(t.asObj)a=i.setInstance(t.obj);else{let e=t.props.arguments||(t.props.argument?[t.props.argument]:[]);a=i.makeInstance(t.props.Class,e)}return this.nodesByInst.set(a,o),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 s=t.componentId,i=this.findNode(s);if(i.children){for(let t of Array.from(i.children.values()))t.parent=null,t.container.destroy();i.children.clear()}i.parent&&(i.parent.container.forgetChild(s),i.parent.children.delete(s)),this.nodes.delete(s),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})}nextTick(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=null}},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)}static getComponentName(t){if("string"==typeof t.componentName)return t.componentName;if(t.constructor&&t.constructor.name)return t.constructor.name;let e=/function (.+)\(/.exec(t.toString());return e&&e.length>1?e[1]:"Function"}}class ChildEmitter{constructor(t){this.app=t,this.destroyed=!1}emit(t,e,n){if(!this.callbacks)return;let s=this.callbacks.get(t.eventName);if(!s)return;let i=[];for(let r of s)r.filter.componentName&&r.filter.componentName===t.sourceName||!r.filter.deep&&e||r.filter.group&&!ChildEmitter.hasGroup(r.filter.group,n)||i.push(r);this.callCbList(i,t)}listen(t,e={}){if(this.destroyed)throw new Error("Cannot call 'listen' in a destroyed child-emitter");this.callbacks||(this.callbacks=new Map);let n=[],s=()=>this.destroyed||!n;const i=(s,i,o)=>{if(this.destroyed||!n||!this.callbacks)return r;let a=this.callbacks.get(t);a||this.callbacks.set(t,a=[]);let l=a.length;n.push(l),a[l]={mode:s,cb:i,thisArg:o,filter:e}};let r={onEvent:(t,e)=>(i("event",t,e),r),onData:(t,e)=>(i("data",t,e),r),disable:()=>{if(s()||!this.callbacks)return;let e=this.callbacks.get(t);if(e)for(let t of n)delete e[t];n=null},isDisabled:s};return r}destroy(){this.callbacks=null,this.destroyed=!0}static hasGroup(t,e){let n="string"==typeof t?[t]:t;for(let t of n)if(e.has(t))return!0;return!1}callCbList(t,e){for(let n of t)try{call(n,e)}catch(t){this.app.errorHandler(t)}}}class Container{constructor(t,e,n,s){this.app=t,this.componentName=e,this.componentId=n,this.emitter=new Emitter(t,["destroy"]),this.childEmitter=new ChildEmitter(t),this.bkb=makeBkb(this,s),this.dash=makeDash(this,this.bkb)}makeInstance(t,e){return this.inst?this.inst:this.setInstance(new t(this.dash,...e))}setInstance(t){if(this.inst)return this.inst;if(!this.bkb)throw new Error(`Destroyed component`);return this.inst=t,t}getInstance(){if(!this.inst){if(this.bkb)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.childEmitter.destroy(),this.bkb=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){let e=this.app.createComponent(t,this);if(t.props.group){this.childGroups||(this.childGroups=new Map);let n="string"==typeof t.props.group?[t.props.group]:t.props.group;for(let t of n){let n=this.childGroups.get(t);n||this.childGroups.set(t,n=new Set),n.add(e.componentId)}}return e}broadcast(t,e){e&&e.sync?this.emitter.emit(t):this.app.nextTick(()=>this.emitter.emit(t))}emit(t,e,n){n&&n.sync?this.emitSync(this.createEvent(t,e)):this.app.nextTick(()=>this.emitSync(this.createEvent(t,e)))}createEvent(t,e){let n=!0;return Object.freeze({eventName:t,sourceName:this.componentName,sourceId:this.componentId,source:this.getInstance(),data:e,stopPropagation:()=>{n=!1},[Container.canPropagateSymb]:()=>n})}emitSync(t){this.emitter.emit(t);let e=this.app.getParentOf(this.componentId);e&&e.bubbleUpEvent(t,!1,this.componentId)}bubbleUpEvent(t,e,n){if(t[Container.canPropagateSymb]&&!t[Container.canPropagateSymb]())return;this.childEmitter.emit(t,e,this.getGroupsOf(n));let s=this.app.getParentOf(this.componentId);s&&s.bubbleUpEvent(t,!0,this.componentId)}listenToParent(t,e){let n=this.getParent(e);if(n)return n.emitter.listen(t,this);if(e.componentName)throw new Error(`Unknown parent ${e.componentName}`);return Emitter.empty()}listenTo(t,e){return this.app.getContainerByInst(t).emitter.listen(e,this)}getGroupsOf(t){let e=new Set;if(this.childGroups)for(let[n,s]of Array.from(this.childGroups.entries()))s.has(t)&&e.add(n);return e}getParent(t){let e=this;for(;e=this.app.getParentOf(e.componentId);)if(!t||!t.componentName||t.componentName===e.componentName)return e}find(t){if(t.deep)throw new Error(`Cannot call "find" with filter deep`);let e=this.getChildContainers(t.group),n=[];for(let s of e)t.componentName&&t.componentName!==s.componentName||n.push(s.getInstance());return n}findSingle(t){let e=this.find(t);if(1!==e.length)throw new Error(`Cannot find single ${JSON.stringify(t)} in ${this.componentName} ${this.componentId}`);return e[0]}count(t){return this.find(t).length}has(t){return this.count(t)>0}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 s=[];for(let t of n.values())s.push(this.app.getContainer(t));return s}}Container.canPropagateSymb=Symbol("canPropagate");class Emitter{constructor(t,e){this.app=t,this.strictEvents=!1,this.destroyed=!1,this.fromEolCancelers=[],e&&this.exposeEvents(e,!1)}static empty(){let t={onData:()=>t,onEvent:()=>t,disable:()=>{},isDisabled:()=>!1};return t}exposeEvents(t,e){if(this.destroyed)throw new Error(`Cannot call exposeEvents in a destroyed transmitter`);this.eventNames||(this.eventNames=new Set);for(let e of t)this.eventNames.add(e);e&&(this.strictEvents=!0)}emit(t){if(!this.callbacks)return;if(this.strictEvents&&this.eventNames&&!this.eventNames.has(t.eventName))throw new Error(`Unexposed event: ${t.eventName}`);let e=this.callbacks.get(t.eventName);e&&this.callCbList(e,t)}listen(t,e){if(this.destroyed||!this.fromEolCancelers)throw new Error(`Cannot call listen in a destroyed emitter`);if(e&&!e.bkb)throw new Error(`Cannot call listen from a destroyed component`);this.callbacks||(this.callbacks=new Map);let n=[],s=()=>this.destroyed||!n;const i=(e,s,i)=>{if(this.destroyed||!n||!this.callbacks)return o;let r=this.callbacks.get(t);r||this.callbacks.set(t,r=[]);let a=r.length;n.push(a),r[a]={mode:e,cb:s,thisArg:i}};let r,o={onEvent:(t,e)=>(i("event",t,e),o),onData:(t,e)=>(i("data",t,e),o),disable:()=>{if(s()||!this.callbacks)return;let e=this.callbacks.get(t);if(e)for(let t of n)delete e[t];n=null,r&&(r(),r=null)},isDisabled:s};if(e){let t=e.bkb.listen("destroy").onEvent(()=>o.disable()),n=this.fromEolCancelers.length;this.fromEolCancelers[n]=t,r=(()=>{t.disable(),this.fromEolCancelers&&delete this.fromEolCancelers[n]})}else r=null;return o}destroy(){if(this.fromEolCancelers)for(let t in this.fromEolCancelers)this.fromEolCancelers.hasOwnProperty(t)&&this.fromEolCancelers[t].disable();this.fromEolCancelers=null,this.callbacks=null,this.eventNames=null,this.destroyed=!0}callCbList(t,e){for(let n in t)if(t.hasOwnProperty(n))try{call(t[n],e)}catch(t){this.app.errorHandler(t)}}}export{createApplication,asApplication};
{
"name": "bkb",
"version": "0.17.4",
"version": "0.18.0",
"description": "A JavaScript Framework for Front-end Applications.",

@@ -5,0 +5,0 @@ "main": "bkb.min.js",

@@ -9,2 +9,2 @@ ## Web Site

© 2016 Paleo; Released under the [CC0 License](http://creativecommons.org/publicdomain/zero/1.0/).
© 2017 Paleo; Released under the [CC0 License](http://creativecommons.org/publicdomain/zero/1.0/).
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc