@blac/core 2.0.0-rc-2 → 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,25 +214,25 @@ 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
- n(this, "_observers", /* @__PURE__ */ new Set());
273
- this.bloc = t;
233
+ s(this, "bloc");
234
+ s(this, "_observers", /* @__PURE__ */ new Set());
235
+ this.bloc = e;
274
236
  }
275
237
  /**
276
238
  * Gets the number of active observers
@@ -291,18 +253,17 @@ class D {
291
253
  * @param observer - The observer to subscribe
292
254
  * @returns A function that can be called to unsubscribe the observer
293
255
  */
294
- subscribe(t) {
295
- return this._observers.add(t), h.instance.dispatchEvent(_.LISTENER_ADDED, this.bloc, { listenerId: t.id }), t.lastState || (t.lastState = t.dependencyArray ? t.dependencyArray(this.bloc.state) : []), () => {
296
- 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);
297
259
  };
298
260
  }
299
261
  /**
300
262
  * Unsubscribes an observer from state changes
301
263
  * @param observer - The observer to unsubscribe
302
264
  */
303
- unsubscribe(t) {
304
- var e;
305
- (e = t.dispose) == null || e.call(t), this._observers.delete(t), 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,102 +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], l = o[p] || [];
320
- for (let d = 0; d < S.length; d++)
321
- if (!Object.is(S[d], l[d])) {
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
- this.unsubscribe(t);
296
+ clear() {
297
+ this._observers.forEach((e) => {
298
+ this.unsubscribe(e);
338
299
  }), this._observers.clear();
339
300
  }
340
301
  }
341
- class E {
302
+ class g {
342
303
  /**
343
304
  * Creates a new BlocBase instance with the given initial state.
344
305
  * Sets up the observer, registers with the Blac manager, and initializes addons.
345
306
  *
346
307
  * @param initialState The initial state value for this Bloc
347
308
  */
348
- constructor(t) {
309
+ constructor(e) {
310
+ s(this, "uid", crypto.randomUUID());
349
311
  /**
350
312
  * Defines how dependencies are selected from the state for efficient updates.
351
313
  * When provided, observers will only be notified when selected dependencies change.
352
314
  */
353
- n(this, "defaultDependencySelector");
354
- /**
355
- * @internal
356
- * Optional array of addons to extend the functionality of this Bloc.
357
- */
358
- n(this, "_addons");
315
+ s(this, "defaultDependencySelector");
359
316
  /**
360
317
  * @internal
361
318
  * Indicates if this specific Bloc instance is isolated from others of the same type.
362
319
  */
363
- n(this, "_isolated", !1);
320
+ s(this, "_isolated", !1);
364
321
  /**
365
322
  * @internal
366
323
  * Observable responsible for managing state listeners and notifying consumers.
367
324
  */
368
- n(this, "_observer");
325
+ s(this, "_observer");
369
326
  /**
370
327
  * @internal
371
328
  * Reference to the global Blac manager instance.
372
329
  */
373
- n(this, "_blac", h.getInstance());
330
+ s(this, "_blac", c.getInstance());
374
331
  /**
375
332
  * The unique identifier for this Bloc instance.
376
333
  * Defaults to the class name, but can be customized.
377
334
  */
378
- n(this, "_id");
335
+ s(this, "_id");
379
336
  /**
380
337
  * @internal
381
338
  * Reference string used internally for tracking and debugging.
382
339
  */
383
- n(this, "_instanceRef");
340
+ s(this, "_instanceRef");
384
341
  /**
385
342
  * @internal
386
343
  * Indicates if this specific Bloc instance should be kept alive when no consumers are present.
387
344
  */
388
- n(this, "_keepAlive", !1);
345
+ s(this, "_keepAlive", !1);
389
346
  /**
390
347
  * @readonly
391
348
  * Timestamp when this Bloc instance was created, useful for debugging and performance tracking.
392
349
  */
393
- n(this, "_createdAt", Date.now());
350
+ s(this, "_createdAt", Date.now());
394
351
  /**
395
352
  * @internal
396
353
  * The current state of the Bloc.
397
354
  */
398
- n(this, "_state");
355
+ s(this, "_state");
399
356
  /**
400
357
  * @internal
401
358
  * The previous state of the Bloc, maintained for comparison and history.
402
359
  */
403
- n(this, "_oldState");
360
+ s(this, "_oldState");
404
361
  /**
405
362
  * Props passed during Bloc instance creation.
406
363
  * Can be used to configure or parameterize the Bloc's behavior.
407
364
  */
408
- n(this, "props", null);
365
+ s(this, "props", null);
409
366
  /**
410
367
  * @internal
411
368
  * Updates the Bloc instance's ID to a new value.
@@ -413,20 +370,20 @@ class E {
413
370
  *
414
371
  * @param id The new ID to assign to this Bloc instance
415
372
  */
416
- n(this, "_updateId", (t) => {
417
- const e = this._id;
418
- !t || t === e || (this._id = t);
373
+ s(this, "_updateId", (e) => {
374
+ const t = this._id;
375
+ !e || e === t || (this._id = e);
419
376
  });
420
377
  /**
421
378
  * @internal
422
379
  * Optional function to be called when the Bloc is disposed.
423
380
  */
424
- n(this, "onDispose");
381
+ s(this, "onDispose");
425
382
  /**
426
383
  * @internal
427
384
  * Set of consumer IDs currently listening to this Bloc's state changes.
428
385
  */
429
- n(this, "_consumers", /* @__PURE__ */ new Set());
386
+ s(this, "_consumers", /* @__PURE__ */ new Set());
430
387
  /**
431
388
  * @internal
432
389
  * Registers a new consumer to this Bloc instance.
@@ -434,8 +391,8 @@ class E {
434
391
  *
435
392
  * @param consumerId The unique ID of the consumer being added
436
393
  */
437
- n(this, "_addConsumer", (t) => {
438
- 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));
439
396
  });
440
397
  /**
441
398
  * @internal
@@ -444,22 +401,10 @@ class E {
444
401
  *
445
402
  * @param consumerId The unique ID of the consumer being removed
446
403
  */
447
- n(this, "_removeConsumer", (t) => {
448
- 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 });
404
+ s(this, "_removeConsumer", (e) => {
405
+ this._consumers.has(e) && (this._consumers.delete(e), c.log("BlocBase._removeConsumer", this, e));
449
406
  });
450
- /**
451
- * @internal
452
- * Initializes all registered addons for this Bloc instance.
453
- * Calls the onInit lifecycle method on each addon if defined.
454
- */
455
- n(this, "_connectAddons", () => {
456
- const { _addons: t } = this;
457
- t && t.forEach((e) => {
458
- var s;
459
- (s = e.onInit) == null || s.call(e, this);
460
- });
461
- });
462
- n(this, "lastUpdate", Date.now());
407
+ s(this, "lastUpdate", Date.now());
463
408
  /**
464
409
  * @internal
465
410
  * Updates the state and notifies all observers of the change.
@@ -468,12 +413,12 @@ class E {
468
413
  * @param oldState The previous state for comparison
469
414
  * @param action Optional metadata about what caused the state change
470
415
  */
471
- n(this, "_pushState", (t, e, s) => {
472
- 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();
473
418
  });
474
- this._state = t, this._observer = new D(this), this._blac.dispatchEvent(_.BLOC_CREATED, this), this._id = this.constructor.name;
475
- const e = this.constructor;
476
- 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;
477
422
  }
478
423
  get isIsolated() {
479
424
  return this._isolated;
@@ -501,8 +446,8 @@ class E {
501
446
  * Notifies the Blac manager and clears all observers.
502
447
  */
503
448
  _dispose() {
504
- var t;
505
- 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);
506
451
  }
507
452
  }
508
453
  /**
@@ -510,93 +455,47 @@ class E {
510
455
  * Use this when state should not be shared between components.
511
456
  * @default false
512
457
  */
513
- n(E, "isolated", !1), /**
458
+ s(g, "isolated", !1), /**
514
459
  * When true, the Bloc instance persists even when there are no active consumers.
515
460
  * Useful for maintaining state between component unmount/remount cycles.
516
461
  * @default false
517
462
  */
518
- n(E, "keepAlive", !1);
519
- class M extends E {
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(t) {
527
- if (Object.is(t, this.state))
528
- return;
529
- const e = this.state, s = t;
530
- this._pushState(s, e);
531
- }
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(t, e = !1) {
541
- if (typeof this.state != "object" || this.state === null) {
542
- h.warn(
543
- "Cubit.patch: was called on a cubit where the state is not an object. This is a no-op."
544
- );
545
- return;
546
- }
547
- let s = !1;
548
- if (e)
549
- s = !0;
550
- else
551
- for (const i in t)
552
- if (Object.prototype.hasOwnProperty.call(t, i)) {
553
- const o = this.state[i];
554
- if (!Object.is(t[i], o)) {
555
- s = !0;
556
- break;
557
- }
558
- }
559
- s && this.emit({
560
- ...this.state,
561
- ...t
562
- });
563
- }
564
- }
565
- class w extends E {
463
+ s(g, "keepAlive", !1);
464
+ class b extends g {
566
465
  constructor() {
567
466
  super(...arguments);
568
467
  // Stores handlers: Map<EventConstructor (subtype of A), HandlerFunction>
569
468
  // The handler's event parameter will be correctly typed to the specific EventConstructor
570
469
  // by the 'on' method's signature.
571
- n(this, "eventHandlers", /* @__PURE__ */ new Map());
470
+ s(this, "eventHandlers", /* @__PURE__ */ new Map());
572
471
  /**
573
472
  * Dispatches an action/event to the Bloc.
574
473
  * If a handler is registered for this specific event type (via 'on'), it will be invoked.
575
474
  * Asynchronous handlers are awaited.
576
475
  * @param action The action/event instance to be processed.
577
476
  */
578
- n(this, "add", async (e) => {
579
- 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);
580
479
  if (i) {
581
- const a = (o) => {
582
- const g = this.state;
583
- this._pushState(o, g, e);
480
+ const a = (r) => {
481
+ const l = this.state;
482
+ this._pushState(r, l, t);
584
483
  };
585
484
  try {
586
- await i(e, a);
587
- } catch (o) {
588
- h.error(
589
- `[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${s.name}':`,
590
- 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,
591
490
  "Action:",
592
- e
491
+ t
593
492
  );
594
493
  }
595
494
  } else {
596
- const a = e.constructor.name || "UnnamedConstructor";
597
- h.warn(
495
+ const a = t.constructor.name || "UnnamedConstructor";
496
+ c.warn(
598
497
  `[Bloc ${this._name}:${String(this._id)}] No handler registered for action type: '${a}'. Action was:`,
599
- e
498
+ t
600
499
  );
601
500
  }
602
501
  });
@@ -608,63 +507,66 @@ class w extends E {
608
507
  * @param handler A function that processes the event and can emit new states.
609
508
  * The 'event' parameter in the handler will be typed to the specific eventConstructor.
610
509
  */
611
- on(e, s) {
612
- this.eventHandlers.has(e) && h.warn(
613
- `[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.`
614
513
  ), this.eventHandlers.set(
615
- e,
616
- s
514
+ t,
515
+ n
617
516
  );
618
517
  }
619
518
  }
620
- function I(c) {
621
- switch (c) {
622
- case "localStorage":
623
- return localStorage;
624
- case "sessionStorage":
625
- return sessionStorage;
626
- default:
627
- 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);
628
531
  }
629
- }
630
- function $(c = {}) {
631
- const {
632
- keyPrefix: t = "blac",
633
- keyName: e,
634
- defaultValue: s,
635
- storageType: i = "localStorage"
636
- } = c, a = (l) => `${t}:${String(l)}`, o = (l) => {
637
- var d;
638
- try {
639
- const u = I(i).getItem(a(l));
640
- if (typeof u != "string")
641
- return s;
642
- const f = JSON.parse(u);
643
- return typeof f.v < "u" ? f.v : s;
644
- } catch (u) {
645
- return (d = c.onError) == null || d.call(c, u), s;
646
- }
647
- }, g = (l) => {
648
- const d = e ?? l._id, u = o(d);
649
- l._pushState(u, null);
650
- };
651
- let p = "";
652
- return {
653
- name: "Persist",
654
- onInit: g,
655
- onEmit: ({ newState: l, cubit: d }) => {
656
- const u = e ?? d._id, f = JSON.stringify({ v: l });
657
- 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;
658
546
  }
659
- };
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
+ }
660
564
  }
661
565
  export {
662
- h as Blac,
663
- _ as BlacLifecycleEvent,
664
- D as BlacObservable,
665
- w as Bloc,
666
- E as BlocBase,
667
- M as Cubit,
668
- $ as Persist
566
+ c as Blac,
567
+ _ as BlacObservable,
568
+ b as Bloc,
569
+ g as BlocBase,
570
+ m as Cubit
669
571
  };
670
572
  //# sourceMappingURL=index.es.js.map