]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor: use Map to allow arbitrary ids
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:09:27 +0000 (19:09 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:21:46 +0000 (19:21 +0100)
src/rootStore.ts
src/store.ts

index 09262a1e0e3588eb5a3ebcdca714fb3c76487ab5..db9c8a37adfdc6b7df18f07d52cfae6a6d3f95ee 100644 (file)
@@ -15,10 +15,7 @@ export const getActiveReq = () => activeReq
  * be able to reset the store instance between requests on the server
  */
 
-export const storesMap = new WeakMap<
-  NonNullObject,
-  Record<string, GenericStore>
->()
+export const storesMap = new WeakMap<NonNullObject, Map<string, GenericStore>>()
 
 /**
  * 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
@@ -51,9 +48,10 @@ export function getRootState(req: NonNullObject): Record<string, StateTree> {
   if (!stores) return {}
   const rootState = {} as Record<string, StateTree>
 
-  for (const store of Object.values(stores)) {
+  // forEach is the only one that also works on IE11
+  stores.forEach(store => {
     rootState[store.id] = store.state
-  }
+  })
 
   return rootState
 }
index 51c64134b2108c6bd34ecdcb2293ad407ef7459c..9bd3329d9e4e15b5cc6840e68caa48b09b32ae3e 100644 (file)
@@ -177,16 +177,13 @@ export function createStore<
     if (reqKey) setActiveReq(reqKey)
     const req = getActiveReq()
     let stores = storesMap.get(req)
-    if (!stores) storesMap.set(req, (stores = {}))
+    if (!stores) storesMap.set(req, (stores = new Map()))
 
-    let store = stores[id] as Store<Id, S, G, A>
+    let store = stores.get(id) as Store<Id, S, G, A>
     if (!store) {
-      stores[id] = store = buildStore(
+      stores.set(
         id,
-        state,
-        getters,
-        actions,
-        getInitialState(id)
+        (store = buildStore(id, state, getters, actions, getInitialState(id)))
       )
 
       if (isClient) useStoreDevtools(store)