Comparing version 1.0.27 to 1.0.29
@@ -11,12 +11,14 @@ import { BlocBase, BlocInstanceId } from './BlocBase'; | ||
BLOC_DISPOSED = "BLOC_DISPOSED", | ||
BLOC_CREATED = "BLOC_CREATED", | ||
BLOC_RESURRECTED = "BLOC_RESURRECTED", | ||
LISTENER_REMOVED = "LISTENER_REMOVED", | ||
LISTENER_ADDED = "LISTENER_ADDED", | ||
STATE_CHANGED = "STATE_CHANGED", | ||
BLOC_CREATED = "BLOC_CREATED" | ||
STATE_CHANGED = "STATE_CHANGED" | ||
} | ||
export interface EventParams { | ||
[BlacLifecycleEvent.BLOC_DISPOSED]: undefined; | ||
[BlacLifecycleEvent.BLOC_CREATED]: undefined; | ||
[BlacLifecycleEvent.BLOC_RESURRECTED]: undefined; | ||
[BlacLifecycleEvent.LISTENER_REMOVED]: undefined; | ||
[BlacLifecycleEvent.LISTENER_ADDED]: undefined; | ||
[BlacLifecycleEvent.BLOC_CREATED]: undefined; | ||
[BlacLifecycleEvent.STATE_CHANGED]: { | ||
@@ -51,2 +53,3 @@ newState: any; | ||
disposeBloc: (bloc: BlocBase<any, any>) => void; | ||
resurrectBloc: (bloc: BlocBase<any, any>) => void; | ||
createBlocInstanceMapKey(blocClassName: string, id: BlocInstanceId): string; | ||
@@ -53,0 +56,0 @@ unregisterBlocInstance(bloc: BlocBase<any, any>): void; |
@@ -0,1 +1,2 @@ | ||
import { BlocBase } from './BlocBase'; | ||
import { BlocHookDependencyArrayFn } from './types'; | ||
@@ -7,4 +8,8 @@ | ||
lastState?: unknown[]; | ||
id: string; | ||
passive?: boolean; | ||
}; | ||
export declare class BlacObservable<S> { | ||
bloc: BlocBase<any, any>; | ||
constructor(bloc: BlocBase<any, any>); | ||
private _observers; | ||
@@ -11,0 +16,0 @@ get size(): number; |
import { Blac } from './Blac'; | ||
import { default as BlacEvent } from './BlacEvent'; | ||
import { BlacObservable, BlacObserver } from './BlacObserver'; | ||
import { BlacObservable } from './BlacObserver'; | ||
import { default as BlacAddon } from './addons/BlacAddon'; | ||
@@ -12,9 +12,9 @@ | ||
static addons?: BlacAddon[]; | ||
addons?: BlacAddon[]; | ||
isolated: boolean; | ||
isBlacLive: boolean; | ||
observer: BlacObservable<any>; | ||
blac: Blac; | ||
id: BlocInstanceId; | ||
readonly createdAt: number; | ||
_addons?: BlacAddon[]; | ||
_isolated: boolean; | ||
_isBlacLive: boolean; | ||
_observer: BlacObservable<any>; | ||
_blac: Blac; | ||
_id: BlocInstanceId; | ||
readonly _createdAt: number; | ||
constructor(initialState: S); | ||
@@ -25,10 +25,9 @@ _state: S; | ||
get state(): S; | ||
get name(): string; | ||
updateId: (id?: BlocInstanceId) => void; | ||
addSubscriber: (observerItem: BlacObserver<S>) => (() => void); | ||
dispose(): void; | ||
handleUnsubscribe: (callback: BlacObserver<S>) => void; | ||
connectAddons: () => void; | ||
pushState: (newState: S, oldState: S, action?: any) => void; | ||
onEvent(event: BlacEvent<any>): void; | ||
get _name(): string; | ||
_updateId: (id?: BlocInstanceId) => void; | ||
_dispose(): Promise<void>; | ||
_resurrect(): void; | ||
_connectAddons: () => void; | ||
_pushState: (newState: S, oldState: S, action?: any) => void; | ||
_onEvent(event: BlacEvent<any>): void; | ||
} |
@@ -1,2 +0,2 @@ | ||
"use strict";var S=Object.defineProperty;var D=(r,t,e)=>t in r?S(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var n=(r,t,e)=>D(r,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var u=(r=>(r.BLOC_DISPOSED="BLOC_DISPOSED",r.LISTENER_REMOVED="LISTENER_REMOVED",r.LISTENER_ADDED="LISTENER_ADDED",r.STATE_CHANGED="STATE_CHANGED",r.BLOC_CREATED="BLOC_CREATED",r))(u||{});const o=class o{constructor(t={}){n(this,"createdAt",Date.now());n(this,"blocInstanceMap",new Map);n(this,"isolatedBlocMap",new Map);n(this,"pluginList",[]);n(this,"postChangesToDocument",!1);n(this,"dispatchEvent",t=>{this.log("Broadcast signal",t),Array.from(this.blocInstanceMap.values()).forEach(s=>{var i;(i=s.onEvent)==null||i.call(s,t)})});n(this,"log",(...t)=>{o.enableLog&&console.log(`☢️ [Blac ${this.createdAt}]`,...t)});n(this,"addPlugin",t=>{this.pluginList.findIndex(s=>s.name===t.name)===-1&&(this.log("Add plugin",t.name),this.pluginList.push(t))});n(this,"reportToPlugins",(t,e,s)=>{this.pluginList.forEach(i=>{i.onEvent(t,e,s)})});n(this,"report",(t,e,s)=>{const i=e.constructor;switch(t){case"BLOC_DISPOSED":this.disposeBloc(e);break;case"LISTENER_REMOVED":e.observer.size===0&&!i.keepAlive&&this.disposeBloc(e);break}this.reportToPlugins(t,e,s)});n(this,"disposeBloc",t=>{const e=t.constructor;this.log("Dispose bloc",{bloc:t,blocName:t.name,blocId:t.id,isolated:e.isolated}),e.isolated?this.unregisterIsolatedBlocInstance(t):this.unregisterBlocInstance(t)});n(this,"getAllBlocs",(t,e={})=>{const s=t,{searchIsolated:i=s.isolated}=e;if(i){const a=this.isolatedBlocMap.get(t);if(a)return a}else return Array.from(this.blocInstanceMap.values()).filter(c=>c instanceof t);return[]});const{__unsafe_ignore_singleton:e=!1}=t;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(t,e){return`${t}:${e}`}unregisterBlocInstance(t){const e=this.createBlocInstanceMapKey(t.name,t.id);this.log("Unregister bloc",e),this.blocInstanceMap.delete(e)}registerBlocInstance(t){const e=this.createBlocInstanceMapKey(t.name,t.id);this.log("Register bloc",e),this.blocInstanceMap.set(e,t)}findRegisteredBlocInstance(t,e){if(t.isolated)return;const i=this.createBlocInstanceMapKey(t.name,e);return this.blocInstanceMap.get(i)}registerIsolatedBlocInstance(t){const e=t.constructor,s=this.isolatedBlocMap.get(e);this.log("Register isolated bloc",e.name,t.id),s?s.push(t):this.isolatedBlocMap.set(e,[t])}unregisterIsolatedBlocInstance(t){const e=t.constructor,s=this.isolatedBlocMap.get(e);if(this.log("Unregister isolated bloc",e.name,t.id),s){const i=s.findIndex(a=>a.id===t.id);s.splice(i,1),s.length===0&&this.isolatedBlocMap.delete(e)}}findIsolatedBlocInstance(t,e){const s=this.isolatedBlocMap.get(t);if(s)return s.find(i=>i.id===e)}createNewBlocInstance(t,e,s){const i=t,a=new t(s);return a.updateId(e),this.log("Create new bloc instance",{blocClass:t,id:e,props:s,newBloc:a}),i.isolated?(this.registerIsolatedBlocInstance(a),a):(this.registerBlocInstance(a),a)}getBloc(t,e={}){const s=t.isolated,i=e.id||t.name,a=s?this.findIsolatedBlocInstance(t,i):this.findRegisteredBlocInstance(t,i),{reconnect:c}=e;if(a)if(c)a.dispose();else return a;return this.createNewBlocInstance(t,i,e.props)}};n(o,"instance",new o),n(o,"getAllBlocs",o.instance.getAllBlocs),n(o,"addPlugin",o.instance.addPlugin),n(o,"dispatchEvent",o.instance.dispatchEvent),n(o,"enableLog",!1);let I=o;class B{constructor(){n(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,e,s){this._observers.forEach(i=>{let a=!1;if(i.dependencyArray){let c=i.lastState||[];const f=i.dependencyArray(t,e);c.length!==f.length&&(c=i.dependencyArray(e,e)),i.lastState=f;for(let h=0;h<f.length;h++)if(f[h]!==c[h]){a=!0;break}}else a=!0;if(a)return i.fn(t,e,s)})}dispose(){this._observers.clear()}}class g{constructor(t){n(this,"addons");n(this,"isolated",!1);n(this,"isBlacLive",!0);n(this,"observer");n(this,"blac",I.getInstance());n(this,"id");n(this,"createdAt",Date.now());n(this,"_state");n(this,"_oldState");n(this,"_props",null);n(this,"updateId",t=>{const e=this.id;!t||t===e||(this.id=t)});n(this,"addSubscriber",t=>(this.blac.report(u.LISTENER_ADDED,this),this.observer.subscribe(t),()=>this.handleUnsubscribe(t)));n(this,"handleUnsubscribe",t=>{setTimeout(()=>{this.observer.unsubscribe(t),this.blac.report(u.LISTENER_REMOVED,this)},0)});n(this,"connectAddons",()=>{const{addons:t}=this;if(t)for(const e of t)e.onEmit&&this.observer.subscribe({fn:(s,i)=>{var a;(a=e.onEmit)==null||a.call(e,{newState:s,oldState:i,cubit:this})}}),e.onInit&&e.onInit(this)});n(this,"pushState",(t,e,s)=>{this._state=t,this._oldState=e,this.observer.notify(t,e,s),this.blac.report(u.STATE_CHANGED,this,{newState:t,oldState:e})});this._state=t,this.observer=new B,this.blac.report(u.BLOC_CREATED,this),this.id=this.constructor.name,this.isolated=this.constructor.isolated,this.addons=this.constructor.addons,this.connectAddons()}get state(){return this._state}get name(){return this.constructor.name}dispose(){this.blac.report(u.BLOC_DISPOSED,this),this.isBlacLive=!1,this.observer.dispose()}onEvent(t){}}n(g,"isolated",!1),n(g,"keepAlive",!1),n(g,"isBlacClass",!0),n(g,"addons");class m extends g{emit(t){if(t===this.state)return;const e=this.state,s=t;this.pushState(s,e)}patch(t,e=!1){let s=!1;if(!e)for(const i in t){const a=this.state[i];if(t[i]!==a){s=!0;break}}s&&this.emit({...this.state,...t})}}class v extends g{constructor(){super(...arguments);n(this,"add",e=>{const s=this.state,i=this.reducer(e,this.state);this.pushState(i,s,e)})}}function b(r){switch(r){case"localStorage":return localStorage;case"sessionStorage":return sessionStorage;default:return localStorage}}function y(r={}){const{keyPrefix:t="blac",keyName:e,defaultValue:s,storageType:i="localStorage"}=r,a=l=>`${t}:${l}`,c=l=>{const p=b(i).getItem(a(l));if(typeof p!="string")return s;try{const d=JSON.parse(p);return typeof d.v<"u"?d.v:s}catch{return s}},f=l=>{const p=e??l.id,d=c(p);typeof d!==void 0&&l.pushState(d,null)};let h="";return{name:"Persist",onInit:f,onEmit:({newState:l,cubit:p})=>{const d=e??p.id,E=JSON.stringify({v:l});E!==h&&(b(i).setItem(a(d),E),h=E)}}}exports.Blac=I;exports.BlacLifecycleEvent=u;exports.BlacObservable=B;exports.Bloc=v;exports.BlocBase=g;exports.Cubit=m;exports.Persist=y; | ||
"use strict";var S=Object.defineProperty;var D=(a,t,e)=>t in a?S(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e;var n=(a,t,e)=>D(a,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var u=(a=>(a.BLOC_DISPOSED="BLOC_DISPOSED",a.BLOC_CREATED="BLOC_CREATED",a.BLOC_RESURRECTED="BLOC_RESURRECTED",a.LISTENER_REMOVED="LISTENER_REMOVED",a.LISTENER_ADDED="LISTENER_ADDED",a.STATE_CHANGED="STATE_CHANGED",a))(u||{});const o=class o{constructor(t={}){n(this,"createdAt",Date.now());n(this,"blocInstanceMap",new Map);n(this,"isolatedBlocMap",new Map);n(this,"pluginList",[]);n(this,"postChangesToDocument",!1);n(this,"dispatchEvent",t=>{this.log("Broadcast signal",t),Array.from(this.blocInstanceMap.values()).forEach(s=>{var i;(i=s._onEvent)==null||i.call(s,t)})});n(this,"log",(...t)=>{o.enableLog&&console.log(`☢️ [Blac ${this.createdAt}]`,...t)});n(this,"addPlugin",t=>{this.pluginList.findIndex(s=>s.name===t.name)===-1&&(this.log("Add plugin",t.name),this.pluginList.push(t))});n(this,"reportToPlugins",(t,e,s)=>{this.pluginList.forEach(i=>{i.onEvent(t,e,s)})});n(this,"report",(t,e,s)=>{const i=e.constructor;switch(this.log(t,e,s),t){case"BLOC_DISPOSED":this.disposeBloc(e);break;case"LISTENER_REMOVED":e._observer.size===0&&!i.keepAlive&&e._dispose();break;case"LISTENER_ADDED":e._observer.size<=1&&e._resurrect();break;case"BLOC_RESURRECTED":this.resurrectBloc(e);break}this.reportToPlugins(t,e,s)});n(this,"disposeBloc",t=>{const e=t.constructor;t._isBlacLive=!1,e.isolated?this.unregisterIsolatedBlocInstance(t):this.unregisterBlocInstance(t)});n(this,"resurrectBloc",t=>{const e=t.constructor;t._isBlacLive=!0,e.isolated?this.registerIsolatedBlocInstance(t):this.registerBlocInstance(t)});n(this,"getAllBlocs",(t,e={})=>{const s=t,{searchIsolated:i=s.isolated}=e;if(i){const r=this.isolatedBlocMap.get(t);if(r)return r}else return Array.from(this.blocInstanceMap.values()).filter(c=>c instanceof t);return[]});const{__unsafe_ignore_singleton:e=!1}=t;if(o.instance&&!e)return o.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,e){return`${t}:${e}`}unregisterBlocInstance(t){const e=this.createBlocInstanceMapKey(t._name,t._id);this.blocInstanceMap.delete(e)}registerBlocInstance(t){const e=this.createBlocInstanceMapKey(t._name,t._id);this.blocInstanceMap.set(e,t)}findRegisteredBlocInstance(t,e){if(t.isolated)return;const i=this.createBlocInstanceMapKey(t.name,e);return this.blocInstanceMap.get(i)}registerIsolatedBlocInstance(t){const e=t.constructor,s=this.isolatedBlocMap.get(e);s?s.push(t):this.isolatedBlocMap.set(e,[t])}unregisterIsolatedBlocInstance(t){const e=t.constructor,s=this.isolatedBlocMap.get(e);if(s){const i=s.findIndex(r=>r._id===t._id);s.splice(i,1),s.length===0&&this.isolatedBlocMap.delete(e)}}findIsolatedBlocInstance(t,e){const s=this.isolatedBlocMap.get(t);if(s)return s.find(i=>i._id===e)}createNewBlocInstance(t,e,s){const i=t,r=new t(s);return r._updateId(e),i.isolated?(this.registerIsolatedBlocInstance(r),r):(this.registerBlocInstance(r),r)}getBloc(t,e={}){const s=t.isolated,i=e.id||t.name,r=s?this.findIsolatedBlocInstance(t,i):this.findRegisteredBlocInstance(t,i),{reconnect:c}=e;if(r)if(c)r.dispose();else return r;return this.createNewBlocInstance(t,i,e.props)}};n(o,"instance",new o),n(o,"getAllBlocs",o.instance.getAllBlocs),n(o,"addPlugin",o.instance.addPlugin),n(o,"dispatchEvent",o.instance.dispatchEvent),n(o,"enableLog",!1);let p=o;class B{constructor(t){n(this,"bloc");n(this,"_observers",new Set);this.bloc=t}get size(){return this._observers.size}get observers(){return this._observers}subscribe(t){return this._observers.add(t),p.instance.report(u.LISTENER_ADDED,this.bloc),()=>this.unsubscribe(t)}unsubscribe(t){this._observers.delete(t),p.instance.report(u.LISTENER_REMOVED,this.bloc)}notify(t,e,s){this._observers.forEach(i=>{let r=!1;if(i.dependencyArray){let c=i.lastState||[];const f=i.dependencyArray(t,e);c.length!==f.length&&(c=i.dependencyArray(e,e)),i.lastState=f;for(let h=0;h<f.length;h++)if(f[h]!==c[h]){r=!0;break}}else r=!0;if(r)return i.fn(t,e,s)})}dispose(){this._observers.clear()}}class _{constructor(t){n(this,"_addons");n(this,"_isolated",!1);n(this,"_isBlacLive",!0);n(this,"_observer");n(this,"_blac",p.getInstance());n(this,"_id");n(this,"_createdAt",Date.now());n(this,"_state");n(this,"_oldState");n(this,"_props",null);n(this,"_updateId",t=>{const e=this._id;!t||t===e||(this._id=t)});n(this,"_connectAddons",()=>{const{_addons:t}=this;if(t)for(const e of t)e.onEmit&&this._observer.subscribe({fn:(s,i)=>{var r;(r=e.onEmit)==null||r.call(e,{newState:s,oldState:i,cubit:this})},passive:!0,id:e.name}),e.onInit&&e.onInit(this)});n(this,"_pushState",(t,e,s)=>{this._state=t,this._oldState=e,this._observer.notify(t,e,s),this._blac.report(u.STATE_CHANGED,this,{newState:t,oldState:e})});this._state=t,this._observer=new B(this),this._blac.report(u.BLOC_CREATED,this),this._id=this.constructor.name,this._isolated=this.constructor.isolated,this._addons=this.constructor.addons,this._connectAddons()}get state(){return this._state}get _name(){return this.constructor.name}async _dispose(){this._blac.report(u.BLOC_DISPOSED,this),this._observer.dispose()}_resurrect(){this._blac.report(u.BLOC_RESURRECTED,this)}_onEvent(t){}}n(_,"isolated",!1),n(_,"keepAlive",!1),n(_,"isBlacClass",!0),n(_,"addons");class b extends _{emit(t){if(Object.is(t,this.state))return;const e=this.state,s=t;this._pushState(s,e)}patch(t,e=!1){let s=!1;if(!e)for(const i in t){const r=this.state[i];if(!Object.is(t[i],r)){s=!0;break}}s&&this.emit({...this.state,...t})}}class v extends _{constructor(){super(...arguments);n(this,"add",e=>{const s=this.state,i=this.reducer(e,this.state);this._pushState(i,s,e)})}}function I(a){switch(a){case"localStorage":return localStorage;case"sessionStorage":return sessionStorage;default:return localStorage}}function R(a={}){const{keyPrefix:t="blac",keyName:e,defaultValue:s,storageType:i="localStorage"}=a,r=l=>`${t}:${l}`,c=l=>{const g=I(i).getItem(r(l));if(typeof g!="string")return s;try{const d=JSON.parse(g);return typeof d.v<"u"?d.v:s}catch{return s}},f=l=>{const g=e??l._id,d=c(g);typeof d!==void 0&&l._pushState(d,null)};let h="";return{name:"Persist",onInit:f,onEmit:({newState:l,cubit:g})=>{const d=e??g._id,E=JSON.stringify({v:l});E!==h&&(I(i).setItem(r(d),E),h=E)}}}exports.Blac=p;exports.BlacLifecycleEvent=u;exports.BlacObservable=B;exports.Bloc=v;exports.BlocBase=_;exports.Cubit=b;exports.Persist=R; | ||
//# sourceMappingURL=index.cjs.js.map |
181
index.es.js
var B = Object.defineProperty; | ||
var S = (a, t, e) => t in a ? B(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e; | ||
var n = (a, t, e) => S(a, typeof t != "symbol" ? t + "" : t, e); | ||
var f = /* @__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))(f || {}); | ||
var D = (a, t, e) => t in a ? B(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e; | ||
var n = (a, t, e) => D(a, typeof t != "symbol" ? t + "" : t, e); | ||
var u = /* @__PURE__ */ ((a) => (a.BLOC_DISPOSED = "BLOC_DISPOSED", a.BLOC_CREATED = "BLOC_CREATED", a.BLOC_RESURRECTED = "BLOC_RESURRECTED", a.LISTENER_REMOVED = "LISTENER_REMOVED", a.LISTENER_ADDED = "LISTENER_ADDED", a.STATE_CHANGED = "STATE_CHANGED", a))(u || {}); | ||
const o = class o { | ||
@@ -15,3 +15,3 @@ constructor(t = {}) { | ||
var i; | ||
(i = s.onEvent) == null || i.call(s, t); | ||
(i = s._onEvent) == null || i.call(s, t); | ||
}); | ||
@@ -32,3 +32,3 @@ }); | ||
const i = e.constructor; | ||
switch (t) { | ||
switch (this.log(t, e, s), t) { | ||
case "BLOC_DISPOSED": | ||
@@ -38,4 +38,10 @@ this.disposeBloc(e); | ||
case "LISTENER_REMOVED": | ||
e.observer.size === 0 && !i.keepAlive && this.disposeBloc(e); | ||
e._observer.size === 0 && !i.keepAlive && e._dispose(); | ||
break; | ||
case "LISTENER_ADDED": | ||
e._observer.size <= 1 && e._resurrect(); | ||
break; | ||
case "BLOC_RESURRECTED": | ||
this.resurrectBloc(e); | ||
break; | ||
} | ||
@@ -46,9 +52,8 @@ this.reportToPlugins(t, e, s); | ||
const e = t.constructor; | ||
this.log("Dispose bloc", { | ||
bloc: t, | ||
blocName: t.name, | ||
blocId: t.id, | ||
isolated: e.isolated | ||
}), e.isolated ? this.unregisterIsolatedBlocInstance(t) : this.unregisterBlocInstance(t); | ||
t._isBlacLive = !1, e.isolated ? this.unregisterIsolatedBlocInstance(t) : this.unregisterBlocInstance(t); | ||
}); | ||
n(this, "resurrectBloc", (t) => { | ||
const e = t.constructor; | ||
t._isBlacLive = !0, e.isolated ? this.registerIsolatedBlocInstance(t) : this.registerBlocInstance(t); | ||
}); | ||
n(this, "getAllBlocs", (t, e = {}) => { | ||
@@ -66,3 +71,3 @@ const s = t, { searchIsolated: i = s.isolated } = e; | ||
return o.instance; | ||
this.log("Create new Blac instance"), o.instance = this; | ||
o.instance = this; | ||
} | ||
@@ -81,8 +86,8 @@ static getInstance() { | ||
unregisterBlocInstance(t) { | ||
const e = this.createBlocInstanceMapKey(t.name, t.id); | ||
this.log("Unregister bloc", e), this.blocInstanceMap.delete(e); | ||
const e = this.createBlocInstanceMapKey(t._name, t._id); | ||
this.blocInstanceMap.delete(e); | ||
} | ||
registerBlocInstance(t) { | ||
const e = this.createBlocInstanceMapKey(t.name, t.id); | ||
this.log("Register bloc", e), this.blocInstanceMap.set(e, t); | ||
const e = this.createBlocInstanceMapKey(t._name, t._id); | ||
this.blocInstanceMap.set(e, t); | ||
} | ||
@@ -96,8 +101,8 @@ findRegisteredBlocInstance(t, e) { | ||
const e = t.constructor, s = this.isolatedBlocMap.get(e); | ||
this.log("Register isolated bloc", e.name, t.id), s ? s.push(t) : this.isolatedBlocMap.set(e, [t]); | ||
s ? s.push(t) : this.isolatedBlocMap.set(e, [t]); | ||
} | ||
unregisterIsolatedBlocInstance(t) { | ||
const e = t.constructor, s = this.isolatedBlocMap.get(e); | ||
if (this.log("Unregister isolated bloc", e.name, t.id), s) { | ||
const i = s.findIndex((r) => r.id === t.id); | ||
if (s) { | ||
const i = s.findIndex((r) => r._id === t._id); | ||
s.splice(i, 1), s.length === 0 && this.isolatedBlocMap.delete(e); | ||
@@ -109,12 +114,7 @@ } | ||
if (s) | ||
return s.find((i) => i.id === e); | ||
return s.find((i) => i._id === e); | ||
} | ||
createNewBlocInstance(t, e, s) { | ||
const i = t, r = new t(s); | ||
return r.updateId(e), this.log("Create new bloc instance", { | ||
blocClass: t, | ||
id: e, | ||
props: s, | ||
newBloc: r | ||
}), i.isolated ? (this.registerIsolatedBlocInstance(r), r) : (this.registerBlocInstance(r), r); | ||
return r._updateId(e), i.isolated ? (this.registerIsolatedBlocInstance(r), r) : (this.registerBlocInstance(r), r); | ||
} | ||
@@ -132,6 +132,8 @@ getBloc(t, e = {}) { | ||
n(o, "instance", new o()), n(o, "getAllBlocs", o.instance.getAllBlocs), n(o, "addPlugin", o.instance.addPlugin), n(o, "dispatchEvent", o.instance.dispatchEvent), n(o, "enableLog", !1); | ||
let E = o; | ||
class D { | ||
constructor() { | ||
let p = o; | ||
class S { | ||
constructor(t) { | ||
n(this, "bloc"); | ||
n(this, "_observers", /* @__PURE__ */ new Set()); | ||
this.bloc = t; | ||
} | ||
@@ -145,6 +147,6 @@ get size() { | ||
subscribe(t) { | ||
return this._observers.add(t), () => this.unsubscribe(t); | ||
return this._observers.add(t), p.instance.report(u.LISTENER_ADDED, this.bloc), () => this.unsubscribe(t); | ||
} | ||
unsubscribe(t) { | ||
this._observers.delete(t); | ||
this._observers.delete(t), p.instance.report(u.LISTENER_REMOVED, this.bloc); | ||
} | ||
@@ -156,6 +158,6 @@ notify(t, e, s) { | ||
let c = i.lastState || []; | ||
const u = i.dependencyArray(t, e); | ||
c.length !== u.length && (c = i.dependencyArray(e, e)), i.lastState = u; | ||
for (let h = 0; h < u.length; h++) | ||
if (u[h] !== c[h]) { | ||
const _ = i.dependencyArray(t, e); | ||
c.length !== _.length && (c = i.dependencyArray(e, e)), i.lastState = _; | ||
for (let h = 0; h < _.length; h++) | ||
if (_[h] !== c[h]) { | ||
r = !0; | ||
@@ -174,29 +176,23 @@ break; | ||
} | ||
class p { | ||
class g { | ||
constructor(t) { | ||
n(this, "addons"); | ||
n(this, "isolated", !1); | ||
n(this, "isBlacLive", !0); | ||
n(this, "observer"); | ||
n(this, "blac", E.getInstance()); | ||
n(this, "id"); | ||
n(this, "createdAt", Date.now()); | ||
n(this, "_addons"); | ||
n(this, "_isolated", !1); | ||
n(this, "_isBlacLive", !0); | ||
n(this, "_observer"); | ||
n(this, "_blac", p.getInstance()); | ||
n(this, "_id"); | ||
n(this, "_createdAt", Date.now()); | ||
n(this, "_state"); | ||
n(this, "_oldState"); | ||
n(this, "_props", null); | ||
n(this, "updateId", (t) => { | ||
const e = this.id; | ||
!t || t === e || (this.id = t); | ||
n(this, "_updateId", (t) => { | ||
const e = this._id; | ||
!t || t === e || (this._id = t); | ||
}); | ||
n(this, "addSubscriber", (t) => (this.blac.report(f.LISTENER_ADDED, this), this.observer.subscribe(t), () => this.handleUnsubscribe(t))); | ||
n(this, "handleUnsubscribe", (t) => { | ||
setTimeout(() => { | ||
this.observer.unsubscribe(t), this.blac.report(f.LISTENER_REMOVED, this); | ||
}, 0); | ||
}); | ||
n(this, "connectAddons", () => { | ||
const { addons: t } = this; | ||
n(this, "_connectAddons", () => { | ||
const { _addons: t } = this; | ||
if (t) | ||
for (const e of t) | ||
e.onEmit && this.observer.subscribe({ | ||
e.onEmit && this._observer.subscribe({ | ||
fn: (s, i) => { | ||
@@ -209,7 +205,9 @@ var r; | ||
}); | ||
} | ||
}, | ||
passive: !0, | ||
id: e.name | ||
}), e.onInit && e.onInit(this); | ||
}); | ||
n(this, "pushState", (t, e, s) => { | ||
this._state = t, this._oldState = e, this.observer.notify(t, e, s), this.blac.report(f.STATE_CHANGED, this, { | ||
n(this, "_pushState", (t, e, s) => { | ||
this._state = t, this._oldState = e, this._observer.notify(t, e, s), this._blac.report(u.STATE_CHANGED, this, { | ||
newState: t, | ||
@@ -219,3 +217,3 @@ oldState: e | ||
}); | ||
this._state = t, this.observer = new D(), this.blac.report(f.BLOC_CREATED, this), this.id = this.constructor.name, this.isolated = this.constructor.isolated, this.addons = this.constructor.addons, this.connectAddons(); | ||
this._state = t, this._observer = new S(this), this._blac.report(u.BLOC_CREATED, this), this._id = this.constructor.name, this._isolated = this.constructor.isolated, this._addons = this.constructor.addons, this._connectAddons(); | ||
} | ||
@@ -225,13 +223,16 @@ get state() { | ||
} | ||
get name() { | ||
get _name() { | ||
return this.constructor.name; | ||
} | ||
dispose() { | ||
this.blac.report(f.BLOC_DISPOSED, this), this.isBlacLive = !1, this.observer.dispose(); | ||
async _dispose() { | ||
this._blac.report(u.BLOC_DISPOSED, this), this._observer.dispose(); | ||
} | ||
onEvent(t) { | ||
_resurrect() { | ||
this._blac.report(u.BLOC_RESURRECTED, this); | ||
} | ||
_onEvent(t) { | ||
} | ||
} | ||
n(p, "isolated", !1), n(p, "keepAlive", !1), n(p, "isBlacClass", !0), n(p, "addons"); | ||
class v extends p { | ||
n(g, "isolated", !1), n(g, "keepAlive", !1), n(g, "isBlacClass", !0), n(g, "addons"); | ||
class A extends g { | ||
/** | ||
@@ -242,6 +243,6 @@ * Update the state then will notify all observers | ||
emit(t) { | ||
if (t === this.state) | ||
if (Object.is(t, this.state)) | ||
return; | ||
const e = this.state, s = t; | ||
this.pushState(s, e); | ||
this._pushState(s, e); | ||
} | ||
@@ -258,3 +259,3 @@ /** | ||
const r = this.state[i]; | ||
if (t[i] !== r) { | ||
if (!Object.is(t[i], r)) { | ||
s = !0; | ||
@@ -267,3 +268,3 @@ break; | ||
} | ||
class y extends p { | ||
class v extends g { | ||
constructor() { | ||
@@ -277,7 +278,7 @@ super(...arguments); | ||
const s = this.state, i = this.reducer(e, this.state); | ||
this.pushState(i, s, e); | ||
this._pushState(i, s, e); | ||
}); | ||
} | ||
} | ||
function b(a) { | ||
function I(a) { | ||
switch (a) { | ||
@@ -292,3 +293,3 @@ case "localStorage": | ||
} | ||
function M(a = {}) { | ||
function y(a = {}) { | ||
const { | ||
@@ -300,7 +301,7 @@ keyPrefix: t = "blac", | ||
} = a, r = (l) => `${t}:${l}`, c = (l) => { | ||
const g = b(i).getItem(r(l)); | ||
if (typeof g != "string") | ||
const f = I(i).getItem(r(l)); | ||
if (typeof f != "string") | ||
return s; | ||
try { | ||
const d = JSON.parse(g); | ||
const d = JSON.parse(f); | ||
return typeof d.v < "u" ? d.v : s; | ||
@@ -310,5 +311,5 @@ } catch { | ||
} | ||
}, u = (l) => { | ||
const g = e ?? l.id, d = c(g); | ||
typeof d !== void 0 && l.pushState(d, null); | ||
}, _ = (l) => { | ||
const f = e ?? l._id, d = c(f); | ||
typeof d !== void 0 && l._pushState(d, null); | ||
}; | ||
@@ -318,6 +319,6 @@ let h = ""; | ||
name: "Persist", | ||
onInit: u, | ||
onEmit: ({ newState: l, cubit: g }) => { | ||
const d = e ?? g.id, I = JSON.stringify({ v: l }); | ||
I !== h && (b(i).setItem(r(d), I), h = I); | ||
onInit: _, | ||
onEmit: ({ newState: l, cubit: f }) => { | ||
const d = e ?? f._id, E = JSON.stringify({ v: l }); | ||
E !== h && (I(i).setItem(r(d), E), h = E); | ||
} | ||
@@ -327,10 +328,10 @@ }; | ||
export { | ||
E as Blac, | ||
f as BlacLifecycleEvent, | ||
D as BlacObservable, | ||
y as Bloc, | ||
p as BlocBase, | ||
v as Cubit, | ||
M as Persist | ||
p as Blac, | ||
u as BlacLifecycleEvent, | ||
S as BlacObservable, | ||
v as Bloc, | ||
g as BlocBase, | ||
A as Cubit, | ||
y as Persist | ||
}; | ||
//# sourceMappingURL=index.es.js.map |
{ | ||
"name": "blac", | ||
"version": "1.0.27", | ||
"version": "1.0.29", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "index.cjs.js", |
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
76223
564