From: Eduardo San Martin Morote Date: Mon, 20 Jan 2020 18:09:27 +0000 (+0100) Subject: refactor: use Map to allow arbitrary ids X-Git-Tag: 0.0.5~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1d27907a27c11ea000b17adec21a056bd0c6a87;p=thirdparty%2Fvuejs%2Fpinia.git refactor: use Map to allow arbitrary ids --- 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)