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

feat: deprecate reducer in favor of actionReducer and streamReducer APIs #558

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

sseppola
Copy link
Contributor

@sseppola sseppola commented Dec 6, 2022

This PR deprecates the overloaded reducer api in favor of clearer separate functions: one for action reducers and one for stream reducers because they serve different purposes, and deserve their own api and description.

With the separation of StreamReducerCreator and ActionReducerCreator types I was not able to get the reducer type to act the same. So when upgrading there is a new TS error when passing an array of action creators UnknownActionCreator[], which is trivially fixed by using the actionReducer instead. This could be considered a "breaking" change, but the functionality is the same.

Copy link
Contributor

@holographic-principle holographic-principle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even use the "action reducer" flavor of the current reducer with multiple action creators anywhere?

Seems to be a very niche thing if we do. Maybe we should actually limit it to 1.

BTW, someone should give you access to the rxbeach repo. I'm not an admin there so I can't.

@sseppola
Copy link
Contributor Author

sseppola commented Dec 6, 2022

@holographic-principle, I found at least 3 places in ardoq-front where we use the multiple action flavor because of the TS errors.

I think it would be interesting to study our use to see if some patterns and perhaps a better API structure emerge. I'm hoping we can achieve full hot module reloading and fast refresh as discussed in #555.

It looks like I have access to rxbeach, I can merge this PR.

Copy link

@chriskr chriskr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments on naming, other than that LGTM.

src/reducer.ts Outdated Show resolved Hide resolved
src/reducer.ts Outdated
reducer: Reducer<State, VoidPayload>
): RegisteredReducer<State, VoidPayload>;
<State, Payload = VoidPayload>(
actionCreator: UnknownActionCreator | UnknownActionCreator[],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't aware of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is quite clear, so I was comfortable making this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, I've removed this option in the new actionReducer because:

  • it was hardly if ever used in ardoq-front
  • it was very annoying to type correctly, mostly forcing us to use any

src/reducer.ts Outdated Show resolved Hide resolved
src/reducer.ts Outdated Show resolved Hide resolved
src/reducer.ts Outdated Show resolved Hide resolved
src/reducer.ts Show resolved Hide resolved
@sseppola
Copy link
Contributor Author

I'm awaiting merging this PR until I have free time to follow through in ardoq-front.

@sseppola
Copy link
Contributor Author

Note to self and everyone interested.
Consider:

  • make actionReducer pass the whole action to its reducer function to align with reducers in React
  • remove streamReducer entirely in favor of state$.pipe, I think this is the same thing but pushing towards using the rxjs apis instead of custom ones.

@rafaa2
Copy link
Contributor

rafaa2 commented Jan 27, 2023

@sseppola: I personally have a problem with exposing action$ in the first place.
I believe action$ should not be accessible and developers use the provided APIs in rxBeach.
This will ensure that WE have better consistent patterns.

@sseppola
Copy link
Contributor Author

@rafaa2, I just noticed how we start a routine:

const routineSubscription = subscribeRoutine(action$, theRoutine, error$);

We basically have to expose action$ due to this, unless we make subscribeRoutine connect to action$ automatically.

@rafaa2
Copy link
Contributor

rafaa2 commented Jan 31, 2023

@sseppola very good point! And that was exactly my recommendation at first.
Then after we implemented it. We run into some issues (i can't honestly remember what were they now, but we had some)

@sseppola sseppola force-pushed the feature/deprecate_overloaded_reducer_creator branch from ab9a2cd to 9531ca0 Compare May 18, 2023 10:31
@sseppola
Copy link
Contributor Author

sseppola commented May 18, 2023

This is ready to go now, I just have a question regarding naming before I move on:

Technically our reducer is not a reducer, it creates a reducer. So for clarity I propose that we name the new functions:

  • createActionReducer
  • createStreamReducer

@@ -6,7 +6,7 @@ import { incrementMocks } from './internal/testing/mock';
const { reducers, actionCreators, handlers } = incrementMocks;
const { actions, words, numbers, errors } = incrementMocks.marbles;
const reducerArray = Object.values(reducers);
const alwaysReset = reducer(
const alwaysReset = reducer<number, any>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the problem with reducers that accept multiple actionCreators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants