Skip to content

Commit bdaac46

Browse files
committed
refactor: replace inline store implementation with imported state-manager module
1 parent e46b787 commit bdaac46

File tree

1 file changed

+5
-342
lines changed

1 file changed

+5
-342
lines changed

public/js/deps.js

Lines changed: 5 additions & 342 deletions
Original file line numberDiff line numberDiff line change
@@ -13,353 +13,16 @@ import * as renderer from './modules/spa-router/src/renderer.js';
1313
import * as componentLoader from './modules/spa-router/src/component-loader.js';
1414
export { Router, transitions, renderer, componentLoader };
1515

16-
// Create a simple store implementation to avoid circular dependencies
17-
export function createStore(name, initialState = {}) {
18-
// Create a proxy to intercept property access and modification
19-
const state = new Proxy(initialState, {
20-
set(target, property, value) {
21-
const oldValue = target[property];
22-
target[property] = value;
23-
24-
// Only notify if value actually changed
25-
if (oldValue !== value) {
26-
store.notify(property);
27-
}
28-
29-
return true;
30-
}
31-
});
32-
33-
// Subscribers for state changes
34-
const subscribers = new Set();
35-
36-
// Store object
37-
const store = {
38-
name,
39-
state,
40-
41-
// Subscribe to state changes
42-
subscribe(callback) {
43-
subscribers.add(callback);
44-
45-
// Return unsubscribe function
46-
return () => {
47-
subscribers.delete(callback);
48-
};
49-
},
50-
51-
// Notify subscribers of state changes
52-
notify(property) {
53-
subscribers.forEach(callback => {
54-
try {
55-
callback(state, property);
56-
} catch (error) {
57-
console.error(`Error in store subscriber callback:`, error);
58-
}
59-
});
60-
}
61-
};
62-
63-
return store;
64-
}
65-
66-
// Simple StoreConnector implementation
67-
export function StoreConnector(store) {
68-
return (BaseComponent) => {
69-
try {
70-
// Handle case where BaseComponent might be undefined or null
71-
let ParentClass;
72-
73-
if (BaseComponent && typeof BaseComponent === 'function') {
74-
// Check if BaseComponent is a valid constructor
75-
try {
76-
// Test if we can create an instance of BaseComponent
77-
new BaseComponent();
78-
ParentClass = BaseComponent;
79-
} catch (error) {
80-
console.warn('StoreConnector: BaseComponent is not a valid constructor, using HTMLElement instead');
81-
ParentClass = HTMLElement;
82-
}
83-
} else {
84-
console.warn('StoreConnector: No BaseComponent provided, using HTMLElement instead');
85-
ParentClass = HTMLElement;
86-
}
87-
88-
// Create the connected component class
89-
return class ConnectedComponent extends ParentClass {
90-
constructor() {
91-
super();
92-
this._storeUnsubscribe = null;
93-
this._boundElements = new Map();
94-
}
95-
96-
connectedCallback() {
97-
// Call the parent connectedCallback if it exists
98-
if (super.connectedCallback) {
99-
super.connectedCallback();
100-
}
101-
102-
// Subscribe to store changes
103-
if (store && typeof store.subscribe === 'function') {
104-
this._storeUnsubscribe = store.subscribe((state, property) => {
105-
// Trigger render or update if needed
106-
if (typeof this.render === 'function') {
107-
this.render();
108-
}
109-
});
110-
}
111-
}
112-
113-
disconnectedCallback() {
114-
// Unsubscribe from store
115-
if (this._storeUnsubscribe) {
116-
this._storeUnsubscribe();
117-
this._storeUnsubscribe = null;
118-
}
119-
120-
// Call the parent disconnectedCallback if it exists
121-
if (super.disconnectedCallback) {
122-
super.disconnectedCallback();
123-
}
124-
}
125-
};
126-
} catch (error) {
127-
console.error('StoreConnector: Error creating connected component:', error);
128-
// Return a basic HTMLElement subclass as fallback
129-
return class FallbackComponent extends HTMLElement {
130-
constructor() {
131-
super();
132-
console.warn('Using fallback component due to StoreConnector error');
133-
}
134-
};
135-
}
136-
};
137-
}
138-
// Import and re-export spa-router (commented out to avoid duplicate exports)
139-
// export { Router, transitions, renderer, componentLoader } from 'https://esm.sh/@profullstack/[email protected]';
16+
// Import and re-export state-manager
17+
import { createStore, StoreConnector } from './modules/state-manager/dist/index.js';
18+
export { createStore, StoreConnector };
14019

14120
// Import and re-export localizer from compiled version
14221
import localizerDefault from './modules/localizer/dist/index.mjs';
14322
export const localizer = localizerDefault;
14423
export function _t(key, options = {}) {
14524
return localizer.translate(key, options);
14625
}
147-
// Import and re-export localizer (commented out to avoid duplicate exports)
148-
// export { localizer, _t } from 'https://esm.sh/@profullstack/[email protected]';
149-
150-
// We're not using enhanced-router anymore, just export the Router directly
151-
// This is a compatibility layer to avoid changing all the code that uses enhancedRouter
152-
export const enhancedRouter = {
153-
createEnhancedRouter: (options = {}) => {
154-
console.log('Using @profullstack/spa-router directly');
155-
return new Router({
156-
rootElement: options.rootElement || '#app',
157-
transition: options.transition,
158-
renderer: options.renderer,
159-
errorHandler: options.errorHandler
160-
});
161-
}
162-
};
163-
164-
// Create safe wrappers for other external modules
165-
export const apiKeyManager = (() => {
166-
try {
167-
const importPromise = import('https://esm.sh/@profullstack/[email protected]');
168-
return new Proxy({}, {
169-
get(target, prop) {
170-
return async (...args) => {
171-
try {
172-
const module = await importPromise;
173-
if (typeof module[prop] === 'function') {
174-
return module[prop](...args);
175-
} else if (prop in module) {
176-
return module[prop];
177-
}
178-
throw new Error(`Method ${prop} not found in api-key-manager module`);
179-
} catch (error) {
180-
console.error(`Error using apiKeyManager.${prop}:`, error);
181-
return null;
182-
}
183-
};
184-
}
185-
});
186-
} catch (error) {
187-
console.error('Error importing api-key-manager module:', error);
188-
return new Proxy({}, {
189-
get() {
190-
return () => Promise.resolve(null);
191-
}
192-
});
193-
}
194-
})();
195-
196-
// Commented out auth-system module to avoid issues with jsonwebtoken in browser
197-
export const authSystem = (() => {
198-
console.warn('Auth system module is disabled');
199-
return new Proxy({}, {
200-
get() {
201-
return () => Promise.resolve(null);
202-
}
203-
});
204-
})();
205-
206-
export const paymentGateway = (() => {
207-
try {
208-
const importPromise = import('https://esm.sh/@profullstack/[email protected]');
209-
return new Proxy({}, {
210-
get(target, prop) {
211-
return async (...args) => {
212-
try {
213-
const module = await importPromise;
214-
if (typeof module[prop] === 'function') {
215-
return module[prop](...args);
216-
} else if (prop in module) {
217-
return module[prop];
218-
}
219-
throw new Error(`Method ${prop} not found in payment-gateway module`);
220-
} catch (error) {
221-
console.error(`Error using paymentGateway.${prop}:`, error);
222-
return null;
223-
}
224-
};
225-
}
226-
});
227-
} catch (error) {
228-
console.error('Error importing payment-gateway module:', error);
229-
return new Proxy({}, {
230-
get() {
231-
return () => Promise.resolve(null);
232-
}
233-
});
234-
}
235-
})();
236-
// Create a simple stateManager implementation to avoid circular dependencies
237-
export const stateManager = (() => {
238-
try {
239-
console.log('[state-manager] Creating simple state manager');
240-
241-
return {
242-
createStateManager: (initialState = {}, options = {}) => {
243-
console.log('[state-manager] Creating state manager instance');
244-
245-
try {
246-
// Create a simple event emitter
247-
const eventEmitter = {
248-
events: {},
249-
on(event, callback) {
250-
if (!this.events[event]) {
251-
this.events[event] = [];
252-
}
253-
this.events[event].push(callback);
254-
return () => this.off(event, callback);
255-
},
256-
off(event, callback) {
257-
if (!this.events[event]) return;
258-
this.events[event] = this.events[event].filter(cb => cb !== callback);
259-
},
260-
emit(event, ...args) {
261-
if (!this.events[event]) return;
262-
this.events[event].forEach(callback => {
263-
try {
264-
callback(...args);
265-
} catch (error) {
266-
console.error(`Error in event callback for ${event}:`, error);
267-
}
268-
});
269-
}
270-
};
271-
272-
// Create a simple state manager
273-
return {
274-
_state: { ...initialState },
275-
getState(path) {
276-
try {
277-
if (!path) return { ...this._state };
278-
return this._state[path];
279-
} catch (error) {
280-
console.error('[state-manager] Error in getState:', error);
281-
return {};
282-
}
283-
},
284-
setState(update, options = {}) {
285-
try {
286-
const newState = typeof update === 'function'
287-
? update(this._state)
288-
: { ...this._state, ...update };
289-
this._state = newState;
290-
return newState;
291-
} catch (error) {
292-
console.error('[state-manager] Error in setState:', error);
293-
return this._state;
294-
}
295-
},
296-
subscribe(callback, paths) {
297-
console.log('[state-manager] Subscribe called');
298-
// Return a no-op unsubscribe function
299-
return () => {
300-
console.log('[state-manager] Unsubscribe called');
301-
};
302-
}
303-
};
304-
} catch (error) {
305-
console.error('[state-manager] Error creating state manager instance:', error);
306-
return {
307-
_state: {},
308-
getState: () => ({}),
309-
setState: () => ({}),
310-
subscribe: () => (() => {})
311-
};
312-
}
313-
}
314-
};
315-
} catch (error) {
316-
console.error('[state-manager] Error in stateManager initialization:', error);
317-
318-
// Return a fallback implementation
319-
return {
320-
createStateManager: () => ({
321-
_state: {},
322-
getState: () => ({}),
323-
setState: () => ({}),
324-
subscribe: () => (() => {})
325-
})
326-
};
327-
}
328-
})();
32926

330-
// Create a safe wrapper for storage-service to handle potential AWS SDK errors
331-
export const storageService = (() => {
332-
try {
333-
// Try to import the storage-service module
334-
const importPromise = import('https://esm.sh/@profullstack/[email protected]');
335-
336-
// Return a proxy that will attempt to use the module but gracefully handle errors
337-
return new Proxy({}, {
338-
get(target, prop) {
339-
// Return a function that will try to use the module method
340-
return async (...args) => {
341-
try {
342-
const module = await importPromise;
343-
if (typeof module[prop] === 'function') {
344-
return module[prop](...args);
345-
} else if (prop in module) {
346-
return module[prop];
347-
}
348-
throw new Error(`Method ${prop} not found in storage-service module`);
349-
} catch (error) {
350-
console.error(`Error using storage-service.${prop}:`, error);
351-
return null;
352-
}
353-
};
354-
}
355-
});
356-
} catch (error) {
357-
console.error('Error importing storage-service module:', error);
358-
// Return a dummy object with methods that return null
359-
return new Proxy({}, {
360-
get() {
361-
return () => Promise.resolve(null);
362-
}
363-
});
364-
}
365-
})();
27+
// Import and re-export enhanced-router
28+
export * as enhancedRouter from './modules/enhanced-router/dist/index.js';

0 commit comments

Comments
 (0)