Skip to content

Instantly share code, notes, and snippets.

@bpander
Created August 22, 2019 14:47
Show Gist options
  • Save bpander/427005077265b5b34565f719d8252bf9 to your computer and use it in GitHub Desktop.
Save bpander/427005077265b5b34565f719d8252bf9 to your computer and use it in GitHub Desktop.
import { configureActionsWith } from './configured-actions';
interface CounterState { count: number }
const initialState: CounterState = { count: 0 };
const { configureAction, reducer } = configureActionsWith(initialState, 'COUNTER');
export const counterReducer = reducer;
export const addAction = configureAction<{ amt: number }>(
'ADD',
payload => state => ({ ...state, count: state.count + payload.amt }),
);
export const subtractAction = configureAction<{ howMany: number }>(
'SUBTRACT',
payload => state => ({ ...state, count: state.count - payload.howMany }),
);
// Usage
// dispatch(addAction.create({ amt: 12 }));
// const l = makeLens<CounterState>();
// export const addActionLens = configureAction<{ amt: number }>(
// 'ADD_WITH_LENS',
// payload => l.k('count').set(count => count + payload.amt),
// );
// Thunks
// export const addThenDelayThenSubtract = (n1: number, n2: number) => async (dispatch: Dispatch) => {
// dispatch(addAction.create({ amt: n1 }));
// await sleep(100);
// dispatch(subtractAction.create({ howMany: n2 }));
// };
// addThenDelayThenSubtract(1, 2)(dispatch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment