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

Add helpful hint in the README about avoiding infinite recursion #18

Open
ElliotNB opened this issue Aug 21, 2018 · 1 comment
Open

Comments

@ElliotNB
Copy link
Owner

With regular Proxy objects you can trigger an infinite loop by modifying an object that was just modified within the set handler:

let validator = {
  set: function(obj, prop, value) {
    if (prop === 'age') {
      if (!Number.isInteger(value)) {
        throw new TypeError('The age is not an integer');
      }
      if (value > 200) {
        throw new RangeError('The age seems invalid');
      }
    }
	console.log('new set');
	person.age = 10;
    // The default behavior to store the value
    obj[prop] = value;

    // Indicate success
    return true;
  }
};

let person = new Proxy({}, validator);
person.age = 15;

A developer is unlikely to make this kind of with a regular Proxy because they are small in scope and easy to understand. However, because Observable-Slim handles this for the user, it can become less clear what's going on if they set a proxy value for the same property within a handler function. If it's not possible to add a more helpful JS error without sacrificing performance, then we should at least include a helpful hint in the README.

@ElliotNB ElliotNB changed the title Add helpful hint in the README about avoiding infinite loops Add helpful hint in the README about avoiding infinite recursion Aug 21, 2018
@alexey13
Copy link

alexey13 commented May 1, 2019

Hi! How to avoid this? Tried to use proxy.object = Object.assign({}, object) but not helped. In console log i get "too much recursion"

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

No branches or pull requests

2 participants