Skip to content

Instantly share code, notes, and snippets.

@pzuraq
Created February 1, 2019 18:05
Show Gist options
  • Save pzuraq/d45c8a055e236e160759127cffdc064e to your computer and use it in GitHub Desktop.
Save pzuraq/d45c8a055e236e160759127cffdc064e to your computer and use it in GitHub Desktop.
class Foo {
  @tracked bar;

  get baz() {
    return this.bar;
  }
}

let foo = new Foo();

In order to make this work, we need to know that bar is a dependency of baz when we access foo.baz. Currently, we know this because bar is instrumented.

In order to know this with proxies, we must not only wrap foo in a proxy, we must recursively wrap it in a proxy. Otherwise, baz is a black box, we have no idea what it accessed:

class Foo {
  bar;

  get baz() {
    return this.bar;
  }
}

let foo = new Foo();
let proxy = new Proxy(foo, {
  get(target, key) {
    let getter = Object.getOwnPropertyDescriptor(foo, key).get;

    return get.call(proxy);
  }
});

Every access to this must be watched, even internally. Now, if we add a private field internally, we break:

class Foo {
  #bar;

  get baz() {
    return this.#bar;
  }
}

let foo = new Foo();
let proxy = new Proxy(foo, {
  get(target, key) {
    let getter = Object.getOwnPropertyDescriptor(foo, key).get;

    return get.call(proxy);
  }
});

foo.baz; // ERROR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment