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.

Data cycle within ember-changeset triggers invalidation-after-set assertion

See original GitHub issue

While validating, a changeset can consume its own _errors property before writing to it. This addon marks _errors as tracked:

https://github.com/poteto/ember-changeset/blob/072f062711df3f7e73ba81a887274002a3c98242/addon/index.js#L37

But if you read and then write a tracked property during rendering, Ember will throw the dreaded:

You attempted to update _errors on changeset:[object Object], but it had already been used previously in the same computation.

assertion.

Here is a tiny app that crashes at boot: https://github.com/ef4/--ember-changeset-bug-repro/commit/90a2a00cfc109f0bda5cf9dd4793532e173cb0c0

Both the read and the write happen on this line:

https://github.com/validated-changeset/validated-changeset/blob/a23494a9a481fe639d4e00aaffdfc9e54ea50cce/src/index.ts#L795

this[ERRORS] = this._deleteKey(ERRORS, key) as Errors<any>;

First, _deleteKey’s implementation consumes this._errors, and then we try to immediately assign to it.

I suspect the same bug is also lurking in rollbackInvalid(), which does the same thing.

The fix here is to make sure _errors is always only an output to your computation, never an input. Recompute the whole _errors whenever it is needed without reference to the old value. And if you need caching, make caches that are not tracked.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14

github_iconTop GitHub Comments

1reaction
ef4commented, Jul 7, 2021

Also keep in mind that write-then-read is totally fine, it’s only read-then-write that is a problem.

(To think about why it’s a problem, consider that reading a tracked property causes that tracked property to be an automatic dependency of the code you’re running. If your code then changes that property, it just invalidated itself.)

1reaction
ef4commented, Jul 7, 2021

However, this is where I believe we would have to take a different path with the set** property on changeset workflows if we did come across an error.

No, that’s really not different.

The state the user sets is purely an input to the changeset. You would only ever read from it, never write it. The resulting validation state is purely an output of the changeset. You would only ever write to it, never read from it.

I have seen artificial delays added to avoid this issue.

Yes, please don’t ever do this unless you’re deliberately forcing a double render and know why you want a double render (measuring DOM is the typical reason).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Errors with Ember v3.22 · Issue #553 · poteto/ember-changeset
Just tried upgrading an app to Ember v3.22 and started getting some errors in changeset. index.js:172 Uncaught (in promise) Error: Assertion ...
Read more >
@ember-data/canary-features | Yarn - Package Manager
This package provides utilities for configuring addon-build setup for @ember-data/ packages. It is directly depended upon by those packages and should not be ......
Read more >
ember-changeset - npm
ember-changeset only allows valid changes to be set, so your Objects will never become invalid (assuming you have 100% validation coverage).
Read more >
Understanding what to expect from ember-changeset
I am wondering if there is a way to harness ember-changeset to do what I want. Here is what I would expect from...
Read more >
THURSDAY, APRIL 6, 1978 - GovInfo
SIAP amendments may have been pre viously issued by the FAA in a Nation al Flight Data Center (FDC) Notice to.
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