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: enable HMR of persistentReducedStreams (opt in) #555

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

Conversation

sseppola
Copy link
Contributor

@sseppola sseppola commented Dec 5, 2022

Adds:

  • a global config object in order to configure the internals of rxbeach
  • enables opt-in HMR of persistentReducedStreams

What it does NOT handle:

  • HMR of reduceState() and action$.pipe()s

I'm hoping we can use this to start a discussion about how we can enable full HMR support over time, the challenge is mainly accessing the internal state of observables in order to set the initial state based on the previous state like this PR does with persistentReducedStreams.

Without being well versed in RXJS, I think the pattern of directly subscribing to the $action stream is problematic because it exposes implementation details to the end user, and prevents us from wrapping the registration to add things like HMR. To get where we want we need to be able to:

  1. Access a $action stream observable's internal state
    • eg. in development, tap into the stream and store its local state where the re-registration can access it
  2. Unregister an the observable from the $action stream
    • .pipe seems to remove this flexibility from us/requires that the developer register HMR manually
    • Perhaps we abstract the action$.pipe subscription in order to combine the action$ stream with a development-mode takeUntil(onDestroy$)
  3. Re-register the observable with the new implementation and previous state
    • The biggest question here is where do we get the previous state from, but there are many ways of doing this

I think it should be possible, my biggest problem at the moment is that I do not know RXJS well enough.

Not sure who else should review, but please add anyone who could have insight into this as I'm actively looking for feedback.

@@ -48,9 +49,31 @@ export const persistentReducedStream = <State>(
tag(name)
);

const stream = new ObservableState(name, source$, initialState);
try {
Copy link

@chriskr chriskr Dec 6, 2022

Choose a reason for hiding this comment

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

Why are you using try-catch instead of module.hot?

That error detection seems the wrong trigger for hot reloading?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question!

This file is not the file that is being hot reloaded, it is the file that uses this function that will be reloaded. So that file will run multiple times and register a persistentReducedStream each time, which typically is a duplicate reducer error that needs to be handled in order to allow HMR to happen.

Copy link

Choose a reason for hiding this comment

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

I see. Using that error type is an indirect pattern, that is basically not ideal. I think it would be better to add some info in dev mode to the subscriber that this is a subscription triggered by a hot reloading to make the reason for the re-subscription explicit.

Copy link
Contributor

@rafaa2 rafaa2 left a comment

Choose a reason for hiding this comment

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

For this part of it. All looks good to me, except for the comment from chriskr.

But also have you tested that this is working?

@sseppola
Copy link
Contributor Author

It's working, but a better solution is required. I haven't had time to revisit it yet, but I'll circle back to it. I'm getting exposed to a lot more work with streams and rxbeach so I'll be better suited for the next iteration 👌

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.

3 participants