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(store): action status "PreHandler" #2305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

XavierDupessey
Copy link

Before this change, it was not possible to prepare for an action before it was processed by NGXS.

This is because the DISPATCHED event is emitted only after the action handlers have been executed, which is unclear from the documentation: ofActionDispatched: This will ONLY grab actions that have just been dispatched

Since changing this behavior would break the public API, this update introduces a new event, PreHandler, which allows reacting to an action before any handler is executed.

PR Checklist

Please check if your PR fulfills the following requirements:

Doc will be adapted if the PR is accepted (page to update is https://www.ngxs.io/concepts/actions/action-handlers)

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Currently, it was not possible to prepare for an action before it was processed by NGXS.

What is the new behavior?

The action PreHandler is emitted before NGXS executes any handler.

Does this PR introduce a breaking change?

[ ] Yes
[x] No (except if triggering more ofAction(MyAction) is a breaking change?

Before this change, it was not possible to prepare for an action before
it was processed by NGXS.

This is because the DISPATCHED event is emitted only after the action
handlers have been executed, which is unclear from the documentation:
ofActionDispatched: This will ONLY grab actions that have just been
dispatched

Since changing this behavior would break the public API, this update
introduces a new event, PreHandler, which allows reacting to an action
before any handler is executed.
@XavierDupessey
Copy link
Author

FYI @markwhitfeld this is the new feature I mentioned earlier :)

Copy link

nx-cloud bot commented Feb 17, 2025

View your CI Pipeline Execution ↗ for commit af4af0e.

Command Status Duration Result
nx run-many --target=test --all --configuration... ✅ Succeeded 2m 24s View ↗
nx run-many --target=lint --all --exclude=creat... ✅ Succeeded 6s View ↗
nx lint-types store ✅ Succeeded 4s View ↗
nx run-many --target=build --all ✅ Succeeded 1m 30s View ↗

☁️ Nx Cloud last updated this comment at 2025-02-17 16:32:51 UTC

Copy link

codeclimate bot commented Feb 17, 2025

Code Climate has analyzed commit af4af0e and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 95.3% (0.0% change).

View more on Code Climate.

Copy link
Member

@markwhitfeld markwhitfeld left a comment

Choose a reason for hiding this comment

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

Hi @XavierDupessey
Have you thought about implementing this callback through a dedicated plugin?
The Plugin API allows you to intercept each action call before it calls the handlers.
This plugin could expose a BeforeDispatch stream that is similar to the Actions stream.

@XavierDupessey
Copy link
Author

No, I hadn't considered implementing it as a plugin first. But you're right, it's very straightforward using the handle function provided by NgxsPlugin from @ngxs/store/plugins.

If you believe there's no need to expose this event in the core, I'm totally fine with going the plugin route instead! 🙂

@markwhitfeld
Copy link
Member

I'll have a chat with the team about it, but for now I think that the plugin route is best.
If you would like to contribute a page to our "Recipes" section of the docs with the code that you create and some instructions, that would be a great way to share the pattern with the community.

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.

2 participants