Open azhiv opened 2 years ago
This looks like a valid bug. Looking into it! Thx for submitting!
@azhiv you have presented us with quite an interesting problem! The reason is because you are getting the "not yet" applied value. The question is, if you haven't called changeset.execute()
, what should we return for bar
. Since bar
derives its value from an internal value foo
, if we got the descriptor, then we would get the original value since you have yet to call execute()
. Do you happen to see a path forward here?
https://github.com/validated-changeset/validated-changeset/pull/165
Where we return the "change" instead of bar
.
@snewcomer I don't see a way forward here unless you build/obtain an object that represents the current state of the amended entity.
Using proxy is a way to go. We leverage it in our project for this purpose, so that inside the get
function we possess both target
(the original model) and receiver
(the proxy object), so that it's possible to pass the latter to the descriptor call:
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), prop);
if (descriptor) {
// method is either 'get' or 'set'
const descriptorMethod = descriptor[method];
if (!descriptor.enumerable && descriptorMethod) {
// It is a corresponding method defined on the model. We invoke with this=Proxy
// In this way it is evaluated with all changes applied to ChangeBuffer in place.
return descriptorMethod.call(receiver, value),
...
@azhiv I don't see one either :(. If we execute the get, we may change state, which would be unintentional and against the paradigm of a changeset - to avoid mutations until you want them.
Consider the following class:
If you create a change buffer over an instance of this class and modify it:
the results are a bit unexpected. Would you consider getting the property descriptor
and then executing it against the change buffer instance?