From 8e811775bb705bee9c15b1d9d97b88f4c78a278a Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 4 Aug 2021 08:20:30 +0200 Subject: [PATCH] chore: more isVue2 --- playground/src/stores/jokes.ts | 6 ++++-- src/hmr.ts | 8 ++++++-- src/store.ts | 22 ++++++++++++++-------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/playground/src/stores/jokes.ts b/playground/src/stores/jokes.ts index a1911c1f..f249a5f7 100644 --- a/playground/src/stores/jokes.ts +++ b/playground/src/stores/jokes.ts @@ -6,6 +6,7 @@ export const useJokes = defineStore('jokes', { state: () => ({ current: null as null | Joke, jokes: [] as Joke[], + // hello: true, }), actions: { async fetchJoke() { @@ -17,7 +18,8 @@ export const useJokes = defineStore('jokes', { this.jokes.push(this.current) } - this.current = await getRandomJoke() + this.$patch({ current: await getRandomJoke() }) + // this.current = await getRandomJoke() }, }, }) @@ -45,5 +47,5 @@ export const useJokesSetup = defineStore('jokes-setup', () => { 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)) } diff --git a/src/hmr.ts b/src/hmr.ts index f6cdf536..2ea6fa71 100644 --- a/src/hmr.ts +++ b/src/hmr.ts @@ -1,4 +1,4 @@ -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' @@ -45,7 +45,11 @@ export function patchObject( } 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 + } } } diff --git a/src/store.ts b/src/store.ts index c4cdcf04..f2d4dd51 100644 --- a/src/store.ts +++ b/src/store.ts @@ -105,8 +105,7 @@ function createOptionsStore< 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() : {} @@ -116,7 +115,8 @@ function createOptionsStore< // 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( @@ -205,8 +205,7 @@ function createSetupStore< const initialState = pinia.state.value[$id] as UnwrapRef | 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] = {} @@ -369,8 +368,9 @@ function createSetupStore< 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) @@ -386,9 +386,15 @@ function createSetupStore< // 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 -- 2.47.2