]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor(setup): correctly extract the state
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 9 Jul 2021 09:44:43 +0000 (11:44 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 19 Jul 2021 09:51:12 +0000 (11:51 +0200)
src/index.ts
src/store.ts

index 09813b1b93172690cf7cef3c5a5c466a7935188e..b691321507cddfd110a23d3c78fc13a2c3b594b0 100644 (file)
@@ -2,7 +2,7 @@ export { setActivePinia } from './rootStore'
 export { createPinia } from './createPinia'
 export type { Pinia, PiniaStorePlugin, PiniaPluginContext } from './rootStore'
 
-export { defineStore } from './store'
+export { defineStore, defineSetupStore } from './store'
 
 export type {
   StateTree,
index cef3dc9eab6c8d88f382f7005a0e1e46ec338908..aa28b93afb19e1ce93da2c98504a99b2e2204439 100644 (file)
@@ -16,7 +16,6 @@ import {
   effectScope,
   onScopeDispose,
   EffectScope,
-  getCurrentScope,
   onUnmounted,
   ComputedRef,
   toRef,
@@ -400,22 +399,6 @@ function buildStoreToUse<
   return store
 }
 
-type _SetupOfStateActions<
-  S extends StateTree,
-  A extends Record<any, _Method>
-> = (StateTree extends S ? {} : S) & (Record<any, _Method> extends A ? {} : A)
-
-function stateBuilder<S extends StateTree>(
-  builder: (initial?: S | undefined) => S
-) {
-  return {} as S
-}
-
-const a = stateBuilder<{ n: number; toggle: boolean }>((initial) => ({
-  n: initial?.n || 2,
-  toggle: true,
-}))
-
 export interface DefineSetupStoreOptions<
   Id extends string,
   S extends StateTree,
@@ -438,7 +421,6 @@ function createSetupStore<
 >(
   $id: Id,
   setup: () => SS,
-  initialState: S | undefined,
   {
     // @ts-expect-error
     hydrate = innerPatch,
@@ -473,6 +455,12 @@ function createSetupStore<
   let subscriptions: SubscriptionCallback<S>[] = markRaw([])
   let actionSubscriptions: StoreOnActionListener<Id, S, G, A>[] = markRaw([])
   let debuggerEvents: DebuggerEvent[] | DebuggerEvent
+  const initialState = pinia.state.value[$id] as S | undefined
+
+  if (!initialState) {
+    // should be set in Vue 2
+    pinia.state.value[$id] = {}
+  }
 
   const triggerSubscriptions: SubscriptionCallback<S> = (mutation, state) => {
     subscriptions.forEach((callback) => {
@@ -490,8 +478,6 @@ function createSetupStore<
     scope = effectScope()
     return scope.run(() => {
       const store = setup()
-      // TODO: extract state and set it to pinia.state.value[$id]
-      // pinia.state.value[$id] = initialState || buildState()
 
       watch(
         () => pinia.state.value[$id] as UnwrapRef<S>,
@@ -634,7 +620,7 @@ function createSetupStore<
       }
     } else if ((isRef(prop) && !isComputed(prop)) || isReactive(prop)) {
       // mark it as a piece of state to be serialized
-      pinia.state.value[$id] = toRef(setupStore as any, key)
+      pinia.state.value[$id][key] = toRef(setupStore as any, key)
     } else if (__DEV__ && IS_CLIENT) {
       // add getters for devtools
       if (isComputed(prop)) {
@@ -691,6 +677,10 @@ function createSetupStore<
     }
   })
 
+  if (initialState) {
+    hydrate(store, initialState)
+  }
+
   return store
 }