buildActions => ts

typescript
Yanick Champoux 2019-10-23 13:57:40 -04:00
parent d9bac9dd7d
commit ca89c53c0b
3 changed files with 18 additions and 12 deletions

View File

@ -1,8 +1,14 @@
import fp from 'lodash/fp'; import fp from 'lodash/fp';
import { Action } from '../types';
function actionFor(type) { interface ActionCreator {
const creator = ( (payload = undefined, meta = undefined) => ( ...args: any[] ): Action;
fp.pickBy(v => v !== undefined)({type, payload, meta}) _genericAction?: boolean
}
function actionFor(type:string) {
const creator : ActionCreator = ( (payload = undefined, meta = undefined) =>
fp.pickBy(v => v !== undefined)({type, payload, meta}) as Action
); );
creator._genericAction = true; creator._genericAction = true;
@ -11,7 +17,7 @@ function actionFor(type) {
} }
export default function buildActions( export default function buildActions(
creators = {}, creators : { [action: string]: Function } = {},
mutations = {}, mutations = {},
effects = {}, effects = {},
subActions = [], subActions = [],
@ -31,7 +37,7 @@ export default function buildActions(
...generic, ...generic,
...crafted, ...crafted,
...Object.entries(creators).map( ...Object.entries(creators).map(
([type, payload]) => [type, (...args) => ({ type, payload: payload(...args) })] ([type, payload]: [ string, Function ]) => [type, (...args: any) => ({ type, payload: payload(...args) })]
), ),
]; ];

View File

@ -1,9 +1,9 @@
import fp from 'lodash/fp'; import fp from 'lodash/fp';
import { Dictionary, Mutation, UpduxAction } from '../types'; import { Dictionary, Mutation, Action } from '../types';
function buildUpreducer<S>(initial: S, mutations: Dictionary<Mutation<S>> ) { function buildUpreducer<S>(initial: S, mutations: Dictionary<Mutation<S>> ) {
return (action :UpduxAction) => (state: S) => { return (action :Action) => (state: S) => {
if (state === null) state = initial; if (state === null) state = initial;
const a = const a =

View File

@ -1,9 +1,9 @@
import { Action } from 'redux';
export type UpduxAction = Action & Partial<{ export type Action = {
payload: any, type: string,
meta: any, payload?: any,
}> meta?: any,
}
export type Dictionary<T> = { [key: string]: T }; export type Dictionary<T> = { [key: string]: T };