@blac/core 2.0.4 → 2.0.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.
Files changed (81) hide show
  1. package/README.md +98 -126
  2. package/dist/StateContainerRegistry-Brmferpf.js +588 -0
  3. package/dist/StateContainerRegistry-Brmferpf.js.map +1 -0
  4. package/dist/StateContainerRegistry-CVPTMaHn.cjs +629 -0
  5. package/dist/StateContainerRegistry-CVPTMaHn.cjs.map +1 -0
  6. package/dist/branded-DndTHjv2.cjs +18 -0
  7. package/dist/branded-DndTHjv2.cjs.map +1 -0
  8. package/dist/branded-mwGzzPF1.js +13 -0
  9. package/dist/branded-mwGzzPF1.js.map +1 -0
  10. package/dist/config-BInS_9WT.cjs +101 -0
  11. package/dist/config-BInS_9WT.cjs.map +1 -0
  12. package/dist/config-BmFnPVM0.js +42 -0
  13. package/dist/config-BmFnPVM0.js.map +1 -0
  14. package/dist/constants.d.cts +81 -0
  15. package/dist/constants.d.ts +81 -0
  16. package/dist/core/Cubit.d.ts +24 -0
  17. package/dist/core/StateContainer.d.ts +177 -0
  18. package/dist/core/StateContainerRegistry.d.ts +189 -0
  19. package/dist/debug.cjs +11 -26
  20. package/dist/debug.d.cts +35 -0
  21. package/dist/debug.d.ts +35 -0
  22. package/dist/debug.js +2 -18
  23. package/dist/decorators/blac.d.ts +40 -0
  24. package/dist/decorators/index.d.ts +1 -0
  25. package/dist/index.cjs +162 -55
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.d.cts +17 -794
  28. package/dist/index.d.ts +17 -794
  29. package/dist/index.js +145 -31
  30. package/dist/index.js.map +1 -1
  31. package/dist/plugin/BlacPlugin.d.ts +143 -0
  32. package/dist/plugin/PluginManager.d.ts +81 -0
  33. package/dist/plugins.cjs +3 -3
  34. package/dist/plugins.d.cts +25 -0
  35. package/dist/plugins.d.ts +25 -0
  36. package/dist/plugins.js +2 -3
  37. package/dist/registry/acquire.d.ts +2 -0
  38. package/dist/registry/borrow.d.ts +9 -0
  39. package/dist/registry/config.d.ts +8 -0
  40. package/dist/registry/ensure.d.ts +2 -0
  41. package/dist/registry/index.d.ts +7 -0
  42. package/dist/registry/management.d.ts +4 -0
  43. package/dist/registry/queries.d.ts +5 -0
  44. package/dist/registry/release.d.ts +2 -0
  45. package/dist/resolve-dependencies-B22YjFGk.cjs +773 -0
  46. package/dist/resolve-dependencies-B22YjFGk.cjs.map +1 -0
  47. package/dist/resolve-dependencies-Cvq0glJF.js +648 -0
  48. package/dist/resolve-dependencies-Cvq0glJF.js.map +1 -0
  49. package/dist/tracked-B_MUXIpe.cjs +216 -0
  50. package/dist/tracked-B_MUXIpe.cjs.map +1 -0
  51. package/dist/tracked-I_q9buYg.js +181 -0
  52. package/dist/tracked-I_q9buYg.js.map +1 -0
  53. package/dist/tracking.cjs +3 -3
  54. package/dist/tracking.js +2 -3
  55. package/dist/types/branded.d.ts +26 -0
  56. package/dist/types/utilities.d.ts +51 -0
  57. package/dist/types.cjs +3 -14
  58. package/dist/types.js +1 -12
  59. package/dist/utils/idGenerator.d.ts +73 -0
  60. package/dist/utils/static-props.d.ts +35 -0
  61. package/dist/watch/index.d.ts +2 -0
  62. package/dist/watch/watch.d.ts +50 -0
  63. package/dist/watch-entry.cjs +7 -109
  64. package/dist/watch-entry.d.cts +26 -0
  65. package/dist/watch-entry.d.ts +26 -0
  66. package/dist/watch-entry.js +1 -104
  67. package/package.json +29 -51
  68. package/dist/StateContainerRegistry.cjs.map +0 -1
  69. package/dist/StateContainerRegistry.js.map +0 -1
  70. package/dist/debug.cjs.map +0 -1
  71. package/dist/debug.js.map +0 -1
  72. package/dist/management.cjs.map +0 -1
  73. package/dist/management.js.map +0 -1
  74. package/dist/resolve-dependencies.cjs.map +0 -1
  75. package/dist/resolve-dependencies.js.map +0 -1
  76. package/dist/types.cjs.map +0 -1
  77. package/dist/types.js.map +0 -1
  78. package/dist/watch-entry.cjs.map +0 -1
  79. package/dist/watch-entry.js.map +0 -1
  80. package/dist/watch.cjs.map +0 -1
  81. package/dist/watch.js.map +0 -1
@@ -0,0 +1,189 @@
1
+ import type { StateContainer } from './StateContainer';
2
+ import { InstanceReadonlyState, StateContainerConstructor } from '../types/utilities';
3
+ /**
4
+ * Entry in the instance registry, tracking the instance and its reference count
5
+ * @template T - Instance type
6
+ */
7
+ export interface InstanceEntry<T = any> {
8
+ /** The state container instance */
9
+ instance: T;
10
+ /** Number of active references to this instance */
11
+ refCount: number;
12
+ }
13
+ /**
14
+ * Lifecycle events emitted by the registry
15
+ */
16
+ export type LifecycleEvent = 'created' | 'stateChanged' | 'disposed';
17
+ /**
18
+ * Listener function type for each lifecycle event
19
+ * @template E - The lifecycle event type
20
+ */
21
+ export 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;
22
+ /**
23
+ * Central registry for managing StateContainer instances.
24
+ * Handles instance lifecycle, ref counting, and lifecycle event emission.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const registry = new StateContainerRegistry();
29
+ * const instance = registry.acquire(MyBloc); // ownership, must release
30
+ * const other = registry.ensure(OtherBloc); // no ownership, bloc-to-bloc
31
+ * registry.on('stateChanged', (container, prev, next) => {
32
+ * console.log('State changed:', prev, '->', next);
33
+ * });
34
+ * ```
35
+ */
36
+ export declare class StateContainerRegistry {
37
+ private readonly instancesByConstructor;
38
+ private readonly types;
39
+ private readonly typeConfigs;
40
+ private readonly listeners;
41
+ /**
42
+ * Register a type for lifecycle event tracking
43
+ * @param constructor - The StateContainer class constructor
44
+ */
45
+ registerType<T extends StateContainerConstructor>(constructor: T): void;
46
+ /**
47
+ * Register a StateContainer class with configuration
48
+ * @param constructor - The StateContainer class constructor
49
+ * @param isolated - Whether instances should be isolated (component-scoped)
50
+ * @throws Error if type is already registered
51
+ */
52
+ register<T extends StateContainerConstructor>(constructor: T, isolated?: boolean): void;
53
+ private ensureInstancesMap;
54
+ /**
55
+ * Get the instances Map for a specific class (public API for stats/debugging)
56
+ */
57
+ getInstancesMap<T extends StateContainerConstructor>(Type: T): Map<string, InstanceEntry>;
58
+ /**
59
+ * Acquire an instance with ref counting (ownership semantics).
60
+ * Creates a new instance if one doesn't exist, or returns existing and increments ref count.
61
+ * You must call `release()` when done to decrement the ref count.
62
+ * @param Type - The StateContainer class constructor
63
+ * @param instanceKey - Instance key (defaults to 'default')
64
+ * @param options - Acquisition options
65
+ * @param options.canCreate - Whether to create new instance if not found (default: true)
66
+ * @param options.countRef - Whether to increment ref count (default: true)
67
+ * @returns The state container instance
68
+ */
69
+ acquire<T extends StateContainerConstructor = StateContainerConstructor>(Type: T, instanceKey?: string, options?: {
70
+ canCreate?: boolean;
71
+ countRef?: boolean;
72
+ }): InstanceType<T>;
73
+ /**
74
+ * Borrow an existing instance without incrementing ref count (borrowing semantics).
75
+ * Tracks cross-bloc dependency for reactive updates.
76
+ * @param Type - The StateContainer class constructor
77
+ * @param instanceKey - Instance key (defaults to 'default')
78
+ * @returns The state container instance
79
+ * @throws Error if instance doesn't exist
80
+ */
81
+ borrow<T extends StateContainerConstructor = StateContainerConstructor>(Type: T, instanceKey?: string): InstanceType<T>;
82
+ /**
83
+ * Safely borrow an existing instance (borrowing semantics with error handling).
84
+ * Returns discriminated union for type-safe conditional access.
85
+ * @param Type - The StateContainer class constructor
86
+ * @param instanceKey - Instance key (defaults to 'default')
87
+ * @returns Discriminated union with either the instance or an error
88
+ */
89
+ borrowSafe<T extends StateContainerConstructor = StateContainerConstructor>(Type: T, instanceKey?: string): {
90
+ error: Error;
91
+ instance: null;
92
+ } | {
93
+ error: null;
94
+ instance: InstanceType<T>;
95
+ };
96
+ /**
97
+ * Ensure an instance exists without taking ownership (for bloc-to-bloc communication).
98
+ * Gets existing instance OR creates it if it doesn't exist, without incrementing ref count.
99
+ * Tracks cross-bloc dependency for reactive updates.
100
+ *
101
+ * Use this in bloc-to-bloc communication when you need to ensure an instance exists
102
+ * but don't want to claim ownership (no ref count increment).
103
+ * @param Type - The StateContainer class constructor
104
+ * @param instanceKey - Instance key (defaults to 'default')
105
+ * @returns The state container instance
106
+ */
107
+ ensure<T extends StateContainerConstructor = StateContainerConstructor>(Type: T, instanceKey?: string): InstanceType<T>;
108
+ /**
109
+ * Release a reference to an instance.
110
+ * Decrements ref count and disposes when it reaches 0 (unless keepAlive).
111
+ * @param Type - The StateContainer class constructor
112
+ * @param instanceKey - Instance key (defaults to 'default')
113
+ * @param forceDispose - Force immediate disposal regardless of ref count
114
+ */
115
+ release<T extends StateContainerConstructor>(Type: T, instanceKey?: string, forceDispose?: boolean): void;
116
+ /**
117
+ * Get all instances of a specific type.
118
+ * @param Type - The StateContainer class constructor
119
+ * @returns Array of all instances
120
+ */
121
+ getAll<T extends StateContainerConstructor>(Type: T): InstanceReadonlyState<T>[];
122
+ /**
123
+ * Safely iterate over all instances of a type.
124
+ * Skips disposed instances and catches callback errors.
125
+ * @param Type - The StateContainer class constructor
126
+ * @param callback - Function to call for each instance
127
+ */
128
+ forEach<T extends StateContainerConstructor>(Type: T, callback: (instance: InstanceReadonlyState<T>) => void): void;
129
+ /**
130
+ * Clear all instances of a specific type (disposes them).
131
+ * @param Type - The StateContainer class constructor
132
+ */
133
+ clear<T extends StateContainerConstructor>(Type: T): void;
134
+ /**
135
+ * Get reference count for an instance.
136
+ * @param Type - The StateContainer class constructor
137
+ * @param instanceKey - Instance key (defaults to 'default')
138
+ * @returns Current ref count (0 if instance doesn't exist)
139
+ */
140
+ getRefCount<T extends StateContainerConstructor>(Type: T, instanceKey?: string): number;
141
+ /**
142
+ * Check if an instance exists.
143
+ * @param Type - The StateContainer class constructor
144
+ * @param instanceKey - Instance key (defaults to 'default')
145
+ * @returns true if instance exists
146
+ */
147
+ hasInstance<T extends StateContainerConstructor>(Type: T, instanceKey?: string): boolean;
148
+ /**
149
+ * Clear all instances from all types (for testing)
150
+ *
151
+ * Iterates all registered types and clears their instances.
152
+ * Also clears type tracking to reset the registry state.
153
+ */
154
+ clearAll(): void;
155
+ /**
156
+ * Get registry statistics for debugging.
157
+ * @returns Object with registeredTypes, totalInstances, and typeBreakdown
158
+ */
159
+ getStats(): {
160
+ registeredTypes: number;
161
+ totalInstances: number;
162
+ typeBreakdown: Record<string, number>;
163
+ };
164
+ /**
165
+ * Get all registered types (for plugin system).
166
+ * @returns Array of all registered StateContainer class constructors
167
+ */
168
+ getTypes(): StateContainerConstructor[];
169
+ /**
170
+ * Subscribe to lifecycle events
171
+ * @param event - The lifecycle event to listen for
172
+ * @param listener - The listener function to call when the event occurs
173
+ * @returns Unsubscribe function
174
+ */
175
+ on<E extends LifecycleEvent>(event: E, listener: LifecycleListener<E>): () => void;
176
+ /**
177
+ * Emit lifecycle event to all listeners
178
+ * @internal - Called by StateContainer lifecycle methods
179
+ */
180
+ emit(event: LifecycleEvent, ...args: any[]): void;
181
+ }
182
+ /**
183
+ * Global default registry instance
184
+ */
185
+ export declare const globalRegistry: StateContainerRegistry;
186
+ /**
187
+ * Get the global plugin manager
188
+ */
189
+ export declare function getPluginManager(): any;
package/dist/debug.cjs CHANGED
@@ -1,27 +1,12 @@
1
- const require_StateContainerRegistry = require('./StateContainerRegistry.cjs');
2
- const require_management = require('./management.cjs');
3
-
4
- //#region src/registry/config.ts
5
- let _registry = require_StateContainerRegistry.globalRegistry;
6
- function getRegistry() {
7
- return _registry;
8
- }
9
- function setRegistry(registry) {
10
- _registry.clearAll();
11
- _registry = registry;
12
- }
13
- function getStats() {
14
- return _registry.getStats();
15
- }
16
-
17
- //#endregion
18
- exports.forEach = require_management.forEach;
19
- exports.getAll = require_management.getAll;
20
- exports.getRefCount = require_management.getRefCount;
21
- exports.getRegistry = getRegistry;
22
- exports.getStats = getStats;
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_StateContainerRegistry = require("./StateContainerRegistry-CVPTMaHn.cjs");
3
+ const require_config = require("./config-BInS_9WT.cjs");
4
+ exports.forEach = require_config.forEach;
5
+ exports.getAll = require_config.getAll;
6
+ exports.getRefCount = require_config.getRefCount;
7
+ exports.getRegistry = require_config.getRegistry;
8
+ exports.getStats = require_config.getStats;
23
9
  exports.globalRegistry = require_StateContainerRegistry.globalRegistry;
24
- exports.hasInstance = require_management.hasInstance;
25
- exports.register = require_management.register;
26
- exports.setRegistry = setRegistry;
27
- //# sourceMappingURL=debug.cjs.map
10
+ exports.hasInstance = require_config.hasInstance;
11
+ exports.register = require_config.register;
12
+ exports.setRegistry = require_config.setRegistry;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Debug/Advanced Subpath Export
3
+ *
4
+ * Advanced registry introspection and debugging utilities.
5
+ * Import from '@blac/core/debug'
6
+ *
7
+ * These utilities are intended for debugging, testing, and advanced use cases.
8
+ * Most applications don't need these - use the main '@blac/core' exports instead.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { getStats, globalRegistry, hasInstance } from '@blac/core/debug';
13
+ *
14
+ * // Get registry statistics
15
+ * const stats = getStats();
16
+ * console.log(`Total instances: ${stats.totalInstances}`);
17
+ *
18
+ * // Check if an instance exists
19
+ * if (hasInstance(UserBloc)) {
20
+ * console.log('UserBloc is registered');
21
+ * }
22
+ *
23
+ * // Iterate over all instances of a type
24
+ * forEach(UserBloc, (instance) => {
25
+ * console.log(instance.state);
26
+ * });
27
+ * ```
28
+ *
29
+ * @packageDocumentation
30
+ */
31
+ export { hasInstance, getRefCount, getAll, forEach } from './registry/queries';
32
+ export { register } from './registry/management';
33
+ export { getRegistry, setRegistry, getStats } from './registry/config';
34
+ export { globalRegistry } from './core/StateContainerRegistry';
35
+ export type { LifecycleEvent, LifecycleListener, InstanceEntry, } from './core/StateContainerRegistry';
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Debug/Advanced Subpath Export
3
+ *
4
+ * Advanced registry introspection and debugging utilities.
5
+ * Import from '@blac/core/debug'
6
+ *
7
+ * These utilities are intended for debugging, testing, and advanced use cases.
8
+ * Most applications don't need these - use the main '@blac/core' exports instead.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { getStats, globalRegistry, hasInstance } from '@blac/core/debug';
13
+ *
14
+ * // Get registry statistics
15
+ * const stats = getStats();
16
+ * console.log(`Total instances: ${stats.totalInstances}`);
17
+ *
18
+ * // Check if an instance exists
19
+ * if (hasInstance(UserBloc)) {
20
+ * console.log('UserBloc is registered');
21
+ * }
22
+ *
23
+ * // Iterate over all instances of a type
24
+ * forEach(UserBloc, (instance) => {
25
+ * console.log(instance.state);
26
+ * });
27
+ * ```
28
+ *
29
+ * @packageDocumentation
30
+ */
31
+ export { hasInstance, getRefCount, getAll, forEach } from './registry/queries';
32
+ export { register } from './registry/management';
33
+ export { getRegistry, setRegistry, getStats } from './registry/config';
34
+ export { globalRegistry } from './core/StateContainerRegistry';
35
+ export type { LifecycleEvent, LifecycleListener, InstanceEntry, } from './core/StateContainerRegistry';
package/dist/debug.js CHANGED
@@ -1,19 +1,3 @@
1
- import { n as globalRegistry } from "./StateContainerRegistry.js";
2
- import { a as getAll, i as forEach, o as getRefCount, r as register, s as hasInstance } from "./management.js";
3
-
4
- //#region src/registry/config.ts
5
- let _registry = globalRegistry;
6
- function getRegistry() {
7
- return _registry;
8
- }
9
- function setRegistry(registry) {
10
- _registry.clearAll();
11
- _registry = registry;
12
- }
13
- function getStats() {
14
- return _registry.getStats();
15
- }
16
-
17
- //#endregion
1
+ import { n as globalRegistry } from "./StateContainerRegistry-Brmferpf.js";
2
+ import { c as getAll, l as getRefCount, n as getStats, o as register, r as setRegistry, s as forEach, t as getRegistry, u as hasInstance } from "./config-BmFnPVM0.js";
18
3
  export { forEach, getAll, getRefCount, getRegistry, getStats, globalRegistry, hasInstance, register, setRegistry };
19
- //# sourceMappingURL=debug.js.map
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Configuration options for the @blac decorator.
3
+ * Only one option can be specified at a time (union type).
4
+ */
5
+ export type BlacOptions =
6
+ /** Mark bloc as isolated (each component gets its own instance) */
7
+ {
8
+ isolated: true;
9
+ }
10
+ /** Mark bloc to never be auto-disposed when ref count reaches 0 */
11
+ | {
12
+ keepAlive: true;
13
+ }
14
+ /** Exclude bloc from DevTools tracking (prevents infinite loops) */
15
+ | {
16
+ excludeFromDevTools: true;
17
+ };
18
+ /**
19
+ * Decorator to configure StateContainer classes.
20
+ *
21
+ * @example Decorator syntax (requires experimentalDecorators or TC39 decorators)
22
+ * ```ts
23
+ * @blac({ isolated: true })
24
+ * class FormBloc extends Cubit<FormState> {}
25
+ *
26
+ * @blac({ keepAlive: true })
27
+ * class AuthBloc extends Cubit<AuthState> {}
28
+ *
29
+ * @blac({ excludeFromDevTools: true })
30
+ * class InternalBloc extends Cubit<InternalState> {}
31
+ * ```
32
+ *
33
+ * @example Function syntax (no decorator support needed)
34
+ * ```ts
35
+ * const FormBloc = blac({ isolated: true })(
36
+ * class extends Cubit<FormState> {}
37
+ * );
38
+ * ```
39
+ */
40
+ export declare function blac(options: BlacOptions): <T extends new (...args: any[]) => any>(target: T, _context?: ClassDecoratorContext) => T;
@@ -0,0 +1 @@
1
+ export { blac, type BlacOptions } from './blac';