@blac/core 2.0.0-rc-8 → 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 +185 -4
- package/dist/index.es.js +1099 -601
- package/dist/index.es.js.map +1 -1
- package/package.json +5 -4
- package/src/Blac.ts +63 -40
- package/src/BlacObserver.ts +58 -24
- package/src/Bloc.ts +166 -157
- package/src/BlocBase.ts +266 -90
- 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 +11 -3
- 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 */
|
|
@@ -38,6 +45,7 @@ export declare class Blac {
|
|
|
38
45
|
/** Flag to enable/disable logging */
|
|
39
46
|
static enableLog: boolean;
|
|
40
47
|
static logLevel: 'warn' | 'log';
|
|
48
|
+
static logSpy: ((...args: unknown[]) => void) | null;
|
|
41
49
|
/**
|
|
42
50
|
* Logs messages to console when logging is enabled
|
|
43
51
|
* @param args - Arguments to log
|
|
@@ -205,6 +213,54 @@ export declare class Blac {
|
|
|
205
213
|
static get validateConsumers(): () => void;
|
|
206
214
|
}
|
|
207
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
|
+
|
|
208
264
|
/**
|
|
209
265
|
* Configuration options for the Blac instance
|
|
210
266
|
*/
|
|
@@ -281,7 +337,7 @@ export declare type BlacObserver<S> = {
|
|
|
281
337
|
/** Dispose function for the observer */
|
|
282
338
|
dispose?: () => void;
|
|
283
339
|
/** Cached state values used for dependency comparison */
|
|
284
|
-
lastState?: unknown[]
|
|
340
|
+
lastState?: unknown[];
|
|
285
341
|
/** Unique identifier for the observer */
|
|
286
342
|
id: string;
|
|
287
343
|
};
|
|
@@ -354,6 +410,7 @@ export declare abstract class BlocBase<S, P = unknown> {
|
|
|
354
410
|
*/
|
|
355
411
|
readonly _createdAt: number;
|
|
356
412
|
/* Excluded from this release type: _disposalState */
|
|
413
|
+
/* Excluded from this release type: _disposalRequestTime */
|
|
357
414
|
/* Excluded from this release type: _state */
|
|
358
415
|
/* Excluded from this release type: _oldState */
|
|
359
416
|
/**
|
|
@@ -375,10 +432,17 @@ export declare abstract class BlocBase<S, P = unknown> {
|
|
|
375
432
|
/**
|
|
376
433
|
* Returns the current state of the Bloc.
|
|
377
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.
|
|
378
436
|
*/
|
|
379
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;
|
|
380
443
|
/* Excluded from this release type: _name */
|
|
381
444
|
/* Excluded from this release type: _updateId */
|
|
445
|
+
/* Excluded from this release type: _atomicStateTransition */
|
|
382
446
|
/* Excluded from this release type: _dispose */
|
|
383
447
|
/* Excluded from this release type: onDispose */
|
|
384
448
|
/* Excluded from this release type: _consumers */
|
|
@@ -412,7 +476,10 @@ export declare type BlocClassNoParams<B> = new (args: never[]) => B;
|
|
|
412
476
|
* Represents a constructor type for a Bloc that can take any parameters
|
|
413
477
|
* @template B - The type of the Bloc instance
|
|
414
478
|
*/
|
|
415
|
-
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
|
+
};
|
|
416
483
|
|
|
417
484
|
/**
|
|
418
485
|
* Extracts the constructor parameters type from a BlocBase
|
|
@@ -459,6 +526,17 @@ export declare type BlocHookDependencyArrayFn<S, I = any> = (currentState: S, pr
|
|
|
459
526
|
|
|
460
527
|
export declare type BlocInstanceId = string | number | undefined;
|
|
461
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
|
+
|
|
462
540
|
/**
|
|
463
541
|
* Extracts the state type from either a Bloc or Cubit
|
|
464
542
|
* @template T - The Bloc or Cubit type to extract the state from
|
|
@@ -492,6 +570,49 @@ export declare class BlocTest {
|
|
|
492
570
|
static expectStates<T extends BlocBase<S>, S>(bloc: T, expectedStates: S[], timeout?: number): Promise<void>;
|
|
493
571
|
}
|
|
494
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
|
+
|
|
495
616
|
/**
|
|
496
617
|
* A Cubit is a simpler version of a Bloc that doesn't handle events.
|
|
497
618
|
* It manages state and provides methods to update it.
|
|
@@ -517,6 +638,11 @@ export declare abstract class Cubit<S, P = null> extends BlocBase<S, P> {
|
|
|
517
638
|
patch(statePatch: S extends object ? Partial<S> : S, ignoreChangeCheck?: boolean): void;
|
|
518
639
|
}
|
|
519
640
|
|
|
641
|
+
export declare interface DependencyArray {
|
|
642
|
+
statePaths: string[];
|
|
643
|
+
classPaths: string[];
|
|
644
|
+
}
|
|
645
|
+
|
|
520
646
|
declare type DependencySelector<S> = (currentState: S, previousState: S | undefined, instance: any) => unknown[];
|
|
521
647
|
|
|
522
648
|
/**
|
|
@@ -536,6 +662,8 @@ export declare interface GetBlocOptions<B extends BlocBase<unknown>> {
|
|
|
536
662
|
onMount?: (bloc: B) => void;
|
|
537
663
|
instanceRef?: string;
|
|
538
664
|
throwIfNotFound?: boolean;
|
|
665
|
+
/** Force creation of a new instance for isolated blocs, bypassing instance lookup */
|
|
666
|
+
forceNewInstance?: boolean;
|
|
539
667
|
}
|
|
540
668
|
|
|
541
669
|
/**
|
|
@@ -600,11 +728,64 @@ export declare class MockCubit<S> extends Cubit<S> {
|
|
|
600
728
|
clearStateHistory(): void;
|
|
601
729
|
}
|
|
602
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
|
+
|
|
603
770
|
/**
|
|
604
771
|
* Sets a custom instance manager (useful for testing or advanced patterns)
|
|
605
772
|
*/
|
|
606
773
|
export declare function setBlacInstanceManager(manager: BlacInstanceManager): void;
|
|
607
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
|
+
|
|
608
789
|
/**
|
|
609
790
|
* Extracts the state type from a BlocBase instance
|
|
610
791
|
* @template B - The BlocBase type to extract the state from
|