-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Conversation
Preview docs changes for 08e7ad7 at https://previews.ngrx.io/pr2520-08e7ad7/ |
302e37f
to
7a94b55
Compare
What do you think about using a dictionary of |
@brandonroberts that works! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank @timdeschryver !
modules/store/src/runtime_checks.ts
Outdated
} | ||
|
||
const duplicates = Object.entries(REGISTERED_ACTION_TYPES) | ||
.filter(([_type, registrations]) => registrations > 1) |
There was a problem hiding this comment.
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.
modules/store/src/models.ts
Outdated
/** | ||
* Verifies that action types are not registered more than once | ||
*/ | ||
strictActionTypeUniqueness: boolean; |
There was a problem hiding this comment.
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?
modules/store/src/runtime_checks.ts
Outdated
export function _runtimeChecksFactory( | ||
runtimeChecks: RuntimeChecks | ||
): RuntimeChecks { | ||
return runtimeChecks; | ||
} | ||
|
||
export function _actionTypeUniquenessCheck(config: RuntimeChecks) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit : void
:)
🤔 now why does it format differently ... |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
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?
Other information