File tree Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 11import { useNuxtApp } from '#app'
2- import type { Pinia } from 'pinia'
2+ import {
3+ defineStore as _defineStore ,
4+ type Pinia ,
5+ type StoreGeneric ,
6+ } from 'pinia'
37export * 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 )
You can’t perform that action at this time.
0 commit comments