@blac/react 2.0.0-rc.5 → 2.0.0-rc.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,453 +1,12 @@
1
- import { RefObject } from "react";
2
-
3
- //#region ../blac/src/types/events.d.ts
4
-
5
1
  /**
6
- * Base event interface for type-safe event system
7
- */
8
- /**
9
- * Base event interface
10
- * Used by Vertex for event-driven state management
11
- */
12
- interface BaseEvent {
13
- readonly type: string;
14
- readonly timestamp: number;
15
- readonly source?: string;
16
- }
17
- //#endregion
18
- //#region ../blac/src/core/Vertex.d.ts
19
-
20
- /**
21
- * Event handler signature (synchronous only)
22
- */
23
- type EventHandler<E$1 extends BaseEvent, S$1> = (event: E$1, emit: (state: S$1) => void) => void;
24
- /**
25
- * Type-safe event constructor
26
- */
27
- type EventConstructor<T$1 extends BaseEvent = BaseEvent> = new (...args: never[]) => T$1;
28
- /**
29
- * Vertex is an event-driven state container (Bloc pattern)
30
- *
31
- * Includes its own simple event system since events are only needed
32
- * for the Vertex pattern, not for the base StateContainer.
33
- */
34
- declare abstract class Vertex<S$1, E$1 extends BaseEvent = BaseEvent> extends StateContainer<S$1> {
35
- private eventHandlers;
36
- private isProcessing;
37
- private eventQueue;
38
- /**
39
- * Create a new Vertex
40
- * @param initialState Initial state value
41
- * @param config Configuration options
42
- */
43
- constructor(initialState: S$1, config?: StateContainerConfig);
44
- /**
45
- * Register an event handler
46
- * Arrow function to maintain correct 'this' binding
47
- */
48
- protected on: <T extends E$1>(EventClass: EventConstructor<T>, handler: EventHandler<T, S$1>) => void;
49
- /**
50
- * Add an event to be processed
51
- * Arrow function to maintain correct 'this' binding in React
52
- */
53
- add: (event: E$1) => void;
54
- /**
55
- * Process an event (synchronously)
56
- */
57
- private processEvent;
58
- /**
59
- * Handle a single event (synchronously)
60
- */
61
- private handleEvent;
62
- /**
63
- * Hook for handling event processing errors
64
- */
65
- protected onEventError(event: E$1, error: Error): void;
66
- }
67
- //#endregion
68
- //#region ../blac/src/core/StateContainerRegistry.d.ts
69
- /**
70
- * Lifecycle events that can be observed
71
- */
72
- type LifecycleEvent = 'created' | 'stateChanged' | 'eventAdded' | 'disposed';
73
- /**
74
- * Listener function types for each lifecycle event
75
- */
76
- type LifecycleListener<E$1 extends LifecycleEvent> = E$1 extends 'created' ? (container: StateContainer<any>) => void : E$1 extends 'stateChanged' ? (container: StateContainer<any>, previousState: any, currentState: any) => void : E$1 extends 'eventAdded' ? (container: Vertex<any, any>, event: any) => void : E$1 extends 'disposed' ? (container: StateContainer<any>) => void : never;
77
- /**
78
- * Registry for managing StateContainer instances
79
- */
80
- declare class StateContainerRegistry {
81
- private readonly instances;
82
- private readonly types;
83
- private readonly listeners;
84
- /**
85
- * Register a type with isolation mode
86
- */
87
- register<T extends StateContainer<any>>(constructor: new (...args: any[]) => T, isolated?: boolean): void;
88
- /**
89
- * Get or create a StateContainer instance
90
- * For isolated: always creates new
91
- * For shared: singleton per key
92
- */
93
- getOrCreate<T extends StateContainer<any>>(constructor: new (...args: any[]) => T, key?: string, ...args: any[]): T;
94
- /**
95
- * Release a reference to an instance
96
- * Disposes when ref count reaches zero (unless static keepAlive is true)
97
- */
98
- release<T extends StateContainer<any>>(constructor: new (...args: any[]) => T, key?: string, forceDispose?: boolean): void;
99
- /**
100
- * Get all instances of a type (shared only, isolated aren't tracked)
101
- */
102
- getAll<T extends StateContainer<any>>(constructor: new (...args: any[]) => T): T[];
103
- /**
104
- * Clear all instances of a type
105
- */
106
- clear<T extends StateContainer<any>>(constructor: new (...args: any[]) => T): void;
107
- /**
108
- * Clear all instances from all types (for testing)
109
- */
110
- clearAll(): void;
111
- /**
112
- * Get registry statistics (for debugging)
113
- */
114
- getStats(): {
115
- registeredTypes: number;
116
- totalInstances: number;
117
- typeBreakdown: Record<string, number>;
118
- };
119
- /**
120
- * Get reference count for an instance
121
- */
122
- getRefCount<T extends StateContainer<any>>(constructor: new (...args: any[]) => T, key?: string): number;
123
- /**
124
- * Check if an instance exists
125
- */
126
- hasInstance<T extends StateContainer<any>>(constructor: new (...args: any[]) => T, key?: string): boolean;
127
- /**
128
- * Subscribe to lifecycle events
129
- * @param event - The lifecycle event to listen for
130
- * @param listener - The listener function to call when the event occurs
131
- * @returns Unsubscribe function
132
- */
133
- on<E extends LifecycleEvent>(event: E, listener: LifecycleListener<E>): () => void;
134
- /**
135
- * Emit lifecycle event to all listeners
136
- * @internal - Called by StateContainer/Vertex lifecycle methods
137
- */
138
- emit(event: LifecycleEvent, ...args: any[]): void;
139
- }
140
- //#endregion
141
- //#region ../blac/src/core/StateContainer.d.ts
142
- /**
143
- * Configuration options for StateContainer
144
- */
145
- interface StateContainerConfig {
146
- /** Container name for debugging */
147
- name?: string;
148
- /** Enable debug logging */
149
- debug?: boolean;
150
- /** Custom instance identifier */
151
- instanceId?: string;
152
- }
153
- /**
154
- * Listener function for state changes
155
- */
156
- type StateListener<S$1> = (state: S$1) => void;
157
- /**
158
- * Base abstract class for all state containers
159
- */
160
- declare abstract class StateContainer<S$1> {
161
- protected static _registry: StateContainerRegistry;
162
- /**
163
- * Set a custom registry (mainly for testing)
164
- */
165
- static setRegistry(registry: StateContainerRegistry): void;
166
- /**
167
- * Register a type as isolated or shared
168
- */
169
- static register<T extends StateContainer<any>>(this: new (...args: any[]) => T, isolated?: boolean): void;
170
- /**
171
- * Get or create an instance
172
- * @param key - Optional instance key for shared instances
173
- * @param args - Constructor arguments
174
- */
175
- static getOrCreate<T extends StateContainer<any>>(this: new (...args: any[]) => T, key?: string, ...args: any[]): T;
176
- /**
177
- * Release a reference to an instance
178
- * @param key - Optional instance key
179
- * @param forceDispose - Force dispose regardless of ref count or keepAlive
180
- */
181
- static release<T extends StateContainer<any>>(this: new (...args: any[]) => T, key?: string, forceDispose?: boolean): void;
182
- /**
183
- * Get all instances of this type
184
- */
185
- static getAll<T extends StateContainer<any>>(this: new (...args: any[]) => T): T[];
186
- /**
187
- * Clear all instances of this type
188
- */
189
- static clear<T extends StateContainer<any>>(this: new (...args: any[]) => T): void;
190
- /**
191
- * Clear all instances from all types (for testing)
192
- */
193
- static clearAllInstances(): void;
194
- /**
195
- * Get registry statistics (for debugging)
196
- */
197
- static getStats(): {
198
- registeredTypes: number;
199
- totalInstances: number;
200
- typeBreakdown: Record<string, number>;
201
- };
202
- /**
203
- * Get reference count for an instance
204
- * @param key - Optional instance key
205
- */
206
- static getRefCount<T extends StateContainer<any>>(this: new (...args: any[]) => T, key?: string): number;
207
- /**
208
- * Check if an instance exists
209
- * @param key - Optional instance key
210
- */
211
- static hasInstance<T extends StateContainer<any>>(this: new (...args: any[]) => T, key?: string): boolean;
212
- private _state;
213
- private readonly listeners;
214
- private _disposed;
215
- private readonly config;
216
- readonly name: string;
217
- readonly debug: boolean;
218
- readonly instanceId: string;
219
- /**
220
- * Create a new StateContainer
221
- */
222
- constructor(initialState: S$1, config?: StateContainerConfig);
223
- /**
224
- * Get the current state
225
- */
226
- get state(): S$1;
227
- /**
228
- * Check if disposed
229
- */
230
- get isDisposed(): boolean;
231
- /**
232
- * Subscribe to state changes
233
- * @returns Unsubscribe function
234
- */
235
- subscribe(listener: StateListener<S$1>): () => void;
236
- /**
237
- * Dispose the container
238
- */
239
- dispose(): void;
240
- /**
241
- * Emit a new state (with change detection)
242
- */
243
- protected emit(newState: S$1): void;
244
- /**
245
- * Update state using a function
246
- */
247
- protected update(updater: (current: S$1) => S$1): void;
248
- /**
249
- * Optional disposal hook for subclasses
250
- */
251
- protected onDispose?(): void;
252
- /**
253
- * Optional state change hook for subclasses
254
- */
255
- protected onStateChange?(newState: S$1, previousState: S$1): void;
256
- }
257
- //#endregion
258
- //#region ../blac/src/types/branded.d.ts
259
- /**
260
- * Branded types for type-safe IDs
261
- * These types ensure that IDs can't be accidentally mixed up
262
- */
263
- /**
264
- * Helper type for cases where any object type is acceptable
265
- * Used sparingly only where type cannot be more specific
266
- * eslint-disable-next-line @typescript-eslint/no-explicit-any
267
- */
268
- type AnyObject = any;
269
- //#endregion
270
- //#region ../blac/src/types/utilities.d.ts
271
- /**
272
- * Extract state type from StateContainer
273
- *
274
- * @example
275
- * ```ts
276
- * class CounterBloc extends Cubit<number> { ... }
277
- * type State = ExtractState<CounterBloc>; // number
278
- * ```
279
- */
280
- type ExtractState<T$1> = T$1 extends StateContainer<infer S> ? S : never;
281
- /**
282
- * Bloc constructor type - represents a class constructor for StateContainer subclasses
283
- * Includes static methods from StateContainer for instance management
284
- *
285
- * Note: Uses `any` for state type to support variance - the actual Bloc instance
286
- * will have the correct specific types inferred at usage sites.
287
- *
288
- * @example
289
- * ```ts
290
- * function createBloc<TBloc extends StateContainer<any>>(
291
- * BlocClass: BlocConstructor<TBloc>
292
- * ): TBloc {
293
- * return BlocClass.getOrCreate();
294
- * }
295
- * ```
296
- */
297
- type BlocConstructor<TBloc extends StateContainer<any>> = (new (...args: any[]) => TBloc) & {
298
- getOrCreate?(instanceKey?: string, ...args: any[]): TBloc;
299
- release?(instanceKey?: string): void;
300
- isolated?: boolean;
301
- keepAlive?: boolean;
302
- };
303
- //#endregion
304
- //#region src/types.d.ts
305
- /**
306
- * Configuration options for the useBloc hook
307
- *
308
- * @template TBloc - The StateContainer type
309
- *
310
- * @example
311
- * ```tsx
312
- * const [state, bloc] = useBloc(CounterBloc, {
313
- * staticProps: { initialCount: 0 },
314
- * dependencies: (state) => [state.count],
315
- * onMount: (bloc) => console.log('Mounted'),
316
- * });
317
- * ```
318
- */
319
- interface UseBlocOptions<TBloc extends StateContainer<AnyObject>> {
320
- /**
321
- * Static props to pass to the Bloc constructor
322
- * Type should match the constructor's first parameter
323
- */
324
- staticProps?: AnyObject;
325
- /**
326
- * Custom instance ID for shared blocs
327
- * - For isolated blocs, each useBloc call gets its own instance
328
- * - For shared blocs, the same instanceId will share the same bloc instance
329
- */
330
- instanceId?: string;
331
- /**
332
- * Manual dependency tracking function
333
- * - When provided, automatic proxy tracking is disabled
334
- * - Component only re-renders when the returned values change (shallow equality)
335
- *
336
- * @param state - Current state of the bloc
337
- * @param bloc - The bloc instance
338
- * @returns Array of values to track
339
- */
340
- dependencies?: (state: ExtractState<TBloc>, bloc: TBloc) => unknown[];
341
- /**
342
- * Control automatic dependency tracking (default: true)
343
- * - true: Automatically track accessed properties via Proxy
344
- * - false: Disable tracking, all state changes trigger re-render
345
- * - Ignored when `dependencies` option is provided
346
- *
347
- * @default true
348
- */
349
- autoTrack?: boolean;
350
- /**
351
- * Callback invoked when the component mounts
352
- *
353
- * @param bloc - The bloc instance
354
- */
355
- onMount?: (bloc: TBloc) => void;
356
- /**
357
- * Callback invoked when the component unmounts
358
- *
359
- * @param bloc - The bloc instance
360
- */
361
- onUnmount?: (bloc: TBloc) => void;
362
- }
363
- /**
364
- * Return type of the useBloc hook
365
- *
366
- * @template TBloc - The StateContainer type
367
- *
368
- * @returns A tuple containing:
369
- * - [0]: The current state (may be a Proxy for tracking)
370
- * - [1]: The bloc instance with methods
371
- * - [2]: Component ref (internal use only)
372
- */
373
- type UseBlocReturn<TBloc extends StateContainer<AnyObject>> = [ExtractState<TBloc>, TBloc, RefObject<ComponentRef>];
374
- /**
375
- * @internal
376
- * Component reference object for internal tracking
377
- * Used to persist instance IDs across React Strict Mode remounts
378
- */
379
- type ComponentRef = {
380
- __blocInstanceId?: string;
381
- __bridge?: any;
382
- };
383
- //#endregion
384
- //#region src/useBloc.d.ts
385
- /**
386
- * Lifecycle: INITIAL MOUNT
387
- * 1. useMemo runs once - creates bloc, subscribeFn, getSnapshotFn
388
- * 2. useSyncExternalStore calls getSnapshotFn (1st time) - lazy creates tracker, starts tracking, returns proxy
389
- * 3. Component renders - proxy tracks property accesses
390
- * 4. useSyncExternalStore calls getSnapshotFn (2nd time) - captures tracked paths, starts new tracking, returns proxy
391
- * 5. useSyncExternalStore calls subscribeFn - sets up state change listener
392
- *
393
- * Lifecycle: STATE CHANGE
394
- * 1. Bloc state changes
395
- * 2. subscribeFn callback checks hasChanges() - only re-renders if tracked paths changed
396
- * 3. If re-render: getSnapshotFn captures previous paths, starts tracking, returns proxy
397
- *
398
- * Lifecycle: RE-RENDER (parent re-render)
399
- * 1. useMemo returns cached values (same bloc, subscribeFn, getSnapshotFn)
400
- * 2. useSyncExternalStore calls getSnapshotFn - captures paths, starts tracking, returns proxy
401
- */
402
- declare function useBloc<TBloc extends StateContainer<AnyObject>>(BlocClass: BlocConstructor<TBloc>, options?: UseBlocOptions<TBloc>): UseBlocReturn<TBloc>;
403
- //#endregion
404
- //#region src/useBlocActions.d.ts
405
- /**
406
- * Configuration options for the useBlocActions hook
407
- *
408
- * @template TBloc - The StateContainer type
409
- */
410
- interface UseBlocActionsOptions<TBloc extends StateContainer<AnyObject>> {
411
- /**
412
- * Static props to pass to the Bloc constructor
413
- * Type should match the constructor's first parameter
414
- */
415
- staticProps?: AnyObject;
416
- /**
417
- * Custom instance ID for shared blocs
418
- * - For isolated blocs, each useBlocActions call gets its own instance
419
- * - For shared blocs, the same instanceId will share the same bloc instance
420
- */
421
- instanceId?: string;
422
- /**
423
- * Callback invoked when the component mounts
424
- *
425
- * @param bloc - The bloc instance
426
- */
427
- onMount?: (bloc: TBloc) => void;
428
- /**
429
- * Callback invoked when the component unmounts
430
- *
431
- * @param bloc - The bloc instance
432
- */
433
- onUnmount?: (bloc: TBloc) => void;
434
- }
435
- /**
436
- * React hook for accessing bloc instance without state subscription.
437
- * Use this when you only need to call bloc methods/actions without reading state.
438
- *
439
- * Benefits over useBloc:
440
- * - No state subscription overhead
441
- * - No proxy tracking
442
- * - Component never re-renders due to bloc state changes
443
- * - Lighter weight for action-only components
2
+ * React Integration
444
3
  *
445
- * @template TBloc - The StateContainer type
446
- * @param BlocClass - The bloc class constructor
447
- * @param options - Optional configuration
448
- * @returns The bloc instance
4
+ * Clean integration between React and StateContainer architecture.
5
+ * Constructor-based API with automatic type inference.
6
+ * Supports concurrent (useSyncExternalStore) mode for optimal performance.
449
7
  */
450
- declare function useBlocActions<TBloc extends StateContainer<AnyObject>>(BlocClass: BlocConstructor<TBloc>, options?: UseBlocActionsOptions<TBloc>): TBloc;
451
- //#endregion
452
- export { type UseBlocActionsOptions, type UseBlocOptions, type UseBlocReturn, useBloc, useBlocActions };
8
+ export { useBloc } from './useBloc';
9
+ export { useBlocActions } from './useBlocActions';
10
+ export type { UseBlocOptions, UseBlocReturn } from './types';
11
+ export type { UseBlocActionsOptions } from './useBlocActions';
453
12
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7D,YAAY,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC"}