Created
September 1, 2025 07:43
-
-
Save IronGhost63/c0425a0c9c76a7c43fd11d4f9b5ec2d2 to your computer and use it in GitHub Desktop.
Dispatch and Listen to events when variable change value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const stateStorage = {}; | |
| window.STATE = new Proxy( stateStorage, { | |
| set: ( target, key, value ) => { | |
| const stateUpdateEvent = new CustomEvent( 'stateUpdated', { | |
| detail: {key, value} | |
| }); | |
| target[key] = value; | |
| window.dispatchEvent( stateUpdateEvent ); | |
| return true; | |
| }, | |
| }); | |
| window.addEventListener('stateUpdated', (e) => { | |
| if( e.detail.key === 'totalCartItems' ) { | |
| console.log(`value of ${key} is now ${value}`); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment