Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Tracked descriptors not accessible after property initialization #415

Description

@dill-larson

Background

I'm attempting to create a custom decorator to apply to a @tracked property in order to execute a function whenever the property's value is set.

Here's a simplified version of the component and the decorator:

// component.ts
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { log } from './decorators';
export default class Foo extends Component {
  @tracked
  @log
  bar = '';
}
// decorators.ts
export default function log(target: Object, propertyKey: string): ReturnType<PropertyDecorator> {
  const trackedDescriptor = Object.getOwnPropertyDescriptor(target, propertyKey); // returns undefined

  Object.defineProperty(target, propertyKey, {
    enumerable: true,
    writable: true,
    get() {
        const value = trackedDescriptor.get?.call(this);
        return value;
    },
    set(newValue: unknown) {
        trackedDescriptor.set?.call(this, newValue);
        // custom logic
        console.log(newValue);
    }
  });
}

Problem

When trying to access the tracked descriptor in a subsequent decorator, it will always be undefined. Thus preventing me from creating a custom decorator for a @tracked property. Am I going about this incorrectly? Any help would be appreciated, thanks!

Version

ember-cli: 3.28.6
node: 16.20.0
os: darwin arm64
@glimmer/component: "^1.0.4"
@glimmer/tracking: "^1.0.4"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions