@blac/core 2.0.0 → 2.0.3
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/README.md +137 -177
- package/dist/StateContainerRegistry.cjs.map +1 -0
- package/dist/StateContainerRegistry.js.map +1 -0
- package/dist/debug.cjs +27 -0
- package/dist/debug.cjs.map +1 -0
- package/dist/debug.js +19 -0
- package/dist/debug.js.map +1 -0
- package/dist/dependency-manager.cjs.map +1 -0
- package/dist/dependency-manager.js.map +1 -0
- package/dist/index.cjs +346 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +794 -0
- package/dist/index.d.ts +675 -386
- package/dist/index.js +310 -0
- package/dist/index.js.map +1 -0
- package/dist/management.cjs.map +1 -0
- package/dist/management.js.map +1 -0
- package/dist/plugins.cjs +4 -0
- package/dist/plugins.js +3 -0
- package/dist/tracking/dependency-manager.d.ts +37 -0
- package/dist/tracking/index.d.ts +12 -0
- package/dist/tracking/path-utils.d.ts +52 -0
- package/dist/tracking/tracked.d.ts +70 -0
- package/dist/tracking/tracking-proxy.d.ts +197 -0
- package/dist/tracking-proxy.cjs.map +1 -0
- package/dist/tracking-proxy.js.map +1 -0
- package/dist/tracking.cjs +19 -0
- package/dist/tracking.d.cts +9 -0
- package/dist/tracking.d.ts +9 -0
- package/dist/tracking.js +4 -0
- package/dist/types.cjs +14 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +8 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -0
- package/dist/watch-entry.cjs +111 -0
- package/dist/watch-entry.cjs.map +1 -0
- package/dist/watch-entry.js +107 -0
- package/dist/watch-entry.js.map +1 -0
- package/dist/watch.cjs.map +1 -0
- package/dist/watch.js.map +1 -0
- package/package.json +122 -19
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.es.js +0 -670
- package/dist/index.es.js.map +0 -1
- package/src/Blac.ts +0 -483
- package/src/BlacEvent.ts +0 -5
- package/src/BlacObservable.ts +0 -88
- package/src/BlacObserver.ts +0 -129
- package/src/BlacPlugin.ts +0 -13
- package/src/Bloc.ts +0 -109
- package/src/BlocBase.ts +0 -248
- package/src/Cubit.ts +0 -69
- package/src/addons/BlacAddon.ts +0 -17
- package/src/addons/Persist.ts +0 -105
- package/src/addons/index.ts +0 -1
- package/src/index.ts +0 -8
- package/src/types.ts +0 -63
package/dist/index.d.ts
CHANGED
|
@@ -1,505 +1,794 @@
|
|
|
1
|
+
export declare function acquire<T extends StateContainerConstructor>(BlocClass: T, instanceKey?: string): InstanceType<T>;
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
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
|
+
* ```
|
|
5
17
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
18
|
+
* @example Function syntax (no decorator support needed)
|
|
19
|
+
* ```ts
|
|
20
|
+
* const FormBloc = blac({ isolated: true })(
|
|
21
|
+
* class extends Cubit<FormState> {}
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
11
24
|
*/
|
|
12
|
-
export declare
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
*/
|
|
41
|
-
log: (...args: unknown[]) => void;
|
|
42
|
-
static log: (...args: unknown[]) => void;
|
|
43
|
-
/**
|
|
44
|
-
* Gets the singleton instance of Blac
|
|
45
|
-
* @returns The Blac instance
|
|
46
|
-
*/
|
|
47
|
-
static getInstance(): Blac;
|
|
48
|
-
/**
|
|
49
|
-
* Logs a warning message
|
|
50
|
-
* @param message - Warning message
|
|
51
|
-
* @param args - Additional arguments
|
|
52
|
-
*/
|
|
53
|
-
warn: (message: string, ...args: unknown[]) => void;
|
|
54
|
-
static warn: (message: string, ...args: unknown[]) => void;
|
|
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;
|
|
55
53
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @param message - Error message
|
|
58
|
-
* @param args - Additional arguments
|
|
54
|
+
* Called when the plugin is installed (optional)
|
|
59
55
|
*/
|
|
60
|
-
|
|
61
|
-
static error: (message: string, ...args: unknown[]) => void;
|
|
56
|
+
onInstall?(context: PluginContext): void;
|
|
62
57
|
/**
|
|
63
|
-
*
|
|
64
|
-
* from the old instance.
|
|
58
|
+
* Called when the plugin is uninstalled
|
|
65
59
|
*/
|
|
66
|
-
|
|
67
|
-
static resetInstance: () => void;
|
|
60
|
+
onUninstall?(): void;
|
|
68
61
|
/**
|
|
69
|
-
*
|
|
70
|
-
* @param plugin - The plugin to add
|
|
62
|
+
* Called when a state container instance is created
|
|
71
63
|
*/
|
|
72
|
-
|
|
64
|
+
onInstanceCreated?(instance: StateContainer<any>, context: PluginContext): void;
|
|
73
65
|
/**
|
|
74
|
-
*
|
|
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
|
|
66
|
+
* Called when state changes in a container instance
|
|
78
67
|
*/
|
|
79
|
-
|
|
68
|
+
onStateChanged?<S extends object = any>(instance: StateContainer<S>, previousState: S, currentState: S, callstack: string | undefined, context: PluginContext): void;
|
|
80
69
|
/**
|
|
81
|
-
*
|
|
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
|
|
70
|
+
* Called when a state container instance is disposed
|
|
91
71
|
*/
|
|
92
|
-
|
|
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 {
|
|
93
79
|
/**
|
|
94
|
-
*
|
|
95
|
-
* @param bloc - The bloc instance to dispose
|
|
80
|
+
* Required initialization hook called when plugin is installed
|
|
96
81
|
*/
|
|
97
|
-
|
|
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);
|
|
98
163
|
/**
|
|
99
|
-
*
|
|
100
|
-
* @param
|
|
101
|
-
* @param id - The instance ID
|
|
102
|
-
* @returns A unique key string in the format "className:id"
|
|
164
|
+
* Replace state with a new value and notify all listeners
|
|
165
|
+
* @param newState - The new state value
|
|
103
166
|
*/
|
|
104
|
-
|
|
167
|
+
emit(newState: S): void;
|
|
105
168
|
/**
|
|
106
|
-
*
|
|
107
|
-
* @param
|
|
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
|
|
108
171
|
*/
|
|
109
|
-
|
|
172
|
+
update(updater: (current: S) => S): void;
|
|
110
173
|
/**
|
|
111
|
-
*
|
|
112
|
-
* @param bloc - The bloc instance to register
|
|
174
|
+
* Merge partial state changes into current state (only for object states)
|
|
113
175
|
*/
|
|
114
|
-
|
|
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;
|
|
115
187
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* @
|
|
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
|
|
120
195
|
*/
|
|
121
|
-
|
|
196
|
+
sync(newDeps: Set<StateContainerInstance>, onChange: () => void, exclude?: StateContainerInstance): boolean;
|
|
122
197
|
/**
|
|
123
|
-
*
|
|
124
|
-
* @param bloc - The isolated bloc instance to register
|
|
198
|
+
* Add a single dependency subscription.
|
|
125
199
|
*/
|
|
126
|
-
|
|
200
|
+
add(dep: StateContainerInstance, onChange: () => void): void;
|
|
127
201
|
/**
|
|
128
|
-
*
|
|
129
|
-
* @param bloc - The isolated bloc instance to unregister
|
|
202
|
+
* Check if a dependency is currently subscribed.
|
|
130
203
|
*/
|
|
131
|
-
|
|
204
|
+
has(dep: StateContainerInstance): boolean;
|
|
132
205
|
/**
|
|
133
|
-
*
|
|
206
|
+
* Get the current set of dependencies.
|
|
134
207
|
*/
|
|
135
|
-
|
|
208
|
+
getDependencies(): Set<StateContainerInstance>;
|
|
136
209
|
/**
|
|
137
|
-
*
|
|
138
|
-
* @param blocClass - The bloc class to instantiate
|
|
139
|
-
* @param id - The instance ID
|
|
140
|
-
* @param props - Properties to pass to the bloc constructor
|
|
141
|
-
* @param instanceRef - Optional reference string for the instance
|
|
142
|
-
* @returns The newly created bloc instance
|
|
210
|
+
* Clean up all active subscriptions.
|
|
143
211
|
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
* Gets or creates a bloc instance based on the provided class and options.
|
|
147
|
-
* If a bloc with the given ID exists, it will be returned. Otherwise, a new instance will be created.
|
|
148
|
-
*
|
|
149
|
-
* @param blocClass - The bloc class to get or create
|
|
150
|
-
* @param options - Options including:
|
|
151
|
-
* - id: The instance ID (defaults to class name if not provided)
|
|
152
|
-
* - props: Properties to pass to the bloc constructor
|
|
153
|
-
* - instanceRef: Optional reference string for the instance
|
|
154
|
-
* @returns The bloc instance
|
|
155
|
-
*/
|
|
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>;
|
|
158
|
-
/**
|
|
159
|
-
* Gets a bloc instance or throws an error if it doesn't exist
|
|
160
|
-
* @param blocClass - The bloc class to get
|
|
161
|
-
* @param options - Options including:
|
|
162
|
-
* - id: The instance ID (defaults to class name if not provided)
|
|
163
|
-
* - props: Properties to pass to the bloc constructor
|
|
164
|
-
* - instanceRef: Optional reference string for the instance
|
|
165
|
-
*/
|
|
166
|
-
getBlocOrThrow: <B extends BlocConstructor<unknown>>(blocClass: B, options?: {
|
|
167
|
-
id?: BlocInstanceId;
|
|
168
|
-
props?: InferPropsFromGeneric<B>;
|
|
169
|
-
instanceRef?: string;
|
|
170
|
-
}) => InstanceType<B>;
|
|
171
|
-
static getBlocOrThrow: <B extends BlocConstructor<unknown>>(blocClass: B, options?: {
|
|
172
|
-
id?: BlocInstanceId;
|
|
173
|
-
props?: InferPropsFromGeneric<B> | undefined;
|
|
174
|
-
instanceRef?: string;
|
|
175
|
-
}) => InstanceType<B>;
|
|
176
|
-
/**
|
|
177
|
-
* Gets all instances of a specific bloc class
|
|
178
|
-
* @param blocClass - The bloc class to search for
|
|
179
|
-
* @param options - Options including:
|
|
180
|
-
* - searchIsolated: Whether to search in isolated blocs (defaults to bloc's isolated property)
|
|
181
|
-
* @returns Array of matching bloc instances
|
|
182
|
-
*/
|
|
183
|
-
getAllBlocs: <B extends BlocConstructor<unknown>>(blocClass: B, options?: {
|
|
184
|
-
searchIsolated?: boolean;
|
|
185
|
-
}) => InstanceType<B>[];
|
|
212
|
+
cleanup(): void;
|
|
213
|
+
private areSetsEqual;
|
|
186
214
|
}
|
|
187
215
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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]>;
|
|
192
230
|
};
|
|
193
231
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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;
|
|
199
239
|
|
|
200
|
-
declare
|
|
240
|
+
export declare function forEach<T extends StateContainerConstructor>(BlocClass: T, callback: (instance: InstanceReadonlyState<T>) => void): void;
|
|
201
241
|
|
|
202
242
|
/**
|
|
203
|
-
*
|
|
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
|
|
204
250
|
*/
|
|
205
|
-
export declare
|
|
206
|
-
|
|
207
|
-
|
|
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;
|
|
208
298
|
}
|
|
209
299
|
|
|
210
300
|
/**
|
|
211
|
-
*
|
|
212
|
-
* These events are used to track the lifecycle of blocs and their consumers.
|
|
301
|
+
* Metadata information about a state container instance for debugging and inspection
|
|
213
302
|
*/
|
|
214
|
-
export declare
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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;
|
|
222
326
|
}
|
|
223
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
|
+
|
|
224
338
|
/**
|
|
225
|
-
*
|
|
226
|
-
*
|
|
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`
|
|
227
343
|
*/
|
|
228
|
-
export declare
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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 {
|
|
242
373
|
/**
|
|
243
|
-
*
|
|
244
|
-
* @returns The Set of all BlacObserver instances
|
|
374
|
+
* Get metadata for a specific instance
|
|
245
375
|
*/
|
|
246
|
-
|
|
376
|
+
getInstanceMetadata(instance: StateContainer<any>): InstanceMetadata;
|
|
247
377
|
/**
|
|
248
|
-
*
|
|
249
|
-
* @param observer - The observer to subscribe
|
|
250
|
-
* @returns A function that can be called to unsubscribe the observer
|
|
378
|
+
* Get current state from a container
|
|
251
379
|
*/
|
|
252
|
-
|
|
380
|
+
getState<S extends object = any>(instance: StateContainer<S>): S;
|
|
253
381
|
/**
|
|
254
|
-
*
|
|
255
|
-
* @param observer - The observer to unsubscribe
|
|
382
|
+
* Get all instances of a specific type
|
|
256
383
|
*/
|
|
257
|
-
|
|
384
|
+
queryInstances<T extends StateContainer<any>>(typeClass: new (...args: any[]) => T): T[];
|
|
258
385
|
/**
|
|
259
|
-
*
|
|
260
|
-
* @param newState - The new state value
|
|
261
|
-
* @param oldState - The previous state value
|
|
262
|
-
* @param action - Optional action that triggered the state change
|
|
386
|
+
* Get all registered state container types
|
|
263
387
|
*/
|
|
264
|
-
|
|
388
|
+
getAllTypes(): Array<new (...args: any[]) => StateContainer<any>>;
|
|
265
389
|
/**
|
|
266
|
-
*
|
|
390
|
+
* Get registry statistics
|
|
267
391
|
*/
|
|
268
|
-
|
|
392
|
+
getStats(): {
|
|
393
|
+
registeredTypes: number;
|
|
394
|
+
totalInstances: number;
|
|
395
|
+
typeBreakdown: Record<string, number>;
|
|
396
|
+
};
|
|
269
397
|
}
|
|
270
398
|
|
|
271
399
|
/**
|
|
272
|
-
*
|
|
273
|
-
*
|
|
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
|
+
* ```
|
|
274
408
|
*/
|
|
275
|
-
export declare
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
/** Optional function to determine if the observer should be notified of state changes */
|
|
279
|
-
dependencyArray?: BlocHookDependencyArrayFn<S>;
|
|
280
|
-
/** Dispose function for the observer */
|
|
281
|
-
dispose?: () => void;
|
|
282
|
-
/** Cached state values used for dependency comparison */
|
|
283
|
-
lastState?: unknown[][];
|
|
284
|
-
/** Unique identifier for the observer */
|
|
285
|
-
id: string;
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
export declare interface BlacPlugin {
|
|
289
|
-
name: string;
|
|
290
|
-
onEvent(event: BlacLifecycleEvent, bloc: BlocBase<any>, params?: unknown): void;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
export declare abstract class Bloc<S, // State type
|
|
294
|
-
A extends object, // Base Action/Event type, constrained to object
|
|
295
|
-
P = unknown> extends BlocBase<S, P> {
|
|
296
|
-
readonly eventHandlers: Map<new (...args: any[]) => A, (event: A, emit: (newState: S) => void) => void | Promise<void>>;
|
|
409
|
+
export declare class PluginManager {
|
|
410
|
+
private plugins;
|
|
411
|
+
private registry;
|
|
297
412
|
/**
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
* @param eventConstructor The constructor of the event to handle (e.g., LoadDataEvent).
|
|
301
|
-
* @param handler A function that processes the event and can emit new states.
|
|
302
|
-
* The 'event' parameter in the handler will be typed to the specific eventConstructor.
|
|
413
|
+
* Create a new PluginManager
|
|
414
|
+
* @param registry - The StateContainerRegistry to monitor for lifecycle events
|
|
303
415
|
*/
|
|
304
|
-
|
|
416
|
+
constructor(registry: StateContainerRegistry);
|
|
305
417
|
/**
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
* @
|
|
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
|
|
310
422
|
*/
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Base class for both Blocs and Cubits that provides core state management functionality.
|
|
316
|
-
* Handles state transitions, observer notifications, lifecycle management, and addon integration.
|
|
317
|
-
*
|
|
318
|
-
* @abstract This class should be extended, not instantiated directly
|
|
319
|
-
* @template S The type of state managed by this Bloc
|
|
320
|
-
* @template P The type of props that can be passed during instance creation (optional)
|
|
321
|
-
*/
|
|
322
|
-
export declare abstract class BlocBase<S, P = unknown> {
|
|
423
|
+
install(plugin: BlacPlugin, config?: PluginConfig): void;
|
|
323
424
|
/**
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
* @
|
|
425
|
+
* Uninstall a plugin by name
|
|
426
|
+
* @param pluginName - The name of the plugin to uninstall
|
|
427
|
+
* @throws Error if plugin is not installed
|
|
327
428
|
*/
|
|
328
|
-
|
|
329
|
-
get isIsolated(): boolean;
|
|
429
|
+
uninstall(pluginName: string): void;
|
|
330
430
|
/**
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
* @
|
|
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
|
|
334
434
|
*/
|
|
335
|
-
|
|
336
|
-
get isKeepAlive(): boolean;
|
|
435
|
+
getPlugin(pluginName: string): BlacPlugin | undefined;
|
|
337
436
|
/**
|
|
338
|
-
*
|
|
339
|
-
*
|
|
437
|
+
* Get all installed plugins
|
|
438
|
+
* @returns Array of all installed plugins
|
|
340
439
|
*/
|
|
341
|
-
|
|
342
|
-
/* Excluded from this release type: _addons */
|
|
343
|
-
/* Excluded from this release type: _isolated */
|
|
344
|
-
/* Excluded from this release type: _observer */
|
|
345
|
-
/* Excluded from this release type: _blac */
|
|
440
|
+
getAllPlugins(): BlacPlugin[];
|
|
346
441
|
/**
|
|
347
|
-
*
|
|
348
|
-
*
|
|
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
|
|
349
445
|
*/
|
|
350
|
-
|
|
351
|
-
/* Excluded from this release type: _instanceRef */
|
|
352
|
-
/* Excluded from this release type: _keepAlive */
|
|
446
|
+
hasPlugin(pluginName: string): boolean;
|
|
353
447
|
/**
|
|
354
|
-
*
|
|
355
|
-
* Timestamp when this Bloc instance was created, useful for debugging and performance tracking.
|
|
448
|
+
* Uninstall all plugins
|
|
356
449
|
*/
|
|
357
|
-
|
|
358
|
-
/* Excluded from this release type: _state */
|
|
359
|
-
/* Excluded from this release type: _oldState */
|
|
450
|
+
clear(): void;
|
|
360
451
|
/**
|
|
361
|
-
*
|
|
362
|
-
* Can be used to configure or parameterize the Bloc's behavior.
|
|
452
|
+
* Setup lifecycle hooks to notify plugins
|
|
363
453
|
*/
|
|
364
|
-
|
|
454
|
+
private setupLifecycleHooks;
|
|
365
455
|
/**
|
|
366
|
-
*
|
|
367
|
-
* Sets up the observer, registers with the Blac manager, and initializes addons.
|
|
368
|
-
*
|
|
369
|
-
* @param initialState The initial state value for this Bloc
|
|
456
|
+
* Notify all plugins of a lifecycle event
|
|
370
457
|
*/
|
|
371
|
-
|
|
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;
|
|
372
467
|
/**
|
|
373
|
-
*
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
get state(): S;
|
|
377
|
-
/* Excluded from this release type: _name */
|
|
378
|
-
/* Excluded from this release type: _updateId */
|
|
379
|
-
/* Excluded from this release type: _dispose */
|
|
380
|
-
/* Excluded from this release type: onDispose */
|
|
381
|
-
/* Excluded from this release type: _consumers */
|
|
382
|
-
/* Excluded from this release type: _addConsumer */
|
|
383
|
-
/* Excluded from this release type: _removeConsumer */
|
|
384
|
-
/* Excluded from this release type: _connectAddons */
|
|
385
|
-
lastUpdate: number;
|
|
386
|
-
/* Excluded from this release type: _pushState */
|
|
468
|
+
* Get current environment
|
|
469
|
+
*/
|
|
470
|
+
private getCurrentEnvironment;
|
|
387
471
|
}
|
|
388
472
|
|
|
389
|
-
|
|
390
|
-
* Represents the abstract base types for Bloc and Cubit
|
|
391
|
-
*/
|
|
392
|
-
export declare type BlocBaseAbstract = typeof Bloc<any, any, any> | typeof Cubit<any, any>;
|
|
473
|
+
/* Excluded from this release type: ProxyState */
|
|
393
474
|
|
|
394
|
-
|
|
395
|
-
* Represents a constructor type for a Bloc that takes no parameters
|
|
396
|
-
* @template B - The type of the Bloc instance
|
|
397
|
-
*/
|
|
398
|
-
export declare type BlocClassNoParams<B> = new (args: never[]) => B;
|
|
475
|
+
export declare function release<T extends StateContainerConstructor>(BlocClass: T, instanceKey?: string, forceDispose?: boolean): void;
|
|
399
476
|
|
|
400
|
-
|
|
401
|
-
* Represents a constructor type for a Bloc that can take any parameters
|
|
402
|
-
* @template B - The type of the Bloc instance
|
|
403
|
-
*/
|
|
404
|
-
export declare type BlocConstructor<B> = new (...args: any) => B;
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* Extracts the constructor parameters type from a BlocBase
|
|
408
|
-
* @template B - The BlocBase type to extract the constructor parameters from
|
|
409
|
-
*/
|
|
410
|
-
export declare type BlocConstructorParameters<B extends BlocBase<any>> = BlocConstructor<B> extends new (...args: infer P) => B ? P : never;
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Represents a function type for determining hook dependencies based on state changes
|
|
414
|
-
* @template B - The BlocGeneric type
|
|
415
|
-
*/
|
|
416
|
-
export declare type BlocHookDependencyArrayFn<S> = (newState: S, oldState: S) => unknown[][];
|
|
477
|
+
/* Excluded from this release type: setActiveTracker */
|
|
417
478
|
|
|
418
|
-
|
|
479
|
+
/* Excluded from this release type: shallowEqual */
|
|
419
480
|
|
|
420
|
-
|
|
421
|
-
* Extracts the state type from either a Bloc or Cubit
|
|
422
|
-
* @template T - The Bloc or Cubit type to extract the state from
|
|
423
|
-
*/
|
|
424
|
-
export declare type BlocState<T> = T extends BlocBase<infer S> ? S : never;
|
|
481
|
+
/* Excluded from this release type: startDependency */
|
|
425
482
|
|
|
426
483
|
/**
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
*
|
|
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
|
+
* ```
|
|
431
501
|
*/
|
|
432
|
-
export declare abstract class
|
|
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);
|
|
433
519
|
/**
|
|
434
|
-
*
|
|
435
|
-
*
|
|
436
|
-
*
|
|
437
|
-
* @param state - The new state to set
|
|
520
|
+
* Initialize configuration for this instance.
|
|
521
|
+
* Called by the registry after construction.
|
|
522
|
+
* @param config - Configuration options
|
|
438
523
|
*/
|
|
439
|
-
|
|
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;
|
|
440
529
|
/**
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
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
|
|
447
544
|
*/
|
|
448
|
-
|
|
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;
|
|
449
566
|
}
|
|
450
567
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
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;
|
|
459
578
|
}
|
|
460
579
|
|
|
461
580
|
/**
|
|
462
|
-
*
|
|
463
|
-
* @template
|
|
581
|
+
* Constructor type for StateContainer classes
|
|
582
|
+
* @template S - State type managed by the container
|
|
464
583
|
*/
|
|
465
|
-
export declare type
|
|
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
|
+
};
|
|
466
589
|
|
|
467
590
|
/**
|
|
468
|
-
*
|
|
591
|
+
* Central registry for managing StateContainer instances.
|
|
592
|
+
* Handles instance lifecycle, ref counting, and lifecycle event emission.
|
|
469
593
|
*
|
|
470
|
-
* @
|
|
471
|
-
*
|
|
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
|
+
* ```
|
|
472
603
|
*/
|
|
473
|
-
|
|
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;
|
|
474
687
|
/**
|
|
475
|
-
*
|
|
688
|
+
* Get all instances of a specific type.
|
|
689
|
+
* @param Type - The StateContainer class constructor
|
|
690
|
+
* @returns Array of all instances
|
|
476
691
|
*/
|
|
477
|
-
|
|
692
|
+
getAll<T extends StateContainerConstructor>(Type: T): InstanceReadonlyState<T>[];
|
|
478
693
|
/**
|
|
479
|
-
*
|
|
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
|
|
480
698
|
*/
|
|
481
|
-
|
|
699
|
+
forEach<T extends StateContainerConstructor>(Type: T, callback: (instance: InstanceReadonlyState<T>) => void): void;
|
|
482
700
|
/**
|
|
483
|
-
*
|
|
701
|
+
* Clear all instances of a specific type (disposes them).
|
|
702
|
+
* @param Type - The StateContainer class constructor
|
|
484
703
|
*/
|
|
485
|
-
|
|
704
|
+
clear<T extends StateContainerConstructor>(Type: T): void;
|
|
486
705
|
/**
|
|
487
|
-
*
|
|
488
|
-
* @
|
|
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)
|
|
489
710
|
*/
|
|
490
|
-
|
|
711
|
+
getRefCount<T extends StateContainerConstructor>(Type: T, instanceKey?: string): number;
|
|
491
712
|
/**
|
|
492
|
-
*
|
|
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
|
|
493
717
|
*/
|
|
494
|
-
|
|
495
|
-
|
|
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;
|
|
496
755
|
|
|
497
|
-
|
|
756
|
+
/**
|
|
757
|
+
* System events emitted by StateContainer lifecycle
|
|
758
|
+
*/
|
|
759
|
+
export declare type SystemEvent = 'stateChanged' | 'dispose';
|
|
760
|
+
|
|
761
|
+
/* Excluded from this release type: SystemEventHandler */
|
|
498
762
|
|
|
499
763
|
/**
|
|
500
|
-
*
|
|
501
|
-
* @template
|
|
764
|
+
* Payload types for each system event
|
|
765
|
+
* @template S - State type
|
|
502
766
|
*/
|
|
503
|
-
export declare
|
|
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
|
+
}
|
|
504
793
|
|
|
505
794
|
export { }
|