@blac/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/blac.mjs ADDED
@@ -0,0 +1,279 @@
1
+ var _ = /* @__PURE__ */ ((o) => (o.BLOC_DISPOSED = "BLOC_DISPOSED", o.BLOC_CREATED = "BLOC_CREATED", o.LISTENER_REMOVED = "LISTENER_REMOVED", o.LISTENER_ADDED = "LISTENER_ADDED", o.STATE_CHANGED = "STATE_CHANGED", o.BLOC_CONSUMER_REMOVED = "BLOC_CONSUMER_REMOVED", o.BLOC_CONSUMER_ADDED = "BLOC_CONSUMER_ADDED", o))(_ || {});
2
+ const a = class a {
3
+ constructor(s = {}) {
4
+ this.createdAt = Date.now(), this.blocInstanceMap = /* @__PURE__ */ new Map(), this.isolatedBlocMap = /* @__PURE__ */ new Map(), this.pluginList = [], this.postChangesToDocument = !1, this.dispatchEvent = (t) => {
5
+ this.log("Broadcast signal", t), Array.from(this.blocInstanceMap.values()).forEach((i) => {
6
+ var r;
7
+ (r = i._onEvent) == null || r.call(i, t);
8
+ });
9
+ }, this.log = (...t) => {
10
+ a.enableLog && console.warn(`☢️ [Blac ${this.createdAt}]`, ...t);
11
+ }, this.addPlugin = (t) => {
12
+ this.pluginList.findIndex((i) => i.name === t.name) === -1 && (this.log("Add plugin", t.name), this.pluginList.push(t));
13
+ }, this.reportToPlugins = (t, n, i) => {
14
+ this.pluginList.forEach((r) => {
15
+ r.onEvent(t, n, i);
16
+ });
17
+ }, this.report = (t, n, i) => {
18
+ switch (this.log(t, n, i), t) {
19
+ case "BLOC_DISPOSED":
20
+ this.disposeBloc(n);
21
+ break;
22
+ case "BLOC_CONSUMER_REMOVED":
23
+ n._consumers.size === 0 && n._observer.size === 0 && !n._keepAlive && n._dispose();
24
+ break;
25
+ }
26
+ this.reportToPlugins(t, n, i);
27
+ }, this.disposeBloc = (t) => {
28
+ const n = t.constructor;
29
+ t._isBlacLive = !1, n.isolated ? this.unregisterIsolatedBlocInstance(t) : this.unregisterBlocInstance(t);
30
+ }, this.getBloc = (t, n = {}) => {
31
+ const i = t.isolated, r = n.id || t.name, c = i ? this.findIsolatedBlocInstance(t, r) : this.findRegisteredBlocInstance(t, r);
32
+ return c || this.createNewBlocInstance(
33
+ t,
34
+ r,
35
+ n.props,
36
+ n.instanceRef
37
+ );
38
+ }, this.getAllBlocs = (t, n = {}) => {
39
+ const i = t, { searchIsolated: r = i.isolated } = n;
40
+ if (r) {
41
+ const c = this.isolatedBlocMap.get(t);
42
+ if (c) return c;
43
+ } else
44
+ return Array.from(this.blocInstanceMap.values()).filter((d) => d instanceof t);
45
+ return [];
46
+ };
47
+ const { __unsafe_ignore_singleton: e = !1 } = s;
48
+ if (a.instance && !e)
49
+ return a.instance;
50
+ a.instance = this;
51
+ }
52
+ static getInstance() {
53
+ return a.instance;
54
+ }
55
+ resetInstance() {
56
+ this.log("Reset Blac instance"), a.instance = new a({
57
+ __unsafe_ignore_singleton: !0
58
+ });
59
+ }
60
+ createBlocInstanceMapKey(s, e) {
61
+ return `${s}:${e}`;
62
+ }
63
+ unregisterBlocInstance(s) {
64
+ const e = this.createBlocInstanceMapKey(s._name, s._id);
65
+ this.blocInstanceMap.delete(e);
66
+ }
67
+ registerBlocInstance(s) {
68
+ const e = this.createBlocInstanceMapKey(s._name, s._id);
69
+ this.blocInstanceMap.set(e, s);
70
+ }
71
+ findRegisteredBlocInstance(s, e) {
72
+ if (s.isolated) return;
73
+ const n = this.createBlocInstanceMapKey(s.name, e);
74
+ return this.blocInstanceMap.get(n);
75
+ }
76
+ registerIsolatedBlocInstance(s) {
77
+ const e = s.constructor, t = this.isolatedBlocMap.get(e);
78
+ t ? t.push(s) : this.isolatedBlocMap.set(e, [s]);
79
+ }
80
+ unregisterIsolatedBlocInstance(s) {
81
+ const e = s.constructor, t = this.isolatedBlocMap.get(e);
82
+ if (t) {
83
+ const n = t.findIndex((i) => i._id === s._id);
84
+ t.splice(n, 1), t.length === 0 && this.isolatedBlocMap.delete(e);
85
+ }
86
+ }
87
+ findIsolatedBlocInstance(s, e) {
88
+ const t = this.isolatedBlocMap.get(s);
89
+ if (t)
90
+ return t.find((n) => n._id === e);
91
+ }
92
+ createNewBlocInstance(s, e, t, n) {
93
+ const i = s, r = new s(t);
94
+ return r._instanceRef = n, r.props = t || null, r._updateId(e), i.isolated ? (this.registerIsolatedBlocInstance(r), r) : (this.registerBlocInstance(r), r);
95
+ }
96
+ };
97
+ a.instance = new a(), a.getAllBlocs = a.instance.getAllBlocs, a.addPlugin = a.instance.addPlugin, a.dispatchEvent = a.instance.dispatchEvent, a.enableLog = !1, a.warn = (s, ...e) => {
98
+ console.warn(`🚨 [Blac ${a.instance.createdAt}]`, s, ...e);
99
+ }, a.error = (s, ...e) => {
100
+ console.error(`🚨 [Blac ${a.instance.createdAt}]`, s, ...e);
101
+ }, a.getBloc = a.instance.getBloc;
102
+ let f = a;
103
+ class S {
104
+ constructor(s) {
105
+ this._observers = /* @__PURE__ */ new Set(), this.bloc = s;
106
+ }
107
+ get size() {
108
+ return this._observers.size;
109
+ }
110
+ get observers() {
111
+ return this._observers;
112
+ }
113
+ subscribe(s) {
114
+ return this._observers.add(s), f.instance.report(_.LISTENER_ADDED, this.bloc), s.lastState || (s.lastState = s.dependencyArray ? s.dependencyArray(this.bloc.state, this.bloc.state) : []), () => this.unsubscribe(s);
115
+ }
116
+ unsubscribe(s) {
117
+ this._observers.delete(s), f.instance.report(_.LISTENER_REMOVED, this.bloc);
118
+ }
119
+ notify(s, e, t) {
120
+ this._observers.forEach((n) => {
121
+ let i = !1;
122
+ if (n.dependencyArray) {
123
+ let r = n.lastState || [];
124
+ const c = n.dependencyArray(s, e);
125
+ for (let d = 0; d < c.length; d++) {
126
+ const E = c[d], l = r[d] || [];
127
+ for (let h = 0; h < E.length; h++)
128
+ if (E[h] !== l[h]) {
129
+ i = !0;
130
+ break;
131
+ }
132
+ }
133
+ n.lastState = c;
134
+ } else
135
+ i = !0;
136
+ if (i)
137
+ return n.fn(s, e, t);
138
+ });
139
+ }
140
+ dispose() {
141
+ this._observers.clear();
142
+ }
143
+ }
144
+ const p = class p {
145
+ constructor(s) {
146
+ this._isolated = !1, this._isBlacLive = !0, this._blac = f.getInstance(), this._keepAlive = !1, this._createdAt = Date.now(), this.props = null, this._updateId = (e) => {
147
+ const t = this._id;
148
+ !e || e === t || (this._id = e);
149
+ }, this._consumers = /* @__PURE__ */ new Set(), this._addConsumer = (e) => {
150
+ this._consumers.add(e), this._blac.report(_.BLOC_CONSUMER_ADDED, this, {
151
+ consumerId: e
152
+ });
153
+ }, this._removeConsumer = (e) => {
154
+ this._consumers.delete(e), this._blac.report(_.BLOC_CONSUMER_REMOVED, this, {
155
+ consumerId: e
156
+ });
157
+ }, this._connectAddons = () => {
158
+ const { _addons: e } = this;
159
+ if (e)
160
+ for (const t of e)
161
+ t.onEmit && this._observer.subscribe({
162
+ fn: (n, i) => {
163
+ var r;
164
+ (r = t.onEmit) == null || r.call(t, {
165
+ newState: n,
166
+ oldState: i,
167
+ cubit: this
168
+ });
169
+ },
170
+ passive: !0,
171
+ id: t.name
172
+ }), t.onInit && t.onInit(this);
173
+ }, this._pushState = (e, t, n) => {
174
+ this._state = e, this._oldState = t, this._observer.notify(e, t, n), this._blac.report(_.STATE_CHANGED, this, {
175
+ newState: e,
176
+ oldState: t
177
+ });
178
+ }, this._state = s, this._observer = new S(this), this._blac.report(_.BLOC_CREATED, this), this._id = this.constructor.name, this._isolated = this.constructor.isolated, this._addons = this.constructor.addons, this._keepAlive = this.constructor.keepAlive, this._connectAddons();
179
+ }
180
+ get state() {
181
+ return this._state;
182
+ }
183
+ get _name() {
184
+ return this.constructor.name;
185
+ }
186
+ _dispose() {
187
+ this._blac.report(_.BLOC_DISPOSED, this), this._observer.dispose();
188
+ }
189
+ _onEvent(s) {
190
+ }
191
+ };
192
+ p.isolated = !1, p.keepAlive = !1, p.isBlacClass = !0;
193
+ let g = p;
194
+ class O extends g {
195
+ /**
196
+ * Update the state then will notify all observers
197
+ * @param state: new state
198
+ **/
199
+ emit(s) {
200
+ if (Object.is(s, this.state))
201
+ return;
202
+ const e = this.state, t = s;
203
+ this._pushState(t, e);
204
+ }
205
+ /**
206
+ * Merges current state object with the parameter "state" and emits the new state
207
+ * Warning: only works when the state is an object.
208
+ * @param state: Partial state that should change
209
+ **/
210
+ patch(s, e = !1) {
211
+ let t = !1;
212
+ if (!e)
213
+ for (const n in s) {
214
+ const i = this.state[n];
215
+ if (!Object.is(s[n], i)) {
216
+ t = !0;
217
+ break;
218
+ }
219
+ }
220
+ t && this.emit({ ...this.state, ...s });
221
+ }
222
+ }
223
+ class A extends g {
224
+ constructor() {
225
+ super(...arguments), this.add = (s) => {
226
+ const e = this.state, t = this.reducer(s, this.state);
227
+ this._pushState(t, e, s);
228
+ };
229
+ }
230
+ }
231
+ function D(o) {
232
+ switch (o) {
233
+ case "localStorage":
234
+ return localStorage;
235
+ case "sessionStorage":
236
+ return sessionStorage;
237
+ default:
238
+ return localStorage;
239
+ }
240
+ }
241
+ function B(o = {}) {
242
+ const {
243
+ keyPrefix: s = "blac",
244
+ keyName: e,
245
+ defaultValue: t,
246
+ storageType: n = "localStorage"
247
+ } = o, i = (l) => `${s}:${l}`, r = (l) => {
248
+ const h = D(n).getItem(i(l));
249
+ if (typeof h != "string")
250
+ return t;
251
+ try {
252
+ const u = JSON.parse(h);
253
+ return typeof u.v < "u" ? u.v : t;
254
+ } catch {
255
+ return t;
256
+ }
257
+ }, c = (l) => {
258
+ const h = e ?? l._id, u = r(h);
259
+ typeof u !== void 0 && l._pushState(u, null);
260
+ };
261
+ let d = "";
262
+ return {
263
+ name: "Persist",
264
+ onInit: c,
265
+ onEmit: ({ newState: l, cubit: h }) => {
266
+ const u = e ?? h._id, I = JSON.stringify({ v: l });
267
+ I !== d && (D(n).setItem(i(u), I), d = I);
268
+ }
269
+ };
270
+ }
271
+ export {
272
+ f as Blac,
273
+ _ as BlacLifecycleEvent,
274
+ S as BlacObservable,
275
+ A as Bloc,
276
+ g as BlocBase,
277
+ O as Cubit,
278
+ B as Persist
279
+ };
@@ -0,0 +1 @@
1
+ (function(l,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(l=typeof globalThis<"u"?globalThis:l||self,c(l.BlacCore={}))})(this,function(l){"use strict";var c=(r=>(r.BLOC_DISPOSED="BLOC_DISPOSED",r.BLOC_CREATED="BLOC_CREATED",r.LISTENER_REMOVED="LISTENER_REMOVED",r.LISTENER_ADDED="LISTENER_ADDED",r.STATE_CHANGED="STATE_CHANGED",r.BLOC_CONSUMER_REMOVED="BLOC_CONSUMER_REMOVED",r.BLOC_CONSUMER_ADDED="BLOC_CONSUMER_ADDED",r))(c||{});const o=class o{constructor(e={}){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(i=>{var a;(a=i._onEvent)==null||a.call(i,t)})},this.log=(...t)=>{o.enableLog&&console.warn(`☢️ [Blac ${this.createdAt}]`,...t)},this.addPlugin=t=>{this.pluginList.findIndex(i=>i.name===t.name)===-1&&(this.log("Add plugin",t.name),this.pluginList.push(t))},this.reportToPlugins=(t,n,i)=>{this.pluginList.forEach(a=>{a.onEvent(t,n,i)})},this.report=(t,n,i)=>{switch(this.log(t,n,i),t){case"BLOC_DISPOSED":this.disposeBloc(n);break;case"BLOC_CONSUMER_REMOVED":n._consumers.size===0&&n._observer.size===0&&!n._keepAlive&&n._dispose();break}this.reportToPlugins(t,n,i)},this.disposeBloc=t=>{const n=t.constructor;t._isBlacLive=!1,n.isolated?this.unregisterIsolatedBlocInstance(t):this.unregisterBlocInstance(t)},this.getBloc=(t,n={})=>{const i=t.isolated,a=n.id||t.name,h=i?this.findIsolatedBlocInstance(t,a):this.findRegisteredBlocInstance(t,a);return h||this.createNewBlocInstance(t,a,n.props,n.instanceRef)},this.getAllBlocs=(t,n={})=>{const i=t,{searchIsolated:a=i.isolated}=n;if(a){const h=this.isolatedBlocMap.get(t);if(h)return h}else return Array.from(this.blocInstanceMap.values()).filter(f=>f instanceof t);return[]};const{__unsafe_ignore_singleton:s=!1}=e;if(o.instance&&!s)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(e,s){return`${e}:${s}`}unregisterBlocInstance(e){const s=this.createBlocInstanceMapKey(e._name,e._id);this.blocInstanceMap.delete(s)}registerBlocInstance(e){const s=this.createBlocInstanceMapKey(e._name,e._id);this.blocInstanceMap.set(s,e)}findRegisteredBlocInstance(e,s){if(e.isolated)return;const n=this.createBlocInstanceMapKey(e.name,s);return this.blocInstanceMap.get(n)}registerIsolatedBlocInstance(e){const s=e.constructor,t=this.isolatedBlocMap.get(s);t?t.push(e):this.isolatedBlocMap.set(s,[e])}unregisterIsolatedBlocInstance(e){const s=e.constructor,t=this.isolatedBlocMap.get(s);if(t){const n=t.findIndex(i=>i._id===e._id);t.splice(n,1),t.length===0&&this.isolatedBlocMap.delete(s)}}findIsolatedBlocInstance(e,s){const t=this.isolatedBlocMap.get(e);if(t)return t.find(n=>n._id===s)}createNewBlocInstance(e,s,t,n){const i=e,a=new e(t);return a._instanceRef=n,a.props=t||null,a._updateId(s),i.isolated?(this.registerIsolatedBlocInstance(a),a):(this.registerBlocInstance(a),a)}};o.instance=new o,o.getAllBlocs=o.instance.getAllBlocs,o.addPlugin=o.instance.addPlugin,o.dispatchEvent=o.instance.dispatchEvent,o.enableLog=!1,o.warn=(e,...s)=>{console.warn(`🚨 [Blac ${o.instance.createdAt}]`,e,...s)},o.error=(e,...s)=>{console.error(`🚨 [Blac ${o.instance.createdAt}]`,e,...s)},o.getBloc=o.instance.getBloc;let p=o;class B{constructor(e){this._observers=new Set,this.bloc=e}get size(){return this._observers.size}get observers(){return this._observers}subscribe(e){return this._observers.add(e),p.instance.report(c.LISTENER_ADDED,this.bloc),e.lastState||(e.lastState=e.dependencyArray?e.dependencyArray(this.bloc.state,this.bloc.state):[]),()=>this.unsubscribe(e)}unsubscribe(e){this._observers.delete(e),p.instance.report(c.LISTENER_REMOVED,this.bloc)}notify(e,s,t){this._observers.forEach(n=>{let i=!1;if(n.dependencyArray){let a=n.lastState||[];const h=n.dependencyArray(e,s);for(let f=0;f<h.length;f++){const I=h[f],u=a[f]||[];for(let d=0;d<I.length;d++)if(I[d]!==u[d]){i=!0;break}}n.lastState=h}else i=!0;if(i)return n.fn(e,s,t)})}dispose(){this._observers.clear()}}const E=class E{constructor(e){this._isolated=!1,this._isBlacLive=!0,this._blac=p.getInstance(),this._keepAlive=!1,this._createdAt=Date.now(),this.props=null,this._updateId=s=>{const t=this._id;!s||s===t||(this._id=s)},this._consumers=new Set,this._addConsumer=s=>{this._consumers.add(s),this._blac.report(c.BLOC_CONSUMER_ADDED,this,{consumerId:s})},this._removeConsumer=s=>{this._consumers.delete(s),this._blac.report(c.BLOC_CONSUMER_REMOVED,this,{consumerId:s})},this._connectAddons=()=>{const{_addons:s}=this;if(s)for(const t of s)t.onEmit&&this._observer.subscribe({fn:(n,i)=>{var a;(a=t.onEmit)==null||a.call(t,{newState:n,oldState:i,cubit:this})},passive:!0,id:t.name}),t.onInit&&t.onInit(this)},this._pushState=(s,t,n)=>{this._state=s,this._oldState=t,this._observer.notify(s,t,n),this._blac.report(c.STATE_CHANGED,this,{newState:s,oldState:t})},this._state=e,this._observer=new B(this),this._blac.report(c.BLOC_CREATED,this),this._id=this.constructor.name,this._isolated=this.constructor.isolated,this._addons=this.constructor.addons,this._keepAlive=this.constructor.keepAlive,this._connectAddons()}get state(){return this._state}get _name(){return this.constructor.name}_dispose(){this._blac.report(c.BLOC_DISPOSED,this),this._observer.dispose()}_onEvent(e){}};E.isolated=!1,E.keepAlive=!1,E.isBlacClass=!0;let g=E;class O extends g{emit(e){if(Object.is(e,this.state))return;const s=this.state,t=e;this._pushState(t,s)}patch(e,s=!1){let t=!1;if(!s)for(const n in e){const i=this.state[n];if(!Object.is(e[n],i)){t=!0;break}}t&&this.emit({...this.state,...e})}}class A extends g{constructor(){super(...arguments),this.add=e=>{const s=this.state,t=this.reducer(e,this.state);this._pushState(t,s,e)}}}function D(r){switch(r){case"localStorage":return localStorage;case"sessionStorage":return sessionStorage;default:return localStorage}}function b(r={}){const{keyPrefix:e="blac",keyName:s,defaultValue:t,storageType:n="localStorage"}=r,i=u=>`${e}:${u}`,a=u=>{const d=D(n).getItem(i(u));if(typeof d!="string")return t;try{const _=JSON.parse(d);return typeof _.v<"u"?_.v:t}catch{return t}},h=u=>{const d=s??u._id,_=a(d);typeof _!==void 0&&u._pushState(_,null)};let f="";return{name:"Persist",onInit:h,onEmit:({newState:u,cubit:d})=>{const _=s??d._id,S=JSON.stringify({v:u});S!==f&&(D(n).setItem(i(_),S),f=S)}}}l.Blac=p,l.BlacLifecycleEvent=c,l.BlacObservable=B,l.Bloc=A,l.BlocBase=g,l.Cubit=O,l.Persist=b,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@blac/core",
3
+ "version": "0.1.0",
4
+ "files": [
5
+ "dist"
6
+ ],
7
+ "main": "./dist/blac.umd.js",
8
+ "module": "./dist/blac.mjs",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/blac.mjs",
12
+ "require": "./dist/blac.umd.js"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "build": "vite build"
17
+ },
18
+ "keywords": [],
19
+ "author": "",
20
+ "license": "ISC",
21
+ "description": "",
22
+ "devDependencies": {
23
+ "vite": "^6.0.5"
24
+ }
25
+ }