TanStack / query

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
https://tanstack.com/query
MIT License
40.1k stars 2.69k forks source link

fix(angular-query-experimental): run mutation callback in injection context #7360

Open ShacharHarshuv opened 2 weeks ago

ShacharHarshuv commented 2 weeks ago

Previously, the injectMutation callback ran in injection context only if accessed in the same context as the component initialization (i.e. before the effect callback run). By wrapping the effect callback in injection context, it is ensured that the callback will always run in injection context, and any inject calls will not fail.

Note there is a separate issue here, and it's that the callback is always run twice - once synchronously when initialization the mutation (when you create the MutationObserver), and secondly as the first callback of the effect. This is something that can potentially be improved to prevent redundant code run.

Another related issue - I would argue that if any signal dependencies of the mutation changed, you don't necessarily want to immediately run the callback again. You probably want to be "lazy" and run it again only when the mutation object is accessed. With the current implementation, that callback might unnecessarily run when there are a lot of dependency changes, but no mutation is used. (For example, imagine there is an "opened todo item" state, and a delete button on it. The user might switched the opened todo item state frequently without actually using the delete mutation).

No breaking changes, any code that worked before should still work.

vercel[bot] commented 2 weeks ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **query** | ⬜️ Ignored ([Inspect](https://vercel.com/tanstack/query/8GnFSG4gUvA4JFXqgZyzPeC7MU7y)) | [Visit Preview](https://query-git-fork-shacharharshuv-inject-mutation-c-1bd7f6-tanstack.vercel.app) | | May 8, 2024 6:08pm |
nx-cloud[bot] commented 2 weeks ago

☁️ Nx Cloud Report

CI is running/has finished running commands for commit f34b8118bdc250026bd460dece4c1e1406cc2649. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


🟥 Failed Commands
nx affected --targets=test:format,test:sherif,test:knip,test:eslint,test:lib,test:types,test:build,build,test:attw --parallel=3

Sent with 💌 from NxCloud.

codesandbox-ci[bot] commented 2 weeks ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f34b8118bdc250026bd460dece4c1e1406cc2649:

Sandbox Source
@tanstack/query-example-angular-basic Configuration
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration
ShacharHarshuv commented 1 week ago

Resolved conflicts. @arnoud-dv can you review?

ShacharHarshuv commented 1 week ago

The tests failures don't seem to be related to my change, as they fail in the react adapter.