Skip to content

Commit

Permalink
Allow returning undefined from MutationAction to skip calling the…
Browse files Browse the repository at this point in the history
… mutation
  • Loading branch information
filips123 committed Jan 2, 2021
1 parent 08249c5 commit 0aeef04
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
7 changes: 6 additions & 1 deletion docs/pages/core/mutationactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ Note that if **S** denotes the type of _state_, then the object returned from a
`MutationAction` function must of type **Partial\<S\>**
The keys present inside the return value (for eg, here `posts`) are replaced into
the store.
:::
:::

:::tip NOTE
When a `MutationAction` function returns `undefined`, the mutation part of the
`MutationAction` will not be called, and the state will remain the same.
:::
7 changes: 4 additions & 3 deletions src/mutationaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function mutationActionDecoratorFactory<T extends Object>(params: MutationAction
return function (
target: T,
key: string | symbol,
descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<Partial<T>>>
descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<Partial<T> | undefined>>
) {
const module = target.constructor as Mod<T, any>
if (!module.hasOwnProperty('mutations')) {
Expand All @@ -27,6 +27,7 @@ function mutationActionDecoratorFactory<T extends Object>(params: MutationAction
) {
try {
const actionPayload = await mutactFunction.call(context, payload)
if (actionPayload === undefined) return
context.commit(key as string, actionPayload)
} catch (e) {
if (params.rawError) {
Expand Down Expand Up @@ -88,12 +89,12 @@ export function MutationAction<T>(
export function MutationAction<T, K, M extends K>(
paramsOrTarget: MutationActionParams<T> | M,
key?: string | symbol,
descriptor?: TypedPropertyDescriptor<(...args: any[]) => Promise<Partial<K>>>
descriptor?: TypedPropertyDescriptor<(...args: any[]) => Promise<Partial<K> | undefined>>
):
| ((
target: T,
key: string | symbol,
descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<Partial<T>>>
descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<Partial<T> | undefined>>
) => void)
| void {
if (!key && !descriptor) {
Expand Down
17 changes: 17 additions & 0 deletions test/mutationaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Vue.use(Vuex)
@Module
class MyModule extends VuexModule {
count: number = 0
anotherCount: number = 0
fruit: string = 'Apple'
vegetable: string | null = null

Expand All @@ -17,6 +18,12 @@ class MyModule extends VuexModule {
return { count: newcount }
}

@MutationAction
async updateAnotherCountConditionally({ newCount, shouldUpdate }: { newCount: number, shouldUpdate: boolean }) {
if (!shouldUpdate) return
return { anotherCount: newCount }
}

@MutationAction
async changeVeggie() {
return {vegetable: 'Carrot'}
Expand Down Expand Up @@ -75,4 +82,14 @@ describe('dispatching moduleaction works', () => {
await store.dispatch('changeFruit', 'Guava')
expect(store.state.mm.fruit).to.equal('Guava')
})

it('should be able to skip update', async function () {
expect(store.state.mm.anotherCount).to.equal(0)

await store.dispatch('updateAnotherCountConditionally', { newCount: 5, shouldUpdate: true })
expect(store.state.mm.anotherCount).to.equal(5)

await store.dispatch('updateAnotherCountConditionally', { newCount: 10, shouldUpdate: false })
expect(store.state.mm.anotherCount).to.equal(5)
})
})

0 comments on commit 0aeef04

Please sign in to comment.