@blac/core 2.0.0 → 2.0.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.
Files changed (60) hide show
  1. package/README.md +137 -177
  2. package/dist/StateContainerRegistry.cjs.map +1 -0
  3. package/dist/StateContainerRegistry.js.map +1 -0
  4. package/dist/debug.cjs +27 -0
  5. package/dist/debug.cjs.map +1 -0
  6. package/dist/debug.js +19 -0
  7. package/dist/debug.js.map +1 -0
  8. package/dist/dependency-manager.cjs.map +1 -0
  9. package/dist/dependency-manager.js.map +1 -0
  10. package/dist/index.cjs +346 -0
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.cts +794 -0
  13. package/dist/index.d.ts +675 -386
  14. package/dist/index.js +310 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/management.cjs.map +1 -0
  17. package/dist/management.js.map +1 -0
  18. package/dist/plugins.cjs +4 -0
  19. package/dist/plugins.js +3 -0
  20. package/dist/tracking/dependency-manager.d.ts +37 -0
  21. package/dist/tracking/index.d.ts +12 -0
  22. package/dist/tracking/path-utils.d.ts +52 -0
  23. package/dist/tracking/tracked.d.ts +70 -0
  24. package/dist/tracking/tracking-proxy.d.ts +197 -0
  25. package/dist/tracking-proxy.cjs.map +1 -0
  26. package/dist/tracking-proxy.js.map +1 -0
  27. package/dist/tracking.cjs +19 -0
  28. package/dist/tracking.d.cts +9 -0
  29. package/dist/tracking.d.ts +9 -0
  30. package/dist/tracking.js +4 -0
  31. package/dist/types.cjs +14 -0
  32. package/dist/types.cjs.map +1 -0
  33. package/dist/types.d.cts +8 -0
  34. package/dist/types.d.ts +8 -0
  35. package/dist/types.js +13 -0
  36. package/dist/types.js.map +1 -0
  37. package/dist/watch-entry.cjs +111 -0
  38. package/dist/watch-entry.cjs.map +1 -0
  39. package/dist/watch-entry.js +107 -0
  40. package/dist/watch-entry.js.map +1 -0
  41. package/dist/watch.cjs.map +1 -0
  42. package/dist/watch.js.map +1 -0
  43. package/package.json +122 -19
  44. package/dist/index.cjs.js +0 -2
  45. package/dist/index.cjs.js.map +0 -1
  46. package/dist/index.es.js +0 -670
  47. package/dist/index.es.js.map +0 -1
  48. package/src/Blac.ts +0 -483
  49. package/src/BlacEvent.ts +0 -5
  50. package/src/BlacObservable.ts +0 -88
  51. package/src/BlacObserver.ts +0 -129
  52. package/src/BlacPlugin.ts +0 -13
  53. package/src/Bloc.ts +0 -109
  54. package/src/BlocBase.ts +0 -248
  55. package/src/Cubit.ts +0 -69
  56. package/src/addons/BlacAddon.ts +0 -17
  57. package/src/addons/Persist.ts +0 -105
  58. package/src/addons/index.ts +0 -1
  59. package/src/index.ts +0 -8
  60. package/src/types.ts +0 -63
package/dist/index.es.js DELETED
@@ -1,670 +0,0 @@
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 {
6
- /**
7
- * Creates a new Blac instance.
8
- * @param options - Configuration options including singleton control
9
- */
10
- constructor(t = {}) {
11
- /** Timestamp when the instance was created */
12
- n(this, "createdAt", Date.now());
13
- /** Map storing all registered bloc instances by their class name and ID */
14
- n(this, "blocInstanceMap", /* @__PURE__ */ new Map());
15
- /** Map storing isolated bloc instances grouped by their constructor */
16
- n(this, "isolatedBlocMap", /* @__PURE__ */ new Map());
17
- n(this, "pluginList", []);
18
- /** Flag to control whether changes should be posted to document */
19
- n(this, "postChangesToDocument", !1);
20
- /**
21
- * Logs messages to console when logging is enabled
22
- * @param args - Arguments to log
23
- */
24
- n(this, "log", (...t) => {
25
- r.enableLog && console.warn(`☢️ [Blac ${this.createdAt.toString()}]`, ...t);
26
- });
27
- /**
28
- * Logs a warning message
29
- * @param message - Warning message
30
- * @param args - Additional arguments
31
- */
32
- n(this, "warn", (t, ...e) => {
33
- r.enableLog && console.warn(`🚨 [Blac ${String(r.instance.createdAt)}]`, t, ...e);
34
- });
35
- /**
36
- * Logs an error message
37
- * @param message - Error message
38
- * @param args - Additional arguments
39
- */
40
- n(this, "error", (t, ...e) => {
41
- r.enableLog && console.error(`🚨 [Blac ${String(r.instance.createdAt)}]`, t, ...e);
42
- });
43
- /**
44
- * Resets the Blac instance to a new one, disposing non-keepAlive blocs
45
- * from the old instance.
46
- */
47
- n(this, "resetInstance", () => {
48
- 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) => {
54
- i._dispose();
55
- });
56
- }), this.blocInstanceMap.clear(), this.isolatedBlocMap.clear(), r.instance = new r({
57
- __unsafe_ignore_singleton: !0
58
- });
59
- });
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
- /**
103
- * Disposes of a bloc instance by removing it from the appropriate registry
104
- * @param bloc - The bloc instance to dispose
105
- */
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);
109
- });
110
- /**
111
- * Gets or creates a bloc instance based on the provided class and options.
112
- * If a bloc with the given ID exists, it will be returned. Otherwise, a new instance will be created.
113
- *
114
- * @param blocClass - The bloc class to get or create
115
- * @param options - Options including:
116
- * - id: The instance ID (defaults to class name if not provided)
117
- * - props: Properties to pass to the bloc constructor
118
- * - instanceRef: Optional reference string for the instance
119
- * @returns The bloc instance
120
- */
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;
132
- }
133
- return this.log(`[${t.name}:${String(a)}] No existing instance found. Creating new one.`), this.createNewBlocInstance(
134
- t,
135
- a,
136
- e
137
- );
138
- });
139
- /**
140
- * Gets a bloc instance or throws an error if it doesn't exist
141
- * @param blocClass - The bloc class to get
142
- * @param options - Options including:
143
- * - id: The instance ID (defaults to class name if not provided)
144
- * - props: Properties to pass to the bloc constructor
145
- * - instanceRef: Optional reference string for the instance
146
- */
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);
149
- if (a)
150
- return a;
151
- throw new Error(`Bloc ${t.name} not found`);
152
- });
153
- /**
154
- * Gets all instances of a specific bloc class
155
- * @param blocClass - The bloc class to search for
156
- * @param options - Options including:
157
- * - searchIsolated: Whether to search in isolated blocs (defaults to bloc's isolated property)
158
- * @returns Array of matching bloc instances
159
- */
160
- n(this, "getAllBlocs", (t, e = {}) => {
161
- const s = [];
162
- 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));
167
- }
168
- return s;
169
- });
170
- const { __unsafe_ignore_singleton: e = !1 } = t;
171
- if (!e)
172
- return r.instance;
173
- r.instance = this;
174
- }
175
- /**
176
- * Gets the singleton instance of Blac
177
- * @returns The Blac instance
178
- */
179
- static getInstance() {
180
- return r.instance;
181
- }
182
- /**
183
- * Creates a unique key for a bloc instance in the map based on the bloc class name and instance ID
184
- * @param blocClassName - The name of the bloc class
185
- * @param id - The instance ID
186
- * @returns A unique key string in the format "className:id"
187
- */
188
- createBlocInstanceMapKey(t, e) {
189
- return `${t}:${String(e)}`;
190
- }
191
- /**
192
- * Unregister a bloc instance from the main registry
193
- * @param bloc - The bloc instance to unregister
194
- */
195
- unregisterBlocInstance(t) {
196
- const e = this.createBlocInstanceMapKey(t._name, t._id);
197
- this.blocInstanceMap.delete(e);
198
- }
199
- /**
200
- * Registers a bloc instance in the main registry
201
- * @param bloc - The bloc instance to register
202
- */
203
- registerBlocInstance(t) {
204
- const e = this.createBlocInstanceMapKey(t._name, t._id);
205
- this.blocInstanceMap.set(e, t);
206
- }
207
- /**
208
- * Finds a registered bloc instance by its class and ID
209
- * @param blocClass - The bloc class to search for
210
- * @param id - The instance ID
211
- * @returns The found bloc instance or undefined if not found
212
- */
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;
217
- }
218
- /**
219
- * Registers an isolated bloc instance in the isolated registry
220
- * @param bloc - The isolated bloc instance to register
221
- */
222
- registerIsolatedBlocInstance(t) {
223
- const e = t.constructor, s = this.isolatedBlocMap.get(e);
224
- s ? s.push(t) : this.isolatedBlocMap.set(e, [t]);
225
- }
226
- /**
227
- * Unregister an isolated bloc instance from the isolated registry
228
- * @param bloc - The isolated bloc instance to unregister
229
- */
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);
235
- }
236
- }
237
- /**
238
- * Finds an isolated bloc instance by its class and ID
239
- */
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;
246
- }
247
- /**
248
- * Creates a new bloc instance and registers it in the appropriate registry
249
- * @param blocClass - The bloc class to instantiate
250
- * @param id - The instance ID
251
- * @param props - Properties to pass to the bloc constructor
252
- * @param instanceRef - Optional reference string for the instance
253
- * @returns The newly created bloc instance
254
- */
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);
258
- }
259
- };
260
- /** 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 {
265
- /**
266
- * Creates a new BlacObservable instance
267
- * @param bloc - The Bloc instance to observe
268
- */
269
- constructor(t) {
270
- /** The Bloc instance this observable is associated with */
271
- n(this, "bloc");
272
- n(this, "_observers", /* @__PURE__ */ new Set());
273
- this.bloc = t;
274
- }
275
- /**
276
- * Gets the number of active observers
277
- * @returns The number of observers currently subscribed
278
- */
279
- get size() {
280
- return this._observers.size;
281
- }
282
- /**
283
- * Gets the set of all observers
284
- * @returns The Set of all BlacObserver instances
285
- */
286
- get observers() {
287
- return this._observers;
288
- }
289
- /**
290
- * Subscribes an observer to state changes
291
- * @param observer - The observer to subscribe
292
- * @returns A function that can be called to unsubscribe the observer
293
- */
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, this.bloc.state) : []), () => {
296
- this.unsubscribe(t);
297
- };
298
- }
299
- /**
300
- * Unsubscribes an observer from state changes
301
- * @param observer - The observer to unsubscribe
302
- */
303
- unsubscribe(t) {
304
- this._observers.delete(t), h.instance.dispatchEvent(_.LISTENER_REMOVED, this.bloc, { listenerId: t.id });
305
- }
306
- /**
307
- * Notifies all observers of a state change
308
- * @param newState - The new state value
309
- * @param oldState - The previous state value
310
- * @param action - Optional action that triggered the state change
311
- */
312
- notify(t, e, s) {
313
- this._observers.forEach((i) => {
314
- let a = !1;
315
- if (i.dependencyArray) {
316
- const o = i.lastState || [], g = i.dependencyArray(t, e);
317
- for (let p = 0; p < g.length; p++) {
318
- const S = g[p], d = o[p] || [];
319
- for (let l = 0; l < S.length; l++)
320
- if (!Object.is(S[l], d[l])) {
321
- a = !0;
322
- break;
323
- }
324
- }
325
- i.lastState = g;
326
- } else
327
- a = !0;
328
- a && i.fn(t, e, s);
329
- });
330
- }
331
- /**
332
- * Disposes of all observers and clears the observer set
333
- */
334
- dispose() {
335
- this._observers.forEach((t) => {
336
- var e;
337
- this.unsubscribe(t), (e = t.dispose) == null || e.call(t);
338
- }), this._observers.clear();
339
- }
340
- }
341
- class E {
342
- /**
343
- * Creates a new BlocBase instance with the given initial state.
344
- * Sets up the observer, registers with the Blac manager, and initializes addons.
345
- *
346
- * @param initialState The initial state value for this Bloc
347
- */
348
- constructor(t) {
349
- /**
350
- * Defines how dependencies are selected from the state for efficient updates.
351
- * When provided, observers will only be notified when selected dependencies change.
352
- */
353
- n(this, "defaultDependencySelector");
354
- /**
355
- * @internal
356
- * Optional array of addons to extend the functionality of this Bloc.
357
- */
358
- n(this, "_addons");
359
- /**
360
- * @internal
361
- * Indicates if this specific Bloc instance is isolated from others of the same type.
362
- */
363
- n(this, "_isolated", !1);
364
- /**
365
- * @internal
366
- * Observable responsible for managing state listeners and notifying consumers.
367
- */
368
- n(this, "_observer");
369
- /**
370
- * @internal
371
- * Reference to the global Blac manager instance.
372
- */
373
- n(this, "_blac", h.getInstance());
374
- /**
375
- * The unique identifier for this Bloc instance.
376
- * Defaults to the class name, but can be customized.
377
- */
378
- n(this, "_id");
379
- /**
380
- * @internal
381
- * Reference string used internally for tracking and debugging.
382
- */
383
- n(this, "_instanceRef");
384
- /**
385
- * @internal
386
- * Indicates if this specific Bloc instance should be kept alive when no consumers are present.
387
- */
388
- n(this, "_keepAlive", !1);
389
- /**
390
- * @readonly
391
- * Timestamp when this Bloc instance was created, useful for debugging and performance tracking.
392
- */
393
- n(this, "_createdAt", Date.now());
394
- /**
395
- * @internal
396
- * The current state of the Bloc.
397
- */
398
- n(this, "_state");
399
- /**
400
- * @internal
401
- * The previous state of the Bloc, maintained for comparison and history.
402
- */
403
- n(this, "_oldState");
404
- /**
405
- * Props passed during Bloc instance creation.
406
- * Can be used to configure or parameterize the Bloc's behavior.
407
- */
408
- n(this, "props", null);
409
- /**
410
- * @internal
411
- * Updates the Bloc instance's ID to a new value.
412
- * Only updates if the new ID is defined and different from the current one.
413
- *
414
- * @param id The new ID to assign to this Bloc instance
415
- */
416
- n(this, "_updateId", (t) => {
417
- const e = this._id;
418
- !t || t === e || (this._id = t);
419
- });
420
- /**
421
- * @internal
422
- * Optional function to be called when the Bloc is disposed.
423
- */
424
- n(this, "onDispose");
425
- /**
426
- * @internal
427
- * Set of consumer IDs currently listening to this Bloc's state changes.
428
- */
429
- n(this, "_consumers", /* @__PURE__ */ new Set());
430
- /**
431
- * @internal
432
- * Registers a new consumer to this Bloc instance.
433
- * Notifies the Blac manager that a consumer has been added.
434
- *
435
- * @param consumerId The unique ID of the consumer being added
436
- */
437
- n(this, "_addConsumer", (t) => {
438
- this._consumers.add(t), this._blac.dispatchEvent(_.BLOC_CONSUMER_ADDED, this, { consumerId: t });
439
- });
440
- /**
441
- * @internal
442
- * Unregisters a consumer from this Bloc instance.
443
- * Notifies the Blac manager that a consumer has been removed.
444
- *
445
- * @param consumerId The unique ID of the consumer being removed
446
- */
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 });
449
- });
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());
463
- /**
464
- * @internal
465
- * Updates the state and notifies all observers of the change.
466
- *
467
- * @param newState The new state to be set
468
- * @param oldState The previous state for comparison
469
- * @param action Optional metadata about what caused the state change
470
- */
471
- n(this, "_pushState", (t, e, s) => {
472
- this._state = t, this._observer.notify(t, e, s), this.lastUpdate = Date.now();
473
- });
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();
477
- }
478
- get isIsolated() {
479
- return this._isolated;
480
- }
481
- get isKeepAlive() {
482
- return this._keepAlive;
483
- }
484
- /**
485
- * Returns the current state of the Bloc.
486
- * Use this getter to access the state in a read-only manner.
487
- */
488
- get state() {
489
- return this._state;
490
- }
491
- /**
492
- * @internal
493
- * Returns the name of the Bloc class for identification and debugging.
494
- */
495
- get _name() {
496
- return this.constructor.name;
497
- }
498
- /**
499
- * @internal
500
- * Cleans up resources and removes this Bloc from the system.
501
- * Notifies the Blac manager and clears all observers.
502
- */
503
- _dispose() {
504
- var t;
505
- this._observer.dispose(), this._blac.dispatchEvent(_.BLOC_DISPOSED, this), (t = this.onDispose) == null || t.call(this);
506
- }
507
- }
508
- /**
509
- * When true, every consumer will receive its own unique instance of this Bloc.
510
- * Use this when state should not be shared between components.
511
- * @default false
512
- */
513
- n(E, "isolated", !1), /**
514
- * When true, the Bloc instance persists even when there are no active consumers.
515
- * Useful for maintaining state between component unmount/remount cycles.
516
- * @default false
517
- */
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 {
566
- constructor() {
567
- super(...arguments);
568
- // Stores handlers: Map<EventConstructor (subtype of A), HandlerFunction>
569
- // The handler's event parameter will be correctly typed to the specific EventConstructor
570
- // by the 'on' method's signature.
571
- n(this, "eventHandlers", /* @__PURE__ */ new Map());
572
- /**
573
- * Dispatches an action/event to the Bloc.
574
- * If a handler is registered for this specific event type (via 'on'), it will be invoked.
575
- * Asynchronous handlers are awaited.
576
- * @param action The action/event instance to be processed.
577
- */
578
- n(this, "add", async (e) => {
579
- const s = e.constructor, i = this.eventHandlers.get(s);
580
- if (i) {
581
- const a = (o) => {
582
- const g = this.state;
583
- this._pushState(o, g, e);
584
- };
585
- 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,
591
- "Action:",
592
- e
593
- );
594
- }
595
- } else {
596
- const a = e.constructor.name || "UnnamedConstructor";
597
- h.warn(
598
- `[Bloc ${this._name}:${String(this._id)}] No handler registered for action type: '${a}'. Action was:`,
599
- e
600
- );
601
- }
602
- });
603
- }
604
- /**
605
- * Registers an event handler for a specific event type.
606
- * This method is typically called in the constructor of a derived Bloc class.
607
- * @param eventConstructor The constructor of the event to handle (e.g., LoadDataEvent).
608
- * @param handler A function that processes the event and can emit new states.
609
- * The 'event' parameter in the handler will be typed to the specific eventConstructor.
610
- */
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.`
614
- ), this.eventHandlers.set(
615
- e,
616
- s
617
- );
618
- }
619
- }
620
- function I(c) {
621
- switch (c) {
622
- case "localStorage":
623
- return localStorage;
624
- case "sessionStorage":
625
- return sessionStorage;
626
- default:
627
- return localStorage;
628
- }
629
- }
630
- function $(c = {}) {
631
- const {
632
- keyPrefix: t = "blac",
633
- keyName: e,
634
- defaultValue: s,
635
- storageType: i = "localStorage"
636
- } = c, a = (d) => `${t}:${String(d)}`, o = (d) => {
637
- var l;
638
- try {
639
- const u = I(i).getItem(a(d));
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 (l = c.onError) == null || l.call(c, u), s;
646
- }
647
- }, g = (d) => {
648
- const l = e ?? d._id, u = o(l);
649
- d._pushState(u, null);
650
- };
651
- let p = "";
652
- return {
653
- name: "Persist",
654
- onInit: g,
655
- onEmit: ({ newState: d, cubit: l }) => {
656
- const u = e ?? l._id, f = JSON.stringify({ v: d });
657
- f !== p && (I(i).setItem(a(u), f), p = f);
658
- }
659
- };
660
- }
661
- 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
669
- };
670
- //# sourceMappingURL=index.es.js.map