Skip to content

Instantly share code, notes, and snippets.

@OmgImAlexis
Created December 6, 2017 01:18
Show Gist options
  • Save OmgImAlexis/9a83a4982be0e0f741aebbf0325adc58 to your computer and use it in GitHub Desktop.
Save OmgImAlexis/9a83a4982be0e0f741aebbf0325adc58 to your computer and use it in GitHub Desktop.
const emitKeyEvents = (obj, path = null) => {
return new Proxy(obj, {
get: (target, key, receiver) => {
if (typeof target[key] === 'object' && target[key] !== null) {
return emitKeyEvents(target[key], path ? `${path}.${key}` : key);
}
return Reflect.get(target, key, receiver);
},
set: (target, key, value, receiver) => {
this.emit(path ? `${path}.${key}` : key, value);
return Reflect.set(target, key, value, receiver);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment