@blac/core 2.0.0-rc-2 → 2.0.0-rc-4

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
@@ -1,28 +1,31 @@
1
1
  /**
2
2
  * Main Blac class that manages the state management system.
3
- * Implements a singleton pattern to ensure only one instance exists.
4
- * Handles bloc lifecycle, plugin management, and instance tracking.
3
+ * Can work with singleton pattern or dependency injection.
4
+ * Handles bloc lifecycle, and instance tracking.
5
5
  *
6
6
  * Key responsibilities:
7
7
  * - Managing bloc instances (creation, disposal, lookup)
8
8
  * - Handling isolated and non-isolated blocs
9
- * - Managing plugins and lifecycle events
10
9
  * - Providing logging and debugging capabilities
11
10
  */
12
11
  export declare class Blac {
13
- /** The singleton instance of Blac */
14
- static instance: Blac;
12
+ /** @deprecated Use getInstance() instead */
13
+ static get instance(): Blac;
15
14
  /** Timestamp when the instance was created */
16
15
  createdAt: number;
17
- static getAllBlocs: <B extends BlocConstructor<unknown>>(blocClass: B, options?: {
16
+ static get getAllBlocs(): <B extends BlocConstructor<unknown>>(blocClass: B, options?: {
18
17
  searchIsolated?: boolean;
19
18
  }) => InstanceType<B>[];
20
- static addPlugin: (plugin: BlacPlugin) => void;
21
19
  /** Map storing all registered bloc instances by their class name and ID */
22
- blocInstanceMap: Map<string, BlocBase<any>>;
20
+ blocInstanceMap: Map<string, BlocBase<unknown>>;
23
21
  /** Map storing isolated bloc instances grouped by their constructor */
24
- isolatedBlocMap: Map<BlocConstructor<any>, BlocBase<any>[]>;
25
- pluginList: BlacPlugin[];
22
+ isolatedBlocMap: Map<BlocConstructor<unknown>, BlocBase<unknown>[]>;
23
+ /** Map for O(1) lookup of isolated blocs by UID */
24
+ private isolatedBlocIndex;
25
+ /** Map tracking UIDs to prevent memory leaks */
26
+ uidRegistry: Map<string, BlocBase<unknown>>;
27
+ /** Set of keep-alive blocs for controlled cleanup */
28
+ keepAliveBlocs: Set<BlocBase<unknown>>;
26
29
  /** Flag to control whether changes should be posted to document */
27
30
  postChangesToDocument: boolean;
28
31
  /**
@@ -39,9 +42,9 @@ export declare class Blac {
39
42
  * @param args - Arguments to log
40
43
  */
41
44
  log: (...args: unknown[]) => void;
42
- static log: (...args: unknown[]) => void;
45
+ static get log(): (...args: unknown[]) => void;
43
46
  /**
44
- * Gets the singleton instance of Blac
47
+ * Gets the current Blac instance
45
48
  * @returns The Blac instance
46
49
  */
47
50
  static getInstance(): Blac;
@@ -51,50 +54,25 @@ export declare class Blac {
51
54
  * @param args - Additional arguments
52
55
  */
53
56
  warn: (message: string, ...args: unknown[]) => void;
54
- static warn: (message: string, ...args: unknown[]) => void;
57
+ static get warn(): (message: string, ...args: unknown[]) => void;
55
58
  /**
56
59
  * Logs an error message
57
60
  * @param message - Error message
58
61
  * @param args - Additional arguments
59
62
  */
60
63
  error: (message: string, ...args: unknown[]) => void;
61
- static error: (message: string, ...args: unknown[]) => void;
64
+ static get error(): (message: string, ...args: unknown[]) => void;
62
65
  /**
63
66
  * Resets the Blac instance to a new one, disposing non-keepAlive blocs
64
67
  * from the old instance.
65
68
  */
66
69
  resetInstance: () => void;
67
70
  static resetInstance: () => void;
68
- /**
69
- * Adds a plugin to the Blac instance
70
- * @param plugin - The plugin to add
71
- */
72
- addPlugin: (plugin: BlacPlugin) => void;
73
- /**
74
- * Dispatches a lifecycle event to all registered plugins
75
- * @param event - The lifecycle event to dispatch
76
- * @param bloc - The bloc instance involved in the event
77
- * @param params - Additional parameters for the event
78
- */
79
- dispatchEventToPlugins: (event: BlacLifecycleEvent, bloc: BlocBase<any>, params?: unknown) => void;
80
- /**
81
- * Dispatches a lifecycle event and handles related cleanup actions.
82
- * This method is responsible for:
83
- * - Logging the event
84
- * - Handling bloc disposal when needed
85
- * - Managing bloc consumer cleanup
86
- * - Forwarding the event to plugins
87
- *
88
- * @param event - The lifecycle event to dispatch
89
- * @param bloc - The bloc instance involved in the event
90
- * @param params - Additional parameters for the event
91
- */
92
- dispatchEvent: (event: BlacLifecycleEvent, bloc: BlocBase<any>, params?: unknown) => void;
93
71
  /**
94
72
  * Disposes of a bloc instance by removing it from the appropriate registry
95
73
  * @param bloc - The bloc instance to dispose
96
74
  */
97
- disposeBloc: (bloc: BlocBase<any>) => void;
75
+ disposeBloc: (bloc: BlocBase<unknown>) => void;
98
76
  /**
99
77
  * Creates a unique key for a bloc instance in the map based on the bloc class name and instance ID
100
78
  * @param blocClassName - The name of the bloc class
@@ -106,12 +84,12 @@ export declare class Blac {
106
84
  * Unregister a bloc instance from the main registry
107
85
  * @param bloc - The bloc instance to unregister
108
86
  */
109
- unregisterBlocInstance(bloc: BlocBase<any>): void;
87
+ unregisterBlocInstance(bloc: BlocBase<unknown>): void;
110
88
  /**
111
89
  * Registers a bloc instance in the main registry
112
90
  * @param bloc - The bloc instance to register
113
91
  */
114
- registerBlocInstance(bloc: BlocBase<any>): void;
92
+ registerBlocInstance(bloc: BlocBase<unknown>): void;
115
93
  /**
116
94
  * Finds a registered bloc instance by its class and ID
117
95
  * @param blocClass - The bloc class to search for
@@ -123,16 +101,20 @@ export declare class Blac {
123
101
  * Registers an isolated bloc instance in the isolated registry
124
102
  * @param bloc - The isolated bloc instance to register
125
103
  */
126
- registerIsolatedBlocInstance(bloc: BlocBase<any>): void;
104
+ registerIsolatedBlocInstance(bloc: BlocBase<unknown>): void;
127
105
  /**
128
106
  * Unregister an isolated bloc instance from the isolated registry
129
107
  * @param bloc - The isolated bloc instance to unregister
130
108
  */
131
- unregisterIsolatedBlocInstance(bloc: BlocBase<any>): void;
109
+ unregisterIsolatedBlocInstance(bloc: BlocBase<unknown>): void;
132
110
  /**
133
- * Finds an isolated bloc instance by its class and ID
111
+ * Finds an isolated bloc instance by its class and ID (O(n) lookup)
134
112
  */
135
113
  findIsolatedBlocInstance<B extends BlocConstructor<unknown>>(blocClass: B, id: BlocInstanceId): InstanceType<B> | undefined;
114
+ /**
115
+ * Finds an isolated bloc instance by UID (O(1) lookup)
116
+ */
117
+ findIsolatedBlocInstanceByUid<B extends BlocBase<unknown>>(uid: string): B | undefined;
136
118
  /**
137
119
  * Creates a new bloc instance and registers it in the appropriate registry
138
120
  * @param blocClass - The bloc class to instantiate
@@ -141,7 +123,9 @@ export declare class Blac {
141
123
  * @param instanceRef - Optional reference string for the instance
142
124
  * @returns The newly created bloc instance
143
125
  */
144
- createNewBlocInstance<B extends BlocConstructor<BlocBase<any>>>(blocClass: B, id: BlocInstanceId, options?: GetBlocOptions<InstanceType<B>>): InstanceType<B>;
126
+ createNewBlocInstance<B extends BlocConstructor<BlocBase<unknown>>>(blocClass: B, id: BlocInstanceId, options?: GetBlocOptions<InstanceType<B>>): InstanceType<B>;
127
+ activateBloc: (bloc: BlocBase<unknown>) => void;
128
+ static get activateBloc(): (bloc: BlocBase<unknown>) => void;
145
129
  /**
146
130
  * Gets or creates a bloc instance based on the provided class and options.
147
131
  * If a bloc with the given ID exists, it will be returned. Otherwise, a new instance will be created.
@@ -153,8 +137,8 @@ export declare class Blac {
153
137
  * - instanceRef: Optional reference string for the instance
154
138
  * @returns The bloc instance
155
139
  */
156
- getBloc: <B extends BlocConstructor<BlocBase<any>>>(blocClass: B, options?: GetBlocOptions<InstanceType<B>>) => InstanceType<B>;
157
- static getBloc: <B extends BlocConstructor<BlocBase<any>>>(blocClass: B, options?: GetBlocOptions<InstanceType<B>>) => InstanceType<B>;
140
+ getBloc: <B extends BlocConstructor<BlocBase<unknown>>>(blocClass: B, options?: GetBlocOptions<InstanceType<B>>) => InstanceType<B>;
141
+ static get getBloc(): <B extends BlocConstructor<BlocBase<unknown>>>(blocClass: B, options?: GetBlocOptions<InstanceType<B>>) => InstanceType<B>;
158
142
  /**
159
143
  * Gets a bloc instance or throws an error if it doesn't exist
160
144
  * @param blocClass - The bloc class to get
@@ -168,7 +152,7 @@ export declare class Blac {
168
152
  props?: InferPropsFromGeneric<B>;
169
153
  instanceRef?: string;
170
154
  }) => InstanceType<B>;
171
- static getBlocOrThrow: <B extends BlocConstructor<unknown>>(blocClass: B, options?: {
155
+ static get getBlocOrThrow(): <B extends BlocConstructor<unknown>>(blocClass: B, options?: {
172
156
  id?: BlocInstanceId;
173
157
  props?: InferPropsFromGeneric<B> | undefined;
174
158
  instanceRef?: string;
@@ -183,22 +167,42 @@ export declare class Blac {
183
167
  getAllBlocs: <B extends BlocConstructor<unknown>>(blocClass: B, options?: {
184
168
  searchIsolated?: boolean;
185
169
  }) => InstanceType<B>[];
170
+ /**
171
+ * Disposes all keep-alive blocs of a specific type
172
+ * @param blocClass - The bloc class to dispose
173
+ */
174
+ disposeKeepAliveBlocs: <B extends BlocConstructor<unknown>>(blocClass?: B) => void;
175
+ static get disposeKeepAliveBlocs(): <B extends BlocConstructor<unknown>>(blocClass?: B | undefined) => void;
176
+ /**
177
+ * Disposes all blocs matching a pattern
178
+ * @param predicate - Function to test each bloc for disposal
179
+ */
180
+ disposeBlocs: (predicate: (bloc: BlocBase<unknown>) => boolean) => void;
181
+ static get disposeBlocs(): (predicate: (bloc: BlocBase<unknown>) => boolean) => void;
182
+ /**
183
+ * Gets memory usage statistics for debugging
184
+ */
185
+ getMemoryStats: () => {
186
+ totalBlocs: number;
187
+ registeredBlocs: number;
188
+ isolatedBlocs: number;
189
+ keepAliveBlocs: number;
190
+ isolatedBlocTypes: number;
191
+ };
192
+ static get getMemoryStats(): () => {
193
+ totalBlocs: number;
194
+ registeredBlocs: number;
195
+ isolatedBlocs: number;
196
+ keepAliveBlocs: number;
197
+ isolatedBlocTypes: number;
198
+ };
199
+ /**
200
+ * Validates consumer references and cleans up orphaned consumers
201
+ */
202
+ validateConsumers: () => void;
203
+ static get validateConsumers(): () => void;
186
204
  }
187
205
 
188
- declare type BlacAddon = {
189
- name: string;
190
- onInit?: BlacAddonInit;
191
- onEmit?: BlacAddonEmit;
192
- };
193
-
194
- declare type BlacAddonEmit = (params: {
195
- oldState: unknown;
196
- newState: unknown;
197
- cubit: BlocBase<any>;
198
- }) => void;
199
-
200
- declare type BlacAddonInit = (bloc: BlocBase<any>) => void;
201
-
202
206
  /**
203
207
  * Configuration options for the Blac instance
204
208
  */
@@ -208,17 +212,12 @@ export declare interface BlacConfig {
208
212
  }
209
213
 
210
214
  /**
211
- * Enum representing different lifecycle events that can occur in the Blac system.
212
- * These events are used to track the lifecycle of blocs and their consumers.
215
+ * Interface for Blac instance management
213
216
  */
214
- export declare enum BlacLifecycleEvent {
215
- BLOC_DISPOSED = "BLOC_DISPOSED",
216
- BLOC_CREATED = "BLOC_CREATED",
217
- LISTENER_REMOVED = "LISTENER_REMOVED",
218
- LISTENER_ADDED = "LISTENER_ADDED",
219
- STATE_CHANGED = "STATE_CHANGED",
220
- BLOC_CONSUMER_REMOVED = "BLOC_CONSUMER_REMOVED",
221
- BLOC_CONSUMER_ADDED = "BLOC_CONSUMER_ADDED"
217
+ export declare interface BlacInstanceManager {
218
+ getInstance(): Blac;
219
+ setInstance(instance: Blac): void;
220
+ resetInstance(): void;
222
221
  }
223
222
 
224
223
  /**
@@ -263,9 +262,9 @@ export declare class BlacObservable<S = unknown> {
263
262
  */
264
263
  notify(newState: S, oldState: S, action?: unknown): void;
265
264
  /**
266
- * Disposes of all observers and clears the observer set
265
+ * Clears the observer set
267
266
  */
268
- dispose(): void;
267
+ clear(): void;
269
268
  }
270
269
 
271
270
  /**
@@ -285,11 +284,6 @@ export declare type BlacObserver<S> = {
285
284
  id: string;
286
285
  };
287
286
 
288
- export declare interface BlacPlugin {
289
- name: string;
290
- onEvent(event: BlacLifecycleEvent, bloc: BlocBase<any>, params?: unknown): void;
291
- }
292
-
293
287
  export declare abstract class Bloc<S, // State type
294
288
  A extends object, // Base Action/Event type, constrained to object
295
289
  P = unknown> extends BlocBase<S, P> {
@@ -320,6 +314,7 @@ P = unknown> extends BlocBase<S, P> {
320
314
  * @template P The type of props that can be passed during instance creation (optional)
321
315
  */
322
316
  export declare abstract class BlocBase<S, P = unknown> {
317
+ uid: `${string}-${string}-${string}-${string}-${string}`;
323
318
  /**
324
319
  * When true, every consumer will receive its own unique instance of this Bloc.
325
320
  * Use this when state should not be shared between components.
@@ -339,10 +334,8 @@ export declare abstract class BlocBase<S, P = unknown> {
339
334
  * When provided, observers will only be notified when selected dependencies change.
340
335
  */
341
336
  defaultDependencySelector: DependencySelector<S> | undefined;
342
- /* Excluded from this release type: _addons */
343
337
  /* Excluded from this release type: _isolated */
344
338
  /* Excluded from this release type: _observer */
345
- /* Excluded from this release type: _blac */
346
339
  /**
347
340
  * The unique identifier for this Bloc instance.
348
341
  * Defaults to the class name, but can be customized.
@@ -379,11 +372,21 @@ export declare abstract class BlocBase<S, P = unknown> {
379
372
  /* Excluded from this release type: _dispose */
380
373
  /* Excluded from this release type: onDispose */
381
374
  /* Excluded from this release type: _consumers */
375
+ /* Excluded from this release type: _consumerRefs */
382
376
  /* Excluded from this release type: _addConsumer */
383
377
  /* Excluded from this release type: _removeConsumer */
384
- /* Excluded from this release type: _connectAddons */
378
+ /* Excluded from this release type: _disposalHandler */
379
+ /* Excluded from this release type: _setDisposalHandler */
380
+ /* Excluded from this release type: _scheduleDisposal */
385
381
  lastUpdate: number;
382
+ /* Excluded from this release type: _batchingEnabled */
383
+ /* Excluded from this release type: _pendingUpdates */
386
384
  /* Excluded from this release type: _pushState */
385
+ /**
386
+ * Enables batching for multiple state updates
387
+ * @param batchFn Function to execute with batching enabled
388
+ */
389
+ batch: <T>(batchFn: () => T) => T;
387
390
  }
388
391
 
389
392
  /**
@@ -450,12 +453,13 @@ export declare abstract class Cubit<S, P = null> extends BlocBase<S, P> {
450
453
 
451
454
  declare type DependencySelector<S> = (newState: S) => unknown[][];
452
455
 
453
- export declare interface GetBlocOptions<B extends BlocBase<any>> {
456
+ export declare interface GetBlocOptions<B extends BlocBase<unknown>> {
454
457
  id?: string;
455
458
  selector?: BlocHookDependencyArrayFn<BlocState<B>>;
456
459
  props?: InferPropsFromGeneric<B>;
457
460
  onMount?: (bloc: B) => void;
458
461
  instanceRef?: string;
462
+ throwIfNotFound?: boolean;
459
463
  }
460
464
 
461
465
  /**
@@ -465,36 +469,9 @@ export declare interface GetBlocOptions<B extends BlocBase<any>> {
465
469
  export declare type InferPropsFromGeneric<T> = T extends Bloc<any, any, infer P> ? P : T extends Cubit<any, infer P> ? P : never;
466
470
 
467
471
  /**
468
- * Persist addon
469
- *
470
- * @param options
471
- * @returns BlacAddon
472
+ * Sets a custom instance manager (useful for testing or advanced patterns)
472
473
  */
473
- export declare function Persist(options?: {
474
- /**
475
- * @default 'blac'
476
- */
477
- keyPrefix?: string;
478
- /**
479
- * @default the bloc's id
480
- */
481
- keyName?: string;
482
- /**
483
- * Used when the value is not found in storage
484
- */
485
- defaultValue?: unknown;
486
- /**
487
- * @default 'localStorage'
488
- * @see StorageType
489
- */
490
- storageType?: StorageType;
491
- /**
492
- * @default false
493
- */
494
- onError?: (e: unknown) => void;
495
- }): BlacAddon;
496
-
497
- declare type StorageType = 'localStorage' | 'sessionStorage';
474
+ export declare function setBlacInstanceManager(manager: BlacInstanceManager): void;
498
475
 
499
476
  /**
500
477
  * Extracts the state type from a BlocBase instance