]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: allow passing the req to useStore
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 17 Jan 2020 11:18:34 +0000 (12:18 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:21:18 +0000 (19:21 +0100)
__tests__/actions.spec.ts
src/store.ts
tsconfig.json

index fb309e322fb40e998b206875e7f2e674ffc9dae1..c2fd6c511d6055522870b584ca6125e1f17ed520 100644 (file)
@@ -87,4 +87,20 @@ describe('Store', () => {
     // a different instance of b store was used
     expect(bStore.state.b).toBe('c')
   })
+
+  it('can force the req', () => {
+    const req1 = {}
+    const req2 = {}
+    const aStore = useA(req1)
+
+    let bStore = useB(req2)
+    bStore.state.b = 'c'
+
+    aStore.swap()
+    expect(aStore.state.a).toBe('b')
+    // a different instance of b store was used
+    expect(bStore.state.b).toBe('c')
+    bStore = useB(req1)
+    expect(bStore.state.b).toBe('a')
+  })
 })
index 0168e5f10f6ff1fd2123d1b5012e644786cbd282..0b96fb6e2a826240b2b40d645a7bf2a134712341 100644 (file)
@@ -213,7 +213,7 @@ export function buildStore<
  * be able to reset the store instance between requests on the server
  */
 
-const storesMap = new WeakMap<
+export const storesMap = new WeakMap<
   NonNullObject,
   Record<string, Store<any, any, any, any>>
 >()
@@ -263,7 +263,8 @@ export function createStore<
 }) {
   const { id, state, getters, actions } = options
 
-  return function useStore(): Store<Id, S, G, A> {
+  return function useStore(reqKey?: object): Store<Id, S, G, A> {
+    if (reqKey) setActiveReq(reqKey)
     const req = getActiveReq()
     let stores = storesMap.get(req)
     if (!stores) storesMap.set(req, (stores = {}))
@@ -278,7 +279,7 @@ export function createStore<
         getInitialState(id)
       )
       // save a reference to the initial state
-      // TODO: this implies that replacing the store cannot be done by the user because we are relying on the object reference
+      // TODO: this implies that replacing the store cannot be done by the user on the server
       setInitialState(store)
       if (isClient) useStoreDevtools(store)
     }
index ebd97242fbe960e2b319ccc01c5ec8091adbf9ef..3d5d2bbe98159a46b64da599efe1b3e9351f3f41 100644 (file)
@@ -1,5 +1,6 @@
 {
   "compilerOptions": {
+    "allowJs": true,
     "target": "esnext",
     "module": "esnext",
     "noEmit": true,
@@ -12,5 +13,5 @@
     "rootDir": ".",
     "baseUrl": "."
   },
-  "include": ["src/**/*.ts", "__tests__/**/**.ts"]
+  "include": ["src/**/*.ts", "nuxt/*.js", "__tests__/**/**.ts"]
 }