snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
10
examples/angular/optimistic-updates/src/app/app.component.ts
Normal file
10
examples/angular/optimistic-updates/src/app/app.component.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { ChangeDetectionStrategy, Component } from '@angular/core'
|
||||
import { OptimisticUpdatesComponent } from './components/optimistic-updates.component'
|
||||
|
||||
@Component({
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
selector: 'app-root',
|
||||
template: `<optimistic-updates />`,
|
||||
imports: [OptimisticUpdatesComponent],
|
||||
})
|
||||
export class AppComponent {}
|
||||
28
examples/angular/optimistic-updates/src/app/app.config.ts
Normal file
28
examples/angular/optimistic-updates/src/app/app.config.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import {
|
||||
provideHttpClient,
|
||||
withFetch,
|
||||
withInterceptors,
|
||||
} from '@angular/common/http'
|
||||
import {
|
||||
QueryClient,
|
||||
provideTanStackQuery,
|
||||
} from '@tanstack/angular-query-experimental'
|
||||
import { withDevtools } from '@tanstack/angular-query-experimental/devtools'
|
||||
import { mockInterceptor } from './interceptor/mock-api.interceptor'
|
||||
import type { ApplicationConfig } from '@angular/core'
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideHttpClient(withFetch(), withInterceptors([mockInterceptor])),
|
||||
provideTanStackQuery(
|
||||
new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
gcTime: 1000 * 60 * 60 * 24, // 24 hours
|
||||
},
|
||||
},
|
||||
}),
|
||||
withDevtools(),
|
||||
),
|
||||
],
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
|
||||
import {
|
||||
injectMutation,
|
||||
injectQuery,
|
||||
} from '@tanstack/angular-query-experimental'
|
||||
import { FormsModule } from '@angular/forms'
|
||||
import { DatePipe } from '@angular/common'
|
||||
import { TasksService } from '../services/tasks.service'
|
||||
|
||||
@Component({
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
selector: 'optimistic-updates',
|
||||
imports: [FormsModule, DatePipe],
|
||||
template: `
|
||||
<p>
|
||||
In this example, new items can be created using a mutation. The new item
|
||||
will be optimistically added to the list in hopes that the server accepts
|
||||
the item. If it does, the list is refetched with the true items from the
|
||||
list. Every now and then, the mutation may fail though. When that happens,
|
||||
the previous list of items is restored and the list is again refetched
|
||||
from the server.
|
||||
</p>
|
||||
|
||||
<hr />
|
||||
@if (tasks.isLoading()) {
|
||||
<p>Loading...</p>
|
||||
}
|
||||
|
||||
<div class="container">
|
||||
<label>
|
||||
<input type="checkbox" [(ngModel)]="failMutation" />
|
||||
Fail Mutation
|
||||
</label>
|
||||
|
||||
<div class="input-container">
|
||||
<input type="text" [(ngModel)]="newItem" placeholder="Enter text" />
|
||||
<button (click)="addItem()">Create</button>
|
||||
<ul>
|
||||
@for (task of tasks.data(); track task) {
|
||||
<li>{{ task }}</li>
|
||||
}
|
||||
</ul>
|
||||
|
||||
<div>
|
||||
Updated At: {{ tasks.dataUpdatedAt() | date: 'MMMM d, h:mm:ss a ' }}
|
||||
</div>
|
||||
</div>
|
||||
@if (!tasks.isLoading() && tasks.isFetching()) {
|
||||
<p>Fetching in background</p>
|
||||
}
|
||||
</div>
|
||||
`,
|
||||
})
|
||||
export class OptimisticUpdatesComponent {
|
||||
#tasksService = inject(TasksService)
|
||||
|
||||
tasks = injectQuery(() => this.#tasksService.allTasks())
|
||||
clearMutation = injectMutation(() => this.#tasksService.addTask())
|
||||
addMutation = injectMutation(() => this.#tasksService.addTask())
|
||||
|
||||
newItem = ''
|
||||
failMutation = false
|
||||
|
||||
addItem() {
|
||||
if (!this.newItem) return
|
||||
|
||||
this.addMutation.mutate({
|
||||
task: this.newItem,
|
||||
failMutation: this.failMutation,
|
||||
})
|
||||
this.newItem = ''
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* MockApiInterceptor is used to simulate API responses for `/api/tasks` endpoints.
|
||||
* It handles the following operations:
|
||||
* - GET: Fetches all tasks from sessionStorage.
|
||||
* - POST: Adds a new task to sessionStorage.
|
||||
* Simulated responses include a delay to mimic network latency.
|
||||
*/
|
||||
import { HttpResponse } from '@angular/common/http'
|
||||
import { delay, of } from 'rxjs'
|
||||
import type {
|
||||
HttpEvent,
|
||||
HttpHandlerFn,
|
||||
HttpInterceptorFn,
|
||||
HttpRequest,
|
||||
} from '@angular/common/http'
|
||||
import type { Observable } from 'rxjs'
|
||||
|
||||
export const mockInterceptor: HttpInterceptorFn = (
|
||||
req: HttpRequest<unknown>,
|
||||
next: HttpHandlerFn,
|
||||
): Observable<HttpEvent<any>> => {
|
||||
const respondWith = (status: number, body: any) =>
|
||||
of(new HttpResponse({ status, body })).pipe(delay(1000))
|
||||
if (req.url === '/api/tasks') {
|
||||
switch (req.method) {
|
||||
case 'GET':
|
||||
return respondWith(
|
||||
200,
|
||||
JSON.parse(
|
||||
sessionStorage.getItem('optimistic-updates-tasks') || '[]',
|
||||
),
|
||||
)
|
||||
case 'POST':
|
||||
const tasks = JSON.parse(
|
||||
sessionStorage.getItem('optimistic-updates-tasks') || '[]',
|
||||
)
|
||||
tasks.push(req.body)
|
||||
sessionStorage.setItem(
|
||||
'optimistic-updates-tasks',
|
||||
JSON.stringify(tasks),
|
||||
)
|
||||
return respondWith(201, {
|
||||
status: 'success',
|
||||
task: req.body,
|
||||
})
|
||||
}
|
||||
}
|
||||
if (req.url === '/api/tasks-wrong-url') {
|
||||
return respondWith(500, {
|
||||
status: 'error',
|
||||
})
|
||||
}
|
||||
|
||||
return next(req)
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import { HttpClient } from '@angular/common/http'
|
||||
import { Injectable, inject } from '@angular/core'
|
||||
import {
|
||||
QueryClient,
|
||||
mutationOptions,
|
||||
queryOptions,
|
||||
} from '@tanstack/angular-query-experimental'
|
||||
|
||||
import { lastValueFrom } from 'rxjs'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class TasksService {
|
||||
#queryClient = inject(QueryClient) // Manages query state and caching
|
||||
#http = inject(HttpClient) // Handles HTTP requests
|
||||
|
||||
/**
|
||||
* Fetches all tasks from the API.
|
||||
* Returns an observable containing an array of task strings.
|
||||
*/
|
||||
allTasks = () =>
|
||||
queryOptions({
|
||||
queryKey: ['tasks'],
|
||||
queryFn: () => {
|
||||
return lastValueFrom(this.#http.get<Array<string>>('/api/tasks'))
|
||||
},
|
||||
})
|
||||
|
||||
/**
|
||||
* Creates a mutation for adding a task.
|
||||
* On success, invalidates and refetches the "tasks" query cache to update the task list.
|
||||
*/
|
||||
addTask() {
|
||||
return mutationOptions({
|
||||
mutationFn: ({
|
||||
task,
|
||||
failMutation = false,
|
||||
}: {
|
||||
task: string
|
||||
failMutation: boolean
|
||||
}) =>
|
||||
lastValueFrom(
|
||||
this.#http.post(
|
||||
`/api/tasks${failMutation ? '-wrong-url' : ''}`,
|
||||
task,
|
||||
),
|
||||
),
|
||||
mutationKey: ['tasks'],
|
||||
onSuccess: () => {
|
||||
this.#queryClient.invalidateQueries({ queryKey: ['tasks'] })
|
||||
},
|
||||
onMutate: async ({ task }) => {
|
||||
// Cancel any outgoing refetches
|
||||
// (so they don't overwrite our optimistic update)
|
||||
await this.#queryClient.cancelQueries({ queryKey: ['tasks'] })
|
||||
|
||||
// Snapshot the previous value
|
||||
const previousTodos = this.#queryClient.getQueryData<Array<string>>([
|
||||
'tasks',
|
||||
])
|
||||
|
||||
// Optimistically update to the new value
|
||||
if (previousTodos) {
|
||||
this.#queryClient.setQueryData<Array<string>>(
|
||||
['tasks'],
|
||||
[...previousTodos, task],
|
||||
)
|
||||
}
|
||||
|
||||
return previousTodos
|
||||
},
|
||||
onError: (err, variables, context) => {
|
||||
if (context) {
|
||||
this.#queryClient.setQueryData<Array<string>>(['tasks'], context)
|
||||
}
|
||||
},
|
||||
// Always refetch after error or success:
|
||||
onSettled: () => {
|
||||
this.#queryClient.invalidateQueries({ queryKey: ['tasks'] })
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
BIN
examples/angular/optimistic-updates/src/favicon.ico
Normal file
BIN
examples/angular/optimistic-updates/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
13
examples/angular/optimistic-updates/src/index.html
Normal file
13
examples/angular/optimistic-updates/src/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>TanStack Query Angular Optimistic Updates Example</title>
|
||||
<base href="/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico" />
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
5
examples/angular/optimistic-updates/src/main.ts
Normal file
5
examples/angular/optimistic-updates/src/main.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser'
|
||||
import { appConfig } from './app/app.config'
|
||||
import { AppComponent } from './app/app.component'
|
||||
|
||||
bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err))
|
||||
Reference in New Issue
Block a user