-
-
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 strictActionWithinNgZone runtime check #2364
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0b1e777
add strictActionWithinNgZone runtime_check
64e5f3f
Default check off,
931a528
Add documentation
86d2c23
Update error to include Action type
652dc5a
Improve tests
bb2525b
add strictActionWithinNgZone runtime_check
8540fa9
Default check off,
722f10d
Add documentation
d989cfc
Update error to include Action type
dfbe89c
Improve tests
0757c6b
Merge branch 'ngrx-strictNgZone' of https://github.com/StephenCooper/…
eda4221
apply ngrx filter
fe2c76e
add missing test case
1785e1b
add strictActionWithinNgZone runtime check
a24a49b
Merge branch 'sc-strictNgZone' of https://github.com/StephenCooper/pl…
d51d4a1
Update modules/store/spec/meta-reducers/inNgZoneAssert_reducer.spec.ts
564c7a8
Update projects/ngrx.io/content/guide/store/configuration/runtime-che…
3c27f64
Update runtime-checks with default status
ac28681
add docs url to error messages
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
modules/store/spec/meta-reducers/inNgZoneAssert_reducer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as ngCore from '@angular/core'; | ||
import { inNgZoneAssertMetaReducer } from '../../src/meta-reducers'; | ||
|
||
describe('inNgZoneAssertMetaReducer:', () => { | ||
it('should not throw if in NgZone', () => { | ||
ngCore.NgZone.isInAngularZone = jasmine | ||
.createSpy('isInAngularZone') | ||
.and.returnValue(true); | ||
expect(() => invokeActionReducer((state: any) => state)).not.toThrow(); | ||
expect(ngCore.NgZone.isInAngularZone).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw when not in NgZone', () => { | ||
ngCore.NgZone.isInAngularZone = jasmine | ||
.createSpy('isInAngularZone') | ||
.and.returnValue(false); | ||
expect(() => invokeActionReducer((state: any) => state)).toThrowError( | ||
`Action 'invoke' running outside NgZone. https://ngrx.io/guide/store/configuration/runtime-checks#strictactionwithinngzone` | ||
); | ||
expect(ngCore.NgZone.isInAngularZone).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call isInAngularZone when check is off', () => { | ||
ngCore.NgZone.isInAngularZone = jasmine.createSpy('isInAngularZone'); | ||
expect(() => | ||
invokeActionReducer((state: any) => state, false) | ||
).not.toThrow(); | ||
expect(ngCore.NgZone.isInAngularZone).not.toHaveBeenCalled(); | ||
}); | ||
|
||
function invokeActionReducer(reduce: Function, checkIsOn = true) { | ||
inNgZoneAssertMetaReducer((state, action) => reduce(state, action), { | ||
action: () => checkIsOn, | ||
})({}, { type: 'invoke' }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as ngCore from '@angular/core'; | ||
import { Action, ActionReducer } from '../models'; | ||
import { RUNTIME_CHECK_URL } from './utils'; | ||
|
||
export function inNgZoneAssertMetaReducer( | ||
reducer: ActionReducer<any, Action>, | ||
checks: { action: (action: Action) => boolean } | ||
) { | ||
return function(state: any, action: Action) { | ||
if (checks.action(action) && !ngCore.NgZone.isInAngularZone()) { | ||
throw new Error( | ||
`Action '${ | ||
action.type | ||
}' running outside NgZone. ${RUNTIME_CHECK_URL}#strictactionwithinngzone` | ||
); | ||
} | ||
return reducer(state, action); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { immutabilityCheckMetaReducer } from './immutability_reducer'; | ||
export { serializationCheckMetaReducer } from './serialization_reducer'; | ||
export { inNgZoneAssertMetaReducer } from './inNgZoneAssert_reducer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we link to the docs here (and also for the realizability check)?
It can guide devs to understand what the problem is, and how to solve it.
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.
That was my thinking behind putting the name of the check in the message.
Direct link to the docs would make that even easier.
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.
Good idea