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.

feat(core): @Volatile() decorator to allow unstable properties for change detection

See original GitHub issue

I’m submitting a …


[ ] Regression (behavior that used to work and stopped working in a new release)
[ ] Bug report 
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

In dev mode, Angular will have additional change detection to make sure all properties not being changed after first change detection.

For example, when we have a getter to call Map#keys(), it would throw ExpressionChangedAfterItHasBeenCheckedError since it returns a new Iterator each time:

@Component({
  template: `
    <ul>
      <li *ngFor="let key of keys">{{ key }}</li>
    </ul>
  `,
})
export class App {
  private map: Map<number>
  
  constructor() {
    this.map = new Map([
      ['a', 1],
      ['b', 2],
      ['c', 3],
    ])
  }
  
  get keys() {
    return this.map.keys()
  }
}

A live example could be found here.

Expected behavior

Since we know that the new iterator is equal to the old one in semantics, we can tell Angular to ignore its (and its sub-properties’) change by adding an @Volatile() decorator to the property:

@Component({
  template: `
    <ul>
      <li *ngFor="let key of keys">{{ key }}</li>
    </ul>
  `,
})
export class App {
  private map: Map<number>
  
  constructor() {
    this.map = new Map([
      ['a', 1],
      ['b', 2],
      ['c', 3],
    ])
  }
  
  @Volatile()
  get keys() {
    return this.map.keys()
  }
}

And no error would be thrown.

Minimal reproduction of the problem with instructions

https://plnkr.co/edit/E5kvFLWNEjN3NVfur7HL?p=preview

What is the motivation / use case for changing the behavior?

This would help with getter-style implementations and some low-level operations.

Relates to:

https://github.com/angular/angular/issues/2246 https://github.com/angular/angular/issues/15464 https://github.com/angular/angular/issues/15721 https://github.com/angular/angular/issues/16049 https://github.com/angular/angular/issues/17070 https://github.com/angular/angular/issues/17347 https://github.com/angular/angular/issues/17572 https://github.com/angular/angular/issues/17724

Alternatives:

  • Introduce a volatile property in Component Metadata to disable the second-time checking at Component level.

Please tell us about your environment


Angular version: not relevant


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: not relevant 
- Platform: not relevant 

Others: none

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dawidgaruscommented, Jun 26, 2017

@trotyl no setter needed, just:

get keys() {
  return Array.from(this.map.keys());
}
1reaction
trotylcommented, Jun 26, 2017

@ChinesePeanuts That is a valid workaround, but just a workaround. No one came to Angular to find out how many workarounds there are, and that’s something should not be addicted to.

The proposal here is to get rid of those workarounds in achieving something that already works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unstable Features - The Cargo Book
-Z allow-features — Provides a way to restrict which unstable features are used. ... Cargo then uses that information for change-detection (if any...
Read more >
prettierx | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
GStreamer: news - Freedesktop.org
The GStreamer team is pleased to announce another development release in the unstable 1.21 release series on the road to the 1.22 stable...
Read more >
CircuitPython Documentation - Read the Docs
Supports native USB on most boards and BLE otherwise, allowing file ... The core code of MicroPython is shared amongst ports including CircuitPython:....
Read more >
Release Notes - Java Bug System - OpenJDK
File.delete: NT Permission problem; [JDK-4088988] - JLS3 14.21 Not detecting unreachable code (exit via break with intervening finally block) ...
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