-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting tracked state inside a resource #707
Comments
The issue is more to do with reading the tracked data before changing it (which we can see is occuring in your template -- since resources (and tracked-data) are pull-based -- meaning I have a longer write here about the problem: #340 (comment) And a related eslint proposal: It's possible I need to write some lints for ember-resources, because with this pattern (and even with concurrency-tasks, which are similar): export default class UserList extends Component {
@tracked isLoading = false;
list = trackedFunction(this, async () => {
this.isLoading = true;
await loadUsers();
});
} you don't want to do this. The abstraction manages its own state. So, maybe I should write the following lint rules? could probably save a bunch of folks some time:
|
Thank you! That all makes sense to me, and those lint rules sound great too. 😄 Curious: what do you think about that confusion for tasks? e.g. when someone writes this: {{#if this.loadUsersTask.isRunning}}
Loading…
{{else}}
{{#each this.users as |user|}}
{{user}}
{{/each}}
{{/if}} … and I have a component like so:
… this throws a similar error. This feels more confusing because we're not changing |
Hm, I just realised that not accessing the task directly for {{#if this.users.isRunning}}
Loading…
{{else}}
{{#each this.users as |user|}}
{{user}}
{{/each}}
{{/if}} I think I understand why that works too. 🤔 |
Hi there! I have some code like this (simplified):
… and used in a template like this:
This throws a you-cannot-update-state-that-you-already-read error:
It makes sense to me given what I understand about ember's rendering: I've read
isLoading
in theif
and then thetrackedFunction
is trying to update it. Apparently this style of code does not work with tasks (e.g.trackedTask
either) — it results in a similar error when using the task'sisRunning
state.While the error makes sense, it also seems confusing. I can think of a few approaches to handle this, e.g. a utility like a
RemoteData
that can wrap loading state inside the resource itself, so that theisLoading
state isn't mutated in the same cycle, only after the promise resolves, e.g. (also simplified)That said, I'm wondering if this is known / if we're doing something wrong, and if there are any other possible approaches to this. Thank you! 😄
The text was updated successfully, but these errors were encountered: