Skip to content

Cyclic Dependencies Possible Improvement For Ref #3

Open
@BorislavBorisov22

Description

@BorislavBorisov22

Currently the setter for reactive objects makes a check to see if new value is different from old value and only then triggers dep effects, which in some cases of cyclic dependencies might save us from a maximum call stack size exceeded error. But on the setter of ref objects we don't have that check and the example code below causes a max call stack size error:

effect(() => {
  console.log(a.value);
  b.value = 1;
});

effect(() => {
  console.log(b.value);
  a.value = 1;
});

Adding pretty much the same check as in reactive can save us from such cases:

export function ref(raw) {
  const r = {
    get value() {
      track(r, 'value');
      return raw;
    },
    set value(newValue) {
      const oldValue = raw;
      raw = newValue;
      if (oldValue !== raw) {
        trigger(r, 'value');
      }
    }
  };

  return r;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions