Skip to content

Instantly share code, notes, and snippets.

@dimatter
Created February 4, 2019 22:07
Show Gist options
  • Save dimatter/b66a5490bbddf4953407cbd311ce88b7 to your computer and use it in GitHub Desktop.
Save dimatter/b66a5490bbddf4953407cbd311ce88b7 to your computer and use it in GitHub Desktop.
mutation notifier
function MutationNotifier (
original,
event,
args,
deep,
path
) {
if (args == null) { args = {}; }
if (deep == null) { deep = false; }
if (path == null) { path = ''; }
let init = true
const proxy = new Proxy({}, {
set(target, property, value, receiver) {
if (value !== target[property]) {
const payload = R.mergeRight(args, {
property,
path,
value,
oldValue: R.clone(target[property])
});
if (!init) {
process.nextTick(() => emit(event, payload));
}
if (deep && L.isObjectLike(value)) {
value = MutationNotifier(value, event, args
, true, `${path}.${property}`.replace(/^\./, ''));
}
}
return Reflect.set(target, property, value, receiver);
},
deleteProperty(target, property) {
const payload = R.mergeRight(args, {
property,
value: undefined,
oldValue: R.clone(target[property])
});
if (!init) {
process.nextTfick(() => emit(event, payload));
}
return Reflect.deleteProperty(target, property);
}
}
);
L.assign(proxy, original)
init = false
return proxy
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment