From: Eduardo San Martin Morote Date: Fri, 17 Jan 2020 11:18:34 +0000 (+0100) Subject: feat: allow passing the req to useStore X-Git-Tag: 0.0.5~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f25062236fc4105bca3165f8fffc3cf3239cf669;p=thirdparty%2Fvuejs%2Fpinia.git feat: allow passing the req to useStore --- diff --git a/__tests__/actions.spec.ts b/__tests__/actions.spec.ts index fb309e32..c2fd6c51 100644 --- a/__tests__/actions.spec.ts +++ b/__tests__/actions.spec.ts @@ -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') + }) }) diff --git a/src/store.ts b/src/store.ts index 0168e5f1..0b96fb6e 100644 --- a/src/store.ts +++ b/src/store.ts @@ -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> >() @@ -263,7 +263,8 @@ export function createStore< }) { const { id, state, getters, actions } = options - return function useStore(): Store { + return function useStore(reqKey?: object): Store { + 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) } diff --git a/tsconfig.json b/tsconfig.json index ebd97242..3d5d2bbe 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"] }