Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "default" section in createReducer() #152

Closed
Jazzmanpw opened this issue Sep 15, 2020 · 4 comments · Fixed by #153
Closed

Add "default" section in createReducer() #152

Jazzmanpw opened this issue Sep 15, 2020 · 4 comments · Fixed by #153
Assignees
Labels
enhancement New feature or request

Comments

@Jazzmanpw
Copy link
Contributor

There are use cases for this. Two examples how I use it:

  • matching action not by the exact type:
function loadingReducer(state, action) {
  if (action.type.includes('REQUEST:STARTED') {
    return true;
  }
  if (action.type.includes('REQUEST:DONE') {
    return false;
  }
  return state;
}
  • extending an existing reducer:
import baseReducer from './another-reducer';

function derivativeReducer(state, action) {
  switch (action.type) {
    case SOME_ACTION: return /* some processing */;
    case ANOTHER_ACTION: return /* more processing */;
    default: return baseReducer(state, action);
  }
}

One of the easiest ways is to make the third optional argument that will be used as the default reducer:

createReducer(initState, handle => [
  handle(action, reducer),
], (state, action) => { /* this will be the default reducer */ });

Do you plan to add something like this? What do you think of this proposal?

@Jazzmanpw
Copy link
Contributor Author

Below is a workaround that may be used to implement the desired behavior. More than that, it even tries to prevent the "default" part from executing if the state was changed by the "switch" reducer, but it's verbose and make much more than it should. It may be much easier to just create a pull request with this fix, but I've never done it before and don't know how one usually does that.

const inc = createAction('INC');
const subtract = createAction('SUBTRACT', res => (v: number) => res(v));

const switchReducer = createReducer(0, handle => [
  handle(inc, state => state + 1),
  handle(subtract, (state, { payload }) => state - payload),
]);

const reducer: typeof switchReducer = (state, action) => {
  const newState = switchReducer(state, action);
  if (newState !== state) {
    return newState;
  }
  // this is the "default" part
  return state;
};

@the-dr-lazy
Copy link
Owner

the-dr-lazy commented Sep 16, 2020

I propose following API:

const reducer = createReducer(defaultState, handle => [
  handle(increment, state => state + 1),
  handle(decrement, state => state - 1),
  handle.default((state, action) => state)
])

PRs welcome. Fork the next branch and read create-reducer.ts, create-handler-map.ts files. Those files need to change.

@the-dr-lazy the-dr-lazy added the enhancement New feature or request label Sep 16, 2020
Jazzmanpw added a commit to Jazzmanpw/deox that referenced this issue Sep 17, 2020
@Jazzmanpw
Copy link
Contributor Author

The changes must be complete. New tests run. There is a couple of other tests that complain about obsolete snapshots, and I'm not sure if I can just ignore them for a moment.
I guess, now I'm waiting for review, don't I?

@Jazzmanpw
Copy link
Contributor Author

Some checks have failed. I'll try to investigate them on my own.

@the-dr-lazy the-dr-lazy linked a pull request Sep 20, 2020 that will close this issue
the-dr-lazy pushed a commit that referenced this issue Oct 1, 2020
github-actions bot pushed a commit that referenced this issue Oct 1, 2020
# [3.3.0](v3.2.2...v3.3.0) (2020-10-01)

### Bug Fixes

* handle.others instead of handle.default ([f9aec53](f9aec53))
* more explicit typing to avoid TS4025 ([e703730](e703730))

### Features

* default handler in createReducer ([611184d](611184d)), closes [#152](#152)
the-dr-lazy pushed a commit that referenced this issue Oct 1, 2020
the-dr-lazy pushed a commit that referenced this issue Oct 1, 2020
# [3.3.0](v3.2.2...v3.3.0) (2020-10-01)

### Bug Fixes

* handle.others instead of handle.default ([f9aec53](f9aec53))
* more explicit typing to avoid TS4025 ([e703730](e703730))

### Features

* default handler in createReducer ([611184d](611184d)), closes [#152](#152)
github-actions bot pushed a commit that referenced this issue Oct 1, 2020
# [3.3.0](v3.2.1...v3.3.0) (2020-10-01)

### Bug Fixes

* handle.others instead of handle.default ([ffe271b](ffe271b))
* more explicit typing to avoid TS4025 ([25b78c3](25b78c3))
* type for plain action creator. ([#144](#144)) ([f228b81](f228b81)), closes [#143](#143)

### Features

* default handler in createReducer ([0b3dfcf](0b3dfcf)), closes [#152](#152)
github-actions bot pushed a commit that referenced this issue Oct 1, 2020
# [3.3.0](v3.2.1...v3.3.0) (2020-10-01)

### Bug Fixes

* handle.others instead of handle.default ([ffe271b](ffe271b))
* more explicit typing to avoid TS4025 ([25b78c3](25b78c3))
* type for plain action creator. ([#144](#144)) ([f228b81](f228b81)), closes [#143](#143)

### Features

* default handler in createReducer ([0b3dfcf](0b3dfcf)), closes [#152](#152)
Jazzmanpw pushed a commit to Jazzmanpw/deox that referenced this issue Oct 6, 2020
# [3.3.0](the-dr-lazy/deox@v3.2.2...v3.3.0) (2020-10-01)

### Bug Fixes

* handle.others instead of handle.default ([f9aec53](the-dr-lazy@f9aec53))
* more explicit typing to avoid TS4025 ([e703730](the-dr-lazy@e703730))

### Features

* default handler in createReducer ([611184d](the-dr-lazy@611184d)), closes [the-dr-lazy#152](the-dr-lazy#152)
Jazzmanpw pushed a commit to Jazzmanpw/deox that referenced this issue Oct 6, 2020
# [3.3.0](the-dr-lazy/deox@v3.2.1...v3.3.0) (2020-10-01)

### Bug Fixes

* handle.others instead of handle.default ([ffe271b](the-dr-lazy@ffe271b))
* more explicit typing to avoid TS4025 ([25b78c3](the-dr-lazy@25b78c3))
* type for plain action creator. ([the-dr-lazy#144](the-dr-lazy#144)) ([f228b81](the-dr-lazy@f228b81)), closes [the-dr-lazy#143](the-dr-lazy#143)

### Features

* default handler in createReducer ([0b3dfcf](the-dr-lazy@0b3dfcf)), closes [the-dr-lazy#152](the-dr-lazy#152)
github-actions bot pushed a commit that referenced this issue Oct 9, 2020
# [3.3.0](v3.2.1...v3.3.0) (2020-10-09)

### Bug Fixes

* handle.others instead of handle.default ([ffe271b](ffe271b))
* more explicit typing to avoid TS4025 ([25b78c3](25b78c3))
* type for plain action creator. ([#144](#144)) ([f228b81](f228b81)), closes [#143](#143)

### Features

* default handler in createReducer ([0b3dfcf](0b3dfcf)), closes [#152](#152)
github-actions bot pushed a commit that referenced this issue Oct 17, 2020
# [3.3.0](v3.2.1...v3.3.0) (2020-10-17)

### Bug Fixes

* handle.others instead of handle.default ([ffe271b](ffe271b))
* more explicit typing to avoid TS4025 ([25b78c3](25b78c3))
* type for plain action creator. ([#144](#144)) ([f228b81](f228b81)), closes [#143](#143)
* use symbol for the `others` handler ([3aa61fe](3aa61fe))

### Features

* default handler in createReducer ([0b3dfcf](0b3dfcf)), closes [#152](#152)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants