]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
chore: more isVue2
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 4 Aug 2021 06:20:30 +0000 (08:20 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 4 Aug 2021 06:20:30 +0000 (08:20 +0200)
playground/src/stores/jokes.ts
src/hmr.ts
src/store.ts

index a1911c1f47739c961c44450c0fb162a3aa19c24a..f249a5f7be42155fe82c29ae47215b39ffcde494 100644 (file)
@@ -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))
 }
index f6cdf536e808cdb2caf09229f8be7f9f27cd66e5..2ea6fa7134b3ab1e85a3bed783935e2a179e923b 100644 (file)
@@ -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
+      }
     }
   }
 
index c4cdcf042981d2c3ebe556bfbbd49aefa56d2c5f..f2d4dd518a8109d5a30fbe931b9c5891844aabbf 100644 (file)
@@ -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<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] = {}
@@ -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