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(upgrade): update $locationShim to handle Location changes before … #36498

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 28 additions & 12 deletions packages/common/upgrade/src/location_shim.ts
Expand Up @@ -8,6 +8,7 @@

import {Location, LocationStrategy, PlatformLocation} from '@angular/common';
import {UpgradeModule} from '@angular/upgrade/static';
import {ReplaySubject} from 'rxjs';

import {UrlCodec} from './params';
import {deepEqual, isAnchor, isPromise} from './utils';
Expand Down Expand Up @@ -50,7 +51,7 @@ export class $locationShim {

private cachedState: unknown = null;


private urlChanges = new ReplaySubject<{newUrl: string, newState: unknown}>(1);

constructor(
$injector: any, private location: Location, private platformLocation: PlatformLocation,
Expand All @@ -71,6 +72,10 @@ export class $locationShim {
this.cacheState();
this.$$state = this.browserState();

this.location.onUrlChange((newUrl, newState) => {
this.urlChanges.next({newUrl, newState});
});

if (isPromise($injector)) {
$injector.then($i => this.initialize($i));
} else {
Expand All @@ -88,7 +93,7 @@ export class $locationShim {
return;
}

let elm: (Node & ParentNode)|null = event.target;
let elm: (Node&ParentNode)|null = event.target;

// traverse the DOM up to find first A tag
while (elm && elm.nodeName.toLowerCase() !== 'a') {
Expand Down Expand Up @@ -124,9 +129,9 @@ export class $locationShim {
}
});

this.location.onUrlChange((newUrl, newState) => {
let oldUrl = this.absUrl();
let oldState = this.$$state;
this.urlChanges.subscribe(({newUrl, newState}) => {
const oldUrl = this.absUrl();
const oldState = this.$$state;
this.$$parse(newUrl);
newUrl = this.absUrl();
this.$$state = newState;
Expand Down Expand Up @@ -286,7 +291,9 @@ export class $locationShim {
* This function emulates the $browser.state() function from AngularJS. It will cause
* history.state to be cached unless changed with deep equality check.
*/
private browserState(): unknown { return this.cachedState; }
private browserState(): unknown {
return this.cachedState;
}

private stripBaseUrl(base: string, url: string) {
if (url.startsWith(base)) {
Expand Down Expand Up @@ -446,7 +453,9 @@ export class $locationShim {
* // => "http://example.com/#/some/path?foo=bar&baz=xoxo"
* ```
*/
absUrl(): string { return this.$$absUrl; }
absUrl(): string {
return this.$$absUrl;
}

/**
* Retrieves the current URL, or sets a new URL. When setting a URL,
Expand Down Expand Up @@ -488,7 +497,9 @@ export class $locationShim {
* // => "http"
* ```
*/
protocol(): string { return this.$$protocol; }
protocol(): string {
return this.$$protocol;
}

/**
* Retrieves the protocol of the current URL.
Expand All @@ -509,7 +520,9 @@ export class $locationShim {
* // => "example.com:8080"
* ```
*/
host(): string { return this.$$host; }
host(): string {
return this.$$host;
}

/**
* Retrieves the port of the current URL.
Expand All @@ -520,7 +533,9 @@ export class $locationShim {
* // => 80
* ```
*/
port(): number|null { return this.$$port; }
port(): number|null {
return this.$$port;
}

/**
* Retrieves the path of the current URL, or changes the path and returns a reference to its own
Expand Down Expand Up @@ -553,7 +568,7 @@ export class $locationShim {
}

/**
* Retrieves a map of the search parameters of the current URL, or changes a search
* Retrieves a map of the search parameters of the current URL, or changes a search
* part and returns a reference to its own instance.
*
*
Expand All @@ -576,7 +591,8 @@ export class $locationShim {
* If the argument is a hash object containing an array of values, these values will be encoded
* as duplicate search parameters in the URL.
*
* @param {(string|Number|Array<string>|boolean)=} paramValue If `search` is a string or number, then `paramValue`
* @param {(string|Number|Array<string>|boolean)=} paramValue If `search` is a string or number,
* then `paramValue`
* will override only a single search property.
*
* If `paramValue` is an array, it will override the property of the `search` component of
Expand Down