11/**
2- * Example component demonstrating state manager integration
2+ * Example component demonstrating state management
33 */
44console . log ( 'Loading state-example.js' ) ;
5- import { createStateManager , createConnectedComponent } from '../modules/state-manager/dist/index.js' ;
6- console . log ( 'Imported createStateManager from state-manager module' ) ;
75
8- // Create a state manager with initial state
9- const stateManager = createStateManager ( {
6+ // Create a simple state object with a Proxy for reactivity
7+ const createSimpleState = ( initialState = { } ) => {
8+ const listeners = new Set ( ) ;
9+
10+ const state = new Proxy ( initialState , {
11+ set ( target , property , value ) {
12+ const oldValue = target [ property ] ;
13+ target [ property ] = value ;
14+
15+ if ( oldValue !== value ) {
16+ listeners . forEach ( listener => listener ( value , property , target ) ) ;
17+ }
18+
19+ return true ;
20+ }
21+ } ) ;
22+
23+ return {
24+ state,
25+ subscribe ( listener ) {
26+ listeners . add ( listener ) ;
27+ return ( ) => listeners . delete ( listener ) ;
28+ }
29+ } ;
30+ } ;
31+
32+ // Create a state store with initial state
33+ const stateStore = createSimpleState ( {
1034 counter : 0 ,
1135 theme : document . documentElement . getAttribute ( 'data-theme' ) || 'light' ,
1236 user : { loggedIn : false }
13- } , {
14- enablePersistence : true ,
15- persistenceKey : 'state_example' ,
16- debug : true
1737} ) ;
1838
1939// Define the component class
@@ -22,33 +42,50 @@ class StateExampleElement extends HTMLElement {
2242 super ( ) ;
2343 console . log ( 'StateExampleElement constructor called' ) ;
2444 this . attachShadow ( { mode : 'open' } ) ;
45+ this . _unsubscribe = null ;
2546 }
2647
2748 connectedCallback ( ) {
2849 console . log ( 'StateExampleElement connected to DOM' ) ;
29- this . render ( ) ;
30- }
31-
32- /**
33- * This method will be called when state changes
34- * @param {object } state - The current state
35- * @param {string } path - The path that changed
36- * @param {object } fullState - The full state object
37- */
38- stateChanged ( state , path , fullState ) {
39- console . log ( 'State changed in component:' , path ) ;
40- console . log ( 'New state:' , fullState ) ;
41- console . log ( 'State value:' , state ) ;
4250
43- // Re-render the component
44- this . render ( ) ;
51+ // Subscribe to state changes
52+ this . _unsubscribe = stateStore . subscribe ( ( value , property , fullState ) => {
53+ console . log ( 'State changed in component:' , property ) ;
54+ console . log ( 'New state:' , fullState ) ;
55+ console . log ( 'State value:' , value ) ;
56+
57+ // Re-render the component
58+ this . render ( ) ;
59+
60+ // If the theme changed, update the document theme
61+ if ( property === 'theme' ) {
62+ document . documentElement . setAttribute ( 'data-theme' , value ) ;
63+ }
64+ } ) ;
4565
46- // If the theme changed, update the document theme
47- if ( path === 'theme' ) {
48- document . documentElement . setAttribute ( 'data-theme' , state ) ;
66+ this . render ( ) ;
67+ }
68+
69+ disconnectedCallback ( ) {
70+ // Clean up subscription when element is removed
71+ if ( this . _unsubscribe ) {
72+ this . _unsubscribe ( ) ;
73+ this . _unsubscribe = null ;
4974 }
5075 }
5176
77+ // Helper method to get state
78+ getState ( property ) {
79+ return stateStore . state [ property ] ;
80+ }
81+
82+ // Helper method to set state
83+ setState ( updates ) {
84+ Object . entries ( updates ) . forEach ( ( [ key , value ] ) => {
85+ stateStore . state [ key ] = value ;
86+ } ) ;
87+ }
88+
5289 render ( ) {
5390 // Get current values from state
5491 const counter = this . getState ( 'counter' ) || 0 ;
@@ -212,9 +249,7 @@ class StateExampleElement extends HTMLElement {
212249 }
213250}
214251
215- // Register the component with the state manager
216- createConnectedComponent ( 'state-example' , StateExampleElement , {
217- statePaths : [ 'counter' , 'theme' , 'user' ]
218- } , stateManager ) ;
252+ // Register the custom element
253+ customElements . define ( 'state-example' , StateExampleElement ) ;
219254
220255export default StateExampleElement ;
0 commit comments