Skip to content

Commit fd75a3d

Browse files
committed
refactor: review
1 parent 5e74248 commit fd75a3d

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

packages/nuxt/playground/pages/usage-after-await.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts" setup>
2-
const useFancyCounter = async () => {
2+
async function useFancyCounter() {
33
await new Promise((resolve) => setTimeout(resolve, 0))
44
55
// ❌ bad usage: the use of a store after an await could lead to using the wrong pinia instance.
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
import { useNuxtApp } from '#app'
2-
import type { Pinia } from 'pinia'
2+
import {
3+
defineStore as _defineStore,
4+
type Pinia,
5+
type StoreGeneric,
6+
} from 'pinia'
37
export * from 'pinia'
48

5-
export const usePinia = () => useNuxtApp().$pinia as Pinia
9+
export const usePinia = () => useNuxtApp().$pinia as Pinia | undefined
10+
11+
export const defineStore: typeof _defineStore =
12+
process.env.NODE_ENV === 'production' && !__TEST__
13+
? _defineStore
14+
: (((
15+
...args: Parameters<typeof _defineStore>
16+
): ReturnType<typeof _defineStore> => {
17+
if (!import.meta.server) {
18+
return _defineStore(...args)
19+
}
20+
21+
const originalUseStore = _defineStore(...args)
22+
function useStore(
23+
pinia?: Pinia | null,
24+
hot?: StoreGeneric
25+
): StoreGeneric {
26+
return originalUseStore(pinia || usePinia(), hot)
27+
}
28+
29+
useStore.$id = originalUseStore.$id
30+
useStore._pinia = originalUseStore._pinia
31+
32+
return useStore
33+
}) as typeof _defineStore)

0 commit comments

Comments
 (0)