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): add runtime check for action type uniqueness #2520

Merged
merged 5 commits into from May 27, 2020

Conversation

timdeschryver
Copy link
Member

PR Checklist

Please check if your PR fulfills the following requirements:

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 there is no way to warn devs when there are multiple actions with the same action type.
I've seen that this might lead to some confusion and some debugging time to spot the cause of a bug.

The most recent one, https://twitter.com/FabianGosebrink/status/1259043401724542978

The ngrx-tslint-rules has a TSLint rule to spot these, but not many people know that this exists.

What if we create a runtime check for it?

What is the new behavior?

In the PR, you see that action types are registered in a global array while the action is instantiated with createAction. When a store module is loaded, we can perform a check to see if there are duplicates in this array, and throw an error if there are.

This new feature is by default disabled, but can be enabled with the strictActionTypeUniqueness runtime check flag.

Thoughts?

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 9, 2020

Preview docs changes for 08e7ad7 at https://previews.ngrx.io/pr2520-08e7ad7/

@brandonroberts
Copy link
Member

What do you think about using a dictionary of ActionType: boolean instead of array for lookups?

@timdeschryver
Copy link
Member Author

@brandonroberts that works!
I will make the change later and with it, I will add a new section to the docs.

Copy link
Member

@alex-okrushko alex-okrushko left a comment

Choose a reason for hiding this comment

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

Thank @timdeschryver !

}

const duplicates = Object.entries(REGISTERED_ACTION_TYPES)
.filter(([_type, registrations]) => registrations > 1)
Copy link
Member

Choose a reason for hiding this comment

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

just [, registrations] if _type is unused.

/**
* Verifies that action types are not registered more than once
*/
strictActionTypeUniqueness: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

Can we keep it optional?

export function _runtimeChecksFactory(
runtimeChecks: RuntimeChecks
): RuntimeChecks {
return runtimeChecks;
}

export function _actionTypeUniquenessCheck(config: RuntimeChecks) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit : void :)

@timdeschryver
Copy link
Member Author

timdeschryver commented May 27, 2020

🤔 now why does it format differently ...
EDIT: this seems to be cause of prettier v2 - should be resolved after merging #2535 and running it on all files.

@brandonroberts brandonroberts merged commit 2972980 into master May 27, 2020
@brandonroberts brandonroberts deleted the pr/runtimecheck-action-type branch May 27, 2020 23:51
BioPhoton pushed a commit to BioPhoton/platform that referenced this pull request Jun 5, 2020
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.

None yet

5 participants