@blac/core 2.0.0-rc-8 → 2.0.0-rc-11

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/src/Bloc.ts CHANGED
@@ -2,177 +2,137 @@ import { Blac } from './Blac';
2
2
  import { BlocBase } from './BlocBase';
3
3
  import { BlocEventConstraint } from './types';
4
4
 
5
- // A should be the base type for all events this Bloc handles and must extend BlocEventConstraint
6
- // to access action.constructor and ensure proper event structure.
7
- // P is for props, changed from any to unknown.
8
5
  export abstract class Bloc<
9
- S, // State type
10
- A extends BlocEventConstraint = BlocEventConstraint, // Base Action/Event type with proper constraints
11
- P = unknown // Props type
6
+ S, // State type
7
+ A extends BlocEventConstraint = BlocEventConstraint, // Base Action/Event type with proper constraints
8
+ P = unknown, // Props type
12
9
  > extends BlocBase<S, P> {
13
- // Stores handlers: Map<EventConstructor (subtype of A), HandlerFunction>
14
- // The handler's event parameter will be correctly typed to the specific EventConstructor
15
- // by the 'on' method's signature.
16
- readonly eventHandlers: Map<
17
- // Key: Constructor of a specific event E (where E extends A)
18
- // TODO: 'any[]' is required for constructor arguments to allow flexible event instantiation.
19
- // Using specific parameter types would break type inference for events with different
20
- // constructor signatures. The 'any[]' enables polymorphic event handling while
21
- // maintaining type safety through the generic constraint 'E extends A'.
22
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
- new (...args: any[]) => A,
24
- // Value: Handler function. 'event: A' is used here for the stored function type.
25
- // The 'on' method ensures the specific handler (event: E) is correctly typed.
26
- (event: A, emit: (newState: S) => void) => void | Promise<void>
27
- > = new Map();
10
+ readonly eventHandlers: Map<
11
+ new (...args: any[]) => A,
12
+ (event: A, emit: (newState: S) => void) => void | Promise<void>
13
+ > = new Map();
28
14
 
29
- /**
30
- * @internal
31
- * Event queue to ensure sequential processing of async events
32
- */
33
- private _eventQueue: A[] = [];
34
-
35
- /**
36
- * @internal
37
- * Flag indicating if an event is currently being processed
38
- */
39
- private _isProcessingEvent = false;
15
+ /**
16
+ * @internal
17
+ * Event queue to ensure sequential processing of async events
18
+ */
19
+ private _eventQueue: A[] = [];
40
20
 
41
- /**
42
- * Registers an event handler for a specific event type.
43
- * This method is typically called in the constructor of a derived Bloc class.
44
- * @param eventConstructor The constructor of the event to handle (e.g., LoadDataEvent).
45
- * @param handler A function that processes the event and can emit new states.
46
- * The 'event' parameter in the handler will be typed to the specific eventConstructor.
47
- */
48
- protected on<E extends A>(
49
- // TODO: 'any[]' is required for constructor arguments (see explanation above).
50
- // This allows events with different constructor signatures to be handled uniformly.
51
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
- eventConstructor: new (...args: any[]) => E,
53
- handler: (event: E, emit: (newState: S) => void) => void | Promise<void>
54
- ): void {
55
- if (this.eventHandlers.has(eventConstructor)) {
56
- // Using Blac.warn or a similar logging mechanism from BlocBase if available,
57
- // otherwise console.warn. Assuming this._name and this._id are available from BlocBase.
58
- Blac.warn(
59
- `[Bloc ${this._name}:${String(this._id)}] Handler for event '${eventConstructor.name}' already registered. It will be overwritten.`
60
- );
61
- }
62
- // Cast the specific handler (event: E) to a more general (event: A) for storage.
63
- // This is safe because E extends A. When the handler is called with an 'action' of type A,
64
- // if it was originally registered for type E, 'action' must be an instance of E.
65
- this.eventHandlers.set(
66
- eventConstructor,
67
- handler as (event: A, emit: (newState: S) => void) => void | Promise<void>
68
- );
21
+ /**
22
+ * @internal
23
+ * Flag indicating if an event is currently being processed
24
+ */
25
+ private _isProcessingEvent = false;
26
+
27
+ /**
28
+ * Registers an event handler for a specific event type.
29
+ * This method is typically called in the constructor of a derived Bloc class.
30
+ * @param eventConstructor The constructor of the event to handle (e.g., LoadDataEvent).
31
+ * @param handler A function that processes the event and can emit new states.
32
+ * The 'event' parameter in the handler will be typed to the specific eventConstructor.
33
+ */
34
+ protected on<E extends A>(
35
+ eventConstructor: new (...args: any[]) => E,
36
+ handler: (event: E, emit: (newState: S) => void) => void | Promise<void>,
37
+ ): void {
38
+ if (this.eventHandlers.has(eventConstructor)) {
39
+ Blac.warn(
40
+ `[Bloc ${this._name}:${String(this._id)}] Handler for event '${eventConstructor.name}' already registered. It will be overwritten.`,
41
+ );
69
42
  }
43
+ this.eventHandlers.set(
44
+ eventConstructor,
45
+ handler as (
46
+ event: A,
47
+ emit: (newState: S) => void,
48
+ ) => void | Promise<void>,
49
+ );
50
+ }
70
51
 
71
- /**
72
- * Dispatches an action/event to the Bloc.
73
- * Events are queued and processed sequentially to prevent race conditions.
74
- * @param action The action/event instance to be processed.
75
- */
76
- public add = async (action: A): Promise<void> => {
77
- // Add event to queue
78
- this._eventQueue.push(action);
79
-
80
- // If not already processing, start processing the queue
81
- if (!this._isProcessingEvent) {
82
- await this._processEventQueue();
83
- }
84
- };
52
+ /**
53
+ * Dispatches an action/event to the Bloc.
54
+ * Events are queued and processed sequentially to prevent race conditions.
55
+ * @param action The action/event instance to be processed.
56
+ */
57
+ public add = async (action: A): Promise<void> => {
58
+ this._eventQueue.push(action);
85
59
 
86
- /**
87
- * @internal
88
- * Processes events from the queue sequentially
89
- */
90
- private async _processEventQueue(): Promise<void> {
91
- // Prevent concurrent processing
92
- if (this._isProcessingEvent) {
93
- return;
94
- }
95
-
96
- this._isProcessingEvent = true;
97
-
98
- try {
99
- while (this._eventQueue.length > 0) {
100
- const action = this._eventQueue.shift()!;
101
- await this._processEvent(action);
102
- }
103
- } finally {
104
- this._isProcessingEvent = false;
105
- }
60
+ if (!this._isProcessingEvent) {
61
+ await this._processEventQueue();
62
+ }
63
+ };
64
+
65
+ /**
66
+ * @internal
67
+ * Processes events from the queue sequentially
68
+ */
69
+ private async _processEventQueue(): Promise<void> {
70
+ if (this._isProcessingEvent) {
71
+ return;
72
+ }
73
+
74
+ this._isProcessingEvent = true;
75
+
76
+ try {
77
+ while (this._eventQueue.length > 0) {
78
+ const action = this._eventQueue.shift()!;
79
+ await this._processEvent(action);
80
+ }
81
+ } finally {
82
+ this._isProcessingEvent = false;
106
83
  }
84
+ }
107
85
 
108
- /**
109
- * @internal
110
- * Processes a single event
111
- */
112
- private async _processEvent(action: A): Promise<void> {
113
- // Using 'any[]' for constructor arguments for broader compatibility.
114
- // TODO: Type assertion required to cast action.constructor to proper event constructor type.
115
- // JavaScript's constructor property returns 'Function', but we need the specific event
116
- // constructor type to look up handlers. This is safe because we validate the action
117
- // extends the BlocEventConstraint interface.
118
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
119
- const eventConstructor = action.constructor as new (...args: any[]) => A;
120
- const handler = this.eventHandlers.get(eventConstructor);
86
+ /**
87
+ * @internal
88
+ * Processes a single event
89
+ */
90
+ private async _processEvent(action: A): Promise<void> {
91
+ const eventConstructor = action.constructor as new (...args: any[]) => A;
92
+ const handler = this.eventHandlers.get(eventConstructor);
121
93
 
122
- if (handler) {
123
- // Define the 'emit' function that handlers will use to update state.
124
- // It captures the current state ('this.state') right before each emission
125
- // to provide the correct 'previousState' to _pushState.
126
- const emit = (newState: S): void => {
127
- const previousState = this.state; // State just before this specific emission
128
- // The 'action' passed to _pushState is the original action that triggered the handler,
129
- // providing context for the state change (e.g., for logging or plugins).
130
- this._pushState(newState, previousState, action);
131
- };
94
+ if (handler) {
95
+ const emit = (newState: S): void => {
96
+ const previousState = this.state; // State just before this specific emission
97
+ this._pushState(newState, previousState, action);
98
+ };
99
+
100
+ try {
101
+ await handler(action, emit);
102
+ } catch (error) {
103
+ const constructorName =
104
+ (action.constructor as { name?: string }).name ||
105
+ 'UnnamedConstructor';
106
+ const errorContext = {
107
+ blocName: this._name,
108
+ blocId: String(this._id),
109
+ eventType: constructorName,
110
+ currentState: this.state,
111
+ action: action,
112
+ timestamp: new Date().toISOString(),
113
+ };
114
+
115
+ Blac.error(
116
+ `[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${constructorName}':`,
117
+ error,
118
+ 'Context:',
119
+ errorContext,
120
+ );
132
121
 
133
- try {
134
- // Await the handler in case it's an async function (e.g., performs API calls).
135
- // The 'action' is passed to the handler, and due to the way 'on' is typed,
136
- // the 'event' parameter within the handler function will be correctly
137
- // typed to its specific class (e.g., LoadMyFeatureData).
138
- await handler(action, emit);
139
- } catch (error) {
140
- // Enhanced error handling with better context
141
- const constructorName = (action.constructor as { name?: string }).name || 'UnnamedConstructor';
142
- const errorContext = {
143
- blocName: this._name,
144
- blocId: String(this._id),
145
- eventType: constructorName,
146
- currentState: this.state,
147
- action: action,
148
- timestamp: new Date().toISOString()
149
- };
150
-
151
- Blac.error(
152
- `[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${constructorName}':`,
153
- error,
154
- "Context:", errorContext
155
- );
156
-
157
- // TODO: Consider implementing error boundary pattern
158
- // For now, we log and continue, but applications may want to:
159
- // 1. Emit an error state
160
- // 2. Re-throw the error
161
- // 3. Call an error handler callback
162
-
163
- // Optional: Re-throw for critical errors (can be configured)
164
- if (error instanceof Error && error.name === 'CriticalError') {
165
- throw error;
166
- }
167
- }
168
- } else {
169
- // Enhanced warning with more context
170
- const constructorName = (action.constructor as { name?: string }).name || 'UnnamedConstructor';
171
- Blac.warn(
172
- `[Bloc ${this._name}:${String(this._id)}] No handler registered for action type: '${constructorName}'.`,
173
- "Registered handlers:", Array.from(this.eventHandlers.keys()).map(k => k.name),
174
- "Action was:", action
175
- );
122
+ if (error instanceof Error && error.name === 'CriticalError') {
123
+ throw error;
176
124
  }
125
+ }
126
+ } else {
127
+ const constructorName =
128
+ (action.constructor as { name?: string }).name || 'UnnamedConstructor';
129
+ Blac.warn(
130
+ `[Bloc ${this._name}:${String(this._id)}] No handler registered for action type: '${constructorName}'.`,
131
+ 'Registered handlers:',
132
+ Array.from(this.eventHandlers.keys()).map((k) => k.name),
133
+ 'Action was:',
134
+ action,
135
+ );
177
136
  }
137
+ }
178
138
  }