]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: allow using getters in other getters
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:14:09 +0000 (19:14 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:21:46 +0000 (19:21 +0100)
__tests__/getters.spec.ts
src/store.ts
src/types.ts

index 3919ce24db4652b9d4c5ce2299bef1db18b4051e..9a56fcc2531d029dfdbf61a4bfb6f102f9e76e35 100644 (file)
@@ -11,6 +11,8 @@ describe('Store', () => {
       }),
       getters: {
         upperCaseName: ({ name }) => name.toUpperCase(),
+        composed: (state, { upperCaseName }) =>
+          (upperCaseName.value as string) + ': ok',
       },
     })()
   }
@@ -58,4 +60,9 @@ describe('Store', () => {
     aStore.state.a = 'b'
     expect(aStore.fromB.value).toBe('b b')
   })
+
+  it('can use other getters', () => {
+    const store = useStore()
+    expect(store.composed.value).toBe('EDUARDO: ok')
+  })
 })
index 9bd3329d9e4e15b5cc6840e68caa48b09b32ae3e..83c86d8ecf2a3bf48b4b29e276a77b29f59ca1f6 100644 (file)
@@ -121,7 +121,7 @@ export function buildStore<
     computedGetters[getterName] = computed(() => {
       setActiveReq(_r)
       // eslint-disable-next-line @typescript-eslint/no-use-before-define
-      return getters[getterName](state.value)
+      return getters[getterName](state.value, computedGetters)
     }) as StoreWithGetters<S, G>[typeof getterName]
   }
 
index 8a59e1112d345673dff9ca7865db032121f3c975..725704387ab71ec480bc8913f485c2eb38480d55 100644 (file)
@@ -17,7 +17,8 @@ export function isPlainObject(
 export type NonNullObject = Record<any, any>
 
 export interface StoreGetter<S extends StateTree, T = any> {
-  (state: S): T
+  // TODO: would be nice to be able to define the getters here
+  (state: S, getters: Record<string, Ref<any>>): T
 }
 
 type TODO = any
@@ -30,13 +31,6 @@ export type SubscriptionCallback<S> = (
   state: S
 ) => void
 
-export type StoreReactiveGetters<
-  S extends StateTree,
-  G extends Record<string, (state: S, getters: any) => any>
-> = {
-  [k in keyof G]: G[k] extends (state: S, getters: any) => infer V ? V : never
-}
-
 export type StoreWithGetters<
   S extends StateTree,
   G extends Record<string, StoreGetter<S>>