state: () => ({
current: null as null | Joke,
jokes: [] as Joke[],
+ // hello: true,
}),
actions: {
async fetchJoke() {
this.jokes.push(this.current)
}
- this.current = await getRandomJoke()
+ this.$patch({ current: await getRandomJoke() })
+ // this.current = await getRandomJoke()
},
},
})
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useJokes, import.meta.hot))
- import.meta.hot.accept(acceptHMRUpdate(useJokesSetup, import.meta.hot))
+ // import.meta.hot.accept(acceptHMRUpdate(useJokesSetup, import.meta.hot))
}
-import { isRef, isReactive } from 'vue-demi'
+import { isRef, isReactive, isVue2, set } from 'vue-demi'
import { Pinia } from './rootStore'
import { isPlainObject, StoreDefinition, StoreGeneric, _Method } from './types'
} else {
// objects are either a bit more complex (e.g. refs) or primitives, so we
// just set the whole thing
- newState[key] = subPatch
+ if (isVue2) {
+ set(newState, key, subPatch)
+ } else {
+ newState[key] = subPatch
+ }
}
}
function setup() {
if (!initialState && (!__DEV__ || !hot)) {
- // only use set in Vue 2 if it's not for HMR
- if (isVue2 && (!__DEV__ || !id.startsWith('__hot'))) {
+ if (isVue2) {
set(pinia.state.value, id, state ? state() : {})
} else {
pinia.state.value[id] = state ? state() : {}
// avoid creating a state in pinia.state.value
const localState =
__DEV__ && hot
- ? toRefs(ref(state ? state() : {}).value)
+ ? // use ref() to unwrap refs inside state TODO: check if this is still necessary
+ toRefs(ref(state ? state() : {}).value)
: initialState || toRefs(pinia.state.value[id])
return assign(
const initialState = pinia.state.value[$id] as UnwrapRef<S> | undefined
if (!initialState && __DEV__ && !hot) {
- // only use set in Vue 2 if it's not for HMR
- if (isVue2 && (!__DEV__ || !$id.startsWith('__hot'))) {
+ if (isVue2) {
set(pinia.state.value, $id, {})
} else {
pinia.state.value[$id] = {}
if ((isRef(prop) && !isComputed(prop)) || isReactive(prop)) {
// mark it as a piece of state to be serialized
if (__DEV__ && hot) {
- hotState.value[key] = toRef(setupStore as any, key)
- // createOptionStore already did this
+ set(hotState.value, key, toRef(setupStore as any, key))
+ // createOptionStore directly sets the state in pinia.state.value so we
+ // can just skip that
} else if (!buildState) {
if (isVue2) {
set(pinia.state.value[$id], key, prop)
// action
} else if (typeof prop === 'function') {
// @ts-expect-error: we are overriding the function we avoid wrapping if
+ const actionValue = __DEV__ && hot ? prop : wrapAction(key, prop)
// this a hot module replacement store because the hotUpdate method needs
// to do it with the right context
- setupStore[key] = __DEV__ && hot ? prop : wrapAction(key, prop)
+ if (isVue2) {
+ set(setupStore, key, actionValue)
+ } else {
+ // @ts-expect-error
+ setupStore[key] = actionValue
+ }
if (__DEV__) {
_hmrPayload.actions[key] = prop