hd-utils
Version:
A handy utils for modern JS developers
29 lines (26 loc) • 944 B
JavaScript
/**
* @description will call the passed object whenever the returned object is changed and will pass the prev and the current object.
* @example const watchedObject = createWatcher({ name: 'John', age: 25 }, (prev, current) => {
console.log('Object changed:', prev, '->', current);
});
watchedObject.age = 30;
// to unwatch
const prototype = Object.getPrototypeOf(watchedObject);
console.log(prototype.unwatch()) // or just watchedObject.__prototype__.unwatch();
*/
export default function createWatcher(target, callback) {
let watch = true;
return new Proxy(target, {
set: (obj, prop, value) => {
const prevValue = { ...obj };
obj[prop] = value;
Object.setPrototypeOf(obj, {
unwatch: function () {
watch = false;
},
});
watch && callback(prevValue, { ...obj });
return true;
},
});
}