Skip to content

Instantly share code, notes, and snippets.

@IronGhost63
Created September 1, 2025 07:43
Show Gist options
  • Select an option

  • Save IronGhost63/c0425a0c9c76a7c43fd11d4f9b5ec2d2 to your computer and use it in GitHub Desktop.

Select an option

Save IronGhost63/c0425a0c9c76a7c43fd11d4f9b5ec2d2 to your computer and use it in GitHub Desktop.
Dispatch and Listen to events when variable change value.
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