@blac/core 2.0.0-rc.4 → 2.0.0-rc.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.
- package/dist/index.cjs +31 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +145 -124
- package/dist/index.d.ts +145 -124
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,141 @@
|
|
|
1
|
-
//#region src/
|
|
1
|
+
//#region src/types/events.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Base event interface for type-safe event system
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Base event interface
|
|
7
|
+
* Used by Vertex for event-driven state management
|
|
8
|
+
*/
|
|
9
|
+
interface BaseEvent {
|
|
10
|
+
readonly type: string;
|
|
11
|
+
readonly timestamp: number;
|
|
12
|
+
readonly source?: string;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/core/Vertex.d.ts
|
|
2
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Event handler signature (synchronous only)
|
|
19
|
+
*/
|
|
20
|
+
type EventHandler<E$1 extends BaseEvent, S$1> = (event: E$1, emit: (state: S$1) => void) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Type-safe event constructor
|
|
23
|
+
*/
|
|
24
|
+
type EventConstructor<T$1 extends BaseEvent = BaseEvent> = new (...args: never[]) => T$1;
|
|
25
|
+
/**
|
|
26
|
+
* Vertex is an event-driven state container (Bloc pattern)
|
|
27
|
+
*
|
|
28
|
+
* Includes its own simple event system since events are only needed
|
|
29
|
+
* for the Vertex pattern, not for the base StateContainer.
|
|
30
|
+
*/
|
|
31
|
+
declare abstract class Vertex<S$1, E$1 extends BaseEvent = BaseEvent> extends StateContainer<S$1> {
|
|
32
|
+
private eventHandlers;
|
|
33
|
+
private isProcessing;
|
|
34
|
+
private eventQueue;
|
|
35
|
+
/**
|
|
36
|
+
* Create a new Vertex
|
|
37
|
+
* @param initialState Initial state value
|
|
38
|
+
* @param config Configuration options
|
|
39
|
+
*/
|
|
40
|
+
constructor(initialState: S$1, config?: StateContainerConfig);
|
|
41
|
+
/**
|
|
42
|
+
* Register an event handler
|
|
43
|
+
* Arrow function to maintain correct 'this' binding
|
|
44
|
+
*/
|
|
45
|
+
protected on: <T extends E$1>(EventClass: EventConstructor<T>, handler: EventHandler<T, S$1>) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Add an event to be processed
|
|
48
|
+
* Arrow function to maintain correct 'this' binding in React
|
|
49
|
+
*/
|
|
50
|
+
add: (event: E$1) => void;
|
|
51
|
+
/**
|
|
52
|
+
* Process an event (synchronously)
|
|
53
|
+
*/
|
|
54
|
+
private processEvent;
|
|
55
|
+
/**
|
|
56
|
+
* Handle a single event (synchronously)
|
|
57
|
+
*/
|
|
58
|
+
private handleEvent;
|
|
59
|
+
/**
|
|
60
|
+
* Hook for handling event processing errors
|
|
61
|
+
*/
|
|
62
|
+
protected onEventError(event: E$1, error: Error): void;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Example: Counter Events
|
|
66
|
+
*/
|
|
67
|
+
declare class IncrementEvent implements BaseEvent {
|
|
68
|
+
readonly amount: number;
|
|
69
|
+
readonly type = "increment";
|
|
70
|
+
readonly timestamp: number;
|
|
71
|
+
constructor(amount?: number);
|
|
72
|
+
}
|
|
73
|
+
declare class DecrementEvent implements BaseEvent {
|
|
74
|
+
readonly amount: number;
|
|
75
|
+
readonly type = "decrement";
|
|
76
|
+
readonly timestamp: number;
|
|
77
|
+
constructor(amount?: number);
|
|
78
|
+
}
|
|
79
|
+
declare class ResetEvent implements BaseEvent {
|
|
80
|
+
readonly type = "reset";
|
|
81
|
+
readonly timestamp: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Example: Counter Vertex
|
|
85
|
+
* Demonstrates proper typing and event handling
|
|
86
|
+
*/
|
|
87
|
+
declare class CounterVertex extends Vertex<number> {
|
|
88
|
+
constructor();
|
|
89
|
+
increment: (amount?: number) => void;
|
|
90
|
+
decrement: (amount?: number) => void;
|
|
91
|
+
reset: () => void;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Auth State and Events for testing async handlers
|
|
95
|
+
*/
|
|
96
|
+
interface AuthState {
|
|
97
|
+
isAuthenticated: boolean;
|
|
98
|
+
isLoading: boolean;
|
|
99
|
+
user: {
|
|
100
|
+
id: string;
|
|
101
|
+
name: string;
|
|
102
|
+
email: string;
|
|
103
|
+
} | null;
|
|
104
|
+
error: string | null;
|
|
105
|
+
}
|
|
106
|
+
declare class LoginEvent implements BaseEvent {
|
|
107
|
+
readonly email: string;
|
|
108
|
+
readonly password: string;
|
|
109
|
+
readonly type = "login";
|
|
110
|
+
readonly timestamp: number;
|
|
111
|
+
constructor(email: string, password: string);
|
|
112
|
+
}
|
|
113
|
+
declare class LogoutEvent implements BaseEvent {
|
|
114
|
+
readonly type = "logout";
|
|
115
|
+
readonly timestamp: number;
|
|
116
|
+
}
|
|
117
|
+
declare class AuthVertex extends Vertex<AuthState> {
|
|
118
|
+
constructor();
|
|
119
|
+
login: (email: string, password: string) => void;
|
|
120
|
+
logout: () => void;
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/core/StateContainerRegistry.d.ts
|
|
124
|
+
/**
|
|
125
|
+
* Lifecycle events that can be observed
|
|
126
|
+
*/
|
|
127
|
+
type LifecycleEvent = 'created' | 'stateChanged' | 'eventAdded' | 'disposed';
|
|
128
|
+
/**
|
|
129
|
+
* Listener function types for each lifecycle event
|
|
130
|
+
*/
|
|
131
|
+
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;
|
|
3
132
|
/**
|
|
4
133
|
* Registry for managing StateContainer instances
|
|
5
134
|
*/
|
|
6
135
|
declare class StateContainerRegistry {
|
|
7
136
|
private readonly instances;
|
|
8
137
|
private readonly types;
|
|
138
|
+
private readonly listeners;
|
|
9
139
|
/**
|
|
10
140
|
* Register a type with isolation mode
|
|
11
141
|
*/
|
|
@@ -49,6 +179,18 @@ declare class StateContainerRegistry {
|
|
|
49
179
|
* Check if an instance exists
|
|
50
180
|
*/
|
|
51
181
|
hasInstance<T extends StateContainer<any>>(constructor: new (...args: any[]) => T, key?: string): boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Subscribe to lifecycle events
|
|
184
|
+
* @param event - The lifecycle event to listen for
|
|
185
|
+
* @param listener - The listener function to call when the event occurs
|
|
186
|
+
* @returns Unsubscribe function
|
|
187
|
+
*/
|
|
188
|
+
on<E extends LifecycleEvent>(event: E, listener: LifecycleListener<E>): () => void;
|
|
189
|
+
/**
|
|
190
|
+
* Emit lifecycle event to all listeners
|
|
191
|
+
* @internal - Called by StateContainer/Vertex lifecycle methods
|
|
192
|
+
*/
|
|
193
|
+
emit(event: LifecycleEvent, ...args: any[]): void;
|
|
52
194
|
}
|
|
53
195
|
/**
|
|
54
196
|
* Global default registry instance
|
|
@@ -75,7 +217,7 @@ type StateListener<S$1> = (state: S$1) => void;
|
|
|
75
217
|
* Base abstract class for all state containers
|
|
76
218
|
*/
|
|
77
219
|
declare abstract class StateContainer<S$1> {
|
|
78
|
-
|
|
220
|
+
protected static _registry: StateContainerRegistry;
|
|
79
221
|
/**
|
|
80
222
|
* Set a custom registry (mainly for testing)
|
|
81
223
|
*/
|
|
@@ -249,127 +391,6 @@ declare class TodoCubit extends Cubit<TodoState> {
|
|
|
249
391
|
get activeTodoCount(): number;
|
|
250
392
|
}
|
|
251
393
|
//#endregion
|
|
252
|
-
//#region src/types/events.d.ts
|
|
253
|
-
/**
|
|
254
|
-
* Base event interface for type-safe event system
|
|
255
|
-
*/
|
|
256
|
-
/**
|
|
257
|
-
* Base event interface
|
|
258
|
-
* Used by Vertex for event-driven state management
|
|
259
|
-
*/
|
|
260
|
-
interface BaseEvent {
|
|
261
|
-
readonly type: string;
|
|
262
|
-
readonly timestamp: number;
|
|
263
|
-
readonly source?: string;
|
|
264
|
-
}
|
|
265
|
-
//#endregion
|
|
266
|
-
//#region src/core/Vertex.d.ts
|
|
267
|
-
/**
|
|
268
|
-
* Event handler signature (synchronous only)
|
|
269
|
-
*/
|
|
270
|
-
type EventHandler<E extends BaseEvent, S$1> = (event: E, emit: (state: S$1) => void) => void;
|
|
271
|
-
/**
|
|
272
|
-
* Type-safe event constructor
|
|
273
|
-
*/
|
|
274
|
-
type EventConstructor<T$1 extends BaseEvent = BaseEvent> = new (...args: never[]) => T$1;
|
|
275
|
-
/**
|
|
276
|
-
* Vertex is an event-driven state container (Bloc pattern)
|
|
277
|
-
*
|
|
278
|
-
* Includes its own simple event system since events are only needed
|
|
279
|
-
* for the Vertex pattern, not for the base StateContainer.
|
|
280
|
-
*/
|
|
281
|
-
declare abstract class Vertex<S$1, E extends BaseEvent = BaseEvent> extends StateContainer<S$1> {
|
|
282
|
-
private eventHandlers;
|
|
283
|
-
private isProcessing;
|
|
284
|
-
private eventQueue;
|
|
285
|
-
/**
|
|
286
|
-
* Create a new Vertex
|
|
287
|
-
* @param initialState Initial state value
|
|
288
|
-
* @param config Configuration options
|
|
289
|
-
*/
|
|
290
|
-
constructor(initialState: S$1, config?: StateContainerConfig);
|
|
291
|
-
/**
|
|
292
|
-
* Register an event handler
|
|
293
|
-
* Arrow function to maintain correct 'this' binding
|
|
294
|
-
*/
|
|
295
|
-
protected on: <T extends E>(EventClass: EventConstructor<T>, handler: EventHandler<T, S$1>) => void;
|
|
296
|
-
/**
|
|
297
|
-
* Add an event to be processed
|
|
298
|
-
* Arrow function to maintain correct 'this' binding in React
|
|
299
|
-
*/
|
|
300
|
-
add: (event: E) => void;
|
|
301
|
-
/**
|
|
302
|
-
* Process an event (synchronously)
|
|
303
|
-
*/
|
|
304
|
-
private processEvent;
|
|
305
|
-
/**
|
|
306
|
-
* Handle a single event (synchronously)
|
|
307
|
-
*/
|
|
308
|
-
private handleEvent;
|
|
309
|
-
/**
|
|
310
|
-
* Hook for handling event processing errors
|
|
311
|
-
*/
|
|
312
|
-
protected onEventError(event: E, error: Error): void;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Example: Counter Events
|
|
316
|
-
*/
|
|
317
|
-
declare class IncrementEvent implements BaseEvent {
|
|
318
|
-
readonly amount: number;
|
|
319
|
-
readonly type = "increment";
|
|
320
|
-
readonly timestamp: number;
|
|
321
|
-
constructor(amount?: number);
|
|
322
|
-
}
|
|
323
|
-
declare class DecrementEvent implements BaseEvent {
|
|
324
|
-
readonly amount: number;
|
|
325
|
-
readonly type = "decrement";
|
|
326
|
-
readonly timestamp: number;
|
|
327
|
-
constructor(amount?: number);
|
|
328
|
-
}
|
|
329
|
-
declare class ResetEvent implements BaseEvent {
|
|
330
|
-
readonly type = "reset";
|
|
331
|
-
readonly timestamp: number;
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* Example: Counter Vertex
|
|
335
|
-
* Demonstrates proper typing and event handling
|
|
336
|
-
*/
|
|
337
|
-
declare class CounterVertex extends Vertex<number> {
|
|
338
|
-
constructor();
|
|
339
|
-
increment: (amount?: number) => void;
|
|
340
|
-
decrement: (amount?: number) => void;
|
|
341
|
-
reset: () => void;
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Auth State and Events for testing async handlers
|
|
345
|
-
*/
|
|
346
|
-
interface AuthState {
|
|
347
|
-
isAuthenticated: boolean;
|
|
348
|
-
isLoading: boolean;
|
|
349
|
-
user: {
|
|
350
|
-
id: string;
|
|
351
|
-
name: string;
|
|
352
|
-
email: string;
|
|
353
|
-
} | null;
|
|
354
|
-
error: string | null;
|
|
355
|
-
}
|
|
356
|
-
declare class LoginEvent implements BaseEvent {
|
|
357
|
-
readonly email: string;
|
|
358
|
-
readonly password: string;
|
|
359
|
-
readonly type = "login";
|
|
360
|
-
readonly timestamp: number;
|
|
361
|
-
constructor(email: string, password: string);
|
|
362
|
-
}
|
|
363
|
-
declare class LogoutEvent implements BaseEvent {
|
|
364
|
-
readonly type = "logout";
|
|
365
|
-
readonly timestamp: number;
|
|
366
|
-
}
|
|
367
|
-
declare class AuthVertex extends Vertex<AuthState> {
|
|
368
|
-
constructor();
|
|
369
|
-
login: (email: string, password: string) => void;
|
|
370
|
-
logout: () => void;
|
|
371
|
-
}
|
|
372
|
-
//#endregion
|
|
373
394
|
//#region src/types/branded.d.ts
|
|
374
395
|
/**
|
|
375
396
|
* Branded types for type-safe IDs
|
|
@@ -860,5 +881,5 @@ declare function generateSimpleId(prefix: string): string;
|
|
|
860
881
|
*/
|
|
861
882
|
declare function __resetIdCounters(): void;
|
|
862
883
|
//#endregion
|
|
863
|
-
export { type AnyObject, AuthState, AuthVertex, BLAC_DEFAULTS, type BaseEvent, BlocConstructor, type Brand, type BrandedId, CounterCubit, CounterVertex, Cubit, DecrementEvent, EventConstructor, EventHandler, ExtractConstructorArgs, ExtractState, IncrementEvent, type InstanceId, type LogConfig, type LogEntry, LogLevel, LoginEvent, LogoutEvent, PathInfo, ProxyTracker, ProxyTrackerState, ResetEvent, StateContainer, StateContainerConfig, StateContainerRegistry, TodoCubit, TodoState, TrackerState, Vertex, __resetIdCounters, captureTrackedPaths, configureLogger, createArrayProxy, createIdGenerator, createLogger, createProxy, createProxyForTarget, createProxyInternal, createProxyTrackerState, createTrackerState, debug, error, generateId, generateSimpleId, getValueAtPath, globalProxyTracker, globalRegistry, hasChanges, hasTrackedData, info, isProxyable, parsePath, shallowEqual, startProxyTracking, startTracking, stopProxyTracking, trackAccess, warn };
|
|
884
|
+
export { type AnyObject, AuthState, AuthVertex, BLAC_DEFAULTS, type BaseEvent, BlocConstructor, type Brand, type BrandedId, CounterCubit, CounterVertex, Cubit, DecrementEvent, EventConstructor, EventHandler, ExtractConstructorArgs, ExtractState, IncrementEvent, type InstanceId, type LifecycleEvent, type LifecycleListener, type LogConfig, type LogEntry, LogLevel, LoginEvent, LogoutEvent, PathInfo, ProxyTracker, ProxyTrackerState, ResetEvent, StateContainer, StateContainerConfig, StateContainerRegistry, TodoCubit, TodoState, TrackerState, Vertex, __resetIdCounters, captureTrackedPaths, configureLogger, createArrayProxy, createIdGenerator, createLogger, createProxy, createProxyForTarget, createProxyInternal, createProxyTrackerState, createTrackerState, debug, error, generateId, generateSimpleId, getValueAtPath, globalProxyTracker, globalRegistry, hasChanges, hasTrackedData, info, isProxyable, parsePath, shallowEqual, startProxyTracking, startTracking, stopProxyTracking, trackAccess, warn };
|
|
864
885
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,141 @@
|
|
|
1
|
-
//#region src/
|
|
1
|
+
//#region src/types/events.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Base event interface for type-safe event system
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Base event interface
|
|
7
|
+
* Used by Vertex for event-driven state management
|
|
8
|
+
*/
|
|
9
|
+
interface BaseEvent {
|
|
10
|
+
readonly type: string;
|
|
11
|
+
readonly timestamp: number;
|
|
12
|
+
readonly source?: string;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/core/Vertex.d.ts
|
|
2
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Event handler signature (synchronous only)
|
|
19
|
+
*/
|
|
20
|
+
type EventHandler<E$1 extends BaseEvent, S$1> = (event: E$1, emit: (state: S$1) => void) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Type-safe event constructor
|
|
23
|
+
*/
|
|
24
|
+
type EventConstructor<T$1 extends BaseEvent = BaseEvent> = new (...args: never[]) => T$1;
|
|
25
|
+
/**
|
|
26
|
+
* Vertex is an event-driven state container (Bloc pattern)
|
|
27
|
+
*
|
|
28
|
+
* Includes its own simple event system since events are only needed
|
|
29
|
+
* for the Vertex pattern, not for the base StateContainer.
|
|
30
|
+
*/
|
|
31
|
+
declare abstract class Vertex<S$1, E$1 extends BaseEvent = BaseEvent> extends StateContainer<S$1> {
|
|
32
|
+
private eventHandlers;
|
|
33
|
+
private isProcessing;
|
|
34
|
+
private eventQueue;
|
|
35
|
+
/**
|
|
36
|
+
* Create a new Vertex
|
|
37
|
+
* @param initialState Initial state value
|
|
38
|
+
* @param config Configuration options
|
|
39
|
+
*/
|
|
40
|
+
constructor(initialState: S$1, config?: StateContainerConfig);
|
|
41
|
+
/**
|
|
42
|
+
* Register an event handler
|
|
43
|
+
* Arrow function to maintain correct 'this' binding
|
|
44
|
+
*/
|
|
45
|
+
protected on: <T extends E$1>(EventClass: EventConstructor<T>, handler: EventHandler<T, S$1>) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Add an event to be processed
|
|
48
|
+
* Arrow function to maintain correct 'this' binding in React
|
|
49
|
+
*/
|
|
50
|
+
add: (event: E$1) => void;
|
|
51
|
+
/**
|
|
52
|
+
* Process an event (synchronously)
|
|
53
|
+
*/
|
|
54
|
+
private processEvent;
|
|
55
|
+
/**
|
|
56
|
+
* Handle a single event (synchronously)
|
|
57
|
+
*/
|
|
58
|
+
private handleEvent;
|
|
59
|
+
/**
|
|
60
|
+
* Hook for handling event processing errors
|
|
61
|
+
*/
|
|
62
|
+
protected onEventError(event: E$1, error: Error): void;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Example: Counter Events
|
|
66
|
+
*/
|
|
67
|
+
declare class IncrementEvent implements BaseEvent {
|
|
68
|
+
readonly amount: number;
|
|
69
|
+
readonly type = "increment";
|
|
70
|
+
readonly timestamp: number;
|
|
71
|
+
constructor(amount?: number);
|
|
72
|
+
}
|
|
73
|
+
declare class DecrementEvent implements BaseEvent {
|
|
74
|
+
readonly amount: number;
|
|
75
|
+
readonly type = "decrement";
|
|
76
|
+
readonly timestamp: number;
|
|
77
|
+
constructor(amount?: number);
|
|
78
|
+
}
|
|
79
|
+
declare class ResetEvent implements BaseEvent {
|
|
80
|
+
readonly type = "reset";
|
|
81
|
+
readonly timestamp: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Example: Counter Vertex
|
|
85
|
+
* Demonstrates proper typing and event handling
|
|
86
|
+
*/
|
|
87
|
+
declare class CounterVertex extends Vertex<number> {
|
|
88
|
+
constructor();
|
|
89
|
+
increment: (amount?: number) => void;
|
|
90
|
+
decrement: (amount?: number) => void;
|
|
91
|
+
reset: () => void;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Auth State and Events for testing async handlers
|
|
95
|
+
*/
|
|
96
|
+
interface AuthState {
|
|
97
|
+
isAuthenticated: boolean;
|
|
98
|
+
isLoading: boolean;
|
|
99
|
+
user: {
|
|
100
|
+
id: string;
|
|
101
|
+
name: string;
|
|
102
|
+
email: string;
|
|
103
|
+
} | null;
|
|
104
|
+
error: string | null;
|
|
105
|
+
}
|
|
106
|
+
declare class LoginEvent implements BaseEvent {
|
|
107
|
+
readonly email: string;
|
|
108
|
+
readonly password: string;
|
|
109
|
+
readonly type = "login";
|
|
110
|
+
readonly timestamp: number;
|
|
111
|
+
constructor(email: string, password: string);
|
|
112
|
+
}
|
|
113
|
+
declare class LogoutEvent implements BaseEvent {
|
|
114
|
+
readonly type = "logout";
|
|
115
|
+
readonly timestamp: number;
|
|
116
|
+
}
|
|
117
|
+
declare class AuthVertex extends Vertex<AuthState> {
|
|
118
|
+
constructor();
|
|
119
|
+
login: (email: string, password: string) => void;
|
|
120
|
+
logout: () => void;
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/core/StateContainerRegistry.d.ts
|
|
124
|
+
/**
|
|
125
|
+
* Lifecycle events that can be observed
|
|
126
|
+
*/
|
|
127
|
+
type LifecycleEvent = 'created' | 'stateChanged' | 'eventAdded' | 'disposed';
|
|
128
|
+
/**
|
|
129
|
+
* Listener function types for each lifecycle event
|
|
130
|
+
*/
|
|
131
|
+
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;
|
|
3
132
|
/**
|
|
4
133
|
* Registry for managing StateContainer instances
|
|
5
134
|
*/
|
|
6
135
|
declare class StateContainerRegistry {
|
|
7
136
|
private readonly instances;
|
|
8
137
|
private readonly types;
|
|
138
|
+
private readonly listeners;
|
|
9
139
|
/**
|
|
10
140
|
* Register a type with isolation mode
|
|
11
141
|
*/
|
|
@@ -49,6 +179,18 @@ declare class StateContainerRegistry {
|
|
|
49
179
|
* Check if an instance exists
|
|
50
180
|
*/
|
|
51
181
|
hasInstance<T extends StateContainer<any>>(constructor: new (...args: any[]) => T, key?: string): boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Subscribe to lifecycle events
|
|
184
|
+
* @param event - The lifecycle event to listen for
|
|
185
|
+
* @param listener - The listener function to call when the event occurs
|
|
186
|
+
* @returns Unsubscribe function
|
|
187
|
+
*/
|
|
188
|
+
on<E extends LifecycleEvent>(event: E, listener: LifecycleListener<E>): () => void;
|
|
189
|
+
/**
|
|
190
|
+
* Emit lifecycle event to all listeners
|
|
191
|
+
* @internal - Called by StateContainer/Vertex lifecycle methods
|
|
192
|
+
*/
|
|
193
|
+
emit(event: LifecycleEvent, ...args: any[]): void;
|
|
52
194
|
}
|
|
53
195
|
/**
|
|
54
196
|
* Global default registry instance
|
|
@@ -75,7 +217,7 @@ type StateListener<S$1> = (state: S$1) => void;
|
|
|
75
217
|
* Base abstract class for all state containers
|
|
76
218
|
*/
|
|
77
219
|
declare abstract class StateContainer<S$1> {
|
|
78
|
-
|
|
220
|
+
protected static _registry: StateContainerRegistry;
|
|
79
221
|
/**
|
|
80
222
|
* Set a custom registry (mainly for testing)
|
|
81
223
|
*/
|
|
@@ -249,127 +391,6 @@ declare class TodoCubit extends Cubit<TodoState> {
|
|
|
249
391
|
get activeTodoCount(): number;
|
|
250
392
|
}
|
|
251
393
|
//#endregion
|
|
252
|
-
//#region src/types/events.d.ts
|
|
253
|
-
/**
|
|
254
|
-
* Base event interface for type-safe event system
|
|
255
|
-
*/
|
|
256
|
-
/**
|
|
257
|
-
* Base event interface
|
|
258
|
-
* Used by Vertex for event-driven state management
|
|
259
|
-
*/
|
|
260
|
-
interface BaseEvent {
|
|
261
|
-
readonly type: string;
|
|
262
|
-
readonly timestamp: number;
|
|
263
|
-
readonly source?: string;
|
|
264
|
-
}
|
|
265
|
-
//#endregion
|
|
266
|
-
//#region src/core/Vertex.d.ts
|
|
267
|
-
/**
|
|
268
|
-
* Event handler signature (synchronous only)
|
|
269
|
-
*/
|
|
270
|
-
type EventHandler<E extends BaseEvent, S$1> = (event: E, emit: (state: S$1) => void) => void;
|
|
271
|
-
/**
|
|
272
|
-
* Type-safe event constructor
|
|
273
|
-
*/
|
|
274
|
-
type EventConstructor<T$1 extends BaseEvent = BaseEvent> = new (...args: never[]) => T$1;
|
|
275
|
-
/**
|
|
276
|
-
* Vertex is an event-driven state container (Bloc pattern)
|
|
277
|
-
*
|
|
278
|
-
* Includes its own simple event system since events are only needed
|
|
279
|
-
* for the Vertex pattern, not for the base StateContainer.
|
|
280
|
-
*/
|
|
281
|
-
declare abstract class Vertex<S$1, E extends BaseEvent = BaseEvent> extends StateContainer<S$1> {
|
|
282
|
-
private eventHandlers;
|
|
283
|
-
private isProcessing;
|
|
284
|
-
private eventQueue;
|
|
285
|
-
/**
|
|
286
|
-
* Create a new Vertex
|
|
287
|
-
* @param initialState Initial state value
|
|
288
|
-
* @param config Configuration options
|
|
289
|
-
*/
|
|
290
|
-
constructor(initialState: S$1, config?: StateContainerConfig);
|
|
291
|
-
/**
|
|
292
|
-
* Register an event handler
|
|
293
|
-
* Arrow function to maintain correct 'this' binding
|
|
294
|
-
*/
|
|
295
|
-
protected on: <T extends E>(EventClass: EventConstructor<T>, handler: EventHandler<T, S$1>) => void;
|
|
296
|
-
/**
|
|
297
|
-
* Add an event to be processed
|
|
298
|
-
* Arrow function to maintain correct 'this' binding in React
|
|
299
|
-
*/
|
|
300
|
-
add: (event: E) => void;
|
|
301
|
-
/**
|
|
302
|
-
* Process an event (synchronously)
|
|
303
|
-
*/
|
|
304
|
-
private processEvent;
|
|
305
|
-
/**
|
|
306
|
-
* Handle a single event (synchronously)
|
|
307
|
-
*/
|
|
308
|
-
private handleEvent;
|
|
309
|
-
/**
|
|
310
|
-
* Hook for handling event processing errors
|
|
311
|
-
*/
|
|
312
|
-
protected onEventError(event: E, error: Error): void;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Example: Counter Events
|
|
316
|
-
*/
|
|
317
|
-
declare class IncrementEvent implements BaseEvent {
|
|
318
|
-
readonly amount: number;
|
|
319
|
-
readonly type = "increment";
|
|
320
|
-
readonly timestamp: number;
|
|
321
|
-
constructor(amount?: number);
|
|
322
|
-
}
|
|
323
|
-
declare class DecrementEvent implements BaseEvent {
|
|
324
|
-
readonly amount: number;
|
|
325
|
-
readonly type = "decrement";
|
|
326
|
-
readonly timestamp: number;
|
|
327
|
-
constructor(amount?: number);
|
|
328
|
-
}
|
|
329
|
-
declare class ResetEvent implements BaseEvent {
|
|
330
|
-
readonly type = "reset";
|
|
331
|
-
readonly timestamp: number;
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* Example: Counter Vertex
|
|
335
|
-
* Demonstrates proper typing and event handling
|
|
336
|
-
*/
|
|
337
|
-
declare class CounterVertex extends Vertex<number> {
|
|
338
|
-
constructor();
|
|
339
|
-
increment: (amount?: number) => void;
|
|
340
|
-
decrement: (amount?: number) => void;
|
|
341
|
-
reset: () => void;
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Auth State and Events for testing async handlers
|
|
345
|
-
*/
|
|
346
|
-
interface AuthState {
|
|
347
|
-
isAuthenticated: boolean;
|
|
348
|
-
isLoading: boolean;
|
|
349
|
-
user: {
|
|
350
|
-
id: string;
|
|
351
|
-
name: string;
|
|
352
|
-
email: string;
|
|
353
|
-
} | null;
|
|
354
|
-
error: string | null;
|
|
355
|
-
}
|
|
356
|
-
declare class LoginEvent implements BaseEvent {
|
|
357
|
-
readonly email: string;
|
|
358
|
-
readonly password: string;
|
|
359
|
-
readonly type = "login";
|
|
360
|
-
readonly timestamp: number;
|
|
361
|
-
constructor(email: string, password: string);
|
|
362
|
-
}
|
|
363
|
-
declare class LogoutEvent implements BaseEvent {
|
|
364
|
-
readonly type = "logout";
|
|
365
|
-
readonly timestamp: number;
|
|
366
|
-
}
|
|
367
|
-
declare class AuthVertex extends Vertex<AuthState> {
|
|
368
|
-
constructor();
|
|
369
|
-
login: (email: string, password: string) => void;
|
|
370
|
-
logout: () => void;
|
|
371
|
-
}
|
|
372
|
-
//#endregion
|
|
373
394
|
//#region src/types/branded.d.ts
|
|
374
395
|
/**
|
|
375
396
|
* Branded types for type-safe IDs
|
|
@@ -860,5 +881,5 @@ declare function generateSimpleId(prefix: string): string;
|
|
|
860
881
|
*/
|
|
861
882
|
declare function __resetIdCounters(): void;
|
|
862
883
|
//#endregion
|
|
863
|
-
export { type AnyObject, AuthState, AuthVertex, BLAC_DEFAULTS, type BaseEvent, BlocConstructor, type Brand, type BrandedId, CounterCubit, CounterVertex, Cubit, DecrementEvent, EventConstructor, EventHandler, ExtractConstructorArgs, ExtractState, IncrementEvent, type InstanceId, type LogConfig, type LogEntry, LogLevel, LoginEvent, LogoutEvent, PathInfo, ProxyTracker, ProxyTrackerState, ResetEvent, StateContainer, StateContainerConfig, StateContainerRegistry, TodoCubit, TodoState, TrackerState, Vertex, __resetIdCounters, captureTrackedPaths, configureLogger, createArrayProxy, createIdGenerator, createLogger, createProxy, createProxyForTarget, createProxyInternal, createProxyTrackerState, createTrackerState, debug, error, generateId, generateSimpleId, getValueAtPath, globalProxyTracker, globalRegistry, hasChanges, hasTrackedData, info, isProxyable, parsePath, shallowEqual, startProxyTracking, startTracking, stopProxyTracking, trackAccess, warn };
|
|
884
|
+
export { type AnyObject, AuthState, AuthVertex, BLAC_DEFAULTS, type BaseEvent, BlocConstructor, type Brand, type BrandedId, CounterCubit, CounterVertex, Cubit, DecrementEvent, EventConstructor, EventHandler, ExtractConstructorArgs, ExtractState, IncrementEvent, type InstanceId, type LifecycleEvent, type LifecycleListener, type LogConfig, type LogEntry, LogLevel, LoginEvent, LogoutEvent, PathInfo, ProxyTracker, ProxyTrackerState, ResetEvent, StateContainer, StateContainerConfig, StateContainerRegistry, TodoCubit, TodoState, TrackerState, Vertex, __resetIdCounters, captureTrackedPaths, configureLogger, createArrayProxy, createIdGenerator, createLogger, createProxy, createProxyForTarget, createProxyInternal, createProxyTrackerState, createTrackerState, debug, error, generateId, generateSimpleId, getValueAtPath, globalProxyTracker, globalRegistry, hasChanges, hasTrackedData, info, isProxyable, parsePath, shallowEqual, startProxyTracking, startTracking, stopProxyTracking, trackAccess, warn };
|
|
864
885
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -81,6 +81,7 @@ function __resetIdCounters() {
|
|
|
81
81
|
var StateContainerRegistry = class {
|
|
82
82
|
instances = /* @__PURE__ */ new Map();
|
|
83
83
|
types = /* @__PURE__ */ new Map();
|
|
84
|
+
listeners = /* @__PURE__ */ new Map();
|
|
84
85
|
/**
|
|
85
86
|
* Register a type with isolation mode
|
|
86
87
|
*/
|
|
@@ -195,6 +196,32 @@ var StateContainerRegistry = class {
|
|
|
195
196
|
const instanceKey = `${constructor.name}:${key || "default"}`;
|
|
196
197
|
return this.instances.has(instanceKey);
|
|
197
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* Subscribe to lifecycle events
|
|
201
|
+
* @param event - The lifecycle event to listen for
|
|
202
|
+
* @param listener - The listener function to call when the event occurs
|
|
203
|
+
* @returns Unsubscribe function
|
|
204
|
+
*/
|
|
205
|
+
on(event, listener) {
|
|
206
|
+
if (!this.listeners.has(event)) this.listeners.set(event, /* @__PURE__ */ new Set());
|
|
207
|
+
this.listeners.get(event).add(listener);
|
|
208
|
+
return () => {
|
|
209
|
+
this.listeners.get(event)?.delete(listener);
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Emit lifecycle event to all listeners
|
|
214
|
+
* @internal - Called by StateContainer/Vertex lifecycle methods
|
|
215
|
+
*/
|
|
216
|
+
emit(event, ...args) {
|
|
217
|
+
const listeners = this.listeners.get(event);
|
|
218
|
+
if (!listeners || listeners.size === 0) return;
|
|
219
|
+
for (const listener of listeners) try {
|
|
220
|
+
listener(...args);
|
|
221
|
+
} catch (error$1) {
|
|
222
|
+
console.error(`[BlaC] Listener error for '${event}':`, error$1);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
198
225
|
};
|
|
199
226
|
/**
|
|
200
227
|
* Global default registry instance
|
|
@@ -290,6 +317,7 @@ var StateContainer = class StateContainer {
|
|
|
290
317
|
this.name = config.name || this.constructor.name;
|
|
291
318
|
this.debug = config.debug ?? false;
|
|
292
319
|
this.instanceId = config.instanceId || generateSimpleId(this.constructor.name);
|
|
320
|
+
StateContainer._registry.emit("created", this);
|
|
293
321
|
}
|
|
294
322
|
/**
|
|
295
323
|
* Get the current state
|
|
@@ -321,6 +349,7 @@ var StateContainer = class StateContainer {
|
|
|
321
349
|
this._disposed = true;
|
|
322
350
|
this.onDispose?.();
|
|
323
351
|
this.listeners.clear();
|
|
352
|
+
StateContainer._registry.emit("disposed", this);
|
|
324
353
|
if (this.debug) console.log(`[${this.name}] Disposed successfully`);
|
|
325
354
|
}
|
|
326
355
|
/**
|
|
@@ -337,6 +366,7 @@ var StateContainer = class StateContainer {
|
|
|
337
366
|
} catch (error$1) {
|
|
338
367
|
console.error(`[${this.name}] Error in listener:`, error$1);
|
|
339
368
|
}
|
|
369
|
+
StateContainer._registry.emit("stateChanged", this, previousState, newState);
|
|
340
370
|
}
|
|
341
371
|
/**
|
|
342
372
|
* Update state using a function
|
|
@@ -511,6 +541,7 @@ var Vertex = class extends StateContainer {
|
|
|
511
541
|
* Arrow function to maintain correct 'this' binding in React
|
|
512
542
|
*/
|
|
513
543
|
add = (event) => {
|
|
544
|
+
StateContainer._registry.emit("eventAdded", this, event);
|
|
514
545
|
this.processEvent(event);
|
|
515
546
|
};
|
|
516
547
|
/**
|