@blac/core 2.0.3 → 2.0.5

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.
Files changed (64) hide show
  1. package/README.md +2 -0
  2. package/dist/constants.d.ts +81 -0
  3. package/dist/core/Cubit.d.ts +24 -0
  4. package/dist/core/StateContainer.d.ts +127 -0
  5. package/dist/core/StateContainerRegistry.d.ts +189 -0
  6. package/dist/debug.d.ts +35 -0
  7. package/dist/decorators/blac.d.ts +40 -0
  8. package/dist/decorators/index.d.ts +1 -0
  9. package/dist/index.cjs +1509 -47
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +18 -794
  12. package/dist/index.d.ts +18 -794
  13. package/dist/index.js +1463 -10
  14. package/dist/index.js.map +1 -1
  15. package/dist/plugin/BlacPlugin.d.ts +110 -0
  16. package/dist/plugin/PluginManager.d.ts +81 -0
  17. package/dist/plugins.d.ts +25 -0
  18. package/dist/registry/acquire.d.ts +2 -0
  19. package/dist/registry/borrow.d.ts +9 -0
  20. package/dist/registry/config.d.ts +8 -0
  21. package/dist/registry/ensure.d.ts +2 -0
  22. package/dist/registry/index.d.ts +7 -0
  23. package/dist/registry/management.d.ts +4 -0
  24. package/dist/registry/queries.d.ts +5 -0
  25. package/dist/registry/release.d.ts +2 -0
  26. package/dist/tracking/index.d.ts +2 -1
  27. package/dist/tracking/resolve-dependencies.d.ts +7 -0
  28. package/dist/tracking/tracking-proxy.d.ts +0 -14
  29. package/dist/types/branded.d.ts +26 -0
  30. package/dist/types/utilities.d.ts +51 -0
  31. package/dist/utils/idGenerator.d.ts +73 -0
  32. package/dist/utils/static-props.d.ts +35 -0
  33. package/dist/watch/index.d.ts +2 -0
  34. package/dist/watch/watch.d.ts +50 -0
  35. package/dist/watch-entry.d.ts +26 -0
  36. package/package.json +6 -82
  37. package/dist/StateContainerRegistry.cjs.map +0 -1
  38. package/dist/StateContainerRegistry.js.map +0 -1
  39. package/dist/debug.cjs +0 -27
  40. package/dist/debug.cjs.map +0 -1
  41. package/dist/debug.js +0 -19
  42. package/dist/debug.js.map +0 -1
  43. package/dist/dependency-manager.cjs.map +0 -1
  44. package/dist/dependency-manager.js.map +0 -1
  45. package/dist/management.cjs.map +0 -1
  46. package/dist/management.js.map +0 -1
  47. package/dist/plugins.cjs +0 -4
  48. package/dist/plugins.js +0 -3
  49. package/dist/tracking-proxy.cjs.map +0 -1
  50. package/dist/tracking-proxy.js.map +0 -1
  51. package/dist/tracking.cjs +0 -19
  52. package/dist/tracking.d.cts +0 -9
  53. package/dist/tracking.js +0 -4
  54. package/dist/types.cjs +0 -14
  55. package/dist/types.cjs.map +0 -1
  56. package/dist/types.d.cts +0 -8
  57. package/dist/types.js +0 -13
  58. package/dist/types.js.map +0 -1
  59. package/dist/watch-entry.cjs +0 -111
  60. package/dist/watch-entry.cjs.map +0 -1
  61. package/dist/watch-entry.js +0 -107
  62. package/dist/watch-entry.js.map +0 -1
  63. package/dist/watch.cjs.map +0 -1
  64. package/dist/watch.js.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,794 +1,18 @@
1
- export declare function acquire<T extends StateContainerConstructor>(BlocClass: T, instanceKey?: string): InstanceType<T>;
2
-
3
- /**
4
- * Decorator to configure StateContainer classes.
5
- *
6
- * @example Decorator syntax (requires experimentalDecorators or TC39 decorators)
7
- * ```ts
8
- * @blac({ isolated: true })
9
- * class FormBloc extends Cubit<FormState> {}
10
- *
11
- * @blac({ keepAlive: true })
12
- * class AuthBloc extends Cubit<AuthState> {}
13
- *
14
- * @blac({ excludeFromDevTools: true })
15
- * class InternalBloc extends Cubit<InternalState> {}
16
- * ```
17
- *
18
- * @example Function syntax (no decorator support needed)
19
- * ```ts
20
- * const FormBloc = blac({ isolated: true })(
21
- * class extends Cubit<FormState> {}
22
- * );
23
- * ```
24
- */
25
- export declare function blac(options: BlacOptions): <T extends new (...args: any[]) => any>(target: T, _context?: ClassDecoratorContext) => T;
26
-
27
- /**
28
- * Configuration options for the @blac decorator.
29
- * Only one option can be specified at a time (union type).
30
- */
31
- export declare type BlacOptions =
32
- /** Mark bloc as isolated (each component gets its own instance) */
33
- {
34
- isolated: true;
35
- }
36
- /** Mark bloc to never be auto-disposed when ref count reaches 0 */
37
- | {
38
- keepAlive: true;
39
- }
40
- /** Exclude bloc from DevTools tracking (prevents infinite loops) */
41
- | {
42
- excludeFromDevTools: true;
43
- };
44
-
45
- /**
46
- * Interface for plugins that extend BlaC functionality
47
- */
48
- export declare interface BlacPlugin {
49
- /** Unique plugin identifier */
50
- readonly name: string;
51
- /** Plugin version identifier */
52
- readonly version: string;
53
- /**
54
- * Called when the plugin is installed (optional)
55
- */
56
- onInstall?(context: PluginContext): void;
57
- /**
58
- * Called when the plugin is uninstalled
59
- */
60
- onUninstall?(): void;
61
- /**
62
- * Called when a state container instance is created
63
- */
64
- onInstanceCreated?(instance: StateContainer<any>, context: PluginContext): void;
65
- /**
66
- * Called when state changes in a container instance
67
- */
68
- onStateChanged?<S extends object = any>(instance: StateContainer<S>, previousState: S, currentState: S, callstack: string | undefined, context: PluginContext): void;
69
- /**
70
- * Called when a state container instance is disposed
71
- */
72
- onInstanceDisposed?(instance: StateContainer<any>, context: PluginContext): void;
73
- }
74
-
75
- /**
76
- * Plugin interface variant that requires mandatory onInstall hook
77
- */
78
- export declare interface BlacPluginWithInit extends BlacPlugin {
79
- /**
80
- * Required initialization hook called when plugin is installed
81
- */
82
- onInstall(context: PluginContext): void;
83
- }
84
-
85
- declare const BLOC_REF_MARKER: unique symbol;
86
-
87
- /**
88
- * Constructor type for StateContainer classes with static registry methods.
89
- * Used for type-safe hook parameters.
90
- * @template TBloc - The StateContainer instance type
91
- */
92
- export declare type BlocConstructor<S extends object = any, T extends new (...args: any[]) => StateContainer<S> = new (...args: any[]) => StateContainer<S>> = (new (...args: any[]) => InstanceType<T>) & {
93
- acquire(instanceKey?: string, ...args: any[]): InstanceType<T>;
94
- borrow(instanceKey?: string, ...args: any[]): InstanceType<T> | null;
95
- borrowSafe(instanceKey?: string, ...args: any[]): {
96
- error: Error;
97
- instance: null;
98
- } | {
99
- error: null;
100
- instance: InstanceType<T>;
101
- };
102
- ensure(instanceKey?: string): InstanceType<T>;
103
- release(instanceKey?: string): void;
104
- isolated?: boolean;
105
- keepAlive?: boolean;
106
- };
107
-
108
- declare type BlocInput = StateContainerConstructor | BlocRef<StateContainerConstructor>;
109
-
110
- /**
111
- * Extract instance type from an abstract class constructor
112
- * @template T - The abstract class constructor type
113
- */
114
- export declare type BlocInstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;
115
-
116
- /**
117
- * Reference to a specific bloc instance by class and instance ID.
118
- */
119
- export declare interface BlocRef<T extends StateContainerConstructor> {
120
- [BLOC_REF_MARKER]: true;
121
- blocClass: T;
122
- instanceId: string;
123
- }
124
-
125
- export declare function borrow<T extends StateContainerConstructor>(BlocClass: T, instanceKey?: string): InstanceType<T>;
126
-
127
- export declare function borrowSafe<T extends StateContainerConstructor>(BlocClass: T, instanceKey?: string): {
128
- error: Error;
129
- instance: null;
130
- } | {
131
- error: null;
132
- instance: InstanceType<T>;
133
- };
134
-
135
- /* Excluded from this release type: capturePaths */
136
-
137
- export declare function clear<T extends StateContainerConstructor>(BlocClass: T): void;
138
-
139
- /* Excluded from this release type: clearActiveTracker */
140
-
141
- export declare function clearAll(): void;
142
-
143
- /* Excluded from this release type: clearExternalDependencies */
144
-
145
- /* Excluded from this release type: commitTrackedGetters */
146
-
147
- /* Excluded from this release type: createBlocProxy */
148
-
149
- /* Excluded from this release type: createDependencyProxy */
150
-
151
- /* Excluded from this release type: createDependencyState */
152
-
153
- /* Excluded from this release type: createGetterState */
154
-
155
- /**
156
- * Simple state container with direct state emission.
157
- * Extends StateContainer with public methods for emitting and updating state.
158
- *
159
- * @template S - State type
160
- */
161
- export declare abstract class Cubit<S extends object = any> extends StateContainer<S> {
162
- constructor(initialState: S);
163
- /**
164
- * Replace state with a new value and notify all listeners
165
- * @param newState - The new state value
166
- */
167
- emit(newState: S): void;
168
- /**
169
- * Transform current state using an updater function and emit the new state
170
- * @param updater - Function that receives current state and returns new state
171
- */
172
- update(updater: (current: S) => S): void;
173
- /**
174
- * Merge partial state changes into current state (only for object states)
175
- */
176
- patch: S extends object ? (partial: Partial<S>) => void : never;
177
- }
178
-
179
- /**
180
- * Manages subscriptions to state container dependencies.
181
- * Provides efficient sync mechanism to add/remove subscriptions
182
- * as dependencies change between callback invocations.
183
- */
184
- export declare class DependencyManager {
185
- private subscriptions;
186
- private currentDeps;
187
- /**
188
- * Sync subscriptions with a new set of dependencies.
189
- * Adds subscriptions for new deps, removes subscriptions for stale deps.
190
- *
191
- * @param newDeps - The new set of dependencies to subscribe to
192
- * @param onChange - Callback to invoke when any dependency changes
193
- * @param exclude - Optional instance to exclude from subscriptions (e.g., primary bloc)
194
- * @returns true if the dependency set changed, false if unchanged
195
- */
196
- sync(newDeps: Set<StateContainerInstance>, onChange: () => void, exclude?: StateContainerInstance): boolean;
197
- /**
198
- * Add a single dependency subscription.
199
- */
200
- add(dep: StateContainerInstance, onChange: () => void): void;
201
- /**
202
- * Check if a dependency is currently subscribed.
203
- */
204
- has(dep: StateContainerInstance): boolean;
205
- /**
206
- * Get the current set of dependencies.
207
- */
208
- getDependencies(): Set<StateContainerInstance>;
209
- /**
210
- * Clean up all active subscriptions.
211
- */
212
- cleanup(): void;
213
- private areSetsEqual;
214
- }
215
-
216
- /* Excluded from this release type: DependencyState */
217
-
218
- export declare function ensure<T extends StateContainerConstructor>(BlocClass: T, instanceKey?: string): InstanceType<T>;
219
-
220
- /**
221
- * Extract constructor argument types from a class
222
- * @template T - The class type
223
- */
224
- export declare type ExtractConstructorArgs<T> = T extends new (...args: infer P) => any ? P : never[];
225
-
226
- declare type ExtractInstance<T> = T extends BlocRef<infer C> ? InstanceType<C> : T extends StateContainerConstructor ? InstanceType<T> : never;
227
-
228
- declare type ExtractInstances<T extends readonly BlocInput[]> = {
229
- [K in keyof T]: ExtractInstance<T[K]>;
230
- };
231
-
232
- /**
233
- * Extract the state type from a StateContainer
234
- * @template T - The StateContainer type
235
- */
236
- export declare type ExtractState<T> = T extends StateContainerConstructor<infer S> ? Readonly<S> : never;
237
-
238
- export declare type ExtractStateMutable<T> = T extends StateContainerConstructor<infer S> ? S : never;
239
-
240
- export declare function forEach<T extends StateContainerConstructor>(BlocClass: T, callback: (instance: InstanceReadonlyState<T>) => void): void;
241
-
242
- /**
243
- * Generate a unique isolated instance key
244
- * Uses base36 encoding for compact, URL-safe identifiers
245
- *
246
- * Format: "isolated-{9-char-random-string}"
247
- * Example: "isolated-k7x2m9p4q"
248
- *
249
- * @returns A unique isolated instance key
250
- */
251
- export declare function generateIsolatedKey(): string;
252
-
253
- export declare function getAll<T extends StateContainerConstructor>(BlocClass: T): InstanceReadonlyState<T>[];
254
-
255
- /**
256
- * Get the global plugin manager
257
- */
258
- export declare function getPluginManager(): any;
259
-
260
- export declare function getRefCount<T extends StateContainerConstructor>(BlocClass: T, instanceKey?: string): number;
261
-
262
- /* Excluded from this release type: GetterState */
263
-
264
- /**
265
- * Global default registry instance
266
- */
267
- export declare const globalRegistry: StateContainerRegistry;
268
-
269
- /* Excluded from this release type: hasDependencyChanges */
270
-
271
- /* Excluded from this release type: hasGetterChanges */
272
-
273
- export declare function hasInstance<T extends StateContainerConstructor>(BlocClass: T, instanceKey?: string): boolean;
274
-
275
- /* Excluded from this release type: hasTrackedData */
276
-
277
- /**
278
- * Create a reference to a specific bloc instance.
279
- *
280
- * @example
281
- * ```ts
282
- * watch(instance(UserBloc, 'user-123'), (userBloc) => {
283
- * console.log(userBloc.state.name);
284
- * });
285
- * ```
286
- */
287
- export declare function instance<T extends StateContainerConstructor>(BlocClass: T, instanceId: string): BlocRef<T>;
288
-
289
- /**
290
- * Entry in the instance registry, tracking the instance and its reference count
291
- * @template T - Instance type
292
- */
293
- export declare interface InstanceEntry<T = any> {
294
- /** The state container instance */
295
- instance: T;
296
- /** Number of active references to this instance */
297
- refCount: number;
298
- }
299
-
300
- /**
301
- * Metadata information about a state container instance for debugging and inspection
302
- */
303
- export declare interface InstanceMetadata {
304
- /** Unique instance identifier */
305
- id: string;
306
- /** Name of the state container class */
307
- className: string;
308
- /** Whether the instance has been disposed */
309
- isDisposed: boolean;
310
- /** Display name for the instance */
311
- name: string;
312
- /** When state last changed (milliseconds) */
313
- lastStateChangeTimestamp: number;
314
- /** Current state value */
315
- state: any;
316
- /** Timestamp when instance was created (milliseconds) */
317
- createdAt: number;
318
- /** Whether this is an isolated (component-scoped) instance */
319
- isIsolated: boolean;
320
- /** Stack trace from when instance was created (for debugging) */
321
- callstack?: string;
322
- /** Previous state value */
323
- previousState?: any;
324
- /** Current state value */
325
- currentState?: any;
326
- }
327
-
328
- export declare type InstanceReadonlyState<T extends StateContainerConstructor = any> = Omit<InstanceType<T>, 'state'> & {
329
- state: ExtractState<T>;
330
- };
331
-
332
- export declare type InstanceState<T extends StateContainerConstructor = any> = Omit<InstanceType<T>, 'state'> & {
333
- state: ExtractStateMutable<T>;
334
- };
335
-
336
- /* Excluded from this release type: invalidateRenderCache */
337
-
338
- /**
339
- * Check if a class is marked as isolated.
340
- * Isolated classes create separate instances per component.
341
- * @param Type - The class constructor to check
342
- * @returns true if the class has `static isolated = true`
343
- */
344
- export declare function isIsolatedClass<T extends StateContainerConstructor>(Type: T): boolean;
345
-
346
- /**
347
- * Lifecycle events emitted by the registry
348
- */
349
- export declare type LifecycleEvent = 'created' | 'stateChanged' | 'disposed';
350
-
351
- /**
352
- * Listener function type for each lifecycle event
353
- * @template E - The lifecycle event type
354
- */
355
- export declare type LifecycleListener<E extends LifecycleEvent> = E extends 'created' ? (container: StateContainer<any>) => void : E extends 'stateChanged' ? (container: StateContainer<any>, previousState: any, currentState: any, callstack?: string) => void : E extends 'disposed' ? (container: StateContainer<any>) => void : never;
356
-
357
- /* Excluded from this release type: PathInfo */
358
-
359
- /**
360
- * Configuration options for plugin installation
361
- */
362
- export declare interface PluginConfig {
363
- /** Enable or disable the plugin */
364
- enabled?: boolean;
365
- /** Environments where plugin runs */
366
- environment?: 'development' | 'production' | 'test' | 'all';
367
- }
368
-
369
- /**
370
- * Safe context API provided to plugins for accessing registry data
371
- */
372
- export declare interface PluginContext {
373
- /**
374
- * Get metadata for a specific instance
375
- */
376
- getInstanceMetadata(instance: StateContainer<any>): InstanceMetadata;
377
- /**
378
- * Get current state from a container
379
- */
380
- getState<S extends object = any>(instance: StateContainer<S>): S;
381
- /**
382
- * Get all instances of a specific type
383
- */
384
- queryInstances<T extends StateContainer<any>>(typeClass: new (...args: any[]) => T): T[];
385
- /**
386
- * Get all registered state container types
387
- */
388
- getAllTypes(): Array<new (...args: any[]) => StateContainer<any>>;
389
- /**
390
- * Get registry statistics
391
- */
392
- getStats(): {
393
- registeredTypes: number;
394
- totalInstances: number;
395
- typeBreakdown: Record<string, number>;
396
- };
397
- }
398
-
399
- /**
400
- * Manages plugin lifecycle for the BlaC state management system.
401
- * Plugins receive notifications about state container lifecycle events.
402
- *
403
- * @example
404
- * ```ts
405
- * const manager = createPluginManager(registry);
406
- * manager.install(myPlugin, { environment: 'development' });
407
- * ```
408
- */
409
- export declare class PluginManager {
410
- private plugins;
411
- private registry;
412
- /**
413
- * Create a new PluginManager
414
- * @param registry - The StateContainerRegistry to monitor for lifecycle events
415
- */
416
- constructor(registry: StateContainerRegistry);
417
- /**
418
- * Install a plugin with optional configuration
419
- * @param plugin - The plugin to install
420
- * @param config - Optional plugin configuration
421
- * @throws Error if plugin is already installed
422
- */
423
- install(plugin: BlacPlugin, config?: PluginConfig): void;
424
- /**
425
- * Uninstall a plugin by name
426
- * @param pluginName - The name of the plugin to uninstall
427
- * @throws Error if plugin is not installed
428
- */
429
- uninstall(pluginName: string): void;
430
- /**
431
- * Get an installed plugin by name
432
- * @param pluginName - The name of the plugin to retrieve
433
- * @returns The plugin instance or undefined if not found
434
- */
435
- getPlugin(pluginName: string): BlacPlugin | undefined;
436
- /**
437
- * Get all installed plugins
438
- * @returns Array of all installed plugins
439
- */
440
- getAllPlugins(): BlacPlugin[];
441
- /**
442
- * Check if a plugin is installed
443
- * @param pluginName - The name of the plugin to check
444
- * @returns true if the plugin is installed
445
- */
446
- hasPlugin(pluginName: string): boolean;
447
- /**
448
- * Uninstall all plugins
449
- */
450
- clear(): void;
451
- /**
452
- * Setup lifecycle hooks to notify plugins
453
- */
454
- private setupLifecycleHooks;
455
- /**
456
- * Notify all plugins of a lifecycle event
457
- */
458
- private notifyPlugins;
459
- /**
460
- * Create plugin context with safe API access
461
- */
462
- private createPluginContext;
463
- /**
464
- * Check if plugin should be enabled based on environment
465
- */
466
- private shouldEnablePlugin;
467
- /**
468
- * Get current environment
469
- */
470
- private getCurrentEnvironment;
471
- }
472
-
473
- /* Excluded from this release type: ProxyState */
474
-
475
- export declare function release<T extends StateContainerConstructor>(BlocClass: T, instanceKey?: string, forceDispose?: boolean): void;
476
-
477
- /* Excluded from this release type: setActiveTracker */
478
-
479
- /* Excluded from this release type: shallowEqual */
480
-
481
- /* Excluded from this release type: startDependency */
482
-
483
- /**
484
- * Abstract base class for all state containers in BlaC.
485
- * Provides lifecycle management, subscription handling, ref counting,
486
- * and integration with the global registry.
487
- *
488
- * @template S - State type managed by this container
489
- *
490
- * @example
491
- * ```ts
492
- * class CounterBloc extends StateContainer<{ count: number }> {
493
- * constructor() {
494
- * super({ count: 0 });
495
- * }
496
- * increment() {
497
- * this.emit({ count: this.state.count + 1 });
498
- * }
499
- * }
500
- * ```
501
- */
502
- export declare abstract class StateContainer<S extends object = any> {
503
- static __excludeFromDevTools: boolean;
504
- static enableStackTrace: boolean;
505
- private _state;
506
- private readonly listeners;
507
- private _disposed;
508
- private config;
509
- private readonly systemEventHandlers;
510
- /** Display name for this instance */
511
- name: string;
512
- /** Whether debug logging is enabled */
513
- debug: boolean;
514
- /** Unique identifier for this instance */
515
- instanceId: string;
516
- /** Timestamp when this instance was created */
517
- createdAt: number;
518
- constructor(initialState: S);
519
- /**
520
- * Initialize configuration for this instance.
521
- * Called by the registry after construction.
522
- * @param config - Configuration options
523
- */
524
- initConfig(config: StateContainerConfig): void;
525
- /** Current state value */
526
- get state(): Readonly<S>;
527
- /** Whether this instance has been disposed */
528
- get isDisposed(): boolean;
529
- /**
530
- * Subscribe to state changes
531
- * @param listener - Function called when state changes
532
- * @returns Unsubscribe function
533
- */
534
- subscribe(listener: StateListener<S>): () => void;
535
- /**
536
- * Dispose this instance and clean up resources.
537
- * Clears all listeners and emits the 'dispose' system event.
538
- */
539
- dispose(): void;
540
- /**
541
- * Emit a new state value and notify all listeners.
542
- * @param newState - The new state value
543
- * @protected
544
- */
545
- protected emit(newState: S): void;
546
- private captureStackTrace;
547
- private formatStackLine;
548
- private cleanFilePath;
549
- /** Timestamp of the last state update */
550
- lastUpdateTimestamp: number;
551
- /**
552
- * Update state using a transform function.
553
- * @param updater - Function that receives current state and returns new state
554
- * @protected
555
- */
556
- protected update(updater: (current: S) => S): void;
557
- /**
558
- * Subscribe to system lifecycle events.
559
- * @param event - The event type to listen for
560
- * @param handler - Handler function called when event occurs
561
- * @returns Unsubscribe function
562
- * @protected
563
- */
564
- protected onSystemEvent: <E extends SystemEvent>(event: E, handler: SystemEventHandler<S, E>) => (() => void);
565
- private emitSystemEvent;
566
- }
567
-
568
- /**
569
- * Configuration options for initializing a StateContainer instance
570
- */
571
- export declare interface StateContainerConfig {
572
- /** Display name for the instance (defaults to class name) */
573
- name?: string;
574
- /** Enable debug logging for this instance */
575
- debug?: boolean;
576
- /** Custom instance identifier */
577
- instanceId?: string;
578
- }
579
-
580
- /**
581
- * Constructor type for StateContainer classes
582
- * @template S - State type managed by the container
583
- */
584
- export declare type StateContainerConstructor<S extends object = any> = new (...args: any[]) => StateContainer<S>;
585
-
586
- export declare type StateContainerInstance<S extends object = any> = Omit<StateContainer<S>, 'state'> & {
587
- state: Readonly<S>;
588
- };
589
-
590
- /**
591
- * Central registry for managing StateContainer instances.
592
- * Handles instance lifecycle, ref counting, and lifecycle event emission.
593
- *
594
- * @example
595
- * ```ts
596
- * const registry = new StateContainerRegistry();
597
- * const instance = registry.acquire(MyBloc); // ownership, must release
598
- * const other = registry.ensure(OtherBloc); // no ownership, bloc-to-bloc
599
- * registry.on('stateChanged', (container, prev, next) => {
600
- * console.log('State changed:', prev, '->', next);
601
- * });
602
- * ```
603
- */
604
- declare class StateContainerRegistry {
605
- private readonly instancesByConstructor;
606
- private readonly types;
607
- private readonly typeConfigs;
608
- private readonly listeners;
609
- /**
610
- * Register a type for lifecycle event tracking
611
- * @param constructor - The StateContainer class constructor
612
- */
613
- registerType<T extends StateContainerConstructor>(constructor: T): void;
614
- /**
615
- * Register a StateContainer class with configuration
616
- * @param constructor - The StateContainer class constructor
617
- * @param isolated - Whether instances should be isolated (component-scoped)
618
- * @throws Error if type is already registered
619
- */
620
- register<T extends StateContainerConstructor>(constructor: T, isolated?: boolean): void;
621
- private ensureInstancesMap;
622
- /**
623
- * Get the instances Map for a specific class (public API for stats/debugging)
624
- */
625
- getInstancesMap<T extends StateContainerConstructor>(Type: T): Map<string, InstanceEntry>;
626
- private trackExecutionContext;
627
- /**
628
- * Acquire an instance with ref counting (ownership semantics).
629
- * Creates a new instance if one doesn't exist, or returns existing and increments ref count.
630
- * You must call `release()` when done to decrement the ref count.
631
- * @param Type - The StateContainer class constructor
632
- * @param instanceKey - Instance key (defaults to 'default')
633
- * @param options - Acquisition options
634
- * @param options.canCreate - Whether to create new instance if not found (default: true)
635
- * @param options.countRef - Whether to increment ref count (default: true)
636
- * @param options.trackExecutionContext - Whether to track cross-bloc dependency (default: false)
637
- * @returns The state container instance
638
- */
639
- acquire<T extends StateContainerConstructor = StateContainerConstructor>(Type: T, instanceKey?: string, options?: {
640
- canCreate?: boolean;
641
- countRef?: boolean;
642
- trackExecutionContext?: boolean;
643
- }): InstanceType<T>;
644
- /**
645
- * Borrow an existing instance without incrementing ref count (borrowing semantics).
646
- * Tracks cross-bloc dependency for reactive updates.
647
- * @param Type - The StateContainer class constructor
648
- * @param instanceKey - Instance key (defaults to 'default')
649
- * @returns The state container instance
650
- * @throws Error if instance doesn't exist
651
- */
652
- borrow<T extends StateContainerConstructor = StateContainerConstructor>(Type: T, instanceKey?: string): InstanceType<T>;
653
- /**
654
- * Safely borrow an existing instance (borrowing semantics with error handling).
655
- * Returns discriminated union for type-safe conditional access.
656
- * @param Type - The StateContainer class constructor
657
- * @param instanceKey - Instance key (defaults to 'default')
658
- * @returns Discriminated union with either the instance or an error
659
- */
660
- borrowSafe<T extends StateContainerConstructor = StateContainerConstructor>(Type: T, instanceKey?: string): {
661
- error: Error;
662
- instance: null;
663
- } | {
664
- error: null;
665
- instance: InstanceType<T>;
666
- };
667
- /**
668
- * Ensure an instance exists without taking ownership (for bloc-to-bloc communication).
669
- * Gets existing instance OR creates it if it doesn't exist, without incrementing ref count.
670
- * Tracks cross-bloc dependency for reactive updates.
671
- *
672
- * Use this in bloc-to-bloc communication when you need to ensure an instance exists
673
- * but don't want to claim ownership (no ref count increment).
674
- * @param Type - The StateContainer class constructor
675
- * @param instanceKey - Instance key (defaults to 'default')
676
- * @returns The state container instance
677
- */
678
- ensure<T extends StateContainerConstructor = StateContainerConstructor>(Type: T, instanceKey?: string): InstanceType<T>;
679
- /**
680
- * Release a reference to an instance.
681
- * Decrements ref count and disposes when it reaches 0 (unless keepAlive).
682
- * @param Type - The StateContainer class constructor
683
- * @param instanceKey - Instance key (defaults to 'default')
684
- * @param forceDispose - Force immediate disposal regardless of ref count
685
- */
686
- release<T extends StateContainerConstructor>(Type: T, instanceKey?: string, forceDispose?: boolean): void;
687
- /**
688
- * Get all instances of a specific type.
689
- * @param Type - The StateContainer class constructor
690
- * @returns Array of all instances
691
- */
692
- getAll<T extends StateContainerConstructor>(Type: T): InstanceReadonlyState<T>[];
693
- /**
694
- * Safely iterate over all instances of a type.
695
- * Skips disposed instances and catches callback errors.
696
- * @param Type - The StateContainer class constructor
697
- * @param callback - Function to call for each instance
698
- */
699
- forEach<T extends StateContainerConstructor>(Type: T, callback: (instance: InstanceReadonlyState<T>) => void): void;
700
- /**
701
- * Clear all instances of a specific type (disposes them).
702
- * @param Type - The StateContainer class constructor
703
- */
704
- clear<T extends StateContainerConstructor>(Type: T): void;
705
- /**
706
- * Get reference count for an instance.
707
- * @param Type - The StateContainer class constructor
708
- * @param instanceKey - Instance key (defaults to 'default')
709
- * @returns Current ref count (0 if instance doesn't exist)
710
- */
711
- getRefCount<T extends StateContainerConstructor>(Type: T, instanceKey?: string): number;
712
- /**
713
- * Check if an instance exists.
714
- * @param Type - The StateContainer class constructor
715
- * @param instanceKey - Instance key (defaults to 'default')
716
- * @returns true if instance exists
717
- */
718
- hasInstance<T extends StateContainerConstructor>(Type: T, instanceKey?: string): boolean;
719
- /**
720
- * Clear all instances from all types (for testing)
721
- *
722
- * Iterates all registered types and clears their instances.
723
- * Also clears type tracking to reset the registry state.
724
- */
725
- clearAll(): void;
726
- /**
727
- * Get registry statistics for debugging.
728
- * @returns Object with registeredTypes, totalInstances, and typeBreakdown
729
- */
730
- getStats(): {
731
- registeredTypes: number;
732
- totalInstances: number;
733
- typeBreakdown: Record<string, number>;
734
- };
735
- /**
736
- * Get all registered types (for plugin system).
737
- * @returns Array of all registered StateContainer class constructors
738
- */
739
- getTypes(): StateContainerConstructor[];
740
- /**
741
- * Subscribe to lifecycle events
742
- * @param event - The lifecycle event to listen for
743
- * @param listener - The listener function to call when the event occurs
744
- * @returns Unsubscribe function
745
- */
746
- on<E extends LifecycleEvent>(event: E, listener: LifecycleListener<E>): () => void;
747
- /* Excluded from this release type: emit */
748
- }
749
-
750
- /* Excluded from this release type: StateListener */
751
-
752
- declare const STOP: unique symbol;
753
-
754
- declare type StopSymbol = typeof STOP;
755
-
756
- /**
757
- * System events emitted by StateContainer lifecycle
758
- */
759
- export declare type SystemEvent = 'stateChanged' | 'dispose';
760
-
761
- /* Excluded from this release type: SystemEventHandler */
762
-
763
- /**
764
- * Payload types for each system event
765
- * @template S - State type
766
- */
767
- export declare interface SystemEventPayloads<S> {
768
- /** Emitted when state changes via emit() or update() */
769
- stateChanged: {
770
- state: S;
771
- previousState: S;
772
- };
773
- /** Emitted when the instance is disposed */
774
- dispose: void;
775
- }
776
-
777
- export declare const watch: WatchFn;
778
-
779
- /**
780
- * Combined watch function type.
781
- */
782
- export declare interface WatchFn extends WatchSingleFn {
783
- <T extends readonly BlocInput[]>(blocs: T, callback: (blocs: ExtractInstances<T>) => void | StopSymbol): () => void;
784
- }
785
-
786
- /**
787
- * Watch function signature for single bloc.
788
- */
789
- declare interface WatchSingleFn {
790
- <T extends StateContainerConstructor>(bloc: T | BlocRef<T>, callback: (bloc: InstanceType<T>) => void | StopSymbol): () => void;
791
- STOP: StopSymbol;
792
- }
793
-
794
- export { }
1
+ export { StateContainer } from './core/StateContainer';
2
+ export type { StateContainerConfig, SystemEvent, SystemEventPayloads, } from './core/StateContainer';
3
+ export { Cubit } from './core/Cubit';
4
+ export { acquire, borrow, borrowSafe, ensure, release, clear, clearAll, register, hasInstance, getRefCount, getAll, forEach, getRegistry, setRegistry, getStats, } from './registry';
5
+ export { globalRegistry } from './core/StateContainerRegistry';
6
+ export type { LifecycleEvent, LifecycleListener, InstanceEntry, } from './core/StateContainerRegistry';
7
+ export { blac, type BlacOptions } from './decorators';
8
+ export { getPluginManager } from './core/StateContainerRegistry';
9
+ export type { BlacPlugin, BlacPluginWithInit, PluginContext, PluginConfig, InstanceMetadata, } from './plugin/BlacPlugin';
10
+ export { PluginManager } from './plugin/PluginManager';
11
+ export { watch, instance, type WatchFn, type BlocRef } from './watch';
12
+ export { tracked, createTrackedContext, TrackedContext, type TrackedResult, type TrackedOptions, } from './tracking/tracked';
13
+ export type { StateContainerConstructor, ExtractState, ExtractStateMutable, ExtractConstructorArgs, BlocInstanceType, BlocConstructor, InstanceReadonlyState, InstanceState, StateContainerInstance, } from './types/utilities';
14
+ export type { Brand, BrandedId, InstanceId } from './types/branded';
15
+ export { instanceId } from './types/branded';
16
+ export * from './tracking';
17
+ export { isIsolatedClass } from './utils/static-props';
18
+ export { generateIsolatedKey } from './utils/idGenerator';