Skip to content

Instantly share code, notes, and snippets.

@ktsn
Last active July 15, 2016 10:35
Show Gist options
  • Save ktsn/14b2c49d4d36d23a3e3aa050710be42f to your computer and use it in GitHub Desktop.
Save ktsn/14b2c49d4d36d23a3e3aa050710be42f to your computer and use it in GitHub Desktop.
Vuex action helper
import {
INIT_SOME_DATA,
ADD_SOME_DATA,
MOVE_SOME_DATA,
REMOVE_SOME_DATA,
UPDATE_SOME_DATA
} from '../mutation_types'
import { ISomeData } from '../model/some_data'
import { action } from '../generators/helper'
export const initSomeData = action(
INIT_SOME_DATA,
(data: ISomeData[]) => ({ data })
)
export const addSomeData = action(ADD_SOME_DATA)
export const moveSomeData = action(
MOVE_SOME_DATA,
(index: number, offset: number) => ({ index, offset })
)
export const removeSomeData = action(
REMOVE_SOME_DATA,
(index: number) => ({ index })
)
export const updateSomeData = action(
UPDATE_SOME_DATA,
(key: string, value: string) => ({ [key]: value })
)
import { Store } from 'vuex'
type Action0 = (s: Store<any>) => void;
type Action1<T> = (s: Store<any>, t: T) => void;
type Action2<T, U> = (s: Store<any>, t: T, u: U) => void;
type Action = (s: Store<any>, ...args: any[]) => void;
export function action<T, P>(type: string, creator: (t: T) => P) : Action1<T>
export function action<T, U, P>(type: string, creator: (t: T, u: U) => P) : Action2<T, U>
export function action<P>(type: string) : Action0
export function action<P>(type: string, creator?: (...args: any[]) => P | undefined) : Action {
if (typeof creator === 'undefined') {
creator = () => void 0
}
return ({ dispatch }, ...args) => dispatch({
type,
payload: creator!(...args)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment