@blac/core 2.0.0-rc-7 → 2.0.0-rc-10
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.cjs.js +2 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +194 -7
- package/dist/index.es.js +1096 -563
- package/dist/index.es.js.map +1 -1
- package/package.json +5 -4
- package/src/Blac.ts +204 -99
- package/src/BlacObserver.ts +62 -25
- package/src/Bloc.ts +166 -149
- package/src/BlocBase.ts +269 -85
- package/src/Cubit.ts +1 -1
- package/src/__tests__/Bloc.event.test.ts +419 -0
- package/src/__tests__/BlocBase.lifecycle.test.ts +441 -0
- package/src/__tests__/Cubit.test.ts +473 -0
- package/src/adapter/BlacAdapter.ts +262 -0
- package/src/adapter/ConsumerTracker.ts +241 -0
- package/src/adapter/ProxyFactory.ts +308 -0
- package/src/adapter/__tests__/BlacAdapter.test.ts +561 -0
- package/src/adapter/__tests__/ConsumerTracker.test.ts +562 -0
- package/src/adapter/__tests__/ProxyFactory.test.ts +655 -0
- package/src/adapter/index.ts +3 -0
- package/src/index.ts +2 -1
- package/src/testing.ts +60 -47
- package/src/types.ts +21 -6
- package/src/utils/uuid.ts +4 -4
- package/src/BlacEvent.ts +0 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
export declare interface AdapterOptions<B extends BlocBase<any>> {
|
|
2
|
+
id?: string;
|
|
3
|
+
dependencies?: (bloc: B) => unknown[];
|
|
4
|
+
props?: InferPropsFromGeneric<B>;
|
|
5
|
+
onMount?: (bloc: B) => void;
|
|
6
|
+
onUnmount?: (bloc: B) => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
1
9
|
/**
|
|
2
10
|
* Main Blac class that manages the state management system.
|
|
3
11
|
* Can work with singleton pattern or dependency injection.
|
|
@@ -9,7 +17,6 @@
|
|
|
9
17
|
* - Providing logging and debugging capabilities
|
|
10
18
|
*/
|
|
11
19
|
export declare class Blac {
|
|
12
|
-
/** @deprecated Use getInstance() instead */
|
|
13
20
|
static get instance(): Blac;
|
|
14
21
|
/** Timestamp when the instance was created */
|
|
15
22
|
createdAt: number;
|
|
@@ -21,7 +28,7 @@ export declare class Blac {
|
|
|
21
28
|
/** Map storing isolated bloc instances grouped by their constructor */
|
|
22
29
|
isolatedBlocMap: Map<BlocConstructor<any>, BlocBase<unknown>[]>;
|
|
23
30
|
/** Map for O(1) lookup of isolated blocs by UID */
|
|
24
|
-
|
|
31
|
+
isolatedBlocIndex: Map<string, BlocBase<unknown>>;
|
|
25
32
|
/** Map tracking UIDs to prevent memory leaks */
|
|
26
33
|
uidRegistry: Map<string, BlocBase<unknown>>;
|
|
27
34
|
/** Set of keep-alive blocs for controlled cleanup */
|
|
@@ -37,6 +44,8 @@ export declare class Blac {
|
|
|
37
44
|
});
|
|
38
45
|
/** Flag to enable/disable logging */
|
|
39
46
|
static enableLog: boolean;
|
|
47
|
+
static logLevel: 'warn' | 'log';
|
|
48
|
+
static logSpy: ((...args: unknown[]) => void) | null;
|
|
40
49
|
/**
|
|
41
50
|
* Logs messages to console when logging is enabled
|
|
42
51
|
* @param args - Arguments to log
|
|
@@ -204,6 +213,54 @@ export declare class Blac {
|
|
|
204
213
|
static get validateConsumers(): () => void;
|
|
205
214
|
}
|
|
206
215
|
|
|
216
|
+
/**
|
|
217
|
+
* BlacAdapter orchestrates the connection between Bloc instances and React components.
|
|
218
|
+
* It manages dependency tracking, lifecycle hooks, and proxy creation.
|
|
219
|
+
*/
|
|
220
|
+
export declare class BlacAdapter<B extends BlocConstructor<BlocBase<any>>> {
|
|
221
|
+
readonly id: string;
|
|
222
|
+
readonly blocConstructor: B;
|
|
223
|
+
readonly componentRef: {
|
|
224
|
+
current: object;
|
|
225
|
+
};
|
|
226
|
+
blocInstance: InstanceType<B>;
|
|
227
|
+
private consumerTracker;
|
|
228
|
+
unmountTime: number;
|
|
229
|
+
mountTime: number;
|
|
230
|
+
private dependencyValues?;
|
|
231
|
+
private isUsingDependencies;
|
|
232
|
+
private hasMounted;
|
|
233
|
+
private mountCount;
|
|
234
|
+
options?: AdapterOptions<InstanceType<B>>;
|
|
235
|
+
constructor(instanceProps: {
|
|
236
|
+
componentRef: {
|
|
237
|
+
current: object;
|
|
238
|
+
};
|
|
239
|
+
blocConstructor: B;
|
|
240
|
+
}, options?: typeof BlacAdapter.options);
|
|
241
|
+
trackAccess(consumerRef: object, type: 'state' | 'class', path: string, value?: any): void;
|
|
242
|
+
getConsumerDependencies(consumerRef: object): DependencyArray | null;
|
|
243
|
+
shouldNotifyConsumer(consumerRef: object, changedPaths: Set<string>): boolean;
|
|
244
|
+
updateLastNotified(consumerRef: object): void;
|
|
245
|
+
resetConsumerTracking(): void;
|
|
246
|
+
createStateProxy: <T extends object>(props: {
|
|
247
|
+
target: T;
|
|
248
|
+
}) => T;
|
|
249
|
+
createClassProxy: <T extends object>(props: {
|
|
250
|
+
target: T;
|
|
251
|
+
}) => T;
|
|
252
|
+
updateBlocInstance(): InstanceType<B>;
|
|
253
|
+
createSubscription: (options: {
|
|
254
|
+
onChange: () => void;
|
|
255
|
+
}) => () => void;
|
|
256
|
+
mount: () => void;
|
|
257
|
+
unmount: () => void;
|
|
258
|
+
getProxyState: (state: BlocState<InstanceType<B>>) => BlocState<InstanceType<B>>;
|
|
259
|
+
getProxyBlocInstance: () => InstanceType<B>;
|
|
260
|
+
get calledOnMount(): boolean;
|
|
261
|
+
private hasDependencyValuesChanged;
|
|
262
|
+
}
|
|
263
|
+
|
|
207
264
|
/**
|
|
208
265
|
* Configuration options for the Blac instance
|
|
209
266
|
*/
|
|
@@ -280,7 +337,7 @@ export declare type BlacObserver<S> = {
|
|
|
280
337
|
/** Dispose function for the observer */
|
|
281
338
|
dispose?: () => void;
|
|
282
339
|
/** Cached state values used for dependency comparison */
|
|
283
|
-
lastState?: unknown[]
|
|
340
|
+
lastState?: unknown[];
|
|
284
341
|
/** Unique identifier for the observer */
|
|
285
342
|
id: string;
|
|
286
343
|
};
|
|
@@ -353,6 +410,7 @@ export declare abstract class BlocBase<S, P = unknown> {
|
|
|
353
410
|
*/
|
|
354
411
|
readonly _createdAt: number;
|
|
355
412
|
/* Excluded from this release type: _disposalState */
|
|
413
|
+
/* Excluded from this release type: _disposalRequestTime */
|
|
356
414
|
/* Excluded from this release type: _state */
|
|
357
415
|
/* Excluded from this release type: _oldState */
|
|
358
416
|
/**
|
|
@@ -374,10 +432,17 @@ export declare abstract class BlocBase<S, P = unknown> {
|
|
|
374
432
|
/**
|
|
375
433
|
* Returns the current state of the Bloc.
|
|
376
434
|
* Use this getter to access the state in a read-only manner.
|
|
435
|
+
* Returns the state even during transitional lifecycle states for React compatibility.
|
|
377
436
|
*/
|
|
378
437
|
get state(): S;
|
|
438
|
+
/**
|
|
439
|
+
* Returns whether this Bloc instance has been disposed.
|
|
440
|
+
* @returns true if the bloc is in DISPOSED state
|
|
441
|
+
*/
|
|
442
|
+
get isDisposed(): boolean;
|
|
379
443
|
/* Excluded from this release type: _name */
|
|
380
444
|
/* Excluded from this release type: _updateId */
|
|
445
|
+
/* Excluded from this release type: _atomicStateTransition */
|
|
381
446
|
/* Excluded from this release type: _dispose */
|
|
382
447
|
/* Excluded from this release type: onDispose */
|
|
383
448
|
/* Excluded from this release type: _consumers */
|
|
@@ -411,7 +476,10 @@ export declare type BlocClassNoParams<B> = new (args: never[]) => B;
|
|
|
411
476
|
* Represents a constructor type for a Bloc that can take any parameters
|
|
412
477
|
* @template B - The type of the Bloc instance
|
|
413
478
|
*/
|
|
414
|
-
export declare type BlocConstructor<B> = new (...args: any) => B
|
|
479
|
+
export declare type BlocConstructor<B> = (new (...args: any) => B) & {
|
|
480
|
+
isolated?: boolean;
|
|
481
|
+
keepAlive?: boolean;
|
|
482
|
+
};
|
|
415
483
|
|
|
416
484
|
/**
|
|
417
485
|
* Extracts the constructor parameters type from a BlocBase
|
|
@@ -446,13 +514,29 @@ export declare type BlocEventConstraint = BlocEvent & object;
|
|
|
446
514
|
|
|
447
515
|
/**
|
|
448
516
|
* Function type for determining dependencies that trigger re-renders
|
|
517
|
+
* Similar to React's useEffect dependency array - if any dependency changes, a re-render is triggered
|
|
449
518
|
* @template S The state type
|
|
450
|
-
* @
|
|
519
|
+
* @template I The bloc instance type
|
|
520
|
+
* @param currentState The current state
|
|
521
|
+
* @param previousState The previous state (undefined on first call)
|
|
522
|
+
* @param instance The bloc instance
|
|
523
|
+
* @returns Array of dependencies - if any dependency changes, a re-render is triggered
|
|
451
524
|
*/
|
|
452
|
-
export declare type BlocHookDependencyArrayFn<S> = (
|
|
525
|
+
export declare type BlocHookDependencyArrayFn<S, I = any> = (currentState: S, previousState: S | undefined, instance: I) => unknown[];
|
|
453
526
|
|
|
454
527
|
export declare type BlocInstanceId = string | number | undefined;
|
|
455
528
|
|
|
529
|
+
/**
|
|
530
|
+
* Enum representing the lifecycle states of a Bloc instance
|
|
531
|
+
* Used for atomic state transitions to prevent race conditions
|
|
532
|
+
*/
|
|
533
|
+
export declare enum BlocLifecycleState {
|
|
534
|
+
ACTIVE = "active",
|
|
535
|
+
DISPOSAL_REQUESTED = "disposal_requested",
|
|
536
|
+
DISPOSING = "disposing",
|
|
537
|
+
DISPOSED = "disposed"
|
|
538
|
+
}
|
|
539
|
+
|
|
456
540
|
/**
|
|
457
541
|
* Extracts the state type from either a Bloc or Cubit
|
|
458
542
|
* @template T - The Bloc or Cubit type to extract the state from
|
|
@@ -486,6 +570,49 @@ export declare class BlocTest {
|
|
|
486
570
|
static expectStates<T extends BlocBase<S>, S>(bloc: T, expectedStates: S[], timeout?: number): Promise<void>;
|
|
487
571
|
}
|
|
488
572
|
|
|
573
|
+
export declare interface ConsumerInfo {
|
|
574
|
+
id: string;
|
|
575
|
+
lastNotified: number;
|
|
576
|
+
hasRendered: boolean;
|
|
577
|
+
stateAccesses: Set<string>;
|
|
578
|
+
classAccesses: Set<string>;
|
|
579
|
+
stateValues: Map<string, TrackedValue>;
|
|
580
|
+
classValues: Map<string, TrackedValue>;
|
|
581
|
+
accessCount: number;
|
|
582
|
+
lastAccessTime: number;
|
|
583
|
+
firstAccessTime: number;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* ConsumerTracker manages both consumer registration and dependency tracking.
|
|
588
|
+
* It uses a WeakMap for automatic garbage collection when consumers are no longer referenced.
|
|
589
|
+
*/
|
|
590
|
+
export declare class ConsumerTracker {
|
|
591
|
+
private consumers;
|
|
592
|
+
private registrationCount;
|
|
593
|
+
private activeConsumers;
|
|
594
|
+
register(consumerRef: object, consumerId: string): void;
|
|
595
|
+
unregister(consumerRef: object): void;
|
|
596
|
+
trackAccess(consumerRef: object, type: 'state' | 'class', path: string, value?: any): void;
|
|
597
|
+
resetTracking(consumerRef: object): void;
|
|
598
|
+
getDependencies(consumerRef: object): DependencyArray | null;
|
|
599
|
+
hasValuesChanged(consumerRef: object, newState: any, newBlocInstance: any): boolean;
|
|
600
|
+
shouldNotifyConsumer(consumerRef: object, changedPaths: Set<string>): boolean;
|
|
601
|
+
updateLastNotified(consumerRef: object): void;
|
|
602
|
+
getConsumerInfo(consumerRef: object): ConsumerInfo | undefined;
|
|
603
|
+
setHasRendered(consumerRef: object, hasRendered: boolean): void;
|
|
604
|
+
hasConsumer(consumerRef: object): boolean;
|
|
605
|
+
getStats(): {
|
|
606
|
+
totalRegistrations: number;
|
|
607
|
+
activeConsumers: number;
|
|
608
|
+
};
|
|
609
|
+
private getValueAtPath;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
declare interface ConsumerTracker_2 {
|
|
613
|
+
trackAccess: (consumerRef: object, type: 'state' | 'class', path: string, value?: any) => void;
|
|
614
|
+
}
|
|
615
|
+
|
|
489
616
|
/**
|
|
490
617
|
* A Cubit is a simpler version of a Bloc that doesn't handle events.
|
|
491
618
|
* It manages state and provides methods to update it.
|
|
@@ -511,7 +638,12 @@ export declare abstract class Cubit<S, P = null> extends BlocBase<S, P> {
|
|
|
511
638
|
patch(statePatch: S extends object ? Partial<S> : S, ignoreChangeCheck?: boolean): void;
|
|
512
639
|
}
|
|
513
640
|
|
|
514
|
-
declare
|
|
641
|
+
export declare interface DependencyArray {
|
|
642
|
+
statePaths: string[];
|
|
643
|
+
classPaths: string[];
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
declare type DependencySelector<S> = (currentState: S, previousState: S | undefined, instance: any) => unknown[];
|
|
515
647
|
|
|
516
648
|
/**
|
|
517
649
|
* Cross-platform UUID v4 generator that works in all environments including React Native/Hermes
|
|
@@ -530,6 +662,8 @@ export declare interface GetBlocOptions<B extends BlocBase<unknown>> {
|
|
|
530
662
|
onMount?: (bloc: B) => void;
|
|
531
663
|
instanceRef?: string;
|
|
532
664
|
throwIfNotFound?: boolean;
|
|
665
|
+
/** Force creation of a new instance for isolated blocs, bypassing instance lookup */
|
|
666
|
+
forceNewInstance?: boolean;
|
|
533
667
|
}
|
|
534
668
|
|
|
535
669
|
/**
|
|
@@ -594,11 +728,64 @@ export declare class MockCubit<S> extends Cubit<S> {
|
|
|
594
728
|
clearStateHistory(): void;
|
|
595
729
|
}
|
|
596
730
|
|
|
731
|
+
export declare class ProxyFactory {
|
|
732
|
+
static createStateProxy<T extends object>(options: {
|
|
733
|
+
target: T;
|
|
734
|
+
consumerRef: object;
|
|
735
|
+
consumerTracker: ConsumerTracker_2;
|
|
736
|
+
path?: string;
|
|
737
|
+
}): T;
|
|
738
|
+
static createClassProxy<T extends object>(options: {
|
|
739
|
+
target: T;
|
|
740
|
+
consumerRef: object;
|
|
741
|
+
consumerTracker: ConsumerTracker_2;
|
|
742
|
+
}): T;
|
|
743
|
+
static getProxyState<B extends BlocBase<any>>(options: {
|
|
744
|
+
state: BlocState<B>;
|
|
745
|
+
consumerRef: object;
|
|
746
|
+
consumerTracker: ConsumerTracker_2;
|
|
747
|
+
}): BlocState<B>;
|
|
748
|
+
static getProxyBlocInstance<B extends BlocBase<any>>(options: {
|
|
749
|
+
blocInstance: B;
|
|
750
|
+
consumerRef: object;
|
|
751
|
+
consumerTracker: ConsumerTracker_2;
|
|
752
|
+
}): B;
|
|
753
|
+
static getStats(): {
|
|
754
|
+
totalProxies: number;
|
|
755
|
+
cacheEfficiency: string;
|
|
756
|
+
lifetime: string;
|
|
757
|
+
proxiesPerSecond: string;
|
|
758
|
+
stateProxiesCreated: number;
|
|
759
|
+
classProxiesCreated: number;
|
|
760
|
+
cacheHits: number;
|
|
761
|
+
cacheMisses: number;
|
|
762
|
+
propertyAccesses: number;
|
|
763
|
+
nestedProxiesCreated: number;
|
|
764
|
+
totalProxiesCreated: number;
|
|
765
|
+
createdAt: number;
|
|
766
|
+
};
|
|
767
|
+
static resetStats(): void;
|
|
768
|
+
}
|
|
769
|
+
|
|
597
770
|
/**
|
|
598
771
|
* Sets a custom instance manager (useful for testing or advanced patterns)
|
|
599
772
|
*/
|
|
600
773
|
export declare function setBlacInstanceManager(manager: BlacInstanceManager): void;
|
|
601
774
|
|
|
775
|
+
/**
|
|
776
|
+
* Result of an atomic state transition operation
|
|
777
|
+
*/
|
|
778
|
+
export declare interface StateTransitionResult {
|
|
779
|
+
success: boolean;
|
|
780
|
+
currentState: BlocLifecycleState;
|
|
781
|
+
previousState: BlocLifecycleState;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
export declare interface TrackedValue {
|
|
785
|
+
value: any;
|
|
786
|
+
lastAccessTime: number;
|
|
787
|
+
}
|
|
788
|
+
|
|
602
789
|
/**
|
|
603
790
|
* Extracts the state type from a BlocBase instance
|
|
604
791
|
* @template B - The BlocBase type to extract the state from
|