Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getters/setters are executed against the original model, not the modified one #161

Open
azhiv opened this issue Feb 21, 2022 · 4 comments · May be fixed by #165
Open

getters/setters are executed against the original model, not the modified one #161

azhiv opened this issue Feb 21, 2022 · 4 comments · May be fixed by #165

Comments

@azhiv
Copy link
Contributor

azhiv commented Feb 21, 2022

Consider the following class:

class A {
  foo = 1;

  get bar(): number {
    return 2 * this.foo;
  }

  set bar(value: number) {
    this.foo = value / 2;
  }
}

If you create a change buffer over an instance of this class and modify it:

changebuffer.set('foo', 2);
console.log(changebuffer.get('bar'));      // 2, but I'd expect 4

changebuffer.set('bar', 6);
console.log(changebuffer.get('foo'));      // 1, but I'd expect 3

the results are a bit unexpected. Would you consider getting the property descriptor

const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), prop);

and then executing it against the change buffer instance?

@snewcomer
Copy link
Collaborator

This looks like a valid bug. Looking into it! Thx for submitting!

snewcomer added a commit that referenced this issue Mar 12, 2022
@snewcomer
Copy link
Collaborator

@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?

#165

Where we return the "change" instead of bar.

https://github.com/validated-changeset/validated-changeset/blob/b3f7f63ca2c0213ad6c81ee0084380ef0e8e6f6c/src/index.ts#L1045-L1047

@snewcomer snewcomer linked a pull request Mar 20, 2022 that will close this issue
@azhiv
Copy link
Contributor Author

azhiv commented Mar 30, 2022

@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),
...

@snewcomer
Copy link
Collaborator

snewcomer commented Apr 1, 2022

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants