bug: mutating an immutable property calls related watcher functions w/ new value

Open
#3,490 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript
Domain
frontend

Research direction

Start with the watcher and immutable-property behavior described in the issue, using the linked reproduction repository to reproduce the console output. Trace how @Prop() and @Watch() are processed when the property is mutated internally, then add a regression test showing that the watcher is not called for an immutable property and confirm the existing mutable-property behavior remains intact.

Written by the indexing model from the issue text.

Description

Resolution: Refine
Prerequisites
Stencil Version

2.17.2

Current Behavior

When decorating a function with a @Watch decorator for an immutable property, and changing that property internally, the watcher function is still called despite the property being immutable.

Example

I have the following immutable (by default) property and watcher on that property:

  @Prop() myVar: string = 'foo';

  @Watch('myVar')
  watchMyVar(newValue: string, oldValue: string): void {
    console.log('---watchMyVar START---');
    console.log(newValue, oldValue);
    console.log('---watchMyVar END---');
  }

If I have a method which mutates myVar internally, the watcher function is subsequently called.

mutateImmutableProperty(newVal: string): void {
  this.myVar = newVal;
}

If I then call mutateImmutableProperty, watchMyVar is subsequently called because the value of myVar is updated:

this.mutateImmutableProperty(`${Date.now()}`);

// ---watchMyVar START---
// bar foo
// ---watchMyVar END---

However, myVar is immutable (mutable: false - by default) therefore, I would question if it's correct to call subsequent watcher functions.

Expected Behavior

For the @Watcher functions to not be called on immutable properties when they are modified internally.

Steps to Reproduce
  1. Create a Stencil component which has a property defined as follows:
@Prop() myVar: string = 'foo';
  1. Create a watcher on that property:
@Watch('myVar')
watchMyVar(newValue: string, oldValue: string): void {
  console.log('---watchMyVar START---');
  console.log(newValue, oldValue);
  console.log('---watchMyVar END---');
}
  1. Create a method to mutate the object:
mutateImmutableProperty(newVal: string): void {
  this.myVar = newVal;
}
  1. Call this method internally. I have attached this to the onClick event of a button:
render() {
  return [<div>{this.myVar}</div>, <button onClick={() => this.mutateImmutable(`${Date.now()}`)}>Mutate</button>];
}
  1. Observe the console logs with the following output, illustrating the watcher function being called:
---watchMyVarHandler START---
1658921136478 1658920723269
---watchMyVarHandler END---
Code Reproduction URL

https://github.com/kelvindart/stencil-watcher-bug

Additional Information

Screenshot 2022-07-27 at 12 18 33

Dominant language
TypeScript
Stars
13.1k
Forks
855
Avg merge
4h 7m
Merged PRs (30d)
44

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stenciljs/core

All issues in stenciljs/core

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.