NullVoxPopuli/ember-resources

Quest: Sync with starbeam developments: (and actually get the lint implemented, since we have some footguns to protect against)

Opened this issue · 1 comments

Tasks

  • on.sync

    • manages changes in consumed reactive values
    • allows cleanup behavior to occur per set of reactive changes
  • on.cleanup becomes on.finalize (but keep on.cleanup for compatibility)

  • Lints

    • finish #709
    • add
      • family of lints for inlining resource definitions within the class body
        • explore forbidding consumption of tracked args within the resource body (can lint against arg usage specifically
          • potentially auto-fixable to on.sync + return cleanup function
        • favor linting for the returned () => return value, because returning lazily accessed () => this.args.input is fine, but returning this.args.input is not.
        • prefer defining resources in module space
  • Learning materials

    • the resource body is "the constructor"-
    • on.sync allows easy managing of reactive updates, no worry about the whole thing getting torn down prematurely
    • on.finalize (formally on.cleanup) is tied to the lifetime of the parent context
    • before and after docs
      • how to be closest to starbeam
      • what was awkward before that resources make better

Notes:

resource(({ on }) => {
  evaluateCount++;
  on.sync(() => () => cleanupCount++);

  // THIS IS ILLEGAL. You can't read reactive values in the constructor
  // (because the entire would be torn down each time @input @changes)
  return this.args.input;
});

should instead be

resource(({ on }) => {
  on.sync(() => {
    syncCount++;
    return () => cleanupCount++;
  });

  return Formula(() => this.args.input);
});

however, Formula doesn't exist in ember-resources, and may not make sense today, as ember-resources already supports returning () => some value, and it's already a "cached value" (via the createCache api (which is internal in the helper managers)

Note: on.sync us impossfto implement in ember, pre-starbeam.

But moving the resource body into on.sync in the codemod should be safe/consistent