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

fix(component-store): use asapScheduler to schedule lifecycle hooks check #3580

Merged
merged 1 commit into from
Sep 19, 2022

Conversation

amakhrov
Copy link
Contributor

Switch from asyncScheduler to asapScheduler to prevent the "1 periodic timer(s) still in the queue" error.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] 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?

Closes #3573

What is the new behavior?

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

@netlify
Copy link

netlify bot commented Sep 16, 2022

Deploy Preview for ngrx-io canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit 46357a3
🔍 Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/6325009d6f3cdc00085b33dc

@@ -322,7 +322,7 @@ export class ComponentStore<T extends object> implements OnDestroy {
* but not used with provideComponentStore()
*/
private checkProviderForHooks() {
asyncScheduler.schedule(() => {
asapScheduler.schedule(() => {
if (
isDevMode() &&
Copy link
Contributor Author

@amakhrov amakhrov Sep 16, 2022

Choose a reason for hiding this comment

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

Additionally, I'm wondering whether the isDevMode() check should be performed at the very beginning, guarding the whole asapScheduler.schedule call, instead of just scheduled callback body

Copy link
Member

Choose a reason for hiding this comment

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

I tried using isDevMode() outside the callback body, and it doesn't work as intended.

@markostanimirovic markostanimirovic changed the title fix(component-store): allow to unit-test ComponentStore with fakeAsync fix(component-store): use asapScheduler to schedule lifecycle hooks check Sep 16, 2022
@markostanimirovic
Copy link
Member

I renamed the PR title to avoid potential confusion because we can use ComponentStore within the fakeAsync, but we need to add at least one tick that will flush scheduled macrotasks:

it('works before changes from this PR', fakeAsync(() => {
  new ComponentStore();
  tick(0);
}); // no error

asyncScheduler.schedule(() => {
asapScheduler.schedule(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the PR, @amakhrov! Can you add a unit test that verifies that there is no error?

Copy link
Contributor Author

@amakhrov amakhrov Sep 16, 2022

Choose a reason for hiding this comment

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

what if I just change tick(0) to flushMicrotasks() in the existing tests (there are 2 of them for the hooks feature).

Copy link
Member

Choose a reason for hiding this comment

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

sounds good 👍

Copy link
Member

Choose a reason for hiding this comment

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

I would still prefer a unit test though, the reproduction in the issue would be great.
This verifies that it solves that issue, and it also guard us against our future selves if we would change it back to another scheduler.

Copy link
Contributor

Choose a reason for hiding this comment

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

@timdeschryver It's hard to test for that. I'd say the flushMicrotasks(); change in unit tests is the one that "reproduces" the issue.

@amakhrov amakhrov force-pushed the component-store/asap-scheduler branch from fb028d6 to b83f5cf Compare September 16, 2022 23:02
…heck

This prevent "1 periodic timer(s) still in the queue" error by fakeAsync.
@amakhrov amakhrov force-pushed the component-store/asap-scheduler branch from b83f5cf to 46357a3 Compare September 16, 2022 23:02
asyncScheduler.schedule(() => {
asapScheduler.schedule(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

@timdeschryver It's hard to test for that. I'd say the flushMicrotasks(); change in unit tests is the one that "reproduces" the issue.

@timdeschryver timdeschryver merged commit 02431b4 into ngrx:master Sep 19, 2022
@amakhrov amakhrov deleted the component-store/asap-scheduler branch September 19, 2022 16:46
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.

Error: 1 periodic timer(s) still in the queue when testing a store with fakeAsync
5 participants