@blac/core 2.0.0-rc-7 → 2.0.0-rc-8

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,26 +1,30 @@
1
- var m = Object.defineProperty;
2
- var y = (d, e, t) => e in d ? m(d, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : d[e] = t;
1
+ var I = Object.defineProperty;
2
+ var y = (d, e, t) => e in d ? I(d, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : d[e] = t;
3
3
  var i = (d, e, t) => y(d, typeof e != "symbol" ? e + "" : e, t);
4
4
  const u = class u {
5
5
  getInstance() {
6
- return u._instance || (u._instance = new l({ __unsafe_ignore_singleton: !0 })), u._instance;
6
+ return u._instance || (u._instance = new l({
7
+ __unsafe_ignore_singleton: !0
8
+ })), u._instance;
7
9
  }
8
10
  setInstance(e) {
9
11
  u._instance = e;
10
12
  }
11
13
  resetInstance() {
12
14
  const e = u._instance;
13
- if (u._instance = new l({ __unsafe_ignore_singleton: !0 }), e)
15
+ if (u._instance = new l({
16
+ __unsafe_ignore_singleton: !0
17
+ }), e)
14
18
  for (const t of e.keepAliveBlocs)
15
19
  u._instance.keepAliveBlocs.add(t), u._instance.uidRegistry.set(t.uid, t);
16
20
  }
17
21
  };
18
22
  i(u, "_instance");
19
- let v = u, g = new v();
20
- function M(d) {
23
+ let B = u, g = new B();
24
+ function A(d) {
21
25
  g = d;
22
26
  }
23
- const c = class c {
27
+ const r = class r {
24
28
  /**
25
29
  * Creates a new Blac instance.
26
30
  * @param options - Configuration options including singleton control
@@ -31,6 +35,10 @@ const c = class c {
31
35
  /** Map storing all registered bloc instances by their class name and ID */
32
36
  i(this, "blocInstanceMap", /* @__PURE__ */ new Map());
33
37
  /** Map storing isolated bloc instances grouped by their constructor */
38
+ // TODO: BlocConstructor<any> is required here for type inference to work correctly.
39
+ // Using BlocConstructor<BlocBase<unknown>> would break type inference when storing
40
+ // different bloc types in the same map. The 'any' allows proper polymorphic storage
41
+ // while maintaining type safety at usage sites through the BlocConstructor constraint.
34
42
  i(this, "isolatedBlocMap", /* @__PURE__ */ new Map());
35
43
  /** Map for O(1) lookup of isolated blocs by UID */
36
44
  i(this, "isolatedBlocIndex", /* @__PURE__ */ new Map());
@@ -45,7 +53,7 @@ const c = class c {
45
53
  * @param args - Arguments to log
46
54
  */
47
55
  i(this, "log", (...e) => {
48
- c.enableLog && console.warn(`☢️ [Blac ${this.createdAt.toString()}]`, ...e);
56
+ r.enableLog && r.logLevel === "warn" && console.warn(`☢️ [Blac ${this.createdAt.toString()}]`, ...e), r.enableLog && r.logLevel === "log" && console.log(`☢️ [Blac ${this.createdAt.toString()}]`, ...e);
49
57
  });
50
58
  /**
51
59
  * Logs a warning message
@@ -53,7 +61,11 @@ const c = class c {
53
61
  * @param args - Additional arguments
54
62
  */
55
63
  i(this, "warn", (e, ...t) => {
56
- c.enableLog && console.warn(`🚨 [Blac ${String(c.instance.createdAt)}]`, e, ...t);
64
+ r.enableLog && console.warn(
65
+ `🚨 [Blac ${String(r.instance.createdAt)}]`,
66
+ e,
67
+ ...t
68
+ );
57
69
  });
58
70
  /**
59
71
  * Logs an error message
@@ -61,7 +73,11 @@ const c = class c {
61
73
  * @param args - Additional arguments
62
74
  */
63
75
  i(this, "error", (e, ...t) => {
64
- c.enableLog && console.error(`🚨 [Blac ${String(c.instance.createdAt)}]`, e, ...t);
76
+ r.enableLog && console.error(
77
+ `🚨 [Blac ${String(r.instance.createdAt)}]`,
78
+ e,
79
+ ...t
80
+ );
65
81
  });
66
82
  /**
67
83
  * Resets the Blac instance to a new one, disposing non-keepAlive blocs
@@ -84,20 +100,32 @@ const c = class c {
84
100
  */
85
101
  i(this, "disposeBloc", (e) => {
86
102
  if (e._disposalState !== "active") {
87
- this.log(`[${e._name}:${String(e._id)}] disposeBloc called on already disposed bloc`);
103
+ this.log(
104
+ `[${e._name}:${String(e._id)}] disposeBloc called on already disposed bloc`
105
+ );
88
106
  return;
89
107
  }
90
108
  const t = e.constructor, s = this.createBlocInstanceMapKey(e._name, e._id);
91
- this.log(`[${e._name}:${String(e._id)}] disposeBloc called. Isolated: ${String(t.isolated)}`), e._dispose(), t.isolated ? (this.unregisterIsolatedBlocInstance(e), this.blocInstanceMap.delete(s)) : this.unregisterBlocInstance(e), this.log("dispatched bloc", e);
109
+ this.log(
110
+ `[${e._name}:${String(e._id)}] disposeBloc called. Isolated: ${String(t.isolated)}`
111
+ ), e._dispose(), t.isolated ? (this.unregisterIsolatedBlocInstance(e), this.blocInstanceMap.delete(s)) : this.unregisterBlocInstance(e), this.log("dispatched bloc", e);
92
112
  });
93
113
  i(this, "activateBloc", (e) => {
114
+ if (e._disposalState !== "active") {
115
+ this.log(
116
+ `[${e._name}:${String(e._id)}] activateBloc called on disposed bloc. Ignoring.`
117
+ );
118
+ return;
119
+ }
94
120
  const t = e.constructor;
95
- (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));
121
+ (e.isIsolated ? this.findIsolatedBlocInstance(t, e._id) : this.findRegisteredBlocInstance(t, e._id)) || (this.log(
122
+ `[${e._name}:${String(e._id)}] activateBloc called. Isolated: ${String(e.isIsolated)}`
123
+ ), e.isIsolated ? this.registerIsolatedBlocInstance(e) : this.registerBlocInstance(e));
96
124
  });
97
125
  /**
98
126
  * Gets or creates a bloc instance based on the provided class and options.
99
127
  * If a bloc with the given ID exists, it will be returned. Otherwise, a new instance will be created.
100
- *
128
+ *
101
129
  * @param blocClass - The bloc class to get or create
102
130
  * @param options - Options including:
103
131
  * - id: The instance ID (defaults to class name if not provided)
@@ -108,25 +136,27 @@ const c = class c {
108
136
  i(this, "getBloc", (e, t = {}) => {
109
137
  const { id: s } = t, n = e, o = s ?? e.name;
110
138
  if (n.isolated) {
111
- const r = this.findIsolatedBlocInstance(e, o);
112
- if (r)
113
- return this.log(`[${e.name}:${String(o)}] (getBloc) Found existing isolated instance.`, t), r;
139
+ const c = this.findIsolatedBlocInstance(e, o);
140
+ if (c)
141
+ return c;
114
142
  if (t.throwIfNotFound)
115
143
  throw new Error(`Isolated bloc ${e.name} not found`);
116
144
  } else {
117
- const r = this.findRegisteredBlocInstance(e, o);
118
- if (r)
119
- return this.log(`[${e.name}:${String(o)}] (getBloc) Found existing registered instance.`, t), r;
145
+ const c = this.findRegisteredBlocInstance(e, o);
146
+ if (c)
147
+ return c;
120
148
  if (t.throwIfNotFound)
121
149
  throw new Error(`Registered bloc ${e.name} not found`);
122
150
  }
123
- const a = this.createNewBlocInstance(
124
- e,
125
- o,
126
- t
127
- );
128
- if (this.log(`[${e.name}:${String(o)}] (getBloc) No existing instance found. Creating new one.`, t, a), !a)
129
- throw new Error(`[getBloc] Failed to create bloc instance for ${e.name}. This should never happen.`);
151
+ const a = this.createNewBlocInstance(e, o, t);
152
+ if (this.log(
153
+ `[${e.name}:${String(o)}] (getBloc) No existing instance found. Creating new one.`,
154
+ t,
155
+ a
156
+ ), !a)
157
+ throw new Error(
158
+ `[getBloc] Failed to create bloc instance for ${e.name}. This should never happen.`
159
+ );
130
160
  return a;
131
161
  });
132
162
  /**
@@ -189,7 +219,10 @@ const c = class c {
189
219
  i(this, "getMemoryStats", () => ({
190
220
  totalBlocs: this.uidRegistry.size,
191
221
  registeredBlocs: this.blocInstanceMap.size,
192
- isolatedBlocs: Array.from(this.isolatedBlocMap.values()).reduce((e, t) => e + t.length, 0),
222
+ isolatedBlocs: Array.from(this.isolatedBlocMap.values()).reduce(
223
+ (e, t) => e + t.length,
224
+ 0
225
+ ),
193
226
  keepAliveBlocs: this.keepAliveBlocs.size,
194
227
  isolatedBlocTypes: this.isolatedBlocMap.size
195
228
  }));
@@ -204,7 +237,7 @@ const c = class c {
204
237
  });
205
238
  const { __unsafe_ignore_singleton: t = !1 } = e;
206
239
  if (!t)
207
- return c.instance;
240
+ return r.instance;
208
241
  g.setInstance(this);
209
242
  }
210
243
  /** @deprecated Use getInstance() instead */
@@ -212,10 +245,10 @@ const c = class c {
212
245
  return g.getInstance();
213
246
  }
214
247
  static get getAllBlocs() {
215
- return c.instance.getAllBlocs;
248
+ return r.instance.getAllBlocs;
216
249
  }
217
250
  static get log() {
218
- return c.instance.log;
251
+ return r.instance.log;
219
252
  }
220
253
  /**
221
254
  * Gets the current Blac instance
@@ -225,13 +258,13 @@ const c = class c {
225
258
  return g.getInstance();
226
259
  }
227
260
  static get warn() {
228
- return c.instance.warn;
261
+ return r.instance.warn;
229
262
  }
230
263
  static get error() {
231
- return c.instance.error;
264
+ return r.instance.error;
232
265
  }
233
266
  static get disposeBloc() {
234
- return c.instance.disposeBloc;
267
+ return r.instance.disposeBloc;
235
268
  }
236
269
  /**
237
270
  * Creates a unique key for a bloc instance in the map based on the bloc class name and instance ID
@@ -285,7 +318,7 @@ const c = class c {
285
318
  const t = e.constructor, s = this.isolatedBlocMap.get(t);
286
319
  let n = !1;
287
320
  if (s) {
288
- const a = s.findIndex((r) => r.uid === e.uid);
321
+ const a = s.findIndex((c) => c.uid === e.uid);
289
322
  a !== -1 && (s.splice(a, 1), n = !0), s.length === 0 && this.isolatedBlocMap.delete(t);
290
323
  }
291
324
  const o = this.isolatedBlocIndex.delete(e.uid);
@@ -317,35 +350,35 @@ const c = class c {
317
350
  */
318
351
  createNewBlocInstance(e, t, s = {}) {
319
352
  const { props: n, instanceRef: o } = s, a = new e(n);
320
- return a._instanceRef = o, a.props = n || null, a._updateId(t), a._setDisposalHandler((r) => this.disposeBloc(r)), a.isIsolated ? (this.registerIsolatedBlocInstance(a), a) : (this.registerBlocInstance(a), a);
353
+ return a._instanceRef = o, a.props = n || null, a._updateId(t), a._setDisposalHandler((c) => this.disposeBloc(c)), a.isIsolated ? (this.registerIsolatedBlocInstance(a), a) : (this.registerBlocInstance(a), a);
321
354
  }
322
355
  static get activateBloc() {
323
- return c.instance.activateBloc;
356
+ return r.instance.activateBloc;
324
357
  }
325
358
  static get getBloc() {
326
- return c.instance.getBloc;
359
+ return r.instance.getBloc;
327
360
  }
328
361
  static get getBlocOrThrow() {
329
- return c.instance.getBlocOrThrow;
362
+ return r.instance.getBlocOrThrow;
330
363
  }
331
364
  static get disposeKeepAliveBlocs() {
332
- return c.instance.disposeKeepAliveBlocs;
365
+ return r.instance.disposeKeepAliveBlocs;
333
366
  }
334
367
  static get disposeBlocs() {
335
- return c.instance.disposeBlocs;
368
+ return r.instance.disposeBlocs;
336
369
  }
337
370
  static get getMemoryStats() {
338
- return c.instance.getMemoryStats;
371
+ return r.instance.getMemoryStats;
339
372
  }
340
373
  static get validateConsumers() {
341
- return c.instance.validateConsumers;
374
+ return r.instance.validateConsumers;
342
375
  }
343
376
  };
344
377
  /** Flag to enable/disable logging */
345
- i(c, "enableLog", !1), i(c, "resetInstance", () => {
378
+ i(r, "enableLog", !1), i(r, "logLevel", "warn"), i(r, "resetInstance", () => {
346
379
  g.resetInstance();
347
380
  });
348
- let l = c;
381
+ let l = r;
349
382
  class w {
350
383
  /**
351
384
  * Creates a new BlacObservable instance
@@ -386,7 +419,7 @@ class w {
386
419
  * @param observer - The observer to unsubscribe
387
420
  */
388
421
  unsubscribe(e) {
389
- this._observers.delete(e), this.size === 0 && l.log("BlacObservable.unsubscribe: No observers left.", this.bloc);
422
+ this._observers.delete(e), this.size === 0 && (l.log("BlacObservable.unsubscribe: No observers left.", this.bloc), this.bloc._consumers.size === 0 && !this.bloc._keepAlive && this.bloc._disposalState === "active" && (l.log(`[${this.bloc._name}:${this.bloc._id}] No observers or consumers left. Scheduling disposal.`), this.bloc._scheduleDisposal()));
390
423
  }
391
424
  /**
392
425
  * Notifies all observers of a state change
@@ -398,26 +431,26 @@ class w {
398
431
  this._observers.forEach((n) => {
399
432
  let o = !1;
400
433
  if (n.dependencyArray) {
401
- const a = n.lastState, r = n.dependencyArray(e);
434
+ const a = n.lastState, c = n.dependencyArray(e, t);
402
435
  if (!a)
403
436
  o = !0;
404
- else if (a.length !== r.length)
437
+ else if (a.length !== c.length)
405
438
  o = !0;
406
439
  else
407
- for (let h = 0; h < r.length; h++) {
408
- const _ = r[h], p = a[h] || [];
440
+ for (let h = 0; h < c.length; h++) {
441
+ const _ = a[h] || [], p = c[h] || [];
409
442
  if (_.length !== p.length) {
410
443
  o = !0;
411
444
  break;
412
445
  }
413
- for (let f = 0; f < _.length; f++)
446
+ for (let f = 0; f < p.length; f++)
414
447
  if (!Object.is(_[f], p[f])) {
415
448
  o = !0;
416
449
  break;
417
450
  }
418
451
  if (o) break;
419
452
  }
420
- n.lastState = r;
453
+ n.lastState = c;
421
454
  } else
422
455
  o = !0;
423
456
  o && n.fn(e, t, s);
@@ -430,13 +463,13 @@ class w {
430
463
  this._observers.clear();
431
464
  }
432
465
  }
433
- function S() {
466
+ function b() {
434
467
  return typeof crypto < "u" && crypto.randomUUID ? crypto.randomUUID() : "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(d) {
435
468
  const e = Math.random() * 16 | 0;
436
469
  return (d === "x" ? e : e & 3 | 8).toString(16);
437
470
  });
438
471
  }
439
- class B {
472
+ class v {
440
473
  /**
441
474
  * Creates a new BlocBase instance with the given initial state.
442
475
  * Sets up the observer, registers with the Blac manager, and initializes addons.
@@ -444,7 +477,7 @@ class B {
444
477
  * @param initialState The initial state value for this Bloc
445
478
  */
446
479
  constructor(e) {
447
- i(this, "uid", S());
480
+ i(this, "uid", b());
448
481
  /**
449
482
  * Defines how dependencies are selected from the state for efficient updates.
450
483
  * When provided, observers will only be notified when selected dependencies change.
@@ -558,7 +591,8 @@ class B {
558
591
  * @param consumerRef Optional reference to the consumer object for cleanup validation
559
592
  */
560
593
  i(this, "_addConsumer", (e, t) => {
561
- this._disposalState === "active" && (this._consumers.has(e) || (this._consumers.add(e), t && this._consumerRefs.set(e, new WeakRef(t))));
594
+ var s;
595
+ this._disposalState === "active" && (this._consumers.has(e) || (this._consumers.add(e), t && this._consumerRefs.set(e, new WeakRef(t)), (s = globalThis.Blac) == null || s.log(`[${this._name}:${this._id}] Consumer added. Total consumers: ${this._consumers.size}`)));
562
596
  });
563
597
  /**
564
598
  * @internal
@@ -568,7 +602,8 @@ class B {
568
602
  * @param consumerId The unique ID of the consumer being removed
569
603
  */
570
604
  i(this, "_removeConsumer", (e) => {
571
- this._consumers.has(e) && (this._consumers.delete(e), this._consumerRefs.delete(e), this._consumers.size === 0 && !this._keepAlive && this._disposalState === "active" && this._scheduleDisposal());
605
+ var t, s;
606
+ this._consumers.has(e) && (this._consumers.delete(e), this._consumerRefs.delete(e), (t = globalThis.Blac) == null || t.log(`[${this._name}:${this._id}] Consumer removed. Remaining consumers: ${this._consumers.size}, keepAlive: ${this._keepAlive}`), this._consumers.size === 0 && !this._keepAlive && this._disposalState === "active" && ((s = globalThis.Blac) == null || s.log(`[${this._name}:${this._id}] No consumers left and not keep-alive. Scheduling disposal.`), this._scheduleDisposal()));
572
607
  });
573
608
  /**
574
609
  * @internal
@@ -676,13 +711,13 @@ class B {
676
711
  * Use this when state should not be shared between components.
677
712
  * @default false
678
713
  */
679
- i(B, "isolated", !1), /**
714
+ i(v, "isolated", !1), /**
680
715
  * When true, the Bloc instance persists even when there are no active consumers.
681
716
  * Useful for maintaining state between component unmount/remount cycles.
682
717
  * @default false
683
718
  */
684
- i(B, "keepAlive", !1);
685
- class b extends B {
719
+ i(v, "keepAlive", !1);
720
+ class S extends v {
686
721
  constructor() {
687
722
  super(...arguments);
688
723
  // Stores handlers: Map<EventConstructor (subtype of A), HandlerFunction>
@@ -748,22 +783,22 @@ class b extends B {
748
783
  const s = t.constructor, n = this.eventHandlers.get(s);
749
784
  if (n) {
750
785
  const o = (a) => {
751
- const r = this.state;
752
- this._pushState(a, r, t);
786
+ const c = this.state;
787
+ this._pushState(a, c, t);
753
788
  };
754
789
  try {
755
790
  await n(t, o);
756
791
  } catch (a) {
757
- const r = t.constructor.name || "UnnamedConstructor", h = {
792
+ const c = t.constructor.name || "UnnamedConstructor", h = {
758
793
  blocName: this._name,
759
794
  blocId: String(this._id),
760
- eventType: r,
795
+ eventType: c,
761
796
  currentState: this.state,
762
797
  action: t,
763
798
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
764
799
  };
765
800
  if (l.error(
766
- `[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${r}':`,
801
+ `[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${c}':`,
767
802
  a,
768
803
  "Context:",
769
804
  h
@@ -782,7 +817,7 @@ class b extends B {
782
817
  }
783
818
  }
784
819
  }
785
- class k extends B {
820
+ class k extends v {
786
821
  /**
787
822
  * Updates the current state and notifies all observers of the change.
788
823
  * If the new state is identical to the current state (using Object.is),
@@ -828,7 +863,7 @@ class k extends B {
828
863
  });
829
864
  }
830
865
  }
831
- class x {
866
+ class $ {
832
867
  /**
833
868
  * Sets up a clean test environment
834
869
  */
@@ -855,13 +890,13 @@ class x {
855
890
  return new Promise((n, o) => {
856
891
  const a = setTimeout(() => {
857
892
  o(new Error(`Timeout waiting for state matching predicate after ${s}ms`));
858
- }, s), r = e._observer.subscribe({
893
+ }, s), c = e._observer.subscribe({
859
894
  id: `test-waiter-${crypto.randomUUID()}`,
860
895
  fn: (h) => {
861
- t(h) && (clearTimeout(a), r(), n(h));
896
+ t(h) && (clearTimeout(a), c(), n(h));
862
897
  }
863
898
  });
864
- t(e.state) && (clearTimeout(a), r(), n(e.state));
899
+ t(e.state) && (clearTimeout(a), c(), n(e.state));
865
900
  });
866
901
  }
867
902
  /**
@@ -870,7 +905,7 @@ class x {
870
905
  static async expectStates(e, t, s = 5e3) {
871
906
  const n = [];
872
907
  return new Promise((o, a) => {
873
- const r = setTimeout(() => {
908
+ const c = setTimeout(() => {
874
909
  a(new Error(
875
910
  `Timeout waiting for states. Expected: ${JSON.stringify(t)}, Received: ${JSON.stringify(n)}`
876
911
  ));
@@ -878,12 +913,12 @@ class x {
878
913
  id: `test-expecter-${crypto.randomUUID()}`,
879
914
  fn: (_) => {
880
915
  if (n.push(_), n.length === t.length) {
881
- clearTimeout(r), h();
916
+ clearTimeout(c), h();
882
917
  for (let p = 0; p < t.length; p++) {
883
- const f = t[p], I = n[p];
884
- if (JSON.stringify(f) !== JSON.stringify(I)) {
918
+ const f = t[p], m = n[p];
919
+ if (JSON.stringify(f) !== JSON.stringify(m)) {
885
920
  a(new Error(
886
- `State mismatch at index ${p}. Expected: ${JSON.stringify(f)}, Received: ${JSON.stringify(I)}`
921
+ `State mismatch at index ${p}. Expected: ${JSON.stringify(f)}, Received: ${JSON.stringify(m)}`
887
922
  ));
888
923
  return;
889
924
  }
@@ -895,8 +930,8 @@ class x {
895
930
  });
896
931
  }
897
932
  }
898
- i(x, "originalInstance");
899
- class A extends b {
933
+ i($, "originalInstance");
934
+ class M extends S {
900
935
  constructor(t) {
901
936
  super(t);
902
937
  i(this, "mockHandlers", /* @__PURE__ */ new Map());
@@ -974,14 +1009,14 @@ Memory Leak Detection Report:
974
1009
  export {
975
1010
  l as Blac,
976
1011
  w as BlacObservable,
977
- b as Bloc,
978
- B as BlocBase,
979
- x as BlocTest,
1012
+ S as Bloc,
1013
+ v as BlocBase,
1014
+ $ as BlocTest,
980
1015
  k as Cubit,
981
1016
  R as MemoryLeakDetector,
982
- A as MockBloc,
1017
+ M as MockBloc,
983
1018
  E as MockCubit,
984
- S as generateUUID,
985
- M as setBlacInstanceManager
1019
+ b as generateUUID,
1020
+ A as setBlacInstanceManager
986
1021
  };
987
1022
  //# sourceMappingURL=index.es.js.map