Skip to content

Commit 192a744

Browse files
committed
refactor: replace global state manager with local stores using createStore
1 parent ece90ab commit 192a744

File tree

3 files changed

+36
-482
lines changed

3 files changed

+36
-482
lines changed

public/js/components/state-example.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
* Example component demonstrating state manager integration
33
*/
44
console.log('Loading state-example.js');
5-
import { StateMixin, defaultStateManager } from '../state-manager.js';
6-
console.log('Imported StateMixin and defaultStateManager');
5+
import { createStore, StoreConnector } from '../deps.js';
6+
console.log('Imported createStore and StoreConnector from deps.js');
7+
8+
// Create a store for this component
9+
const store = createStore('stateExample', {
10+
counter: 0,
11+
theme: document.documentElement.getAttribute('data-theme') || 'light',
12+
user: { loggedIn: false }
13+
});
714

815
// Define a base component class
916
class StateExampleBase extends HTMLElement {
@@ -241,22 +248,14 @@ class StateExampleBase extends HTMLElement {
241248
}
242249
}
243250

244-
// Create the connected component using the StateMixin
245-
const StateExample = StateMixin()(StateExampleBase);
251+
// Create the connected component using the StoreConnector
252+
const StateExample = StoreConnector(store)(StateExampleBase);
246253

247254
// Register the custom element
248255
customElements.define('state-example', StateExample);
249256

250257
// Initialize default state if not already set
251-
document.addEventListener('DOMContentLoaded', () => {
252-
// Only set initial values if they don't exist
253-
if (defaultStateManager.getState('counter') === undefined) {
254-
defaultStateManager.setState({
255-
counter: 0,
256-
theme: document.documentElement.getAttribute('data-theme') || 'light',
257-
user: { loggedIn: false }
258-
}, true); // Silent update to avoid unnecessary renders
259-
}
260-
});
258+
// No need for DOMContentLoaded event handler as we're using a local store
259+
// that's already initialized with default values
261260

262261
export default StateExample;

public/js/i18n.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
// In production, this would be imported from the npm package
99
// import { localizer, _t } from '@profullstack/localizer';
1010
// For development, we'll use our deps.js file
11-
import { localizer, _t } from './deps.js';
12-
import defaultStateManager from './state-manager.js';
11+
import { localizer, _t, createStore } from './deps.js';
12+
13+
// Create a store for i18n state
14+
const i18nStore = createStore('i18n', {
15+
currentLanguage: 'en',
16+
availableLanguages: ['en', 'fr', 'de', 'uk', 'ru', 'pl', 'zh', 'ja', 'ar'],
17+
isRTL: false
18+
});
1319

1420
// Store available languages
1521
const AVAILABLE_LANGUAGES = ['en', 'fr', 'de', 'uk', 'ru', 'pl', 'zh', 'ja', 'ar'];
@@ -94,25 +100,18 @@ async function initI18n() {
94100
* Initialize state management for i18n
95101
*/
96102
function initStateManagement() {
97-
// Set up initial i18n state in the state manager
98-
defaultStateManager.setState({
99-
i18n: {
100-
currentLanguage: localizer.getLanguage(),
101-
availableLanguages: AVAILABLE_LANGUAGES,
102-
isRTL: ['ar', 'he', 'fa', 'ur'].includes(localizer.getLanguage())
103-
}
104-
});
103+
// Update the store with current language
104+
i18nStore.state.currentLanguage = localizer.getLanguage();
105+
i18nStore.state.isRTL = ['ar', 'he', 'fa', 'ur'].includes(localizer.getLanguage());
105106

106-
// Subscribe to language changes in the state manager
107-
defaultStateManager.subscribe((state, changedKeys) => {
108-
if (changedKeys.includes('i18n.currentLanguage')) {
109-
const newLang = state.i18n.currentLanguage;
110-
if (newLang !== localizer.getLanguage()) {
111-
console.log(`Language change detected in state manager: ${newLang}`);
112-
changeLanguage(newLang);
113-
}
107+
// Subscribe to language changes in the store
108+
i18nStore.subscribe((state) => {
109+
const newLang = state.currentLanguage;
110+
if (newLang !== localizer.getLanguage()) {
111+
console.log(`Language change detected in store: ${newLang}`);
112+
changeLanguage(newLang);
114113
}
115-
}, 'i18n.currentLanguage');
114+
});
116115
}
117116

118117
/**
@@ -180,14 +179,12 @@ function changeLanguage(language) {
180179
// Apply RTL direction if needed
181180
applyDirectionToDocument();
182181

183-
// Update the state manager (without triggering the subscription callback)
182+
// Update the store (without triggering circular updates)
184183
const isRTL = ['ar', 'he', 'fa', 'ur'].includes(language);
185-
defaultStateManager.setState({
186-
i18n: {
187-
currentLanguage: language,
188-
isRTL
189-
}
190-
}, true); // Silent update to prevent circular updates
184+
185+
// Use direct property assignment to avoid triggering the subscription
186+
i18nStore.state.currentLanguage = language;
187+
i18nStore.state.isRTL = isRTL;
191188

192189
// Dispatch an event to notify that language has changed
193190
window.dispatchEvent(new CustomEvent('language-changed', {

0 commit comments

Comments
 (0)