@blac/core 2.0.0-rc-4 → 2.0.0-rc-6
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/README.md +42 -0
- package/dist/index.cjs.js +10 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +146 -20
- package/dist/index.es.js +447 -207
- package/dist/index.es.js.map +1 -1
- package/package.json +1 -1
- package/src/Blac.ts +68 -35
- package/src/BlacObserver.ts +33 -19
- package/src/Bloc.ts +82 -21
- package/src/BlocBase.ts +115 -47
- package/src/index.ts +7 -0
- package/src/testing.ts +239 -0
- package/src/types.ts +25 -2
- package/src/utils/uuid.ts +22 -0
package/dist/index.d.ts
CHANGED
|
@@ -13,13 +13,13 @@ export declare class Blac {
|
|
|
13
13
|
static get instance(): Blac;
|
|
14
14
|
/** Timestamp when the instance was created */
|
|
15
15
|
createdAt: number;
|
|
16
|
-
static get getAllBlocs(): <B extends BlocConstructor<
|
|
16
|
+
static get getAllBlocs(): <B extends BlocConstructor<any>>(blocClass: B, options?: {
|
|
17
17
|
searchIsolated?: boolean;
|
|
18
18
|
}) => InstanceType<B>[];
|
|
19
19
|
/** Map storing all registered bloc instances by their class name and ID */
|
|
20
20
|
blocInstanceMap: Map<string, BlocBase<unknown>>;
|
|
21
21
|
/** Map storing isolated bloc instances grouped by their constructor */
|
|
22
|
-
isolatedBlocMap: Map<BlocConstructor<
|
|
22
|
+
isolatedBlocMap: Map<BlocConstructor<any>, BlocBase<unknown>[]>;
|
|
23
23
|
/** Map for O(1) lookup of isolated blocs by UID */
|
|
24
24
|
private isolatedBlocIndex;
|
|
25
25
|
/** Map tracking UIDs to prevent memory leaks */
|
|
@@ -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
|
|
@@ -96,7 +97,7 @@ export declare class Blac {
|
|
|
96
97
|
* @param id - The instance ID
|
|
97
98
|
* @returns The found bloc instance or undefined if not found
|
|
98
99
|
*/
|
|
99
|
-
findRegisteredBlocInstance<B extends BlocConstructor<
|
|
100
|
+
findRegisteredBlocInstance<B extends BlocConstructor<any>>(blocClass: B, id: BlocInstanceId): InstanceType<B> | undefined;
|
|
100
101
|
/**
|
|
101
102
|
* Registers an isolated bloc instance in the isolated registry
|
|
102
103
|
* @param bloc - The isolated bloc instance to register
|
|
@@ -110,7 +111,7 @@ export declare class Blac {
|
|
|
110
111
|
/**
|
|
111
112
|
* Finds an isolated bloc instance by its class and ID (O(n) lookup)
|
|
112
113
|
*/
|
|
113
|
-
findIsolatedBlocInstance<B extends BlocConstructor<
|
|
114
|
+
findIsolatedBlocInstance<B extends BlocConstructor<any>>(blocClass: B, id: BlocInstanceId): InstanceType<B> | undefined;
|
|
114
115
|
/**
|
|
115
116
|
* Finds an isolated bloc instance by UID (O(1) lookup)
|
|
116
117
|
*/
|
|
@@ -123,7 +124,7 @@ export declare class Blac {
|
|
|
123
124
|
* @param instanceRef - Optional reference string for the instance
|
|
124
125
|
* @returns The newly created bloc instance
|
|
125
126
|
*/
|
|
126
|
-
createNewBlocInstance<B extends BlocConstructor<BlocBase<
|
|
127
|
+
createNewBlocInstance<B extends BlocConstructor<BlocBase<any>>>(blocClass: B, id: BlocInstanceId, options?: GetBlocOptions<InstanceType<B>>): InstanceType<B>;
|
|
127
128
|
activateBloc: (bloc: BlocBase<unknown>) => void;
|
|
128
129
|
static get activateBloc(): (bloc: BlocBase<unknown>) => void;
|
|
129
130
|
/**
|
|
@@ -137,8 +138,8 @@ export declare class Blac {
|
|
|
137
138
|
* - instanceRef: Optional reference string for the instance
|
|
138
139
|
* @returns The bloc instance
|
|
139
140
|
*/
|
|
140
|
-
getBloc: <B extends BlocConstructor<BlocBase<
|
|
141
|
-
static get getBloc(): <B extends BlocConstructor<BlocBase<
|
|
141
|
+
getBloc: <B extends BlocConstructor<BlocBase<any>>>(blocClass: B, options?: GetBlocOptions<InstanceType<B>>) => InstanceType<B>;
|
|
142
|
+
static get getBloc(): <B extends BlocConstructor<BlocBase<any>>>(blocClass: B, options?: GetBlocOptions<InstanceType<B>>) => InstanceType<B>;
|
|
142
143
|
/**
|
|
143
144
|
* Gets a bloc instance or throws an error if it doesn't exist
|
|
144
145
|
* @param blocClass - The bloc class to get
|
|
@@ -147,12 +148,12 @@ export declare class Blac {
|
|
|
147
148
|
* - props: Properties to pass to the bloc constructor
|
|
148
149
|
* - instanceRef: Optional reference string for the instance
|
|
149
150
|
*/
|
|
150
|
-
getBlocOrThrow: <B extends BlocConstructor<
|
|
151
|
+
getBlocOrThrow: <B extends BlocConstructor<any>>(blocClass: B, options?: {
|
|
151
152
|
id?: BlocInstanceId;
|
|
152
153
|
props?: InferPropsFromGeneric<B>;
|
|
153
154
|
instanceRef?: string;
|
|
154
155
|
}) => InstanceType<B>;
|
|
155
|
-
static get getBlocOrThrow(): <B extends BlocConstructor<
|
|
156
|
+
static get getBlocOrThrow(): <B extends BlocConstructor<any>>(blocClass: B, options?: {
|
|
156
157
|
id?: BlocInstanceId;
|
|
157
158
|
props?: InferPropsFromGeneric<B> | undefined;
|
|
158
159
|
instanceRef?: string;
|
|
@@ -164,15 +165,15 @@ export declare class Blac {
|
|
|
164
165
|
* - searchIsolated: Whether to search in isolated blocs (defaults to bloc's isolated property)
|
|
165
166
|
* @returns Array of matching bloc instances
|
|
166
167
|
*/
|
|
167
|
-
getAllBlocs: <B extends BlocConstructor<
|
|
168
|
+
getAllBlocs: <B extends BlocConstructor<any>>(blocClass: B, options?: {
|
|
168
169
|
searchIsolated?: boolean;
|
|
169
170
|
}) => InstanceType<B>[];
|
|
170
171
|
/**
|
|
171
172
|
* Disposes all keep-alive blocs of a specific type
|
|
172
173
|
* @param blocClass - The bloc class to dispose
|
|
173
174
|
*/
|
|
174
|
-
disposeKeepAliveBlocs: <B extends BlocConstructor<
|
|
175
|
-
static get disposeKeepAliveBlocs(): <B extends BlocConstructor<
|
|
175
|
+
disposeKeepAliveBlocs: <B extends BlocConstructor<any>>(blocClass?: B) => void;
|
|
176
|
+
static get disposeKeepAliveBlocs(): <B extends BlocConstructor<any>>(blocClass?: B | undefined) => void;
|
|
176
177
|
/**
|
|
177
178
|
* Disposes all blocs matching a pattern
|
|
178
179
|
* @param predicate - Function to test each bloc for disposal
|
|
@@ -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
|
|
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
|
-
*
|
|
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:
|
|
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
|
-
*
|
|
417
|
-
|
|
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
|
*/
|