@blac/core 2.0.0-rc-1 → 2.0.0-rc-3

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/index.es.js CHANGED
@@ -1,111 +1,71 @@
1
1
  var B = Object.defineProperty;
2
- var m = (c, t, e) => t in c ? B(c, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : c[t] = e;
3
- var n = (c, t, e) => m(c, typeof t != "symbol" ? t + "" : t, e);
4
- var _ = /* @__PURE__ */ ((c) => (c.BLOC_DISPOSED = "BLOC_DISPOSED", c.BLOC_CREATED = "BLOC_CREATED", c.LISTENER_REMOVED = "LISTENER_REMOVED", c.LISTENER_ADDED = "LISTENER_ADDED", c.STATE_CHANGED = "STATE_CHANGED", c.BLOC_CONSUMER_REMOVED = "BLOC_CONSUMER_REMOVED", c.BLOC_CONSUMER_ADDED = "BLOC_CONSUMER_ADDED", c))(_ || {});
5
- const r = class r {
2
+ var I = (d, e, t) => e in d ? B(d, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : d[e] = t;
3
+ var s = (d, e, t) => I(d, typeof e != "symbol" ? e + "" : e, t);
4
+ const o = class o {
6
5
  /**
7
6
  * Creates a new Blac instance.
8
7
  * @param options - Configuration options including singleton control
9
8
  */
10
- constructor(t = {}) {
9
+ constructor(e = {}) {
11
10
  /** Timestamp when the instance was created */
12
- n(this, "createdAt", Date.now());
11
+ s(this, "createdAt", Date.now());
13
12
  /** Map storing all registered bloc instances by their class name and ID */
14
- n(this, "blocInstanceMap", /* @__PURE__ */ new Map());
13
+ s(this, "blocInstanceMap", /* @__PURE__ */ new Map());
15
14
  /** Map storing isolated bloc instances grouped by their constructor */
16
- n(this, "isolatedBlocMap", /* @__PURE__ */ new Map());
17
- n(this, "pluginList", []);
15
+ s(this, "isolatedBlocMap", /* @__PURE__ */ new Map());
18
16
  /** Flag to control whether changes should be posted to document */
19
- n(this, "postChangesToDocument", !1);
17
+ s(this, "postChangesToDocument", !1);
20
18
  /**
21
19
  * Logs messages to console when logging is enabled
22
20
  * @param args - Arguments to log
23
21
  */
24
- n(this, "log", (...t) => {
25
- r.enableLog && console.warn(`☢️ [Blac ${this.createdAt.toString()}]`, ...t);
22
+ s(this, "log", (...e) => {
23
+ o.enableLog && console.warn(`☢️ [Blac ${this.createdAt.toString()}]`, ...e);
26
24
  });
27
25
  /**
28
26
  * Logs a warning message
29
27
  * @param message - Warning message
30
28
  * @param args - Additional arguments
31
29
  */
32
- n(this, "warn", (t, ...e) => {
33
- r.enableLog && console.warn(`🚨 [Blac ${String(r.instance.createdAt)}]`, t, ...e);
30
+ s(this, "warn", (e, ...t) => {
31
+ o.enableLog && console.warn(`🚨 [Blac ${String(o.instance.createdAt)}]`, e, ...t);
34
32
  });
35
33
  /**
36
34
  * Logs an error message
37
35
  * @param message - Error message
38
36
  * @param args - Additional arguments
39
37
  */
40
- n(this, "error", (t, ...e) => {
41
- r.enableLog && console.error(`🚨 [Blac ${String(r.instance.createdAt)}]`, t, ...e);
38
+ s(this, "error", (e, ...t) => {
39
+ o.enableLog && console.error(`🚨 [Blac ${String(o.instance.createdAt)}]`, e, ...t);
42
40
  });
43
41
  /**
44
42
  * Resets the Blac instance to a new one, disposing non-keepAlive blocs
45
43
  * from the old instance.
46
44
  */
47
- n(this, "resetInstance", () => {
45
+ s(this, "resetInstance", () => {
48
46
  this.log("Reset Blac instance");
49
- const t = new Map(this.blocInstanceMap), e = new Map(this.isolatedBlocMap);
50
- t.forEach((s) => {
51
- s._dispose();
52
- }), e.forEach((s) => {
53
- s.forEach((i) => {
47
+ const e = new Map(this.blocInstanceMap), t = new Map(this.isolatedBlocMap);
48
+ e.forEach((n) => {
49
+ n._dispose();
50
+ }), t.forEach((n) => {
51
+ n.forEach((i) => {
54
52
  i._dispose();
55
53
  });
56
- }), this.blocInstanceMap.clear(), this.isolatedBlocMap.clear(), r.instance = new r({
54
+ }), this.blocInstanceMap.clear(), this.isolatedBlocMap.clear(), o.instance = new o({
57
55
  __unsafe_ignore_singleton: !0
58
56
  });
59
57
  });
60
- /**
61
- * Adds a plugin to the Blac instance
62
- * @param plugin - The plugin to add
63
- */
64
- n(this, "addPlugin", (t) => {
65
- this.pluginList.findIndex((s) => s.name === t.name) === -1 && (this.log("Add plugin", t.name), this.pluginList.push(t));
66
- });
67
- /**
68
- * Dispatches a lifecycle event to all registered plugins
69
- * @param event - The lifecycle event to dispatch
70
- * @param bloc - The bloc instance involved in the event
71
- * @param params - Additional parameters for the event
72
- */
73
- n(this, "dispatchEventToPlugins", (t, e, s) => {
74
- this.pluginList.forEach((i) => {
75
- i.onEvent(t, e, s);
76
- });
77
- });
78
- /**
79
- * Dispatches a lifecycle event and handles related cleanup actions.
80
- * This method is responsible for:
81
- * - Logging the event
82
- * - Handling bloc disposal when needed
83
- * - Managing bloc consumer cleanup
84
- * - Forwarding the event to plugins
85
- *
86
- * @param event - The lifecycle event to dispatch
87
- * @param bloc - The bloc instance involved in the event
88
- * @param params - Additional parameters for the event
89
- */
90
- n(this, "dispatchEvent", (t, e, s) => {
91
- switch (this.log(t, e, s), t) {
92
- case "BLOC_DISPOSED":
93
- this.disposeBloc(e);
94
- break;
95
- case "BLOC_CONSUMER_REMOVED":
96
- case "LISTENER_REMOVED":
97
- this.log(`[${e._name}:${String(e._id)}] Listener/Consumer removed. Listeners: ${String(e._observer.size)}, Consumers: ${String(e._consumers.size)}, KeepAlive: ${String(e._keepAlive)}`), e._observer.size === 0 && e._consumers.size === 0 && !e._keepAlive && (this.log(`[${e._name}:${String(e._id)}] No listeners or consumers left and not keepAlive. Disposing.`), e._dispose());
98
- break;
99
- }
100
- this.dispatchEventToPlugins(t, e, s);
101
- });
102
58
  /**
103
59
  * Disposes of a bloc instance by removing it from the appropriate registry
104
60
  * @param bloc - The bloc instance to dispose
105
61
  */
106
- n(this, "disposeBloc", (t) => {
107
- const e = t.constructor, s = this.createBlocInstanceMapKey(t._name, t._id);
108
- this.log(`[${t._name}:${String(t._id)}] disposeBloc called. Isolated: ${String(e.isolated)}`), e.isolated ? (this.unregisterIsolatedBlocInstance(t), this.blocInstanceMap.delete(s)) : this.unregisterBlocInstance(t), this.dispatchEventToPlugins("BLOC_DISPOSED", t);
62
+ s(this, "disposeBloc", (e) => {
63
+ const t = e.constructor, n = this.createBlocInstanceMapKey(e._name, e._id);
64
+ this.log(`[${e._name}:${String(e._id)}] disposeBloc called. Isolated: ${String(t.isolated)}`), t.isolated ? (this.unregisterIsolatedBlocInstance(e), this.blocInstanceMap.delete(n)) : this.unregisterBlocInstance(e), this.log("dispatched bloc", e);
65
+ });
66
+ s(this, "activateBloc", (e) => {
67
+ const t = e.constructor;
68
+ (e.isIsolated ? this.findIsolatedBlocInstance(t, e._id) : this.findRegisteredBlocInstance(t, e._id)) || (this.log(`[${e._name}:${String(e._id)}] activateBloc called. Isolated: ${String(e.isIsolated)}`), e.isIsolated ? this.registerIsolatedBlocInstance(e) : this.registerBlocInstance(e));
109
69
  });
110
70
  /**
111
71
  * Gets or creates a bloc instance based on the provided class and options.
@@ -118,23 +78,27 @@ const r = class r {
118
78
  * - instanceRef: Optional reference string for the instance
119
79
  * @returns The bloc instance
120
80
  */
121
- n(this, "getBloc", (t, e = {}) => {
122
- const { id: s } = e, i = t, a = s ?? t.name;
123
- if (this.log(`[${t.name}:${String(a)}] getBloc called. Options:`, e), i.isolated) {
124
- const o = this.findIsolatedBlocInstance(t, a);
125
- if (o)
126
- return this.log(`[${t.name}:${String(a)}] Found existing isolated instance.`), o;
127
- }
128
- if (!i.isolated) {
129
- const o = this.findRegisteredBlocInstance(t, a);
130
- if (o)
131
- return this.log(`[${t.name}:${String(a)}] Found existing registered instance.`), o;
81
+ s(this, "getBloc", (e, t = {}) => {
82
+ const { id: n } = t, i = e, a = n ?? e.name;
83
+ if (i.isolated) {
84
+ const l = this.findIsolatedBlocInstance(e, a);
85
+ if (l)
86
+ return this.log(`[${e.name}:${String(a)}] (getBloc) Found existing isolated instance.`, t), l;
87
+ if (t.throwIfNotFound)
88
+ throw new Error(`Isolated bloc ${e.name} not found`);
89
+ } else {
90
+ const l = this.findRegisteredBlocInstance(e, a);
91
+ if (l)
92
+ return this.log(`[${e.name}:${String(a)}] (getBloc) Found existing registered instance.`, t), l;
93
+ if (t.throwIfNotFound)
94
+ throw new Error(`Registered bloc ${e.name} not found`);
132
95
  }
133
- return this.log(`[${t.name}:${String(a)}] No existing instance found. Creating new one.`), this.createNewBlocInstance(
134
- t,
96
+ const r = this.createNewBlocInstance(
97
+ e,
135
98
  a,
136
- e
99
+ t
137
100
  );
101
+ return this.log(`[${e.name}:${String(a)}] (getBloc) No existing instance found. Creating new one.`, t, r), r;
138
102
  });
139
103
  /**
140
104
  * Gets a bloc instance or throws an error if it doesn't exist
@@ -144,11 +108,11 @@ const r = class r {
144
108
  * - props: Properties to pass to the bloc constructor
145
109
  * - instanceRef: Optional reference string for the instance
146
110
  */
147
- n(this, "getBlocOrThrow", (t, e = {}) => {
148
- const s = t.isolated, i = e.id || t.name, a = s ? this.findIsolatedBlocInstance(t, i) : this.findRegisteredBlocInstance(t, i);
111
+ s(this, "getBlocOrThrow", (e, t = {}) => {
112
+ const n = e.isolated, i = t.id || e.name, a = n ? this.findIsolatedBlocInstance(e, i) : this.findRegisteredBlocInstance(e, i);
149
113
  if (a)
150
114
  return a;
151
- throw new Error(`Bloc ${t.name} not found`);
115
+ throw new Error(`Bloc ${e.name} not found`);
152
116
  });
153
117
  /**
154
118
  * Gets all instances of a specific bloc class
@@ -157,27 +121,27 @@ const r = class r {
157
121
  * - searchIsolated: Whether to search in isolated blocs (defaults to bloc's isolated property)
158
122
  * @returns Array of matching bloc instances
159
123
  */
160
- n(this, "getAllBlocs", (t, e = {}) => {
161
- const s = [];
124
+ s(this, "getAllBlocs", (e, t = {}) => {
125
+ const n = [];
162
126
  if (this.blocInstanceMap.forEach((i) => {
163
- i.constructor === t && s.push(i);
164
- }), e.searchIsolated !== !1) {
165
- const i = this.isolatedBlocMap.get(t);
166
- i && s.push(...i.map((a) => a));
127
+ i.constructor === e && n.push(i);
128
+ }), t.searchIsolated !== !1) {
129
+ const i = this.isolatedBlocMap.get(e);
130
+ i && n.push(...i.map((a) => a));
167
131
  }
168
- return s;
132
+ return n;
169
133
  });
170
- const { __unsafe_ignore_singleton: e = !1 } = t;
171
- if (!e)
172
- return r.instance;
173
- r.instance = this;
134
+ const { __unsafe_ignore_singleton: t = !1 } = e;
135
+ if (!t)
136
+ return o.instance;
137
+ o.instance = this;
174
138
  }
175
139
  /**
176
140
  * Gets the singleton instance of Blac
177
141
  * @returns The Blac instance
178
142
  */
179
143
  static getInstance() {
180
- return r.instance;
144
+ return o.instance;
181
145
  }
182
146
  /**
183
147
  * Creates a unique key for a bloc instance in the map based on the bloc class name and instance ID
@@ -185,24 +149,24 @@ const r = class r {
185
149
  * @param id - The instance ID
186
150
  * @returns A unique key string in the format "className:id"
187
151
  */
188
- createBlocInstanceMapKey(t, e) {
189
- return `${t}:${String(e)}`;
152
+ createBlocInstanceMapKey(e, t) {
153
+ return `${e}:${String(t)}`;
190
154
  }
191
155
  /**
192
156
  * Unregister a bloc instance from the main registry
193
157
  * @param bloc - The bloc instance to unregister
194
158
  */
195
- unregisterBlocInstance(t) {
196
- const e = this.createBlocInstanceMapKey(t._name, t._id);
197
- this.blocInstanceMap.delete(e);
159
+ unregisterBlocInstance(e) {
160
+ const t = this.createBlocInstanceMapKey(e._name, e._id);
161
+ this.blocInstanceMap.delete(t);
198
162
  }
199
163
  /**
200
164
  * Registers a bloc instance in the main registry
201
165
  * @param bloc - The bloc instance to register
202
166
  */
203
- registerBlocInstance(t) {
204
- const e = this.createBlocInstanceMapKey(t._name, t._id);
205
- this.blocInstanceMap.set(e, t);
167
+ registerBlocInstance(e) {
168
+ const t = this.createBlocInstanceMapKey(e._name, e._id);
169
+ this.blocInstanceMap.set(t, e);
206
170
  }
207
171
  /**
208
172
  * Finds a registered bloc instance by its class and ID
@@ -210,39 +174,37 @@ const r = class r {
210
174
  * @param id - The instance ID
211
175
  * @returns The found bloc instance or undefined if not found
212
176
  */
213
- findRegisteredBlocInstance(t, e) {
214
- if (t.isolated) return;
215
- const i = this.createBlocInstanceMapKey(t.name, e), a = this.blocInstanceMap.get(i);
216
- return a && this.log(`[${t.name}:${String(e)}] Found registered instance. Returning.`), a;
177
+ findRegisteredBlocInstance(e, t) {
178
+ if (e.isolated) return;
179
+ const i = this.createBlocInstanceMapKey(e.name, t);
180
+ return this.blocInstanceMap.get(i);
217
181
  }
218
182
  /**
219
183
  * Registers an isolated bloc instance in the isolated registry
220
184
  * @param bloc - The isolated bloc instance to register
221
185
  */
222
- registerIsolatedBlocInstance(t) {
223
- const e = t.constructor, s = this.isolatedBlocMap.get(e);
224
- s ? s.push(t) : this.isolatedBlocMap.set(e, [t]);
186
+ registerIsolatedBlocInstance(e) {
187
+ const t = e.constructor, n = this.isolatedBlocMap.get(t);
188
+ n ? n.push(e) : this.isolatedBlocMap.set(t, [e]);
225
189
  }
226
190
  /**
227
191
  * Unregister an isolated bloc instance from the isolated registry
228
192
  * @param bloc - The isolated bloc instance to unregister
229
193
  */
230
- unregisterIsolatedBlocInstance(t) {
231
- const e = t.constructor, s = this.isolatedBlocMap.get(e);
232
- if (s) {
233
- const i = s.findIndex((a) => a._id === t._id);
234
- i !== -1 && s.splice(i, 1), s.length === 0 && this.isolatedBlocMap.delete(e);
194
+ unregisterIsolatedBlocInstance(e) {
195
+ const t = e.constructor, n = this.isolatedBlocMap.get(t);
196
+ if (n) {
197
+ const i = n.findIndex((a) => a._id === e._id);
198
+ i !== -1 && n.splice(i, 1), n.length === 0 && this.isolatedBlocMap.delete(t);
235
199
  }
236
200
  }
237
201
  /**
238
202
  * Finds an isolated bloc instance by its class and ID
239
203
  */
240
- findIsolatedBlocInstance(t, e) {
241
- if (!t.isolated) return;
242
- const i = this.isolatedBlocMap.get(t);
243
- if (!i) return;
244
- const a = i.find((o) => o._id === e);
245
- return a && this.log(`[${t.name}:${String(e)}] Found isolated instance. Returning.`), a;
204
+ findIsolatedBlocInstance(e, t) {
205
+ if (!e.isolated) return;
206
+ const i = this.isolatedBlocMap.get(e);
207
+ return i ? i.find((r) => r._id === t) : void 0;
246
208
  }
247
209
  /**
248
210
  * Creates a new bloc instance and registers it in the appropriate registry
@@ -252,33 +214,32 @@ const r = class r {
252
214
  * @param instanceRef - Optional reference string for the instance
253
215
  * @returns The newly created bloc instance
254
216
  */
255
- createNewBlocInstance(t, e, s = {}) {
256
- const { props: i, instanceRef: a } = s, o = new t(i);
257
- return o._instanceRef = a, o.props = i || null, o._updateId(e), o.isIsolated ? (this.registerIsolatedBlocInstance(o), o) : (this.registerBlocInstance(o), o);
217
+ createNewBlocInstance(e, t, n = {}) {
218
+ const { props: i, instanceRef: a } = n, r = new e(i);
219
+ return r._instanceRef = a, r.props = i || null, r._updateId(t), r.isIsolated ? (this.registerIsolatedBlocInstance(r), r) : (this.registerBlocInstance(r), r);
258
220
  }
259
221
  };
260
222
  /** The singleton instance of Blac */
261
- n(r, "instance", new r()), n(r, "getAllBlocs", r.instance.getAllBlocs), n(r, "addPlugin", r.instance.addPlugin), /** Flag to enable/disable logging */
262
- n(r, "enableLog", !1), n(r, "log", r.instance.log), n(r, "warn", r.instance.warn), n(r, "error", r.instance.error), n(r, "resetInstance", r.instance.resetInstance), n(r, "getBloc", r.instance.getBloc), n(r, "getBlocOrThrow", r.instance.getBlocOrThrow);
263
- let h = r;
264
- class D {
223
+ s(o, "instance", new o()), s(o, "getAllBlocs", o.instance.getAllBlocs), /** Flag to enable/disable logging */
224
+ s(o, "enableLog", !1), s(o, "log", o.instance.log), s(o, "warn", o.instance.warn), s(o, "error", o.instance.error), s(o, "resetInstance", o.instance.resetInstance), s(o, "activateBloc", o.instance.activateBloc), s(o, "getBloc", o.instance.getBloc), s(o, "getBlocOrThrow", o.instance.getBlocOrThrow);
225
+ let c = o;
226
+ class _ {
265
227
  /**
266
228
  * Creates a new BlacObservable instance
267
229
  * @param bloc - The Bloc instance to observe
268
230
  */
269
- constructor(t) {
231
+ constructor(e) {
270
232
  /** The Bloc instance this observable is associated with */
271
- n(this, "bloc");
272
- // private _observers = new Set<BlacObserver<S>>();
273
- n(this, "_observers", []);
274
- this.bloc = t;
233
+ s(this, "bloc");
234
+ s(this, "_observers", /* @__PURE__ */ new Set());
235
+ this.bloc = e;
275
236
  }
276
237
  /**
277
238
  * Gets the number of active observers
278
239
  * @returns The number of observers currently subscribed
279
240
  */
280
241
  get size() {
281
- return this._observers.length;
242
+ return this._observers.size;
282
243
  }
283
244
  /**
284
245
  * Gets the set of all observers
@@ -292,17 +253,17 @@ class D {
292
253
  * @param observer - The observer to subscribe
293
254
  * @returns A function that can be called to unsubscribe the observer
294
255
  */
295
- subscribe(t) {
296
- return this._observers.push(t), h.instance.dispatchEvent(_.LISTENER_ADDED, this.bloc, { listenerId: t.id }), t.lastState || (t.lastState = t.dependencyArray ? t.dependencyArray(this.bloc.state) : []), () => {
297
- this.unsubscribe(t);
256
+ subscribe(e) {
257
+ return c.log("BlacObservable.subscribe: Subscribing observer.", this.bloc, e), this._observers.add(e), e.lastState || (e.lastState = e.dependencyArray ? e.dependencyArray(this.bloc.state) : []), () => {
258
+ c.log("BlacObservable.subscribe: Unsubscribing observer.", this.bloc, e), this.unsubscribe(e);
298
259
  };
299
260
  }
300
261
  /**
301
262
  * Unsubscribes an observer from state changes
302
263
  * @param observer - The observer to unsubscribe
303
264
  */
304
- unsubscribe(t) {
305
- this._observers = this._observers.filter((e) => e.id !== t.id), h.instance.dispatchEvent(_.LISTENER_REMOVED, this.bloc, { listenerId: t.id });
265
+ unsubscribe(e) {
266
+ this._observers.delete(e), this.size === 0 && (c.log("BlacObservable.unsubscribe: No observers left. Disposing bloc.", this.bloc), this.bloc._dispose());
306
267
  }
307
268
  /**
308
269
  * Notifies all observers of a state change
@@ -310,103 +271,98 @@ class D {
310
271
  * @param oldState - The previous state value
311
272
  * @param action - Optional action that triggered the state change
312
273
  */
313
- notify(t, e, s) {
274
+ notify(e, t, n) {
314
275
  this._observers.forEach((i) => {
315
276
  let a = !1;
316
277
  if (i.dependencyArray) {
317
- const o = i.lastState || [], g = i.dependencyArray(t);
318
- for (let p = 0; p < g.length; p++) {
319
- const S = g[p], d = o[p] || [];
320
- for (let l = 0; l < S.length; l++)
321
- if (!Object.is(S[l], d[l])) {
278
+ const r = i.lastState || [], l = i.dependencyArray(e);
279
+ for (let h = 0; h < l.length; h++) {
280
+ const f = l[h], p = r[h] || [];
281
+ for (let u = 0; u < f.length; u++)
282
+ if (!Object.is(f[u], p[u])) {
322
283
  a = !0;
323
284
  break;
324
285
  }
325
286
  }
326
- i.lastState = g;
287
+ i.lastState = l;
327
288
  } else
328
289
  a = !0;
329
- a && i.fn(t, e, s);
290
+ a && i.fn(e, t, n);
330
291
  });
331
292
  }
332
293
  /**
333
- * Disposes of all observers and clears the observer set
294
+ * Clears the observer set
334
295
  */
335
- dispose() {
336
- this._observers.forEach((t) => {
337
- var e;
338
- this.unsubscribe(t), (e = t.dispose) == null || e.call(t);
339
- }), this._observers = [];
296
+ clear() {
297
+ this._observers.forEach((e) => {
298
+ this.unsubscribe(e);
299
+ }), this._observers.clear();
340
300
  }
341
301
  }
342
- class E {
302
+ class g {
343
303
  /**
344
304
  * Creates a new BlocBase instance with the given initial state.
345
305
  * Sets up the observer, registers with the Blac manager, and initializes addons.
346
306
  *
347
307
  * @param initialState The initial state value for this Bloc
348
308
  */
349
- constructor(t) {
309
+ constructor(e) {
310
+ s(this, "uid", crypto.randomUUID());
350
311
  /**
351
312
  * Defines how dependencies are selected from the state for efficient updates.
352
313
  * When provided, observers will only be notified when selected dependencies change.
353
314
  */
354
- n(this, "defaultDependencySelector");
355
- /**
356
- * @internal
357
- * Optional array of addons to extend the functionality of this Bloc.
358
- */
359
- n(this, "_addons");
315
+ s(this, "defaultDependencySelector");
360
316
  /**
361
317
  * @internal
362
318
  * Indicates if this specific Bloc instance is isolated from others of the same type.
363
319
  */
364
- n(this, "_isolated", !1);
320
+ s(this, "_isolated", !1);
365
321
  /**
366
322
  * @internal
367
323
  * Observable responsible for managing state listeners and notifying consumers.
368
324
  */
369
- n(this, "_observer");
325
+ s(this, "_observer");
370
326
  /**
371
327
  * @internal
372
328
  * Reference to the global Blac manager instance.
373
329
  */
374
- n(this, "_blac", h.getInstance());
330
+ s(this, "_blac", c.getInstance());
375
331
  /**
376
332
  * The unique identifier for this Bloc instance.
377
333
  * Defaults to the class name, but can be customized.
378
334
  */
379
- n(this, "_id");
335
+ s(this, "_id");
380
336
  /**
381
337
  * @internal
382
338
  * Reference string used internally for tracking and debugging.
383
339
  */
384
- n(this, "_instanceRef");
340
+ s(this, "_instanceRef");
385
341
  /**
386
342
  * @internal
387
343
  * Indicates if this specific Bloc instance should be kept alive when no consumers are present.
388
344
  */
389
- n(this, "_keepAlive", !1);
345
+ s(this, "_keepAlive", !1);
390
346
  /**
391
347
  * @readonly
392
348
  * Timestamp when this Bloc instance was created, useful for debugging and performance tracking.
393
349
  */
394
- n(this, "_createdAt", Date.now());
350
+ s(this, "_createdAt", Date.now());
395
351
  /**
396
352
  * @internal
397
353
  * The current state of the Bloc.
398
354
  */
399
- n(this, "_state");
355
+ s(this, "_state");
400
356
  /**
401
357
  * @internal
402
358
  * The previous state of the Bloc, maintained for comparison and history.
403
359
  */
404
- n(this, "_oldState");
360
+ s(this, "_oldState");
405
361
  /**
406
362
  * Props passed during Bloc instance creation.
407
363
  * Can be used to configure or parameterize the Bloc's behavior.
408
364
  */
409
- n(this, "props", null);
365
+ s(this, "props", null);
410
366
  /**
411
367
  * @internal
412
368
  * Updates the Bloc instance's ID to a new value.
@@ -414,20 +370,20 @@ class E {
414
370
  *
415
371
  * @param id The new ID to assign to this Bloc instance
416
372
  */
417
- n(this, "_updateId", (t) => {
418
- const e = this._id;
419
- !t || t === e || (this._id = t);
373
+ s(this, "_updateId", (e) => {
374
+ const t = this._id;
375
+ !e || e === t || (this._id = e);
420
376
  });
421
377
  /**
422
378
  * @internal
423
379
  * Optional function to be called when the Bloc is disposed.
424
380
  */
425
- n(this, "onDispose");
381
+ s(this, "onDispose");
426
382
  /**
427
383
  * @internal
428
384
  * Set of consumer IDs currently listening to this Bloc's state changes.
429
385
  */
430
- n(this, "_consumers", /* @__PURE__ */ new Set());
386
+ s(this, "_consumers", /* @__PURE__ */ new Set());
431
387
  /**
432
388
  * @internal
433
389
  * Registers a new consumer to this Bloc instance.
@@ -435,8 +391,8 @@ class E {
435
391
  *
436
392
  * @param consumerId The unique ID of the consumer being added
437
393
  */
438
- n(this, "_addConsumer", (t) => {
439
- this._consumers.add(t), this._blac.dispatchEvent(_.BLOC_CONSUMER_ADDED, this, { consumerId: t });
394
+ s(this, "_addConsumer", (e) => {
395
+ this._consumers.has(e) || (this._consumers.add(e), c.log("BlocBase._addConsumer", this, e));
440
396
  });
441
397
  /**
442
398
  * @internal
@@ -445,22 +401,10 @@ class E {
445
401
  *
446
402
  * @param consumerId The unique ID of the consumer being removed
447
403
  */
448
- n(this, "_removeConsumer", (t) => {
449
- this._blac.log(`[${this._name}:${String(this._id ?? "default_id")}] Removing consumer: ${t}`), this._consumers.delete(t), this._blac.dispatchEvent(_.BLOC_CONSUMER_REMOVED, this, { consumerId: t });
450
- });
451
- /**
452
- * @internal
453
- * Initializes all registered addons for this Bloc instance.
454
- * Calls the onInit lifecycle method on each addon if defined.
455
- */
456
- n(this, "_connectAddons", () => {
457
- const { _addons: t } = this;
458
- t && t.forEach((e) => {
459
- var s;
460
- (s = e.onInit) == null || s.call(e, this);
461
- });
404
+ s(this, "_removeConsumer", (e) => {
405
+ this._consumers.has(e) && (this._consumers.delete(e), c.log("BlocBase._removeConsumer", this, e));
462
406
  });
463
- n(this, "lastUpdate", Date.now());
407
+ s(this, "lastUpdate", Date.now());
464
408
  /**
465
409
  * @internal
466
410
  * Updates the state and notifies all observers of the change.
@@ -469,12 +413,12 @@ class E {
469
413
  * @param oldState The previous state for comparison
470
414
  * @param action Optional metadata about what caused the state change
471
415
  */
472
- n(this, "_pushState", (t, e, s) => {
473
- this._state = t, this._observer.notify(t, e, s), this.lastUpdate = Date.now();
416
+ s(this, "_pushState", (e, t, n) => {
417
+ this._state = e, this._observer.notify(e, t, n), this.lastUpdate = Date.now();
474
418
  });
475
- this._state = t, this._observer = new D(this), this._blac.dispatchEvent(_.BLOC_CREATED, this), this._id = this.constructor.name;
476
- const e = this.constructor;
477
- this._keepAlive = e.keepAlive, this._isolated = e.isolated, this._addons = e.addons, this._connectAddons();
419
+ this._state = e, this._observer = new _(this), c.log("Bloc Created", this), this._id = this.constructor.name;
420
+ const t = this.constructor;
421
+ this._keepAlive = t.keepAlive, this._isolated = t.isolated;
478
422
  }
479
423
  get isIsolated() {
480
424
  return this._isolated;
@@ -502,8 +446,8 @@ class E {
502
446
  * Notifies the Blac manager and clears all observers.
503
447
  */
504
448
  _dispose() {
505
- var t;
506
- this._observer.dispose(), this._blac.dispatchEvent(_.BLOC_DISPOSED, this), (t = this.onDispose) == null || t.call(this);
449
+ var e;
450
+ this._observer.clear(), (e = this.onDispose) == null || e.call(this), c.log("BlocBase._dispose", this), c.instance.disposeBloc(this);
507
451
  }
508
452
  }
509
453
  /**
@@ -511,93 +455,47 @@ class E {
511
455
  * Use this when state should not be shared between components.
512
456
  * @default false
513
457
  */
514
- n(E, "isolated", !1), /**
458
+ s(g, "isolated", !1), /**
515
459
  * When true, the Bloc instance persists even when there are no active consumers.
516
460
  * Useful for maintaining state between component unmount/remount cycles.
517
461
  * @default false
518
462
  */
519
- n(E, "keepAlive", !1);
520
- class M extends E {
521
- /**
522
- * Updates the current state and notifies all observers of the change.
523
- * If the new state is identical to the current state (using Object.is),
524
- * no update will occur.
525
- * @param state - The new state to set
526
- */
527
- emit(t) {
528
- if (Object.is(t, this.state))
529
- return;
530
- const e = this.state, s = t;
531
- this._pushState(s, e);
532
- }
533
- /**
534
- * Partially updates the current state by merging it with the provided state patch.
535
- * This method is only applicable when the state is an object type.
536
- *
537
- * @param statePatch - A partial state object containing only the properties to update
538
- * @param ignoreChangeCheck - If true, skips checking if the state has actually changed
539
- * @throws {TypeError} If the state is not an object type
540
- */
541
- patch(t, e = !1) {
542
- if (typeof this.state != "object" || this.state === null) {
543
- h.warn(
544
- "Cubit.patch: was called on a cubit where the state is not an object. This is a no-op."
545
- );
546
- return;
547
- }
548
- let s = !1;
549
- if (e)
550
- s = !0;
551
- else
552
- for (const i in t)
553
- if (Object.prototype.hasOwnProperty.call(t, i)) {
554
- const o = this.state[i];
555
- if (!Object.is(t[i], o)) {
556
- s = !0;
557
- break;
558
- }
559
- }
560
- s && this.emit({
561
- ...this.state,
562
- ...t
563
- });
564
- }
565
- }
566
- class w extends E {
463
+ s(g, "keepAlive", !1);
464
+ class b extends g {
567
465
  constructor() {
568
466
  super(...arguments);
569
467
  // Stores handlers: Map<EventConstructor (subtype of A), HandlerFunction>
570
468
  // The handler's event parameter will be correctly typed to the specific EventConstructor
571
469
  // by the 'on' method's signature.
572
- n(this, "eventHandlers", /* @__PURE__ */ new Map());
470
+ s(this, "eventHandlers", /* @__PURE__ */ new Map());
573
471
  /**
574
472
  * Dispatches an action/event to the Bloc.
575
473
  * If a handler is registered for this specific event type (via 'on'), it will be invoked.
576
474
  * Asynchronous handlers are awaited.
577
475
  * @param action The action/event instance to be processed.
578
476
  */
579
- n(this, "add", async (e) => {
580
- const s = e.constructor, i = this.eventHandlers.get(s);
477
+ s(this, "add", async (t) => {
478
+ const n = t.constructor, i = this.eventHandlers.get(n);
581
479
  if (i) {
582
- const a = (o) => {
583
- const g = this.state;
584
- this._pushState(o, g, e);
480
+ const a = (r) => {
481
+ const l = this.state;
482
+ this._pushState(r, l, t);
585
483
  };
586
484
  try {
587
- await i(e, a);
588
- } catch (o) {
589
- h.error(
590
- `[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${s.name}':`,
591
- o,
485
+ await i(t, a);
486
+ } catch (r) {
487
+ c.error(
488
+ `[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${n.name}':`,
489
+ r,
592
490
  "Action:",
593
- e
491
+ t
594
492
  );
595
493
  }
596
494
  } else {
597
- const a = e.constructor.name || "UnnamedConstructor";
598
- h.warn(
495
+ const a = t.constructor.name || "UnnamedConstructor";
496
+ c.warn(
599
497
  `[Bloc ${this._name}:${String(this._id)}] No handler registered for action type: '${a}'. Action was:`,
600
- e
498
+ t
601
499
  );
602
500
  }
603
501
  });
@@ -609,63 +507,66 @@ class w extends E {
609
507
  * @param handler A function that processes the event and can emit new states.
610
508
  * The 'event' parameter in the handler will be typed to the specific eventConstructor.
611
509
  */
612
- on(e, s) {
613
- this.eventHandlers.has(e) && h.warn(
614
- `[Bloc ${this._name}:${String(this._id)}] Handler for event '${e.name}' already registered. It will be overwritten.`
510
+ on(t, n) {
511
+ this.eventHandlers.has(t) && c.warn(
512
+ `[Bloc ${this._name}:${String(this._id)}] Handler for event '${t.name}' already registered. It will be overwritten.`
615
513
  ), this.eventHandlers.set(
616
- e,
617
- s
514
+ t,
515
+ n
618
516
  );
619
517
  }
620
518
  }
621
- function I(c) {
622
- switch (c) {
623
- case "localStorage":
624
- return localStorage;
625
- case "sessionStorage":
626
- return sessionStorage;
627
- default:
628
- return localStorage;
519
+ class m extends g {
520
+ /**
521
+ * Updates the current state and notifies all observers of the change.
522
+ * If the new state is identical to the current state (using Object.is),
523
+ * no update will occur.
524
+ * @param state - The new state to set
525
+ */
526
+ emit(e) {
527
+ if (Object.is(e, this.state))
528
+ return;
529
+ const t = this.state, n = e;
530
+ this._pushState(n, t);
629
531
  }
630
- }
631
- function $(c = {}) {
632
- const {
633
- keyPrefix: t = "blac",
634
- keyName: e,
635
- defaultValue: s,
636
- storageType: i = "localStorage"
637
- } = c, a = (d) => `${t}:${String(d)}`, o = (d) => {
638
- var l;
639
- try {
640
- const u = I(i).getItem(a(d));
641
- if (typeof u != "string")
642
- return s;
643
- const f = JSON.parse(u);
644
- return typeof f.v < "u" ? f.v : s;
645
- } catch (u) {
646
- return (l = c.onError) == null || l.call(c, u), s;
647
- }
648
- }, g = (d) => {
649
- const l = e ?? d._id, u = o(l);
650
- d._pushState(u, null);
651
- };
652
- let p = "";
653
- return {
654
- name: "Persist",
655
- onInit: g,
656
- onEmit: ({ newState: d, cubit: l }) => {
657
- const u = e ?? l._id, f = JSON.stringify({ v: d });
658
- f !== p && (I(i).setItem(a(u), f), p = f);
532
+ /**
533
+ * Partially updates the current state by merging it with the provided state patch.
534
+ * This method is only applicable when the state is an object type.
535
+ *
536
+ * @param statePatch - A partial state object containing only the properties to update
537
+ * @param ignoreChangeCheck - If true, skips checking if the state has actually changed
538
+ * @throws {TypeError} If the state is not an object type
539
+ */
540
+ patch(e, t = !1) {
541
+ if (typeof this.state != "object" || this.state === null) {
542
+ c.warn(
543
+ "Cubit.patch: was called on a cubit where the state is not an object. This is a no-op."
544
+ );
545
+ return;
659
546
  }
660
- };
547
+ let n = !1;
548
+ if (t)
549
+ n = !0;
550
+ else
551
+ for (const i in e)
552
+ if (Object.prototype.hasOwnProperty.call(e, i)) {
553
+ const r = this.state[i];
554
+ if (!Object.is(e[i], r)) {
555
+ n = !0;
556
+ break;
557
+ }
558
+ }
559
+ n && this.emit({
560
+ ...this.state,
561
+ ...e
562
+ });
563
+ }
661
564
  }
662
565
  export {
663
- h as Blac,
664
- _ as BlacLifecycleEvent,
665
- D as BlacObservable,
666
- w as Bloc,
667
- E as BlocBase,
668
- M as Cubit,
669
- $ as Persist
566
+ c as Blac,
567
+ _ as BlacObservable,
568
+ b as Bloc,
569
+ g as BlocBase,
570
+ m as Cubit
670
571
  };
671
572
  //# sourceMappingURL=index.es.js.map