Merge pull request #5827 from jaysoo/add-redux-actions

Add redux-actions
This commit is contained in:
Boris Yankov
2015-09-17 16:20:37 +03:00
2 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
/// <reference path="./redux-actions.d.ts" />
const minimalAction: ReduxActions.Action = { type: 'INCREMENT' };
const richerAction: ReduxActions.Action = {
type: 'INCREMENT',
payload: 2,
error: false,
meta: {
remote: true
}
};
const incrementAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction<number>(
'INCREMENT',
(amount: number) => amount
);
const action: ReduxActions.Action = incrementAction(42);
const incrementByAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction<number>(
'INCREMENT_BY',
(amount: number) => amount,
amount => ({ remote: true })
);
let state: number;
const actionHandler = ReduxActions.handleAction<number>(
'INCREMENT',
(state: number, action: ReduxActions.Action) => state + 1
);
state = actionHandler(0, { type: 'INCREMENT' });
const actionHandlerWithReduceMap = ReduxActions.handleAction<number>(
'INCREMENT_BY', {
next(state: number, action: ReduxActions.Action) {
return state + action.payload;
},
throw(state: number) { return state }
}
);
state = actionHandlerWithReduceMap(0, { type: 'INCREMENT' });
const actionsHandler = ReduxActions.handleActions<number>({
'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1,
'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1
});
state = actionsHandler(0, { type: 'INCREMENT' });
const actionsHandlerWithInitialState = ReduxActions.handleActions<number>({
'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1,
'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1
}, 0);
state = actionsHandlerWithInitialState(0, { type: 'INCREMENT' });

34
redux-actions/redux-actions.d.ts vendored Normal file
View File

@@ -0,0 +1,34 @@
// Type definitions for redux-actions v0.8.0
// Project: https://github.com/acdlite/redux-actions
// Definitions by: Jack Hsu <https://github.com/jaysoo>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module ReduxActions {
// FSA-compliant action.
// See: https://github.com/acdlite/flux-standard-action
type Action = {
type: string
payload?: any
error?: boolean
meta?: any
};
type PayloadCreator<T> = (...args: any[]) => T;
type MetaCreator = (...args: any[]) => any;
type Reducer<T> = (state: T, action: Action) => T;
type ReducerMap<T> = {
[actionType: string]: Reducer<T>
};
export function createAction<T>(actionType: string, payloadCreator?: PayloadCreator<T>, metaCreator?: MetaCreator): (...args: any[]) => Action;
export function handleAction<T>(actionType: string, reducer: Reducer<T> | ReducerMap<T>): Reducer<T>;
export function handleActions<T>(reducerMap: ReducerMap<T>, initialState?: T): Reducer<T>;
}
declare module 'redux-actions' {
export = ReduxActions;
}