Skip to content

Commit

Permalink
Reverts the change that removed bound action creators on the stores d…
Browse files Browse the repository at this point in the history
…ispatch
  • Loading branch information
ctrlplusb committed Jul 23, 2019
1 parent 75ce14c commit e8aa7bd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
24 changes: 17 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,16 @@ export type Store<
StoreModel extends object,
StoreConfig extends EasyPeasyConfig<any, any> = any
> = O.Merge<
ReduxStore<State<StoreModel>>,
O.Omit<ReduxStore<State<StoreModel>>, 'dispatch'>,
{
dispatch: Dispatch<StoreModel>;
getActions: () => Actions<StoreModel>;
getMockedActions: () => MockedAction[];
clearMockedActions: () => void;
useStoreActions: <Result = any>(
mapActions: (actions: Actions<StoreModel>) => Result,
) => Result;
useStoreDispatch: () => Dispatch;
useStoreDispatch: () => Dispatch<StoreModel>;
useStoreState: <Result = any>(
mapState: (state: State<StoreModel>) => Result,
dependencies?: Array<any>,
Expand All @@ -261,9 +262,16 @@ export type Store<
// #region Dispatch

/**
* The Redux store Dispatch
* Enhanced version of the Redux Dispatch with action creators bound to it
*
* @example
*
* type DispatchWithActions = Dispatch<StoreModel>;
*/
export type Dispatch = ReduxDispatch;
export type Dispatch<
StoreModel,
Action extends ReduxAction = ReduxAction<any>
> = Actions<StoreModel> & ReduxDispatch<Action>;

// #endregion

Expand Down Expand Up @@ -348,7 +356,7 @@ export function thunk<
actions: Actions<Model>,
payload: Payload,
helpers: {
dispatch: Dispatch;
dispatch: Dispatch<StoreModel>;
getState: () => State<Model>;
getStoreActions: () => Actions<StoreModel>;
getStoreState: () => State<StoreModel>;
Expand Down Expand Up @@ -633,7 +641,9 @@ export function useStoreActions<
* return <AddTodoForm save={(todo) => dispatch({ type: 'ADD_TODO', payload: todo })} />;
* }
*/
export function useStoreDispatch(): Dispatch;
export function useStoreDispatch<StoreModel extends object = {}>(): Dispatch<
StoreModel
>;

// #endregion

Expand Down Expand Up @@ -686,7 +696,7 @@ export function createContextStore<
useStoreActions: <Result = any>(
mapActions: (actions: Actions<StoreModel>) => Result,
) => Result;
useStoreDispatch: () => Dispatch;
useStoreDispatch: () => Dispatch<StoreModel>;
};

export interface UseLocalStore<
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easy-peasy",
"version": "3.0.0-alpha.5",
"version": "3.0.0-alpha.6",
"description": "Vegetarian friendly state for React",
"license": "MIT",
"main": "dist/easy-peasy.cjs.js",
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/typescript/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ const model: Model = {

const store = createStore(model);

store.dispatch.todos.add('foo');
// typings:expect-error
store.dispatch.todos.add(1);
// typings:expect-error
store.dispatch.todos.add();

store.dispatch.todos.clear();

interface ListeningModel {
logs: string[];
doAction: Action<ListeningModel, string>;
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/typescript/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ const count = store.useStoreState(state => state.count);
count + 10;

store.getActions().doAction(true);
store.getActions().doAction(true);
store.dispatch.doAction(true);

// typings:expect-error
store.getActions().doAction(1);
// typings:expect-error
store.getActions().doAction(1);
store.dispatch.doAction(1);

store.getActions().doThunk(1);
store.getActions().doThunk(1);
store.dispatch.doThunk(1);

// typings:expect-error
store.getActions().doThunk(true);
// typings:expect-error
store.getActions().doThunk(true);
store.dispatch.doThunk(true);

store.getMockedActions()[0].type;

Expand Down
13 changes: 13 additions & 0 deletions src/create-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ export default function createStore(model, options = {}) {
references.dispatch = store.dispatch;
references.getState = store.getState;

// attachs the action creators to the stores dispatch
const bindActionCreators = () => {
Object.keys(store.dispatch).forEach(actionsKey => {
delete store.dispatch[actionsKey];
});
Object.keys(references.internals.actionCreators).forEach(key => {
store.dispatch[key] = references.internals.actionCreators[key];
});
};

bindActionCreators();

const rebindStore = removeKey => {
const currentState = store.getState();
if (removeKey) {
Expand All @@ -124,6 +136,7 @@ export default function createStore(model, options = {}) {
bindStoreInternals(store.getState());
store.replaceReducer(references.internals.reducer);
store.getActions().replaceState(references.internals.defaultState);
bindActionCreators();
};

store.addModel = (key, modelForKey) => {
Expand Down

0 comments on commit e8aa7bd

Please sign in to comment.