Skip to content

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

@ElliotNB

Description

@ElliotNB

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions