From a1d27907a27c11ea000b17adec21a056bd0c6a87 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 20 Jan 2020 19:09:27 +0100 Subject: [PATCH] refactor: use Map to allow arbitrary ids --- src/rootStore.ts | 10 ++++------ src/store.ts | 11 ++++------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/rootStore.ts b/src/rootStore.ts index 09262a1e..db9c8a37 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -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 ->() +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 @@ -51,9 +48,10 @@ export function getRootState(req: NonNullObject): Record { if (!stores) return {} const rootState = {} as Record - 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 } diff --git a/src/store.ts b/src/store.ts index 51c64134..9bd3329d 100644 --- a/src/store.ts +++ b/src/store.ts @@ -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 + let store = stores.get(id) as Store 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) -- 2.47.2