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

Fix Lazy Initialization #1644

Open
NullVoxPopuli opened this issue Oct 30, 2024 · 1 comment
Open

Fix Lazy Initialization #1644

NullVoxPopuli opened this issue Oct 30, 2024 · 1 comment

Comments

@NullVoxPopuli
Copy link
Contributor

Problem: @tracked is eagerly consumed when we don't want it to be

Broken:

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

let _id = 0;

function id() {
  return _id++;
}

/**
  *
  * Expected: 1, 2, 3, 4, 3, 4, 5, 6, 7
  * Actual: 1, 2, 3, 4, <Error>
  *
  * Observation:
  *  - Sub's constructor may be entangled with Outer#collection
  *
  */

class Sub extends Component {
  constructor(...args) {
    super(...args);
    console.log(3);
    this.args.register(id());
  }
  <template></template>
}

class Outer extends Component {
  @tracked collection = [];

  register = (id) => {
    console.log(4);
    this.collection.push(id);
    this.collection = this.collection;
  };

  <template>
    {{log 1}}
    {{yield (component Sub register=this.register)}}
    {{log 6}}

    {{! (read collection the first time) }}
    {{log 7}}
    {{#each this.collection as |item|}}
      {{item}}
    {{/each}}
  </template>
}

<template>
  <Outer as |Sub|>
    {{log 2}}
    <Sub />
    <Sub />
    {{log 5}}
  </Outer>
</template>

Works:

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

import {
  createStorage,
  getValue,
  setValue,
} from 'ember-tracked-storage-polyfill';

let _id = 0;

function id() {
  return _id++;
}

/**
  *
  * Expected: 1, 2, 3, 4, 3, 4, 5, 6, 7
  * Actual: 1, 2, 3, 4, 5, 6, 7
  *
  */

class Sub extends Component {
  constructor(...args) {
    super(...args);
    console.log(3);
    this.args.register(id());
  }
  <template></template>
}

class Outer extends Component {
  _collection;
  get collection() {
    if (!this._collection) {
      this._collection = createStorage([]);
    }

    return getValue(this._collection);
  }
  set collection(value) {
    setValue(this._collection, value);
  }

  register = (id) => {
    console.log(4);
    this.collection.push(id);
    this.collection = this.collection;
  };

  <template>
    {{log 1}}
    {{yield (component Sub register=this.register)}}
    {{log 6}}

    {{! (read collection the first time) }}
    {{log 7}}
    {{#each this.collection as |item|}}
      {{item}}
    {{/each}}
  </template>
}

<template>
  <Outer as |Sub|>
    {{log 2}}
    <Sub />
    <Sub />
    {{log 5}}
  </Outer>
</template>
@NullVoxPopuli
Copy link
Contributor Author

using TrackedArray solves:

  collection = new TrackedArray([]);

  register = (id) => {
    console.log(4);
    this.collection.push(id);
  };

I suspect because TrackedArray also uses createStorage.

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

No branches or pull requests

1 participant