Comparing version 1.0.14 to 1.0.15
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var a=(c=>(c.BLOC_DISPOSED="BLOC_DISPOSED",c.LISTENER_REMOVED="LISTENER_REMOVED",c.LISTENER_ADDED="LISTENER_ADDED",c.STATE_CHANGED="STATE_CHANGED",c.BLOC_CREATED="BLOC_CREATED",c))(a||{});const o=class o{constructor(t={}){this.createdAt=Date.now(),this.blocInstanceMap=new Map,this.isolatedBlocMap=new Map,this.pluginList=[],this.postChangesToDocument=!1,this.log=(...e)=>{console.log(`☢️ [Blac ${this.createdAt}]`,...e)},this.addPlugin=e=>{this.pluginList.findIndex(r=>r.name===e.name)===-1&&(this.log("Add plugin",e.name),this.pluginList.push(e))},this.reportToPlugins=(e,i,r)=>{this.pluginList.forEach(n=>{n.onEvent(e,i,r)})},this.report=(e,i,r)=>{const n=i.constructor;switch(e){case"BLOC_DISPOSED":this.disposeBloc(i);break;case"LISTENER_REMOVED":i.observer.size===0&&!n.keepAlive&&this.disposeBloc(i);break}this.reportToPlugins(e,i,r)},this.disposeBloc=e=>{const i=e.constructor;this.log("Dispose bloc",{bloc:e,blocName:e.name,blocId:e.id,isolated:i.isolated}),i.isolated?this.unregisterIsolatedBlocInstance(e):this.unregisterBlocInstance(e)},this.getAllBlocs=(e,i={})=>{const r=e,{searchIsolated:n=r.isolated}=i;if(n){const d=this.isolatedBlocMap.get(e);if(d)return d}else return Array.from(this.blocInstanceMap.values()).filter(h=>h instanceof e);return[]},this.configure=e=>{e.exposeBlacInstance};const{__unsafe_ignore_singleton:s=!1}=t;if(o.instance&&!s)return o.instance;this.log("Create new Blac instance"),o.instance=this}static getInstance(){return o.instance}resetInstance(){this.log("Reset Blac instance"),o.instance=new o({__unsafe_ignore_singleton:!0})}createBlocInstanceMapKey(t,s){return`${t}:${s}`}unregisterBlocInstance(t){const s=this.createBlocInstanceMapKey(t.name,t.id);this.log("Unregister bloc",s),this.blocInstanceMap.delete(s)}registerBlocInstance(t){const s=this.createBlocInstanceMapKey(t.name,t.id);this.log("Register bloc",s),this.blocInstanceMap.set(s,t)}findRegisteredBlocInstance(t,s){if(t.isolated)return;const i=this.createBlocInstanceMapKey(t.name,s);return this.blocInstanceMap.get(i)}registerIsolatedBlocInstance(t){const s=t.constructor,e=this.isolatedBlocMap.get(s);this.log("Register isolated bloc",s.name,t.id),e?e.push(t):this.isolatedBlocMap.set(s,[t])}unregisterIsolatedBlocInstance(t){const s=t.constructor,e=this.isolatedBlocMap.get(s);if(this.log("Unregister isolated bloc",s.name,t.id),e){const i=e.findIndex(r=>r.id===t.id);e.splice(i,1),e.length===0&&this.isolatedBlocMap.delete(s)}}findIsolatedBlocInstance(t,s){const e=this.isolatedBlocMap.get(t);if(e)return e.find(i=>i.id===s)}createNewBlocInstance(t,s,e){const i=t,r=Object.prototype.hasOwnProperty.call(t,"create");i._propsOnInit=e;const n=r?i.create():new t;return n.updateId(s),this.log("Create new bloc instance",{blocClass:t,id:s,props:e,newBloc:n}),i.isolated?(this.registerIsolatedBlocInstance(n),n):(this.registerBlocInstance(n),n)}getBloc(t,s={}){const i=t.isolated,r=s.id||t.name,n=i?this.findIsolatedBlocInstance(t,r):this.findRegisteredBlocInstance(t,r),{reconnect:d}=s;if(n)if(d)n.dispose();else return n;return this.createNewBlocInstance(t,r,s.props)}};o.instance=new o,o.getAllBlocs=o.instance.getAllBlocs,o.addPlugin=o.instance.addPlugin,o.configure=o.instance.configure;let b=o;class I{constructor(){this._observers=new Set}get size(){return this._observers.size}get observers(){return this._observers}subscribe(t){return this._observers.add(t),()=>this.unsubscribe(t)}unsubscribe(t){this._observers.delete(t)}notify(t,s,e){this._observers.forEach(i=>i(t,s,e))}dispose(){this._observers.clear()}}const p=class p{constructor(t){this.isolated=!1,this.isBlacLive=!0,this.blac=b.getInstance(),this.createdAt=Date.now(),this.updateId=s=>{const e=this.id;!s||s===e||(this.id=s)},this.addSubscriber=s=>(this.blac.report(a.LISTENER_ADDED,this),this.observer.subscribe(s),()=>this.handleUnsubscribe(s)),this.handleUnsubscribe=s=>{setTimeout(()=>{this.observer.unsubscribe(s),this.blac.report(a.LISTENER_REMOVED,this)},0)},this.connectAddons=()=>{const{addons:s}=this;if(s)for(const e of s)e.onEmit&&this.observer.subscribe((i,r)=>{var n;(n=e.onEmit)==null||n.call(e,{newState:i,oldState:r,cubit:this})}),e.onInit&&e.onInit(this)},this.pushState=(s,e,i)=>{this._state=s,this.observer.notify(s,e,i),this.blac.report(a.STATE_CHANGED,this,{newState:s,oldState:e})},this.observer=new I,this._state=t,this.blac.report(a.BLOC_CREATED,this),this.id=this.constructor.name,this.isolated=this.constructor.isolated,this.addons=this.constructor.addons,this.connectAddons()}get props(){if(this.localProps)return this.localProps;const t=this.constructor._propsOnInit;if(t)return this.localProps=t,t}set props(t){this.localProps=t}get state(){return this._state}get name(){return this.constructor.name}dispose(){this.blac.report(a.BLOC_DISPOSED,this),this.isBlacLive=!1,this.observer.dispose()}};p.isolated=!1,p.keepAlive=!1,p.isBlacClass=!0;let g=p;class E extends g{constructor(t){super(t),this.addSubscriber=s=>(this.blac.report(a.LISTENER_ADDED,this),this.observer.subscribe(s),()=>this.handleUnsubscribe(s))}emit(t){if(t===this.state)return;const s=this.state,e=t;this.pushState(e,s)}patch(t,s=!1){let e=!1;if(!s)for(const i in t){const r=this.state[i];if(t[i]!==r){e=!0;break}}e&&this.emit({...this.state,...t})}}class S extends g{constructor(t){super(t),this.add=s=>{const e=this.state,i=this.reducer(s,this.state);this.pushState(i,e,s)},this.addSubscriber=s=>(this.blac.report(a.LISTENER_ADDED,this),this.observer.subscribe(s),()=>this.handleUnsubscribe(s))}}function B(c={}){const{localStoragePrefix:t="blac",localStorageKey:s,defaultValue:e}=c,i=h=>{const l=localStorage.getItem(`${t}:${h}`);if(!l)return e;try{return JSON.parse(JSON.parse(l)).persist}catch{return l}},r=h=>{const l=s??h.id,u=i(l);typeof u!==void 0&&h.pushState(u,null)};let n="";return{name:"Persist",onInit:r,onEmit:({newState:h,cubit:l})=>{const u=s??l.id,f=JSON.stringify(`{"persist": ${h}}`);f!==n&&(localStorage.setItem(`${t}:${u}`,f),n=f)}}}exports.Blac=b;exports.BlacEvent=a;exports.BlacObservable=I;exports.Bloc=S;exports.BlocBase=g;exports.Cubit=E;exports.Persist=B; | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var p=(a=>(a.BLOC_DISPOSED="BLOC_DISPOSED",a.LISTENER_REMOVED="LISTENER_REMOVED",a.LISTENER_ADDED="LISTENER_ADDED",a.STATE_CHANGED="STATE_CHANGED",a.BLOC_CREATED="BLOC_CREATED",a))(p||{});const o=class o{constructor(s={}){this.createdAt=Date.now(),this.blocInstanceMap=new Map,this.isolatedBlocMap=new Map,this.pluginList=[],this.postChangesToDocument=!1,this.dispatchEvent=t=>{this.log("Broadcast signal",t),Array.from(this.blocInstanceMap.values()).forEach(n=>{var r;(r=n.onEvent)==null||r.call(n,t)})},this.log=(...t)=>{o.enableLog&&console.log(`☢️ [Blac ${this.createdAt}]`,...t)},this.addPlugin=t=>{this.pluginList.findIndex(n=>n.name===t.name)===-1&&(this.log("Add plugin",t.name),this.pluginList.push(t))},this.reportToPlugins=(t,i,n)=>{this.pluginList.forEach(r=>{r.onEvent(t,i,n)})},this.report=(t,i,n)=>{const r=i.constructor;switch(t){case"BLOC_DISPOSED":this.disposeBloc(i);break;case"LISTENER_REMOVED":i.observer.size===0&&!r.keepAlive&&this.disposeBloc(i);break}this.reportToPlugins(t,i,n)},this.disposeBloc=t=>{const i=t.constructor;this.log("Dispose bloc",{bloc:t,blocName:t.name,blocId:t.id,isolated:i.isolated}),i.isolated?this.unregisterIsolatedBlocInstance(t):this.unregisterBlocInstance(t)},this.getAllBlocs=(t,i={})=>{const n=t,{searchIsolated:r=n.isolated}=i;if(r){const l=this.isolatedBlocMap.get(t);if(l)return l}else return Array.from(this.blocInstanceMap.values()).filter(f=>f instanceof t);return[]},this.configure=t=>{t.exposeBlacInstance};const{__unsafe_ignore_singleton:e=!1}=s;if(o.instance&&!e)return o.instance;this.log("Create new Blac instance"),o.instance=this}static getInstance(){return o.instance}resetInstance(){this.log("Reset Blac instance"),o.instance=new o({__unsafe_ignore_singleton:!0})}createBlocInstanceMapKey(s,e){return`${s}:${e}`}unregisterBlocInstance(s){const e=this.createBlocInstanceMapKey(s.name,s.id);this.log("Unregister bloc",e),this.blocInstanceMap.delete(e)}registerBlocInstance(s){const e=this.createBlocInstanceMapKey(s.name,s.id);this.log("Register bloc",e),this.blocInstanceMap.set(e,s)}findRegisteredBlocInstance(s,e){if(s.isolated)return;const i=this.createBlocInstanceMapKey(s.name,e);return this.blocInstanceMap.get(i)}registerIsolatedBlocInstance(s){const e=s.constructor,t=this.isolatedBlocMap.get(e);this.log("Register isolated bloc",e.name,s.id),t?t.push(s):this.isolatedBlocMap.set(e,[s])}unregisterIsolatedBlocInstance(s){const e=s.constructor,t=this.isolatedBlocMap.get(e);if(this.log("Unregister isolated bloc",e.name,s.id),t){const i=t.findIndex(n=>n.id===s.id);t.splice(i,1),t.length===0&&this.isolatedBlocMap.delete(e)}}findIsolatedBlocInstance(s,e){const t=this.isolatedBlocMap.get(s);if(t)return t.find(i=>i.id===e)}createNewBlocInstance(s,e,t){const i=s,n=Object.prototype.hasOwnProperty.call(s,"create");i._propsOnInit=t;const r=n?i.create():new s;return r.updateId(e),this.log("Create new bloc instance",{blocClass:s,id:e,props:t,newBloc:r}),i.isolated?(this.registerIsolatedBlocInstance(r),r):(this.registerBlocInstance(r),r)}getBloc(s,e={}){const i=s.isolated,n=e.id||s.name,r=i?this.findIsolatedBlocInstance(s,n):this.findRegisteredBlocInstance(s,n),{reconnect:l}=e;if(r)if(l)r.dispose();else return r;return this.createNewBlocInstance(s,n,e.props)}};o.instance=new o,o.getAllBlocs=o.instance.getAllBlocs,o.addPlugin=o.instance.addPlugin,o.configure=o.instance.configure,o.dispatchEvent=o.instance.dispatchEvent,o.enableLog=!1;let I=o;class B{constructor(){this._observers=new Set}get size(){return this._observers.size}get observers(){return this._observers}subscribe(s){return this._observers.add(s),()=>this.unsubscribe(s)}unsubscribe(s){this._observers.delete(s)}notify(s,e,t){this._observers.forEach(i=>i(s,e,t))}dispose(){this._observers.clear()}}class b extends CustomEvent{constructor(s,e){super(s,e)}}const d=class d{constructor(s){this.isolated=!1,this.isBlacLive=!0,this.blac=I.getInstance(),this.createdAt=Date.now(),this.updateId=e=>{const t=this.id;!e||e===t||(this.id=e)},this.addSubscriber=e=>(this.blac.report(b.LISTENER_ADDED,this),this.observer.subscribe(e),()=>this.handleUnsubscribe(e)),this.handleUnsubscribe=e=>{setTimeout(()=>{this.observer.unsubscribe(e),this.blac.report(b.LISTENER_REMOVED,this)},0)},this.connectAddons=()=>{const{addons:e}=this;if(e)for(const t of e)t.onEmit&&this.observer.subscribe((i,n)=>{var r;(r=t.onEmit)==null||r.call(t,{newState:i,oldState:n,cubit:this})}),t.onInit&&t.onInit(this)},this.pushState=(e,t,i)=>{this._state=e,this.observer.notify(e,t,i),this.blac.report(b.STATE_CHANGED,this,{newState:e,oldState:t})},this.observer=new B,this._state=s,this.blac.report(p.BLOC_CREATED,this),this.id=this.constructor.name,this.isolated=this.constructor.isolated,this.addons=this.constructor.addons,this.connectAddons()}get props(){if(this.localProps)return this.localProps;const s=this.constructor._propsOnInit;if(s)return this.localProps=s,s}set props(s){this.localProps=s}get state(){return this._state}get name(){return this.constructor.name}dispose(){this.blac.report(b.BLOC_DISPOSED,this),this.isBlacLive=!1,this.observer.dispose()}onEvent(s){}};d.isolated=!1,d.keepAlive=!1,d.isBlacClass=!0;let g=d;class v extends g{constructor(s){super(s),this.addSubscriber=e=>(this.blac.report(p.LISTENER_ADDED,this),this.observer.subscribe(e),()=>this.handleUnsubscribe(e))}emit(s){if(s===this.state)return;const e=this.state,t=s;this.pushState(t,e)}patch(s,e=!1){let t=!1;if(!e)for(const i in s){const n=this.state[i];if(s[i]!==n){t=!0;break}}t&&this.emit({...this.state,...s})}}class D extends g{constructor(s){super(s),this.add=e=>{const t=this.state,i=this.reducer(e,this.state);this.pushState(i,t,e)},this.addSubscriber=e=>(this.blac.report(p.LISTENER_ADDED,this),this.observer.subscribe(e),()=>this.handleUnsubscribe(e))}}function S(a){switch(a){case"localStorage":return localStorage;case"sessionStorage":return sessionStorage;default:return localStorage}}function m(a={}){const{keyPrefix:s="blac",keyName:e,defaultValue:t,storageType:i="localStorage"}=a,n=c=>`${s}:${c}`,r=c=>{const u=S(i).getItem(n(c));if(typeof u!="string")return t;try{const h=JSON.parse(u);return typeof h.v<"u"?h.v:t}catch{return t}},l=c=>{const u=e??c.id,h=r(u);typeof h!==void 0&&c.pushState(h,null)};let f="";return{name:"Persist",onInit:l,onEmit:({newState:c,cubit:u})=>{const h=e??u.id,E=JSON.stringify({v:c});E!==f&&(S(i).setItem(n(h),E),f=E)}}}exports.Blac=I;exports.BlacLifecycleEvent=p;exports.BlacObservable=B;exports.Bloc=D;exports.BlocBase=g;exports.Cubit=v;exports.Persist=m; | ||
//# sourceMappingURL=index.cjs.js.map |
334
index.es.js
@@ -1,15 +0,20 @@ | ||
var h = /* @__PURE__ */ ((c) => (c.BLOC_DISPOSED = "BLOC_DISPOSED", c.LISTENER_REMOVED = "LISTENER_REMOVED", c.LISTENER_ADDED = "LISTENER_ADDED", c.STATE_CHANGED = "STATE_CHANGED", c.BLOC_CREATED = "BLOC_CREATED", c))(h || {}); | ||
var I = /* @__PURE__ */ ((a) => (a.BLOC_DISPOSED = "BLOC_DISPOSED", a.LISTENER_REMOVED = "LISTENER_REMOVED", a.LISTENER_ADDED = "LISTENER_ADDED", a.STATE_CHANGED = "STATE_CHANGED", a.BLOC_CREATED = "BLOC_CREATED", a))(I || {}); | ||
const o = class o { | ||
constructor(t = {}) { | ||
this.createdAt = Date.now(), this.blocInstanceMap = /* @__PURE__ */ new Map(), this.isolatedBlocMap = /* @__PURE__ */ new Map(), this.pluginList = [], this.postChangesToDocument = !1, this.log = (...e) => { | ||
console.log(`☢️ [Blac ${this.createdAt}]`, ...e); | ||
}, this.addPlugin = (e) => { | ||
this.pluginList.findIndex((r) => r.name === e.name) === -1 && (this.log("Add plugin", e.name), this.pluginList.push(e)); | ||
}, this.reportToPlugins = (e, i, r) => { | ||
this.pluginList.forEach((n) => { | ||
n.onEvent(e, i, r); | ||
constructor(s = {}) { | ||
this.createdAt = Date.now(), this.blocInstanceMap = /* @__PURE__ */ new Map(), this.isolatedBlocMap = /* @__PURE__ */ new Map(), this.pluginList = [], this.postChangesToDocument = !1, this.dispatchEvent = (t) => { | ||
this.log("Broadcast signal", t), Array.from(this.blocInstanceMap.values()).forEach((n) => { | ||
var r; | ||
(r = n.onEvent) == null || r.call(n, t); | ||
}); | ||
}, this.report = (e, i, r) => { | ||
const n = i.constructor; | ||
switch (e) { | ||
}, this.log = (...t) => { | ||
o.enableLog && console.log(`☢️ [Blac ${this.createdAt}]`, ...t); | ||
}, this.addPlugin = (t) => { | ||
this.pluginList.findIndex((n) => n.name === t.name) === -1 && (this.log("Add plugin", t.name), this.pluginList.push(t)); | ||
}, this.reportToPlugins = (t, i, n) => { | ||
this.pluginList.forEach((r) => { | ||
r.onEvent(t, i, n); | ||
}); | ||
}, this.report = (t, i, n) => { | ||
const r = i.constructor; | ||
switch (t) { | ||
case "BLOC_DISPOSED": | ||
@@ -19,28 +24,28 @@ this.disposeBloc(i); | ||
case "LISTENER_REMOVED": | ||
i.observer.size === 0 && !n.keepAlive && this.disposeBloc(i); | ||
i.observer.size === 0 && !r.keepAlive && this.disposeBloc(i); | ||
break; | ||
} | ||
this.reportToPlugins(e, i, r); | ||
}, this.disposeBloc = (e) => { | ||
const i = e.constructor; | ||
this.reportToPlugins(t, i, n); | ||
}, this.disposeBloc = (t) => { | ||
const i = t.constructor; | ||
this.log("Dispose bloc", { | ||
bloc: e, | ||
blocName: e.name, | ||
blocId: e.id, | ||
bloc: t, | ||
blocName: t.name, | ||
blocId: t.id, | ||
isolated: i.isolated | ||
}), i.isolated ? this.unregisterIsolatedBlocInstance(e) : this.unregisterBlocInstance(e); | ||
}, this.getAllBlocs = (e, i = {}) => { | ||
const r = e, { searchIsolated: n = r.isolated } = i; | ||
if (n) { | ||
const d = this.isolatedBlocMap.get(e); | ||
if (d) | ||
return d; | ||
}), i.isolated ? this.unregisterIsolatedBlocInstance(t) : this.unregisterBlocInstance(t); | ||
}, this.getAllBlocs = (t, i = {}) => { | ||
const n = t, { searchIsolated: r = n.isolated } = i; | ||
if (r) { | ||
const l = this.isolatedBlocMap.get(t); | ||
if (l) | ||
return l; | ||
} else | ||
return Array.from(this.blocInstanceMap.values()).filter((a) => a instanceof e); | ||
return Array.from(this.blocInstanceMap.values()).filter((p) => p instanceof t); | ||
return []; | ||
}, this.configure = (e) => { | ||
e.exposeBlacInstance; | ||
}, this.configure = (t) => { | ||
t.exposeBlacInstance; | ||
}; | ||
const { __unsafe_ignore_singleton: s = !1 } = t; | ||
if (o.instance && !s) | ||
const { __unsafe_ignore_singleton: e = !1 } = s; | ||
if (o.instance && !e) | ||
return o.instance; | ||
@@ -57,62 +62,62 @@ this.log("Create new Blac instance"), o.instance = this; | ||
} | ||
createBlocInstanceMapKey(t, s) { | ||
return `${t}:${s}`; | ||
createBlocInstanceMapKey(s, e) { | ||
return `${s}:${e}`; | ||
} | ||
unregisterBlocInstance(t) { | ||
const s = this.createBlocInstanceMapKey(t.name, t.id); | ||
this.log("Unregister bloc", s), this.blocInstanceMap.delete(s); | ||
unregisterBlocInstance(s) { | ||
const e = this.createBlocInstanceMapKey(s.name, s.id); | ||
this.log("Unregister bloc", e), this.blocInstanceMap.delete(e); | ||
} | ||
registerBlocInstance(t) { | ||
const s = this.createBlocInstanceMapKey(t.name, t.id); | ||
this.log("Register bloc", s), this.blocInstanceMap.set(s, t); | ||
registerBlocInstance(s) { | ||
const e = this.createBlocInstanceMapKey(s.name, s.id); | ||
this.log("Register bloc", e), this.blocInstanceMap.set(e, s); | ||
} | ||
findRegisteredBlocInstance(t, s) { | ||
if (t.isolated) | ||
findRegisteredBlocInstance(s, e) { | ||
if (s.isolated) | ||
return; | ||
const i = this.createBlocInstanceMapKey(t.name, s); | ||
const i = this.createBlocInstanceMapKey(s.name, e); | ||
return this.blocInstanceMap.get(i); | ||
} | ||
registerIsolatedBlocInstance(t) { | ||
const s = t.constructor, e = this.isolatedBlocMap.get(s); | ||
this.log("Register isolated bloc", s.name, t.id), e ? e.push(t) : this.isolatedBlocMap.set(s, [t]); | ||
registerIsolatedBlocInstance(s) { | ||
const e = s.constructor, t = this.isolatedBlocMap.get(e); | ||
this.log("Register isolated bloc", e.name, s.id), t ? t.push(s) : this.isolatedBlocMap.set(e, [s]); | ||
} | ||
unregisterIsolatedBlocInstance(t) { | ||
const s = t.constructor, e = this.isolatedBlocMap.get(s); | ||
if (this.log("Unregister isolated bloc", s.name, t.id), e) { | ||
const i = e.findIndex((r) => r.id === t.id); | ||
e.splice(i, 1), e.length === 0 && this.isolatedBlocMap.delete(s); | ||
unregisterIsolatedBlocInstance(s) { | ||
const e = s.constructor, t = this.isolatedBlocMap.get(e); | ||
if (this.log("Unregister isolated bloc", e.name, s.id), t) { | ||
const i = t.findIndex((n) => n.id === s.id); | ||
t.splice(i, 1), t.length === 0 && this.isolatedBlocMap.delete(e); | ||
} | ||
} | ||
findIsolatedBlocInstance(t, s) { | ||
const e = this.isolatedBlocMap.get(t); | ||
if (e) | ||
return e.find((i) => i.id === s); | ||
findIsolatedBlocInstance(s, e) { | ||
const t = this.isolatedBlocMap.get(s); | ||
if (t) | ||
return t.find((i) => i.id === e); | ||
} | ||
createNewBlocInstance(t, s, e) { | ||
const i = t, r = Object.prototype.hasOwnProperty.call( | ||
t, | ||
createNewBlocInstance(s, e, t) { | ||
const i = s, n = Object.prototype.hasOwnProperty.call( | ||
s, | ||
"create" | ||
); | ||
i._propsOnInit = e; | ||
const n = r ? i.create() : new t(); | ||
return n.updateId(s), this.log("Create new bloc instance", { | ||
blocClass: t, | ||
id: s, | ||
props: e, | ||
newBloc: n | ||
}), i.isolated ? (this.registerIsolatedBlocInstance(n), n) : (this.registerBlocInstance(n), n); | ||
i._propsOnInit = t; | ||
const r = n ? i.create() : new s(); | ||
return r.updateId(e), this.log("Create new bloc instance", { | ||
blocClass: s, | ||
id: e, | ||
props: t, | ||
newBloc: r | ||
}), i.isolated ? (this.registerIsolatedBlocInstance(r), r) : (this.registerBlocInstance(r), r); | ||
} | ||
getBloc(t, s = {}) { | ||
const i = t.isolated, r = s.id || t.name, n = i ? this.findIsolatedBlocInstance(t, r) : this.findRegisteredBlocInstance(t, r), { reconnect: d } = s; | ||
if (n) | ||
if (d) | ||
n.dispose(); | ||
getBloc(s, e = {}) { | ||
const i = s.isolated, n = e.id || s.name, r = i ? this.findIsolatedBlocInstance(s, n) : this.findRegisteredBlocInstance(s, n), { reconnect: l } = e; | ||
if (r) | ||
if (l) | ||
r.dispose(); | ||
else | ||
return n; | ||
return this.createNewBlocInstance(t, r, s.props); | ||
return r; | ||
return this.createNewBlocInstance(s, n, e.props); | ||
} | ||
}; | ||
o.instance = new o(), o.getAllBlocs = o.instance.getAllBlocs, o.addPlugin = o.instance.addPlugin, o.configure = o.instance.configure; | ||
let b = o; | ||
class I { | ||
o.instance = new o(), o.getAllBlocs = o.instance.getAllBlocs, o.addPlugin = o.instance.addPlugin, o.configure = o.instance.configure, o.dispatchEvent = o.instance.dispatchEvent, o.enableLog = !1; | ||
let E = o; | ||
class D { | ||
constructor() { | ||
@@ -127,10 +132,10 @@ this._observers = /* @__PURE__ */ new Set(); | ||
} | ||
subscribe(t) { | ||
return this._observers.add(t), () => this.unsubscribe(t); | ||
subscribe(s) { | ||
return this._observers.add(s), () => this.unsubscribe(s); | ||
} | ||
unsubscribe(t) { | ||
this._observers.delete(t); | ||
unsubscribe(s) { | ||
this._observers.delete(s); | ||
} | ||
notify(t, s, e) { | ||
this._observers.forEach((i) => i(t, s, e)); | ||
notify(s, e, t) { | ||
this._observers.forEach((i) => i(s, e, t)); | ||
} | ||
@@ -141,29 +146,34 @@ dispose() { | ||
} | ||
const p = class p { | ||
constructor(t) { | ||
this.isolated = !1, this.isBlacLive = !0, this.blac = b.getInstance(), this.createdAt = Date.now(), this.updateId = (s) => { | ||
const e = this.id; | ||
!s || s === e || (this.id = s); | ||
}, this.addSubscriber = (s) => (this.blac.report(h.LISTENER_ADDED, this), this.observer.subscribe(s), () => this.handleUnsubscribe(s)), this.handleUnsubscribe = (s) => { | ||
class g extends CustomEvent { | ||
constructor(s, e) { | ||
super(s, e); | ||
} | ||
} | ||
const d = class d { | ||
constructor(s) { | ||
this.isolated = !1, this.isBlacLive = !0, this.blac = E.getInstance(), this.createdAt = Date.now(), this.updateId = (e) => { | ||
const t = this.id; | ||
!e || e === t || (this.id = e); | ||
}, this.addSubscriber = (e) => (this.blac.report(g.LISTENER_ADDED, this), this.observer.subscribe(e), () => this.handleUnsubscribe(e)), this.handleUnsubscribe = (e) => { | ||
setTimeout(() => { | ||
this.observer.unsubscribe(s), this.blac.report(h.LISTENER_REMOVED, this); | ||
this.observer.unsubscribe(e), this.blac.report(g.LISTENER_REMOVED, this); | ||
}, 0); | ||
}, this.connectAddons = () => { | ||
const { addons: s } = this; | ||
if (s) | ||
for (const e of s) | ||
e.onEmit && this.observer.subscribe((i, r) => { | ||
var n; | ||
(n = e.onEmit) == null || n.call(e, { | ||
const { addons: e } = this; | ||
if (e) | ||
for (const t of e) | ||
t.onEmit && this.observer.subscribe((i, n) => { | ||
var r; | ||
(r = t.onEmit) == null || r.call(t, { | ||
newState: i, | ||
oldState: r, | ||
oldState: n, | ||
cubit: this | ||
}); | ||
}), e.onInit && e.onInit(this); | ||
}, this.pushState = (s, e, i) => { | ||
this._state = s, this.observer.notify(s, e, i), this.blac.report(h.STATE_CHANGED, this, { | ||
newState: s, | ||
oldState: e | ||
}), t.onInit && t.onInit(this); | ||
}, this.pushState = (e, t, i) => { | ||
this._state = e, this.observer.notify(e, t, i), this.blac.report(g.STATE_CHANGED, this, { | ||
newState: e, | ||
oldState: t | ||
}); | ||
}, this.observer = new I(), this._state = t, this.blac.report(h.BLOC_CREATED, this), this.id = this.constructor.name, this.isolated = this.constructor.isolated, this.addons = this.constructor.addons, this.connectAddons(); | ||
}, this.observer = new D(), this._state = s, this.blac.report(I.BLOC_CREATED, this), this.id = this.constructor.name, this.isolated = this.constructor.isolated, this.addons = this.constructor.addons, this.connectAddons(); | ||
} | ||
@@ -173,8 +183,8 @@ get props() { | ||
return this.localProps; | ||
const t = this.constructor._propsOnInit; | ||
if (t) | ||
return this.localProps = t, t; | ||
const s = this.constructor._propsOnInit; | ||
if (s) | ||
return this.localProps = s, s; | ||
} | ||
set props(t) { | ||
this.localProps = t; | ||
set props(s) { | ||
this.localProps = s; | ||
} | ||
@@ -188,10 +198,12 @@ get state() { | ||
dispose() { | ||
this.blac.report(h.BLOC_DISPOSED, this), this.isBlacLive = !1, this.observer.dispose(); | ||
this.blac.report(g.BLOC_DISPOSED, this), this.isBlacLive = !1, this.observer.dispose(); | ||
} | ||
onEvent(s) { | ||
} | ||
}; | ||
p.isolated = !1, p.keepAlive = !1, p.isBlacClass = !0; | ||
let g = p; | ||
class E extends g { | ||
constructor(t) { | ||
super(t), this.addSubscriber = (s) => (this.blac.report(h.LISTENER_ADDED, this), this.observer.subscribe(s), () => this.handleUnsubscribe(s)); | ||
d.isolated = !1, d.keepAlive = !1, d.isBlacClass = !0; | ||
let f = d; | ||
class B extends f { | ||
constructor(s) { | ||
super(s), this.addSubscriber = (e) => (this.blac.report(I.LISTENER_ADDED, this), this.observer.subscribe(e), () => this.handleUnsubscribe(e)); | ||
} | ||
@@ -202,7 +214,7 @@ /** | ||
**/ | ||
emit(t) { | ||
if (t === this.state) | ||
emit(s) { | ||
if (s === this.state) | ||
return; | ||
const s = this.state, e = t; | ||
this.pushState(e, s); | ||
const e = this.state, t = s; | ||
this.pushState(t, e); | ||
} | ||
@@ -214,48 +226,60 @@ /** | ||
**/ | ||
patch(t, s = !1) { | ||
let e = !1; | ||
if (!s) | ||
for (const i in t) { | ||
const r = this.state[i]; | ||
if (t[i] !== r) { | ||
e = !0; | ||
patch(s, e = !1) { | ||
let t = !1; | ||
if (!e) | ||
for (const i in s) { | ||
const n = this.state[i]; | ||
if (s[i] !== n) { | ||
t = !0; | ||
break; | ||
} | ||
} | ||
e && this.emit({ ...this.state, ...t }); | ||
t && this.emit({ ...this.state, ...s }); | ||
} | ||
} | ||
class D extends g { | ||
constructor(t) { | ||
super(t), this.add = (s) => { | ||
const e = this.state, i = this.reducer(s, this.state); | ||
this.pushState(i, e, s); | ||
}, this.addSubscriber = (s) => (this.blac.report(h.LISTENER_ADDED, this), this.observer.subscribe(s), () => this.handleUnsubscribe(s)); | ||
class m extends f { | ||
constructor(s) { | ||
super(s), this.add = (e) => { | ||
const t = this.state, i = this.reducer(e, this.state); | ||
this.pushState(i, t, e); | ||
}, this.addSubscriber = (e) => (this.blac.report(I.LISTENER_ADDED, this), this.observer.subscribe(e), () => this.handleUnsubscribe(e)); | ||
} | ||
} | ||
function S(c = {}) { | ||
function S(a) { | ||
switch (a) { | ||
case "localStorage": | ||
return localStorage; | ||
case "sessionStorage": | ||
return sessionStorage; | ||
default: | ||
return localStorage; | ||
} | ||
} | ||
function A(a = {}) { | ||
const { | ||
localStoragePrefix: t = "blac", | ||
localStorageKey: s, | ||
defaultValue: e | ||
} = c, i = (a) => { | ||
const l = localStorage.getItem(`${t}:${a}`); | ||
if (!l) | ||
return e; | ||
keyPrefix: s = "blac", | ||
keyName: e, | ||
defaultValue: t, | ||
storageType: i = "localStorage" | ||
} = a, n = (c) => `${s}:${c}`, r = (c) => { | ||
const u = S(i).getItem(n(c)); | ||
if (typeof u != "string") | ||
return t; | ||
try { | ||
return JSON.parse(JSON.parse(l)).persist; | ||
const h = JSON.parse(u); | ||
return typeof h.v < "u" ? h.v : t; | ||
} catch { | ||
return l; | ||
return t; | ||
} | ||
}, r = (a) => { | ||
const l = s ?? a.id, u = i(l); | ||
typeof u !== void 0 && a.pushState(u, null); | ||
}, l = (c) => { | ||
const u = e ?? c.id, h = r(u); | ||
typeof h !== void 0 && c.pushState(h, null); | ||
}; | ||
let n = ""; | ||
let p = ""; | ||
return { | ||
name: "Persist", | ||
onInit: r, | ||
onEmit: ({ newState: a, cubit: l }) => { | ||
const u = s ?? l.id, f = JSON.stringify(`{"persist": ${a}}`); | ||
f !== n && (localStorage.setItem(`${t}:${u}`, f), n = f); | ||
onInit: l, | ||
onEmit: ({ newState: c, cubit: u }) => { | ||
const h = e ?? u.id, b = JSON.stringify({ v: c }); | ||
b !== p && (S(i).setItem(n(h), b), p = b); | ||
} | ||
@@ -265,10 +289,10 @@ }; | ||
export { | ||
b as Blac, | ||
h as BlacEvent, | ||
I as BlacObservable, | ||
D as Bloc, | ||
g as BlocBase, | ||
E as Cubit, | ||
S as Persist | ||
E as Blac, | ||
I as BlacLifecycleEvent, | ||
D as BlacObservable, | ||
m as Bloc, | ||
f as BlocBase, | ||
B as Cubit, | ||
A as Persist | ||
}; | ||
//# sourceMappingURL=index.es.js.map |
{ | ||
"name": "blac", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "index.cjs.js", |
@@ -1,8 +0,7 @@ | ||
import { Bloc } from '../Bloc'; | ||
import { Cubit } from '../Cubit'; | ||
export type BlacAddonInit = (bloc: Cubit<unknown> | Bloc<unknown, unknown>) => void; | ||
import { BlocBase } from '../BlocBase'; | ||
export type BlacAddonInit = (bloc: BlocBase<any>) => void; | ||
export type BlacAddonEmit = (params: { | ||
oldState: unknown; | ||
newState: unknown; | ||
cubit: Cubit<unknown> | Bloc<unknown, unknown>; | ||
cubit: BlocBase<any>; | ||
}) => void; | ||
@@ -9,0 +8,0 @@ type BlacAddon = { |
import BlacAddon from './BlacAddon'; | ||
type StorageType = 'localStorage' | 'sessionStorage'; | ||
/** | ||
* Persist addon | ||
* | ||
* @param options | ||
* @returns BlacAddon | ||
*/ | ||
export declare function Persist(options?: { | ||
@@ -6,11 +13,17 @@ /** | ||
*/ | ||
localStoragePrefix?: string; | ||
keyPrefix?: string; | ||
/** | ||
* @default the bloc's id | ||
*/ | ||
localStorageKey?: string; | ||
keyName?: string; | ||
/** | ||
* Used when the value is not found in localStorage | ||
* Used when the value is not found in storage | ||
*/ | ||
defaultValue?: unknown; | ||
/** | ||
* @default 'localStorage' | ||
* @see StorageType | ||
*/ | ||
storageType?: StorageType; | ||
}): BlacAddon; | ||
export {}; |
@@ -5,6 +5,7 @@ import { BlocBase, BlocInstanceId } from './BlocBase'; | ||
import { BlacPlugin } from './BlacPlugin'; | ||
import BlacEvent from './BlacEvent'; | ||
export interface BlacConfig { | ||
exposeBlacInstance?: boolean; | ||
} | ||
export declare enum BlacEvent { | ||
export declare enum BlacLifecycleEvent { | ||
BLOC_DISPOSED = "BLOC_DISPOSED", | ||
@@ -17,7 +18,7 @@ LISTENER_REMOVED = "LISTENER_REMOVED", | ||
export interface EventParams { | ||
[BlacEvent.BLOC_DISPOSED]: undefined; | ||
[BlacEvent.LISTENER_REMOVED]: undefined; | ||
[BlacEvent.LISTENER_ADDED]: undefined; | ||
[BlacEvent.BLOC_CREATED]: undefined; | ||
[BlacEvent.STATE_CHANGED]: { | ||
[BlacLifecycleEvent.BLOC_DISPOSED]: undefined; | ||
[BlacLifecycleEvent.LISTENER_REMOVED]: undefined; | ||
[BlacLifecycleEvent.LISTENER_ADDED]: undefined; | ||
[BlacLifecycleEvent.BLOC_CREATED]: undefined; | ||
[BlacLifecycleEvent.STATE_CHANGED]: { | ||
newState: any; | ||
@@ -42,2 +43,5 @@ oldState: any; | ||
}); | ||
dispatchEvent: <T>(event: BlacEvent<T>) => void; | ||
static dispatchEvent: <T>(event: BlacEvent<T>) => void; | ||
static enableLog: boolean; | ||
log: (...args: any[]) => void; | ||
@@ -47,4 +51,4 @@ static getInstance(): Blac; | ||
addPlugin: (plugin: BlacPlugin) => void; | ||
reportToPlugins: <B extends BlacEvent>(event: B, bloc: BlocBase<any>, params?: EventParams[B] | undefined) => void; | ||
report: <B extends BlacEvent>(event: B, bloc: BlocBase<any>, params?: EventParams[B] | undefined) => void; | ||
reportToPlugins: <B extends BlacLifecycleEvent>(event: B, bloc: BlocBase<any>, params?: EventParams[B] | undefined) => void; | ||
report: <B extends BlacLifecycleEvent>(event: B, bloc: BlocBase<any>, params?: EventParams[B] | undefined) => void; | ||
disposeBloc: (bloc: BlocBase<any>) => void; | ||
@@ -69,5 +73,1 @@ createBlocInstanceMapKey(blocClassName: string, id: BlocInstanceId): string; | ||
} | ||
declare global { | ||
interface Window { | ||
} | ||
} |
import { Blac } from './Blac'; | ||
import BlacEvent from './BlacEvent'; | ||
import { BlacObservable, BlacObserver } from './BlacObserver'; | ||
@@ -33,2 +34,3 @@ import { BlocProps } from './Cubit'; | ||
pushState: (newState: S, oldState: S, action?: any) => void; | ||
onEvent(event: BlacEvent): void; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
77278
24
540
0