Skip to content

Commit f92013a

Browse files
committed
state manager
1 parent f063c82 commit f92013a

File tree

4 files changed

+30
-179
lines changed

4 files changed

+30
-179
lines changed

public/js/modules/state-manager/dist/event-emitter.js

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,11 @@
66
*/
77

88
/**
9-
* Custom EventEmitter class using browser's native EventTarget or a fallback implementation
9+
* Custom EventEmitter class using browser's native EventTarget
1010
*/
1111
export class EventEmitter {
1212
constructor() {
13-
try {
14-
// Check if EventTarget is supported
15-
if (typeof EventTarget === 'function') {
16-
this._eventTarget = new EventTarget();
17-
} else {
18-
// Fallback for environments without EventTarget
19-
this._eventTarget = {
20-
addEventListener: (event, listener) => {
21-
// No-op in fallback
22-
},
23-
removeEventListener: (event, listener) => {
24-
// No-op in fallback
25-
},
26-
dispatchEvent: (event) => {
27-
return true; // Always return true in fallback
28-
}
29-
};
30-
console.warn('[EventEmitter] EventTarget not supported, using fallback implementation');
31-
}
32-
} catch (error) {
33-
console.error('[EventEmitter] Error creating EventTarget:', error);
34-
// Fallback implementation
35-
this._eventTarget = {
36-
addEventListener: (event, listener) => {
37-
// No-op in fallback
38-
},
39-
removeEventListener: (event, listener) => {
40-
// No-op in fallback
41-
},
42-
dispatchEvent: (event) => {
43-
return true; // Always return true in fallback
44-
}
45-
};
46-
}
47-
13+
this._eventTarget = new EventTarget();
4814
this._listeners = new Map();
4915
}
5016

@@ -161,46 +127,11 @@ export class EventEmitter {
161127
* @returns {boolean} Whether the event had listeners
162128
*/
163129
emit(event, ...args) {
164-
try {
165-
let customEvent;
166-
167-
// Check if CustomEvent is supported
168-
if (typeof CustomEvent === 'function') {
169-
customEvent = new CustomEvent(event, {
170-
detail: args
171-
});
172-
} else {
173-
// Fallback for environments without CustomEvent
174-
customEvent = {
175-
type: event,
176-
detail: args,
177-
preventDefault: () => {},
178-
stopPropagation: () => {}
179-
};
180-
console.warn('[EventEmitter] CustomEvent not supported, using fallback implementation');
181-
}
182-
183-
// Try to dispatch the event
184-
try {
185-
this._eventTarget.dispatchEvent(customEvent);
186-
} catch (dispatchError) {
187-
console.error('[EventEmitter] Error dispatching event:', dispatchError);
188-
189-
// Manual dispatch fallback
190-
if (this._listeners.has(event)) {
191-
const listeners = this._listeners.get(event);
192-
for (const listener of listeners) {
193-
try {
194-
listener(...args);
195-
} catch (listenerError) {
196-
console.error('[EventEmitter] Error in event listener:', listenerError);
197-
}
198-
}
199-
}
200-
}
201-
} catch (error) {
202-
console.error('[EventEmitter] Error in emit:', error);
203-
}
130+
const customEvent = new CustomEvent(event, {
131+
detail: args
132+
});
133+
134+
this._eventTarget.dispatchEvent(customEvent);
204135

205136
return this._listeners.has(event) && this._listeners.get(event).size > 0;
206137
}

public/js/modules/state-manager/dist/index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@ import { EventEmitter } from './event-emitter.js';
88
import { createPersistenceManager } from './persistence.js';
99
import { createWebComponentIntegration } from './web-components.js';
1010
import { createMiddlewareManager } from './middleware.js';
11-
12-
// Create a simple logger to avoid dependency issues
13-
const logger = {
14-
info: (msg) => console.log('[state-manager]', msg),
15-
debug: () => {},
16-
warn: (msg) => console.warn('[state-manager]', msg),
17-
error: (msg) => console.error('[state-manager]', msg)
18-
};
11+
import logger from '../logger.js';
1912

2013
logger.info('Initializing state-manager module');
2114

public/js/modules/state-manager/dist/store-connector.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* This implementation is compatible with the usage in simple-counter.js.
66
*/
77

8-
// Removed import to avoid circular dependency
8+
import defaultStateManager from './index.js';
99

1010
/**
1111
* Create a store for state management
@@ -70,22 +70,7 @@ export function createStore(name, initialState = {}) {
7070
*/
7171
export function StoreConnector(store) {
7272
return (BaseComponent) => {
73-
// Handle case where BaseComponent might be undefined or null
74-
let ParentClass;
75-
76-
try {
77-
// Ensure BaseComponent is a valid constructor or use HTMLElement as fallback
78-
if (BaseComponent && typeof BaseComponent === 'function') {
79-
ParentClass = BaseComponent;
80-
} else {
81-
ParentClass = HTMLElement;
82-
}
83-
} catch (error) {
84-
console.error('[state-manager] Error creating parent class in StoreConnector:', error);
85-
ParentClass = HTMLElement;
86-
}
87-
88-
return class extends ParentClass {
73+
return class extends BaseComponent {
8974
constructor() {
9075
super();
9176
this._storeUnsubscribe = null;

public/js/modules/state-manager/dist/web-components.js

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,7 @@
99
* @param {Object} stateManager - State manager instance
1010
* @returns {Object} Web component integration utilities
1111
*/
12-
export function createWebComponentIntegration(stateManager = null) {
13-
// Create a simple state manager if none is provided
14-
const simpleStateManager = stateManager || {
15-
getState(path) {
16-
return {};
17-
},
18-
setState(update, options) {
19-
return {};
20-
},
21-
subscribe(callback, paths) {
22-
// Return a no-op unsubscribe function
23-
return () => {};
24-
}
25-
};
12+
export function createWebComponentIntegration(stateManager) {
2613
/**
2714
* Create a connected web component
2815
* @param {string} tagName - Custom element tag name
@@ -48,22 +35,7 @@ export function createWebComponentIntegration(stateManager = null) {
4835
};
4936

5037
// Create a new class that extends the base component
51-
// Handle case where BaseComponent might be undefined or null
52-
let ParentClass;
53-
54-
try {
55-
// Ensure BaseComponent is a valid constructor or use HTMLElement as fallback
56-
if (BaseComponent && typeof BaseComponent === 'function') {
57-
ParentClass = BaseComponent;
58-
} else {
59-
ParentClass = HTMLElement;
60-
}
61-
} catch (error) {
62-
console.error('[state-manager] Error creating parent class:', error);
63-
ParentClass = HTMLElement;
64-
}
65-
66-
const ConnectedComponent = class extends ParentClass {
38+
const ConnectedComponent = class extends BaseComponent {
6739
constructor() {
6840
super();
6941

@@ -97,7 +69,7 @@ export function createWebComponentIntegration(stateManager = null) {
9769
* @returns {any} The requested state
9870
*/
9971
getState(path) {
100-
return simpleStateManager.getState(path);
72+
return stateManager.getState(path);
10173
}
10274

10375
/**
@@ -107,7 +79,7 @@ export function createWebComponentIntegration(stateManager = null) {
10779
* @returns {Object} The new state
10880
*/
10981
setState(update, options) {
110-
return simpleStateManager.setState(update, options);
82+
return stateManager.setState(update, options);
11183
}
11284

11385
/**
@@ -151,14 +123,14 @@ export function createWebComponentIntegration(stateManager = null) {
151123
}
152124

153125
// Subscribe to state changes
154-
this._stateUnsubscribe = simpleStateManager.subscribe(
126+
this._stateUnsubscribe = stateManager.subscribe(
155127
this._handleStateChange.bind(this),
156128
config.statePaths
157129
);
158130

159131
// Initial state update
160132
if (typeof config.mapStateToProps === 'function') {
161-
const fullState = simpleStateManager.getState();
133+
const fullState = stateManager.getState();
162134
const props = config.mapStateToProps(fullState, this);
163135

164136
// Update component properties
@@ -223,22 +195,7 @@ export function createWebComponentIntegration(stateManager = null) {
223195
};
224196

225197
return (BaseClass) => {
226-
// Handle case where BaseClass might be undefined or null
227-
let ParentClass;
228-
229-
try {
230-
// Ensure BaseClass is a valid constructor or use HTMLElement as fallback
231-
if (BaseClass && typeof BaseClass === 'function') {
232-
ParentClass = BaseClass;
233-
} else {
234-
ParentClass = HTMLElement;
235-
}
236-
} catch (error) {
237-
console.error('[state-manager] Error creating parent class:', error);
238-
ParentClass = HTMLElement;
239-
}
240-
241-
return class extends ParentClass {
198+
return class extends BaseClass {
242199
constructor() {
243200
super();
244201

@@ -281,7 +238,7 @@ export function createWebComponentIntegration(stateManager = null) {
281238
}
282239

283240
// Subscribe to state changes
284-
this._stateUnsubscribe = simpleStateManager.subscribe(
241+
this._stateUnsubscribe = stateManager.subscribe(
285242
this._handleStateChange.bind(this),
286243
this._statePaths
287244
);
@@ -295,7 +252,7 @@ export function createWebComponentIntegration(stateManager = null) {
295252
* @returns {any} The requested state
296253
*/
297254
getState(path) {
298-
return simpleStateManager.getState(path);
255+
return stateManager.getState(path);
299256
}
300257

301258
/**
@@ -305,7 +262,7 @@ export function createWebComponentIntegration(stateManager = null) {
305262
* @returns {Object} The new state
306263
*/
307264
setState(update, options) {
308-
return simpleStateManager.setState(update, options);
265+
return stateManager.setState(update, options);
309266
}
310267

311268
/**
@@ -350,15 +307,15 @@ export function createWebComponentIntegration(stateManager = null) {
350307

351308
// Subscribe to state changes if not already subscribed
352309
if (!this._stateUnsubscribe && this._statePaths.length > 0) {
353-
this._stateUnsubscribe = simpleStateManager.subscribe(
310+
this._stateUnsubscribe = stateManager.subscribe(
354311
this._handleStateChange.bind(this),
355312
this._statePaths
356313
);
357314
}
358315

359316
// Initial state update
360317
if (typeof config.mapStateToProps === 'function') {
361-
const fullState = simpleStateManager.getState();
318+
const fullState = stateManager.getState();
362319
const props = config.mapStateToProps(fullState, this);
363320

364321
// Update component properties
@@ -408,22 +365,7 @@ export function createWebComponentIntegration(stateManager = null) {
408365
};
409366

410367
return (BaseElement) => {
411-
// Handle case where BaseElement might be undefined or null
412-
let ParentClass;
413-
414-
try {
415-
// Ensure BaseElement is a valid constructor or use HTMLElement as fallback
416-
if (BaseElement && typeof BaseElement === 'function') {
417-
ParentClass = BaseElement;
418-
} else {
419-
ParentClass = HTMLElement;
420-
}
421-
} catch (error) {
422-
console.error('[state-manager] Error creating parent class:', error);
423-
ParentClass = HTMLElement;
424-
}
425-
426-
return class extends ParentClass {
368+
return class extends BaseElement {
427369
constructor() {
428370
super();
429371
this._stateUnsubscribe = null;
@@ -433,14 +375,14 @@ export function createWebComponentIntegration(stateManager = null) {
433375
super.connectedCallback();
434376

435377
// Subscribe to state changes
436-
this._stateUnsubscribe = simpleStateManager.subscribe(
378+
this._stateUnsubscribe = stateManager.subscribe(
437379
this._handleStateChange.bind(this),
438380
config.statePaths
439381
);
440382

441383
// Initial state update
442384
if (typeof config.mapStateToProps === 'function') {
443-
const state = simpleStateManager.getState();
385+
const state = stateManager.getState();
444386
const props = config.mapStateToProps(state, this);
445387

446388
// Update component properties
@@ -482,11 +424,11 @@ export function createWebComponentIntegration(stateManager = null) {
482424
}
483425

484426
getState(path) {
485-
return simpleStateManager.getState(path);
427+
return stateManager.getState(path);
486428
}
487429

488430
setState(update, options) {
489-
return simpleStateManager.setState(update, options);
431+
return stateManager.setState(update, options);
490432
}
491433
};
492434
};
@@ -505,7 +447,7 @@ export function createWebComponentIntegration(stateManager = null) {
505447
* @param {Object} stateManager - State manager instance
506448
* @returns {Function} State mixin factory function
507449
*/
508-
export function StateMixin(stateManager = null) {
450+
export function StateMixin(stateManager) {
509451
return (options = {}) => {
510452
const integration = createWebComponentIntegration(stateManager);
511453
return integration.createStateMixin(options);
@@ -517,10 +459,10 @@ export function StateMixin(stateManager = null) {
517459
* @param {string} tagName - Custom element tag name
518460
* @param {class} BaseComponent - Base component class
519461
* @param {Object} options - Configuration options
520-
* @param {Object} stateManager - State manager instance (optional)
462+
* @param {Object} stateManager - State manager instance
521463
* @returns {class} Connected component class
522464
*/
523-
export function createConnectedComponent(tagName, BaseComponent, options = {}, stateManager = null) {
465+
export function createConnectedComponent(tagName, BaseComponent, options = {}, stateManager) {
524466
const integration = createWebComponentIntegration(stateManager);
525467
return integration.createConnectedComponent(tagName, BaseComponent, options);
526468
}

0 commit comments

Comments
 (0)