]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor: simplify state provider
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:08:52 +0000 (19:08 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:21:46 +0000 (19:21 +0100)
__tests__/store.spec.ts
nuxt/plugin.js
src/rootStore.ts
src/store.ts

index a0570ed3fd0e6b5ba8ef61c1e39c2dfb0a16ee6a..d8cf1afbc53eef9005c6c3a0740a2e2d7e40b484 100644 (file)
@@ -65,18 +65,15 @@ describe('Store', () => {
       }),
     })
 
-    setStateProvider({
-      set: () => {},
-      get: () => ({
-        main: {
-          a: false,
-          nested: {
-            foo: 'bar',
-            a: { b: 'string' },
-          },
+    setStateProvider(() => ({
+      main: {
+        a: false,
+        nested: {
+          foo: 'bar',
+          a: { b: 'string' },
         },
-      }),
-    })
+      },
+    }))
 
     const store = useStore()
 
index c27d5189ba7f8bc1ddb6949264d3e4383f7866fc..b721315143efecefc7b912701abe3b5139ee0679 100644 (file)
@@ -1,52 +1,5 @@
 import Vue from 'vue'
 import { setActiveReq, setStateProvider, getRootState } from 'pinia'
-// import { Plugin } from '@nuxt/types'
-
-// declare module '@nuxt/types' {
-//   interface Context {
-//     ssrContext: {
-//       req: Parameters<Plugin>[0]['req']
-//       nuxt: {
-//         __piniaStates?: Record<string, any>
-//       }
-//     }
-//   }
-// }
-
-// declare global {
-//   interface Window {
-//     __NUXT__: {
-//       __piniaStates: Record<string, any>
-//     }
-//   }
-// }
-
-const piniStatesGetter = () => window.__NUXT__.__piniaStates
-
-const noop = () => {}
-const emptyObjectReturn = () => ({})
-
-/**
- * Create a state provider for nuxt
- * @param {Parameters<import('@nuxt/types').Plugin>[0]['ssrContext'] | undefined} context
- */
-function createStateProvider(context) {
-  /**
-   *
-   * @param {any} store
-   */
-  const piniStatesSetter = store => {
-    // context is always non undefined on server
-    const { nuxt } = context
-    const states = nuxt.__piniaStates || (nuxt.__piniaStates = {})
-    states[store.id] = store.state
-  }
-
-  return {
-    get: process.client ? piniStatesGetter : emptyObjectReturn,
-    set: context ? piniStatesSetter : noop,
-  }
-}
 
 Vue.mixin({
   beforeCreate() {
@@ -58,7 +11,7 @@ Vue.mixin({
         if (context.ssrContext && context.ssrContext.req) {
           setActiveReq(context.ssrContext.req)
         }
-        // setStateProvider(createStateProvider(context.ssrContext))
+
         return setup(props, context)
       }
     }
@@ -72,7 +25,7 @@ Vue.mixin({
         const original = patchedServerPrefetch[i]
         patchedServerPrefetch[i] = function() {
           setActiveReq(this.$ssrContext.req)
-          // setStateProvider(createStateProvider(this.$ssrContext.req))
+
           return original.call(this)
         }
       }
@@ -86,8 +39,6 @@ Vue.mixin({
 /** @type {import('@nuxt/types').Plugin} */
 const myPlugin = context => {
   // console.log('🍍 Pinia Nuxt plugin installed')
-  // setActiveReq(context.req)
-  // setStateProvider(createStateProvider(context.ssrContext))
 
   if (process.server) {
     setActiveReq(context.req)
@@ -95,11 +46,7 @@ const myPlugin = context => {
       nuxtState.pinia = getRootState(context.req)
     })
   } else {
-    setStateProvider({
-      get: () => context.nuxtState.pinia,
-      // TODO: remove the setter
-      set: () => {},
-    })
+    setStateProvider(() => context.nuxtState.pinia)
   }
 }
 
index 23142455885e4408c037681c6928fe91f0aa9347..09262a1e0e3588eb5a3ebcdca714fb3c76487ab5 100644 (file)
@@ -24,8 +24,7 @@ export const storesMap = new WeakMap<
  * A state provider allows to set how states are stored for hydration. e.g. setting a property on a context, getting a property from window
  */
 interface StateProvider {
-  get(): Record<string, StateTree>
-  set(store: GenericStore): any
+  (): Record<string, StateTree>
 }
 
 /**
@@ -39,12 +38,7 @@ export function setStateProvider(stateProvider: StateProvider) {
 
 export function getInitialState(id: string): StateTree | undefined {
   const provider = stateProviders.get(getActiveReq())
-  return provider && provider.get()[id]
-}
-
-export function setInitialState(store: GenericStore): void {
-  const provider = stateProviders.get(getActiveReq())
-  if (provider) provider.set(store)
+  return provider && provider()[id]
 }
 
 /**
index 7781332e414bd5b4071ee7ff4768338a9f813349..51c64134b2108c6bd34ecdcb2293ad407ef7459c 100644 (file)
@@ -17,7 +17,6 @@ import {
   setActiveReq,
   storesMap,
   getInitialState,
-  setInitialState,
 } from './rootStore'
 
 const isClient = typeof window != 'undefined'