@blac/core 2.0.0-rc-6 → 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,24 +136,28 @@ 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
- return this.log(`[${e.name}:${String(o)}] (getBloc) No existing instance found. Creating new one.`, t, a), a;
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
+ );
160
+ return a;
129
161
  });
130
162
  /**
131
163
  * Gets a bloc instance or throws an error if it doesn't exist
@@ -187,7 +219,10 @@ const c = class c {
187
219
  i(this, "getMemoryStats", () => ({
188
220
  totalBlocs: this.uidRegistry.size,
189
221
  registeredBlocs: this.blocInstanceMap.size,
190
- 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
+ ),
191
226
  keepAliveBlocs: this.keepAliveBlocs.size,
192
227
  isolatedBlocTypes: this.isolatedBlocMap.size
193
228
  }));
@@ -202,7 +237,7 @@ const c = class c {
202
237
  });
203
238
  const { __unsafe_ignore_singleton: t = !1 } = e;
204
239
  if (!t)
205
- return c.instance;
240
+ return r.instance;
206
241
  g.setInstance(this);
207
242
  }
208
243
  /** @deprecated Use getInstance() instead */
@@ -210,10 +245,10 @@ const c = class c {
210
245
  return g.getInstance();
211
246
  }
212
247
  static get getAllBlocs() {
213
- return c.instance.getAllBlocs;
248
+ return r.instance.getAllBlocs;
214
249
  }
215
250
  static get log() {
216
- return c.instance.log;
251
+ return r.instance.log;
217
252
  }
218
253
  /**
219
254
  * Gets the current Blac instance
@@ -223,13 +258,13 @@ const c = class c {
223
258
  return g.getInstance();
224
259
  }
225
260
  static get warn() {
226
- return c.instance.warn;
261
+ return r.instance.warn;
227
262
  }
228
263
  static get error() {
229
- return c.instance.error;
264
+ return r.instance.error;
230
265
  }
231
266
  static get disposeBloc() {
232
- return c.instance.disposeBloc;
267
+ return r.instance.disposeBloc;
233
268
  }
234
269
  /**
235
270
  * Creates a unique key for a bloc instance in the map based on the bloc class name and instance ID
@@ -283,7 +318,7 @@ const c = class c {
283
318
  const t = e.constructor, s = this.isolatedBlocMap.get(t);
284
319
  let n = !1;
285
320
  if (s) {
286
- const a = s.findIndex((r) => r.uid === e.uid);
321
+ const a = s.findIndex((c) => c.uid === e.uid);
287
322
  a !== -1 && (s.splice(a, 1), n = !0), s.length === 0 && this.isolatedBlocMap.delete(t);
288
323
  }
289
324
  const o = this.isolatedBlocIndex.delete(e.uid);
@@ -315,35 +350,35 @@ const c = class c {
315
350
  */
316
351
  createNewBlocInstance(e, t, s = {}) {
317
352
  const { props: n, instanceRef: o } = s, a = new e(n);
318
- 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);
319
354
  }
320
355
  static get activateBloc() {
321
- return c.instance.activateBloc;
356
+ return r.instance.activateBloc;
322
357
  }
323
358
  static get getBloc() {
324
- return c.instance.getBloc;
359
+ return r.instance.getBloc;
325
360
  }
326
361
  static get getBlocOrThrow() {
327
- return c.instance.getBlocOrThrow;
362
+ return r.instance.getBlocOrThrow;
328
363
  }
329
364
  static get disposeKeepAliveBlocs() {
330
- return c.instance.disposeKeepAliveBlocs;
365
+ return r.instance.disposeKeepAliveBlocs;
331
366
  }
332
367
  static get disposeBlocs() {
333
- return c.instance.disposeBlocs;
368
+ return r.instance.disposeBlocs;
334
369
  }
335
370
  static get getMemoryStats() {
336
- return c.instance.getMemoryStats;
371
+ return r.instance.getMemoryStats;
337
372
  }
338
373
  static get validateConsumers() {
339
- return c.instance.validateConsumers;
374
+ return r.instance.validateConsumers;
340
375
  }
341
376
  };
342
377
  /** Flag to enable/disable logging */
343
- i(c, "enableLog", !1), i(c, "resetInstance", () => {
378
+ i(r, "enableLog", !1), i(r, "logLevel", "warn"), i(r, "resetInstance", () => {
344
379
  g.resetInstance();
345
380
  });
346
- let l = c;
381
+ let l = r;
347
382
  class w {
348
383
  /**
349
384
  * Creates a new BlacObservable instance
@@ -384,7 +419,7 @@ class w {
384
419
  * @param observer - The observer to unsubscribe
385
420
  */
386
421
  unsubscribe(e) {
387
- 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()));
388
423
  }
389
424
  /**
390
425
  * Notifies all observers of a state change
@@ -396,26 +431,26 @@ class w {
396
431
  this._observers.forEach((n) => {
397
432
  let o = !1;
398
433
  if (n.dependencyArray) {
399
- const a = n.lastState, r = n.dependencyArray(e);
434
+ const a = n.lastState, c = n.dependencyArray(e, t);
400
435
  if (!a)
401
436
  o = !0;
402
- else if (a.length !== r.length)
437
+ else if (a.length !== c.length)
403
438
  o = !0;
404
439
  else
405
- for (let h = 0; h < r.length; h++) {
406
- const _ = r[h], p = a[h] || [];
440
+ for (let h = 0; h < c.length; h++) {
441
+ const _ = a[h] || [], p = c[h] || [];
407
442
  if (_.length !== p.length) {
408
443
  o = !0;
409
444
  break;
410
445
  }
411
- for (let f = 0; f < _.length; f++)
446
+ for (let f = 0; f < p.length; f++)
412
447
  if (!Object.is(_[f], p[f])) {
413
448
  o = !0;
414
449
  break;
415
450
  }
416
451
  if (o) break;
417
452
  }
418
- n.lastState = r;
453
+ n.lastState = c;
419
454
  } else
420
455
  o = !0;
421
456
  o && n.fn(e, t, s);
@@ -428,13 +463,13 @@ class w {
428
463
  this._observers.clear();
429
464
  }
430
465
  }
431
- function S() {
466
+ function b() {
432
467
  return typeof crypto < "u" && crypto.randomUUID ? crypto.randomUUID() : "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(d) {
433
468
  const e = Math.random() * 16 | 0;
434
469
  return (d === "x" ? e : e & 3 | 8).toString(16);
435
470
  });
436
471
  }
437
- class B {
472
+ class v {
438
473
  /**
439
474
  * Creates a new BlocBase instance with the given initial state.
440
475
  * Sets up the observer, registers with the Blac manager, and initializes addons.
@@ -442,7 +477,7 @@ class B {
442
477
  * @param initialState The initial state value for this Bloc
443
478
  */
444
479
  constructor(e) {
445
- i(this, "uid", S());
480
+ i(this, "uid", b());
446
481
  /**
447
482
  * Defines how dependencies are selected from the state for efficient updates.
448
483
  * When provided, observers will only be notified when selected dependencies change.
@@ -556,7 +591,8 @@ class B {
556
591
  * @param consumerRef Optional reference to the consumer object for cleanup validation
557
592
  */
558
593
  i(this, "_addConsumer", (e, t) => {
559
- 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}`)));
560
596
  });
561
597
  /**
562
598
  * @internal
@@ -566,7 +602,8 @@ class B {
566
602
  * @param consumerId The unique ID of the consumer being removed
567
603
  */
568
604
  i(this, "_removeConsumer", (e) => {
569
- 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()));
570
607
  });
571
608
  /**
572
609
  * @internal
@@ -674,13 +711,13 @@ class B {
674
711
  * Use this when state should not be shared between components.
675
712
  * @default false
676
713
  */
677
- i(B, "isolated", !1), /**
714
+ i(v, "isolated", !1), /**
678
715
  * When true, the Bloc instance persists even when there are no active consumers.
679
716
  * Useful for maintaining state between component unmount/remount cycles.
680
717
  * @default false
681
718
  */
682
- i(B, "keepAlive", !1);
683
- class b extends B {
719
+ i(v, "keepAlive", !1);
720
+ class S extends v {
684
721
  constructor() {
685
722
  super(...arguments);
686
723
  // Stores handlers: Map<EventConstructor (subtype of A), HandlerFunction>
@@ -746,22 +783,22 @@ class b extends B {
746
783
  const s = t.constructor, n = this.eventHandlers.get(s);
747
784
  if (n) {
748
785
  const o = (a) => {
749
- const r = this.state;
750
- this._pushState(a, r, t);
786
+ const c = this.state;
787
+ this._pushState(a, c, t);
751
788
  };
752
789
  try {
753
790
  await n(t, o);
754
791
  } catch (a) {
755
- const r = t.constructor.name || "UnnamedConstructor", h = {
792
+ const c = t.constructor.name || "UnnamedConstructor", h = {
756
793
  blocName: this._name,
757
794
  blocId: String(this._id),
758
- eventType: r,
795
+ eventType: c,
759
796
  currentState: this.state,
760
797
  action: t,
761
798
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
762
799
  };
763
800
  if (l.error(
764
- `[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}':`,
765
802
  a,
766
803
  "Context:",
767
804
  h
@@ -780,7 +817,7 @@ class b extends B {
780
817
  }
781
818
  }
782
819
  }
783
- class k extends B {
820
+ class k extends v {
784
821
  /**
785
822
  * Updates the current state and notifies all observers of the change.
786
823
  * If the new state is identical to the current state (using Object.is),
@@ -826,7 +863,7 @@ class k extends B {
826
863
  });
827
864
  }
828
865
  }
829
- class x {
866
+ class $ {
830
867
  /**
831
868
  * Sets up a clean test environment
832
869
  */
@@ -853,13 +890,13 @@ class x {
853
890
  return new Promise((n, o) => {
854
891
  const a = setTimeout(() => {
855
892
  o(new Error(`Timeout waiting for state matching predicate after ${s}ms`));
856
- }, s), r = e._observer.subscribe({
893
+ }, s), c = e._observer.subscribe({
857
894
  id: `test-waiter-${crypto.randomUUID()}`,
858
895
  fn: (h) => {
859
- t(h) && (clearTimeout(a), r(), n(h));
896
+ t(h) && (clearTimeout(a), c(), n(h));
860
897
  }
861
898
  });
862
- t(e.state) && (clearTimeout(a), r(), n(e.state));
899
+ t(e.state) && (clearTimeout(a), c(), n(e.state));
863
900
  });
864
901
  }
865
902
  /**
@@ -868,7 +905,7 @@ class x {
868
905
  static async expectStates(e, t, s = 5e3) {
869
906
  const n = [];
870
907
  return new Promise((o, a) => {
871
- const r = setTimeout(() => {
908
+ const c = setTimeout(() => {
872
909
  a(new Error(
873
910
  `Timeout waiting for states. Expected: ${JSON.stringify(t)}, Received: ${JSON.stringify(n)}`
874
911
  ));
@@ -876,12 +913,12 @@ class x {
876
913
  id: `test-expecter-${crypto.randomUUID()}`,
877
914
  fn: (_) => {
878
915
  if (n.push(_), n.length === t.length) {
879
- clearTimeout(r), h();
916
+ clearTimeout(c), h();
880
917
  for (let p = 0; p < t.length; p++) {
881
- const f = t[p], I = n[p];
882
- if (JSON.stringify(f) !== JSON.stringify(I)) {
918
+ const f = t[p], m = n[p];
919
+ if (JSON.stringify(f) !== JSON.stringify(m)) {
883
920
  a(new Error(
884
- `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)}`
885
922
  ));
886
923
  return;
887
924
  }
@@ -893,8 +930,8 @@ class x {
893
930
  });
894
931
  }
895
932
  }
896
- i(x, "originalInstance");
897
- class A extends b {
933
+ i($, "originalInstance");
934
+ class M extends S {
898
935
  constructor(t) {
899
936
  super(t);
900
937
  i(this, "mockHandlers", /* @__PURE__ */ new Map());
@@ -972,14 +1009,14 @@ Memory Leak Detection Report:
972
1009
  export {
973
1010
  l as Blac,
974
1011
  w as BlacObservable,
975
- b as Bloc,
976
- B as BlocBase,
977
- x as BlocTest,
1012
+ S as Bloc,
1013
+ v as BlocBase,
1014
+ $ as BlocTest,
978
1015
  k as Cubit,
979
1016
  R as MemoryLeakDetector,
980
- A as MockBloc,
1017
+ M as MockBloc,
981
1018
  E as MockCubit,
982
- S as generateUUID,
983
- M as setBlacInstanceManager
1019
+ b as generateUUID,
1020
+ A as setBlacInstanceManager
984
1021
  };
985
1022
  //# sourceMappingURL=index.es.js.map