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

counter.ts

30 lines713 Bytes

1import { DevtoolsEventClient } from './devtools/eventClient'
2import { signal } from '@angular/core'
3​
4export function createCounter() {
5 const count = signal(0)
6 const history: Array<number> = []
7​
8 return {
9 value: () => count(),
10 increment: () => {
11 count.update((n) => n + 1)
12 history.push(count())
13​
14 // The emit eventSuffix must match that of the EventMap defined in eventClient.ts
15 DevtoolsEventClient.emit('counter-state', {
16 count: count(),
17 history,
18 })
19 },
20 decrement: () => {
21 count.update((n) => n - 1)
22 history.push(count())
23​
24 DevtoolsEventClient.emit('counter-state', {
25 count: count(),
26 history,
27 })
28 },
29 }