melanke / Watch.JS

watch the changes of any object or attribute
Other
2.14k stars 219 forks source link

watch does not retain existing setter logic #125

Closed Torinth closed 6 years ago

Torinth commented 6 years ago

Hi if I use Object.defineProperty to create a property with setter logic and then also run watchjs over the object my existing setter logic is not retained. Something along these lines

var setter = (value) => { console.log(value) } var sample = {}; Object.defineProperty(sample, "blah, { get: getter, set: setter, enumerable: true, configurable: true }

sample.blah = "initial"

//shows console log

WatchJS.watch(sample, () =>{ console.log("in watch") }

sample.blah = "whatever"

//the console log in my custom setter above does not get called.

This would be good for applications that already have existing specific class and setter functionality to also be able to watch them without interfering with existing systems

heres example method that if I run this after watch both the custom setter logic and the watch logic fire

createProperty = function (object, propertyName, getter, setter) { var innerSetter = setter; var innerGetter = getter; var existing = Object.getOwnPropertyDescriptor(object, propertyName); if (existing) { if (existing.get) { console.log("found existing getter"); innerGetter = function innerGetter() { return existing.get(); }; } if (existing.set) { console.log("found existing setter"); innerSetter = function innerSetter(value) { existing.set(value); setter(value); }; } } Object.defineProperty(object, propertyName, { get: innerGetter, set: innerSetter, enumerable: true, configurable: true });

melanke commented 6 years ago

+1

Torinth commented 6 years ago

I have a pr that fixes this issue but cant seem to push branches to make a pr with

Torinth commented 6 years ago

what do I need to do in order to get my fix reviewed and possibly merged? I have not contributed to GitHub before so don't know what the normal process is