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(common): prevent duplicate URL change notifications #37404

Conversation

LayZeeDK
Copy link
Contributor

@LayZeeDK LayZeeDK commented Jun 2, 2020

Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using Location#onUrlChange.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

When two URL change listeners are registered, the first one will receive duplicate URL change notifications.

Issue Number: N/A

What is the new behavior?

When two URL change listeners are registered, both of them will only receive a single notification when the URL changes.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Cc @aaronfrost

Location#onUrlChange is only used by NgUpgrade's $location shim for AngularJS.

@LayZeeDK LayZeeDK force-pushed the LayZeeDK/common/bugfix-location-duplicate-url-change-events branch 2 times, most recently from ac26acf to 526aaae Compare June 2, 2020 21:55
@LayZeeDK LayZeeDK marked this pull request as ready for review June 2, 2020 22:10
@LayZeeDK
Copy link
Contributor Author

LayZeeDK commented Jun 2, 2020

Does it make sense to add an ngOnDestroy lifecycle hook to unsubscribe from the _onUrlChangeSubscription in the Location service? Would this make a difference to multiple applications on the same page? Is there a use case for applications being shut down after bootstrapping them? Hot Module Replacement maybe?

@pullapprove pullapprove bot requested a review from kara June 2, 2020 22:18
@JoostK
Copy link
Member

JoostK commented Jun 3, 2020

Does it make sense to add an ngOnDestroy lifecycle hook to unsubscribe from the _onUrlChangeSubscription in the Location service? Would this make a difference to multiple applications on the same page? Is there a use case for applications being shut down after bootstrapping them? Hot Module Replacement maybe?

It looks like it won't be necessary to teardown the subscription manually, as the source is an EventEmitter that is local to the instance, and all referenced memory is also local to the instance. As soon as the instance is no longer referenced from other places, it will become eligible for the GC to collect.

@JoostK JoostK requested review from atscott and removed request for kara June 3, 2020 11:05
@ngbot ngbot bot added this to the needsTriage milestone Jun 3, 2020
@JoostK JoostK added freq1: low action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release risk: low severity3: broken type: bug/fix workaround2: non-obvious labels Jun 3, 2020
@LayZeeDK LayZeeDK force-pushed the LayZeeDK/common/bugfix-location-duplicate-url-change-events branch from c2d172d to 4f0215d Compare June 3, 2020 13:33
Copy link
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

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

👍 Thanks for this fix! Only some small nits in the comments

packages/common/src/location/location.ts Outdated Show resolved Hide resolved
packages/common/test/location/location_spec.ts Outdated Show resolved Hide resolved
@atscott
Copy link
Contributor

atscott commented Jun 3, 2020

presubmit

@LayZeeDK LayZeeDK force-pushed the LayZeeDK/common/bugfix-location-duplicate-url-change-events branch from 4f0215d to 3fd0a17 Compare June 4, 2020 22:01
@LayZeeDK
Copy link
Contributor Author

LayZeeDK commented Jun 4, 2020

Good to go.

I'll open another PR after merge to:

  • Prefer private over @internal
  • Prefer TestBed.inject over inject

in these files.

Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using Location#onUrlChange.
@LayZeeDK LayZeeDK force-pushed the LayZeeDK/common/bugfix-location-duplicate-url-change-events branch from 3fd0a17 to d6ff176 Compare June 4, 2020 22:11
@atscott atscott added the action: merge The PR is ready for merge by the caretaker label Jun 4, 2020
@LayZeeDK
Copy link
Contributor Author

LayZeeDK commented Jun 4, 2020

I think you have to remove the PR action: review label, @atscott 🙂

@atscott atscott removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Jun 4, 2020
atscott pushed a commit that referenced this pull request Jun 4, 2020
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using Location#onUrlChange.

PR Close #37404
@atscott atscott closed this in 3569fdf Jun 4, 2020
@LayZeeDK LayZeeDK deleted the LayZeeDK/common/bugfix-location-duplicate-url-change-events branch June 5, 2020 00:27
ngwattcos pushed a commit to ngwattcos/angular that referenced this pull request Jun 25, 2020
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using Location#onUrlChange.

PR Close angular#37404
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 6, 2020
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using Location#onUrlChange.

PR Close angular#37404
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: router cla: yes freq1: low risk: low target: patch This PR is targeted for the next patch release type: bug/fix workaround2: non-obvious
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants