-
-
Save dimatter/b66a5490bbddf4953407cbd311ce88b7 to your computer and use it in GitHub Desktop.
mutation notifier
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
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