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

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.d.ts CHANGED
@@ -73,6 +73,7 @@ export declare class Blac {
73
73
  * @param bloc - The bloc instance to dispose
74
74
  */
75
75
  disposeBloc: (bloc: BlocBase<unknown>) => void;
76
+ static get disposeBloc(): (bloc: BlocBase<unknown>) => void;
76
77
  /**
77
78
  * Creates a unique key for a bloc instance in the map based on the bloc class name and instance ID
78
79
  * @param blocClassName - The name of the bloc class
@@ -285,9 +286,11 @@ export declare type BlacObserver<S> = {
285
286
  };
286
287
 
287
288
  export declare abstract class Bloc<S, // State type
288
- A extends object, // Base Action/Event type, constrained to object
289
+ A extends BlocEventConstraint = BlocEventConstraint, // Base Action/Event type with proper constraints
289
290
  P = unknown> extends BlocBase<S, P> {
290
291
  readonly eventHandlers: Map<new (...args: any[]) => A, (event: A, emit: (newState: S) => void) => void | Promise<void>>;
292
+ /* Excluded from this release type: _eventQueue */
293
+ /* Excluded from this release type: _isProcessingEvent */
291
294
  /**
292
295
  * Registers an event handler for a specific event type.
293
296
  * This method is typically called in the constructor of a derived Bloc class.
@@ -298,11 +301,12 @@ P = unknown> extends BlocBase<S, P> {
298
301
  protected on<E extends A>(eventConstructor: new (...args: any[]) => E, handler: (event: E, emit: (newState: S) => void) => void | Promise<void>): void;
299
302
  /**
300
303
  * Dispatches an action/event to the Bloc.
301
- * If a handler is registered for this specific event type (via 'on'), it will be invoked.
302
- * Asynchronous handlers are awaited.
304
+ * Events are queued and processed sequentially to prevent race conditions.
303
305
  * @param action The action/event instance to be processed.
304
306
  */
305
307
  add: (action: A) => Promise<void>;
308
+ /* Excluded from this release type: _processEventQueue */
309
+ /* Excluded from this release type: _processEvent */
306
310
  }
307
311
 
308
312
  /**
@@ -314,7 +318,7 @@ P = unknown> extends BlocBase<S, P> {
314
318
  * @template P The type of props that can be passed during instance creation (optional)
315
319
  */
316
320
  export declare abstract class BlocBase<S, P = unknown> {
317
- uid: `${string}-${string}-${string}-${string}-${string}`;
321
+ uid: string;
318
322
  /**
319
323
  * When true, every consumer will receive its own unique instance of this Bloc.
320
324
  * Use this when state should not be shared between components.
@@ -348,6 +352,7 @@ export declare abstract class BlocBase<S, P = unknown> {
348
352
  * Timestamp when this Bloc instance was created, useful for debugging and performance tracking.
349
353
  */
350
354
  readonly _createdAt: number;
355
+ /* Excluded from this release type: _disposalState */
351
356
  /* Excluded from this release type: _state */
352
357
  /* Excluded from this release type: _oldState */
353
358
  /**
@@ -355,6 +360,10 @@ export declare abstract class BlocBase<S, P = unknown> {
355
360
  * Can be used to configure or parameterize the Bloc's behavior.
356
361
  */
357
362
  props: P | null;
363
+ /* Excluded from this release type: _batchingLock */
364
+ /* Excluded from this release type: _pendingUpdates */
365
+ /* Excluded from this release type: _consumerRefs */
366
+ /* Excluded from this release type: _validateConsumers */
358
367
  /**
359
368
  * Creates a new BlocBase instance with the given initial state.
360
369
  * Sets up the observer, registers with the Blac manager, and initializes addons.
@@ -372,7 +381,6 @@ export declare abstract class BlocBase<S, P = unknown> {
372
381
  /* Excluded from this release type: _dispose */
373
382
  /* Excluded from this release type: onDispose */
374
383
  /* Excluded from this release type: _consumers */
375
- /* Excluded from this release type: _consumerRefs */
376
384
  /* Excluded from this release type: _addConsumer */
377
385
  /* Excluded from this release type: _removeConsumer */
378
386
  /* Excluded from this release type: _disposalHandler */
@@ -380,7 +388,6 @@ export declare abstract class BlocBase<S, P = unknown> {
380
388
  /* Excluded from this release type: _scheduleDisposal */
381
389
  lastUpdate: number;
382
390
  /* Excluded from this release type: _batchingEnabled */
383
- /* Excluded from this release type: _pendingUpdates */
384
391
  /* Excluded from this release type: _pushState */
385
392
  /**
386
393
  * Enables batching for multiple state updates
@@ -413,8 +420,34 @@ export declare type BlocConstructor<B> = new (...args: any) => B;
413
420
  export declare type BlocConstructorParameters<B extends BlocBase<any>> = BlocConstructor<B> extends new (...args: infer P) => B ? P : never;
414
421
 
415
422
  /**
416
- * Represents a function type for determining hook dependencies based on state changes
417
- * @template B - The BlocGeneric type
423
+ * Error boundary interface for Bloc error handling
424
+ */
425
+ export declare interface BlocErrorBoundary<S, A extends BlocEventConstraint> {
426
+ onError: (error: Error, event: A, currentState: S, bloc: {
427
+ name: string;
428
+ id: string;
429
+ }) => void | Promise<void>;
430
+ shouldRethrow?: (error: Error, event: A) => boolean;
431
+ }
432
+
433
+ /**
434
+ * Base interface for all Bloc events
435
+ * Events must be objects with a constructor to enable proper type matching
436
+ */
437
+ export declare interface BlocEvent {
438
+ readonly type?: string;
439
+ readonly timestamp?: number;
440
+ }
441
+
442
+ /**
443
+ * Enhanced constraint for Bloc events - must be objects with proper constructor
444
+ */
445
+ export declare type BlocEventConstraint = BlocEvent & object;
446
+
447
+ /**
448
+ * Function type for determining dependencies that trigger re-renders
449
+ * @template S The state type
450
+ * @returns Array of dependency arrays - if any dependency in any array changes, a re-render is triggered
418
451
  */
419
452
  export declare type BlocHookDependencyArrayFn<S> = (newState: S) => unknown[][];
420
453
 
@@ -426,6 +459,33 @@ export declare type BlocInstanceId = string | number | undefined;
426
459
  */
427
460
  export declare type BlocState<T> = T extends BlocBase<infer S> ? S : never;
428
461
 
462
+ /**
463
+ * Test utilities for Blac state management
464
+ */
465
+ export declare class BlocTest {
466
+ private static originalInstance;
467
+ /**
468
+ * Sets up a clean test environment
469
+ */
470
+ static setUp(): void;
471
+ /**
472
+ * Tears down the test environment and restores original state
473
+ */
474
+ static tearDown(): void;
475
+ /**
476
+ * Creates a test bloc with automatic cleanup
477
+ */
478
+ static createBloc<T extends BlocBase<any>>(BlocClass: new (...args: any[]) => T, ...args: any[]): T;
479
+ /**
480
+ * Waits for a bloc to emit a specific state
481
+ */
482
+ static waitForState<T extends BlocBase<S>, S>(bloc: T, predicate: (state: S) => boolean, timeout?: number): Promise<S>;
483
+ /**
484
+ * Expects a bloc to emit specific states in order
485
+ */
486
+ static expectStates<T extends BlocBase<S>, S>(bloc: T, expectedStates: S[], timeout?: number): Promise<void>;
487
+ }
488
+
429
489
  /**
430
490
  * A Cubit is a simpler version of a Bloc that doesn't handle events.
431
491
  * It manages state and provides methods to update it.
@@ -453,6 +513,16 @@ export declare abstract class Cubit<S, P = null> extends BlocBase<S, P> {
453
513
 
454
514
  declare type DependencySelector<S> = (newState: S) => unknown[][];
455
515
 
516
+ /**
517
+ * Cross-platform UUID v4 generator that works in all environments including React Native/Hermes
518
+ * Falls back to crypto.randomUUID() when available, otherwise uses Math.random()
519
+ */
520
+ /**
521
+ * Generates a UUID v4 string compatible with all JavaScript environments
522
+ * @returns A UUID v4 string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
523
+ */
524
+ export declare function generateUUID(): string;
525
+
456
526
  export declare interface GetBlocOptions<B extends BlocBase<unknown>> {
457
527
  id?: string;
458
528
  selector?: BlocHookDependencyArrayFn<BlocState<B>>;
@@ -468,6 +538,62 @@ export declare interface GetBlocOptions<B extends BlocBase<unknown>> {
468
538
  */
469
539
  export declare type InferPropsFromGeneric<T> = T extends Bloc<any, any, infer P> ? P : T extends Cubit<any, infer P> ? P : never;
470
540
 
541
+ /**
542
+ * Memory leak detector for tests
543
+ */
544
+ export declare class MemoryLeakDetector {
545
+ private initialStats;
546
+ constructor();
547
+ /**
548
+ * Check for memory leaks and return a report
549
+ */
550
+ checkForLeaks(): {
551
+ hasLeaks: boolean;
552
+ report: string;
553
+ stats: ReturnType<typeof Blac.getMemoryStats>;
554
+ };
555
+ }
556
+
557
+ /**
558
+ * Mock Bloc for testing
559
+ */
560
+ export declare class MockBloc<S, A extends BlocEventConstraint = BlocEventConstraint> extends Bloc<S, A> {
561
+ private mockHandlers;
562
+ constructor(initialState: S);
563
+ /**
564
+ * Mock an event handler for testing
565
+ */
566
+ mockEventHandler<E extends A>(eventConstructor: new (...args: any[]) => E, handler: (event: E, emit: (newState: S) => void) => void | Promise<void>): void;
567
+ /**
568
+ * Get the number of registered handlers
569
+ */
570
+ getHandlerCount(): number;
571
+ /**
572
+ * Check if a handler is registered for an event type
573
+ */
574
+ hasHandler(eventConstructor: new (...args: any[]) => A): boolean;
575
+ }
576
+
577
+ /**
578
+ * Mock Cubit for testing
579
+ */
580
+ export declare class MockCubit<S> extends Cubit<S> {
581
+ private stateHistory;
582
+ constructor(initialState: S);
583
+ /**
584
+ * Override emit to track state history
585
+ */
586
+ emit(newState: S): void;
587
+ /**
588
+ * Get the history of all states
589
+ */
590
+ getStateHistory(): S[];
591
+ /**
592
+ * Clear state history
593
+ */
594
+ clearStateHistory(): void;
595
+ }
596
+
471
597
  /**
472
598
  * Sets a custom instance manager (useful for testing or advanced patterns)
473
599
  */