Skip to content

Commit c491b84

Browse files
committed
adds redux-actions
1 parent 52b0ea5 commit c491b84

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

redux-actions/redux-actions-tests.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/// <reference path="./redux-actions.d.ts" />
2+
3+
const minimalAction: ReduxActions.Action = { type: 'INCREMENT' };
4+
const richerAction: ReduxActions.Action = {
5+
type: 'INCREMENT',
6+
payload: 2,
7+
error: false,
8+
meta: {
9+
remote: true
10+
}
11+
};
12+
13+
const incrementAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction<number>(
14+
'INCREMENT',
15+
(amount: number) => amount
16+
);
17+
const action: ReduxActions.Action = incrementAction(42);
18+
19+
const incrementByAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction<number>(
20+
'INCREMENT_BY',
21+
(amount: number) => amount,
22+
amount => ({ remote: true })
23+
);
24+
25+
const actionHandler = ReduxActions.handleAction(
26+
'INCREMENT',
27+
(state: number, action: ReduxActions.Action) => state + 1
28+
);
29+
actionHandler(0, { type: 'INCREMENT' });
30+
31+
32+
const actionHandlerWithReduceMap = ReduxActions.handleAction(
33+
'INCREMENT_BY', {
34+
next(state: number, action: ReduxActions.Action) {
35+
return state + action.payload;
36+
},
37+
throw(state: number) { return state }
38+
}
39+
);
40+
actionHandlerWithReduceMap(0, { type: 'INCREMENT' });
41+
42+
const actionsHandler = ReduxActions.handleActions<number>({
43+
'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1,
44+
'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1
45+
});
46+
actionsHandler(0, { type: 'INCREMENT' });
47+
48+
const actionsHandlerWithInitialState = ReduxActions.handleActions<number>({
49+
'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1,
50+
'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1
51+
}, 0);
52+
actionsHandlerWithInitialState(0, { type: 'INCREMENT' });
53+
54+

redux-actions/redux-actions.d.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Type definitions for redux-actions v0.8.0
2+
// Project: https://github.com/acdlite/redux-actions
3+
// Definitions by: Jack Hsu <https://github.com/jaysoo>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
declare module ReduxActions {
6+
// FSA-compliant action.
7+
// See: https://github.com/acdlite/flux-standard-action
8+
type Action = {
9+
type: string
10+
payload?: any
11+
error?: boolean
12+
meta?: any
13+
};
14+
15+
type PayloadCreator<T> = (...args: any[]) => T;
16+
type MetaCreator = (...args: any[]) => any;
17+
18+
type Reducer<T> = (state: T, action: Action) => T;
19+
20+
type ReducerMap<T> = {
21+
[actionType: string]: Reducer<T>
22+
};
23+
24+
export function createAction<T>(actionType: string, payloadCreator?: PayloadCreator<T>, metaCreator?: MetaCreator): (...args: any[]) => Action;
25+
26+
export function handleAction<T>(actionType: string, reducer: Reducer<T> | ReducerMap<T>): Reducer<T>;
27+
28+
export function handleActions<T>(reducerMap: ReducerMap<T>, initialState?: T): Reducer<T>;
29+
}
30+
31+
declare module 'redux-actions' {
32+
export = ReduxActions;
33+
}
34+

0 commit comments

Comments
 (0)