-
-
Notifications
You must be signed in to change notification settings - Fork 975
/
counter.ts
68 lines (57 loc) 路 1.37 KB
/
counter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { acceptHMRUpdate, defineStore } from '../../../src'
const delay = (t: number) => new Promise((r) => setTimeout(r, t))
export const useCounter = defineStore({
id: 'counter',
state: () => ({
n: 2,
incrementedTimes: 0,
decrementedTimes: 0,
numbers: [] as number[],
}),
getters: {
double: (state) => state.n * 2,
},
actions: {
increment(amount = 1) {
if (typeof amount !== 'number') {
amount = 1
}
this.incrementedTimes++
this.n += amount
},
changeMe() {
console.log('change me to test HMR')
},
async fail() {
const n = this.n
await delay(1000)
this.numbers.push(n)
await delay(1000)
if (this.n !== n) {
throw new Error('Someone changed n!')
}
return n
},
async decrementToZero(interval: number = 300, usePatch = true) {
if (this.n <= 0) return
while (this.n > 0) {
if (usePatch) {
this.$patch({
n: this.n - 1,
decrementedTimes: this.decrementedTimes + 1,
})
// this.$patch(state => {
// state.n--
// state.decrementedTimes++
// })
} else {
this.n -= 1
}
await delay(interval)
}
},
},
})
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useCounter, import.meta.hot))
}