Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

auto-refetching.component.ts

49 lines1.2 KB

1import {
2 ChangeDetectionStrategy,
3 Component,
4 inject,
5 signal,
6} from '@angular/core'
7import {
8 injectMutation,
9 injectQuery,
10} from '@tanstack/angular-query-experimental'
11import { NgStyle } from '@angular/common'
12import { TasksService } from '../services/tasks.service'
13​
14@Component({
15 changeDetection: ChangeDetectionStrategy.OnPush,
16 selector: 'auto-refetching-example',
17 templateUrl: './auto-refetching.component.html',
18 imports: [NgStyle],
19})
20export class AutoRefetchingExampleComponent {
21 readonly #tasksService = inject(TasksService)
22​
23 readonly intervalMs = signal(1000)
24​
25 readonly tasks = injectQuery(() =>
26 this.#tasksService.allTasks(this.intervalMs()),
27 )
28​
29 readonly addMutation = injectMutation(() => this.#tasksService.addTask())
30 readonly clearMutation = injectMutation(() =>
31 this.#tasksService.clearAllTasks(),
32 )
33​
34 clearTasks() {
35 this.clearMutation.mutate()
36 }
37​
38 inputChange($event: Event) {
39 const target = $event.target as HTMLInputElement
40 this.intervalMs.set(Number(target.value))
41 }
42​
43 addItem($event: Event) {
44 const target = $event.target as HTMLInputElement
45 const value = target.value
46 this.addMutation.mutate(value)
47 target.value = ''
48 }