question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$watch an Object using strict equality

See original GitHub issue

What problem does this feature solve?

Hi,

I’d like to request a new option to the $watch() function that, in case of watching an instance of an Object or an Array, would make the watch callback be called only when the old instance is strictly unequal the new instance. This would offer an alternative to the current behaviour of always calling the watch callback when watching a non-primitive variable (as per the following comment).

The new option might be called something like: strictObjectComparison: boolean;.

The use case is a situation when a dev knows exactly what they’re doing (i.e. watching an instance of a value object) and they want their watch callbacks to be called only if the instance changes.

This is somewhat related to this bug report: #6616. By adding the requested feature, the problem wouldn’t occur. Frankly, I’m also affected by this idiosyncrasy of the framework and would really appreciate if I could just tell the framework that I know what I’m doing and that I’d like the watched Object to be compared strictly (i.e. with ===) and nothing more.

Thank you.

What does the proposed API look like?

class Person {
  constructor(name) {
    this.name = name;
  }
}

const vm = new Vue({
  data: {
    people: [
      new Person('Phil'),
      new Person('Rob'),
      new Person('Tom'),
    ],
    selectedPerson: null,
  }
});

vm.$watch(
  'selectedPerson', 
  () => { console.log(vm.selectedPerson); }, 
  { strictObjectCompare: true }
);

// should trigger the watch callback
vm.selectedPerson = vm.people[0];

// should not trigger the watch callback
vm.selectedPerson = vm.people[0];

// should trigger the watch callback
vm.selectedPerson = vm.people[1];

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
posvacommented, Apr 7, 2019

As said in the comment above, check between old and current value

0reactions
d-phcommented, Apr 7, 2019

Good day gentlemen,

Yes, this is not a double post, although both this ticket and #9837 touch the same ground but from different angle.

@posva suggested a solution to this ticket that I actually didn’t think of, but does solve this ticket’s problem. This means that we can close this ticket if we don’t want to take any action on its matter anymore.

To be honest, I’m in two minds now. I would appreciate a way to tell to the framework that I want to strict watch an object/array, but the solution that posva suggested is so simple and painless (although not immediately obvious, since watch callbacks should not be called when things don’t change) that it’s not worthwhile increasing the learning curve and maintenance by adding another option to the $watch() function.

Yeah, I don’t mind if this ticket is closed with the solution suggested by posva.

Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strict equality (===) - JavaScript - MDN Web Docs
The strict equality (===) operator checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality...
Read more >
Loose vs Strict Equality in JavaScript - Medium
The Loose Equality or Abstract Equality check is check for the likeness of the two values and it is performed using == (double...
Read more >
Understanding Strict, Abstract Equality Operators in JS - Telerik
When comparing two objects of the same type with either the abstract or strict equality operator, they are equal if both operands reference...
Read more >
Equality & strict-Equality Operators in javaScript
Comparison by reference basically checks to see if the objects given refer to the same location in memory allocation.
Read more >
Which equals operator (== vs ===) should be used in ...
The strict equality operator ( === ) behaves identically to the abstract equality operator ( == ) except no type conversion is done,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found