-
-
Couldn't load subscription status.
- Fork 3.5k
feat(angular-query): add injectMutationState #6621
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/angular-query-experimental/src/__tests__/inject-mutation-state.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { signal } from '@angular/core' | ||
| import { QueryClient } from '@tanstack/query-core' | ||
| import { TestBed } from '@angular/core/testing' | ||
| import { describe, expect, test, vi } from 'vitest' | ||
| import { injectMutation } from '../inject-mutation' | ||
| import { injectMutationState } from '../inject-mutation-state' | ||
| import { provideAngularQuery } from '../providers' | ||
| import { successMutator } from './test-utils' | ||
|
|
||
| describe('injectMutationState', () => { | ||
| let queryClient: QueryClient | ||
|
|
||
| beforeEach(() => { | ||
| queryClient = new QueryClient() | ||
| vi.useFakeTimers() | ||
| TestBed.configureTestingModule({ | ||
| providers: [provideAngularQuery(queryClient)], | ||
| }) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| describe('injectMutationState', () => { | ||
| test('should return variables after calling mutate', async () => { | ||
| const mutationKey = ['mutation'] | ||
| const variables = 'foo123' | ||
|
|
||
| const mutation = TestBed.runInInjectionContext(() => { | ||
| return injectMutation(() => ({ | ||
| mutationKey: mutationKey, | ||
| mutationFn: (params: string) => successMutator(params), | ||
| })) | ||
| }) | ||
|
|
||
| mutation.mutate(variables) | ||
|
|
||
| const mutationState = TestBed.runInInjectionContext(() => { | ||
| return injectMutationState(() => ({ | ||
| filters: { mutationKey, status: 'pending' }, | ||
| select: (m) => m.state.variables, | ||
| })) | ||
| }) | ||
|
|
||
| expect(mutationState()).toEqual([variables]) | ||
| }) | ||
|
|
||
| test('reactive options should update injectMutationState', async () => { | ||
| const mutationKey1 = ['mutation1'] | ||
| const mutationKey2 = ['mutation2'] | ||
| const variables1 = 'foo123' | ||
| const variables2 = 'bar234' | ||
|
|
||
| const [mutation1, mutation2] = TestBed.runInInjectionContext(() => { | ||
| return [ | ||
| injectMutation(() => ({ | ||
| mutationKey: mutationKey1, | ||
| mutationFn: (params: string) => successMutator(params), | ||
| })), | ||
| injectMutation(() => ({ | ||
| mutationKey: mutationKey2, | ||
| mutationFn: (params: string) => successMutator(params), | ||
| })), | ||
| ] | ||
| }) | ||
|
|
||
| mutation1.mutate(variables1) | ||
| mutation2.mutate(variables2) | ||
|
|
||
| const filterKey = signal(mutationKey1) | ||
|
|
||
| const mutationState = TestBed.runInInjectionContext(() => { | ||
| return injectMutationState(() => ({ | ||
| filters: { mutationKey: filterKey(), status: 'pending' }, | ||
| select: (m) => m.state.variables, | ||
| })) | ||
| }) | ||
|
|
||
| expect(mutationState()).toEqual([variables1]) | ||
|
|
||
| filterKey.set(mutationKey2) | ||
| TestBed.flushEffects() | ||
| expect(mutationState()).toEqual([variables2]) | ||
| }) | ||
|
|
||
| test('should return variables after calling mutate', async () => { | ||
| queryClient.clear() | ||
| const mutationKey = ['mutation'] | ||
| const variables = 'bar234' | ||
|
|
||
| const mutation = TestBed.runInInjectionContext(() => { | ||
| return injectMutation(() => ({ | ||
| mutationKey: mutationKey, | ||
| mutationFn: (params: string) => successMutator(params), | ||
| })) | ||
| }) | ||
|
|
||
| mutation.mutate(variables) | ||
|
|
||
| const mutationState = TestBed.runInInjectionContext(() => { | ||
| return injectMutationState() | ||
| }) | ||
|
|
||
| expect(mutationState()[0]?.variables).toEqual(variables) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/angular-query-experimental/src/inject-mutation-state.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { DestroyRef, effect, inject, signal } from '@angular/core' | ||
| import { | ||
| type DefaultError, | ||
| type Mutation, | ||
| type MutationCache, | ||
| type MutationFilters, | ||
| type MutationState, | ||
| notifyManager, | ||
| replaceEqualDeep, | ||
| } from '@tanstack/query-core' | ||
| import { assertInjector } from './util/assert-injector/assert-injector' | ||
| import { injectQueryClient } from './inject-query-client' | ||
| import type { Injector, Signal } from '@angular/core' | ||
|
|
||
| type MutationStateOptions<TResult = MutationState> = { | ||
| filters?: MutationFilters | ||
| select?: ( | ||
| mutation: Mutation<unknown, DefaultError, unknown, unknown>, | ||
| ) => TResult | ||
| } | ||
|
|
||
| function getResult<TResult = MutationState>( | ||
| mutationCache: MutationCache, | ||
| options: MutationStateOptions<TResult>, | ||
| ): Array<TResult> { | ||
| return mutationCache | ||
| .findAll(options.filters) | ||
| .map( | ||
| (mutation): TResult => | ||
| (options.select | ||
| ? options.select( | ||
| mutation as Mutation<unknown, DefaultError, unknown, unknown>, | ||
| ) | ||
| : mutation.state) as TResult, | ||
| ) | ||
| } | ||
|
|
||
| export function injectMutationState<TResult = MutationState>( | ||
| options: () => MutationStateOptions<TResult> = () => ({}), | ||
| injector?: Injector, | ||
| ): Signal<Array<TResult>> { | ||
| return assertInjector(injectMutationState, injector, () => { | ||
| const destroyRef = inject(DestroyRef) | ||
| const queryClient = injectQueryClient() | ||
|
|
||
| const mutationCache = queryClient.getMutationCache() | ||
|
|
||
| const result = signal<Array<TResult>>(getResult(mutationCache, options())) | ||
|
|
||
| effect( | ||
| () => { | ||
| // Setting the signal from an effect because it's both 'computed' from options() | ||
| // and needs to be set imperatively in the mutationCache listener. | ||
| result.set(getResult(mutationCache, options())) | ||
| }, | ||
| { | ||
| allowSignalWrites: true, | ||
| }, | ||
| ) | ||
|
|
||
| const unsubscribe = mutationCache.subscribe( | ||
| notifyManager.batchCalls(() => { | ||
| const nextResult = replaceEqualDeep( | ||
| result(), | ||
| getResult(mutationCache, options()), | ||
| ) | ||
| if (result() !== nextResult) { | ||
| result.set(nextResult) | ||
| } | ||
| }), | ||
| ) | ||
|
|
||
| destroyRef.onDestroy(unsubscribe) | ||
|
|
||
| return result | ||
| }) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the suggested change for this reason? https://twitter.com/GergelySzerovay/status/1754915190213144949
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. More here: angular/angular#53986