Skip to content

Commit 751f286

Browse files
authored
refactor: prefix store properties with $ (#254)
BREAKING CHANGE: all store properties (`id`, `state`, `patch`, `subscribe`, and `reset`) are now prefixed with `$` to allow properties defined with the same type and avoid types breaking. Tip: you can refactor your whole codebase with F2 (or right-click + Refactor) on each of the store's properties
1 parent 7c1899f commit 751f286

File tree

14 files changed

+154
-108
lines changed

14 files changed

+154
-108
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,16 @@ To mutate the state you can either directly change something:
237237
main.counter++
238238
```
239239

240-
or call the method `patch` that allows you apply multiple changes at the same time with a partial `state` object:
240+
or call the method `$patch` that allows you apply multiple changes at the same time with a partial `state` object:
241241

242242
```ts
243-
main.patch({
243+
main.$patch({
244244
counter: -1,
245245
name: 'Abalam',
246246
})
247247
```
248248

249-
The main difference here is that `patch` allows you to group multiple changes into one single entry in the devtools.
249+
The main difference here is that `$patch` allows you to group multiple changes into one single entry in the devtools.
250250

251251
### Replacing the `state`
252252

__tests__/actions.spec.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Actions', () => {
3030
},
3131

3232
setFoo(foo: string) {
33-
this.patch({ nested: { foo } })
33+
this.$patch({ nested: { foo } })
3434
},
3535

3636
combined() {
@@ -52,34 +52,34 @@ describe('Actions', () => {
5252
actions: {
5353
swap() {
5454
const bStore = useB()
55-
const b = bStore.state.b
56-
bStore.state.b = this.state.a
57-
this.state.a = b
55+
const b = bStore.$state.b
56+
bStore.$state.b = this.$state.a
57+
this.$state.a = b
5858
},
5959
},
6060
})
6161

6262
it('can use the store as this', () => {
6363
const store = useStore()
64-
expect(store.state.a).toBe(true)
64+
expect(store.$state.a).toBe(true)
6565
store.toggle()
66-
expect(store.state.a).toBe(false)
66+
expect(store.$state.a).toBe(false)
6767
})
6868

6969
it('store is forced as the context', () => {
7070
const store = useStore()
71-
expect(store.state.a).toBe(true)
71+
expect(store.$state.a).toBe(true)
7272
store.toggle.call(null)
73-
expect(store.state.a).toBe(false)
73+
expect(store.$state.a).toBe(false)
7474
})
7575

7676
it('can call other actions', () => {
7777
const store = useStore()
78-
expect(store.state.a).toBe(true)
79-
expect(store.state.nested.foo).toBe('foo')
78+
expect(store.$state.a).toBe(true)
79+
expect(store.$state.nested.foo).toBe('foo')
8080
store.combined()
81-
expect(store.state.a).toBe(false)
82-
expect(store.state.nested.foo).toBe('bar')
81+
expect(store.$state.a).toBe(false)
82+
expect(store.$state.nested.foo).toBe('bar')
8383
})
8484

8585
it('supports being called between requests', () => {
@@ -91,12 +91,12 @@ describe('Actions', () => {
9191
// simulate a different request
9292
setActiveReq(req2)
9393
const bStore = useB()
94-
bStore.state.b = 'c'
94+
bStore.$state.b = 'c'
9595

9696
aStore.swap()
97-
expect(aStore.state.a).toBe('b')
97+
expect(aStore.$state.a).toBe('b')
9898
// a different instance of b store was used
99-
expect(bStore.state.b).toBe('c')
99+
expect(bStore.$state.b).toBe('c')
100100
})
101101

102102
it('can force the req', () => {
@@ -105,13 +105,13 @@ describe('Actions', () => {
105105
const aStore = useA(req1)
106106

107107
let bStore = useB(req2)
108-
bStore.state.b = 'c'
108+
bStore.$state.b = 'c'
109109

110110
aStore.swap()
111-
expect(aStore.state.a).toBe('b')
111+
expect(aStore.$state.a).toBe('b')
112112
// a different instance of b store was used
113-
expect(bStore.state.b).toBe('c')
113+
expect(bStore.$state.b).toBe('c')
114114
bStore = useB(req1)
115-
expect(bStore.state.b).toBe('a')
115+
expect(bStore.$state.b).toBe('a')
116116
})
117117
})

__tests__/pinia/stores/cart.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useUserStore } from './user'
44
export const useCartStore = defineStore({
55
id: 'cart',
66
state: () => ({
7+
id: 2,
78
rawItems: [] as string[],
89
}),
910
getters: {
@@ -28,7 +29,7 @@ export const useCartStore = defineStore({
2829

2930
removeItem(name: string) {
3031
const i = this.rawItems.indexOf(name)
31-
if (i > -1) this.rawItems.splice(i, 1)
32+
if (i > -1) this.$state.rawItems.splice(i, 1)
3233
},
3334

3435
// TODO: use multiple stores
@@ -39,7 +40,7 @@ export const useCartStore = defineStore({
3940

4041
// console.log('Purchasing', this.items)
4142
const n = this.items.length
42-
this.state.rawItems = []
43+
this.rawItems = []
4344

4445
return { amount: n, user: user.name }
4546
},
@@ -50,23 +51,23 @@ export type CartStore = ReturnType<typeof useCartStore>
5051

5152
export function addItem(name: string) {
5253
const store = useCartStore()
53-
store.state.rawItems.push(name)
54+
store.rawItems.push(name)
5455
}
5556

5657
export function removeItem(name: string) {
5758
const store = useCartStore()
58-
const i = store.state.rawItems.indexOf(name)
59-
if (i > -1) store.state.rawItems.splice(i, 1)
59+
const i = store.rawItems.indexOf(name)
60+
if (i > -1) store.rawItems.splice(i, 1)
6061
}
6162

6263
export async function purchaseItems() {
6364
const cart = useCartStore()
6465
const user = useUserStore()
65-
if (!user.state.name) return
66+
if (!user.name) return
6667

6768
console.log('Purchasing', cart.items)
6869
const n = cart.items.length
69-
cart.state.rawItems = []
70+
cart.rawItems = []
7071

7172
return n
7273
}

__tests__/pinia/stores/user.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const useUserStore = defineStore({
1515
async login(user: string, password: string) {
1616
const userData = await apiLogin(user, password)
1717

18-
this.patch({
18+
this.$patch({
1919
name: user,
2020
...userData,
2121
})
@@ -24,7 +24,7 @@ export const useUserStore = defineStore({
2424
logout() {
2525
this.login('a', 'b').then(() => {})
2626

27-
this.patch({
27+
this.$patch({
2828
name: '',
2929
isAdmin: false,
3030
})
@@ -46,7 +46,7 @@ export function logout() {
4646

4747
store.login('e', 'e').then(() => {})
4848

49-
store.patch({
49+
store.$patch({
5050
name: '',
5151
isAdmin: false,
5252
})

__tests__/ssr.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* @jest-environment node
33
*/
44
import { createPinia } from '../src'
5-
import { useCartStore } from './pinia/stores/cart'
65
import { createSSRApp, inject } from 'vue'
76
import { renderToString, ssrInterpolate } from '@vue/server-renderer'
87
import { useUserStore } from './pinia/stores/user'
8+
import { useCartStore } from './pinia/stores/cart'
99

1010
describe('SSR', () => {
1111
const App = {

__tests__/store.patch.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineStore, setActiveReq } from '../src'
22

3-
describe('store.patch', () => {
3+
describe('store.$patch', () => {
44
const useStore = () => {
55
// create a new store
66
setActiveReq({})
@@ -18,8 +18,8 @@ describe('store.patch', () => {
1818

1919
it('patches a property without touching the rest', () => {
2020
const store = useStore()
21-
store.patch({ a: false })
22-
expect(store.state).toEqual({
21+
store.$patch({ a: false })
22+
expect(store.$state).toEqual({
2323
a: false,
2424
nested: {
2525
foo: 'foo',
@@ -30,16 +30,16 @@ describe('store.patch', () => {
3030

3131
it('patches a nested property without touching the rest', () => {
3232
const store = useStore()
33-
store.patch({ nested: { foo: 'bar' } })
34-
expect(store.state).toEqual({
33+
store.$patch({ nested: { foo: 'bar' } })
34+
expect(store.$state).toEqual({
3535
a: true,
3636
nested: {
3737
foo: 'bar',
3838
a: { b: 'string' },
3939
},
4040
})
41-
store.patch({ nested: { a: { b: 'hello' } } })
42-
expect(store.state).toEqual({
41+
store.$patch({ nested: { a: { b: 'hello' } } })
42+
expect(store.$state).toEqual({
4343
a: true,
4444
nested: {
4545
foo: 'bar',
@@ -50,8 +50,8 @@ describe('store.patch', () => {
5050

5151
it('patches multiple properties at the same time', () => {
5252
const store = useStore()
53-
store.patch({ a: false, nested: { foo: 'hello' } })
54-
expect(store.state).toEqual({
53+
store.$patch({ a: false, nested: { foo: 'hello' } })
54+
expect(store.$state).toEqual({
5555
a: false,
5656
nested: {
5757
foo: 'hello',

0 commit comments

Comments
 (0)