Skip to content

Commit

Permalink
refactor(common): cleanup platformLocation (#50054)
Browse files Browse the repository at this point in the history
* Drop the usage of @Inject()
* Drop `supportsState` as its supported by evergreen browsers.

PR Close #50054
  • Loading branch information
JeanMeche authored and alxhub committed May 9, 2023
1 parent 258de81 commit bc65112
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 34 deletions.
2 changes: 1 addition & 1 deletion goldens/public-api/common/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class AsyncPipe implements OnDestroy, PipeTransform {

// @public
export class BrowserPlatformLocation extends PlatformLocation {
constructor(_doc: any);
constructor();
// (undocumented)
back(): void;
// (undocumented)
Expand Down
37 changes: 7 additions & 30 deletions packages/common/src/location/platform_location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Inject, Injectable, InjectionToken, ɵɵinject} from '@angular/core';
import {inject, Injectable, InjectionToken} from '@angular/core';

import {getDOM} from '../dom_adapter';
import {DOCUMENT} from '../dom_tokens';
Expand All @@ -33,11 +33,7 @@ import {DOCUMENT} from '../dom_tokens';
*
* @publicApi
*/
@Injectable({
providedIn: 'platform',
// See #23917
useFactory: useBrowserPlatformLocation
})
@Injectable({providedIn: 'platform', useFactory: () => inject(BrowserPlatformLocation)})
export abstract class PlatformLocation {
abstract getBaseHrefFromDOM(): string;
abstract getState(): unknown;
Expand Down Expand Up @@ -71,10 +67,6 @@ export abstract class PlatformLocation {
}
}

export function useBrowserPlatformLocation() {
return ɵɵinject(BrowserPlatformLocation);
}

/**
* @description
* Indicates when a location is initialized.
Expand Down Expand Up @@ -112,14 +104,14 @@ export interface LocationChangeListener {
*/
@Injectable({
providedIn: 'platform',
// See #23917
useFactory: createBrowserPlatformLocation,
useFactory: () => new BrowserPlatformLocation(),
})
export class BrowserPlatformLocation extends PlatformLocation {
private _location: Location;
private _history: History;
private _doc = inject(DOCUMENT);

constructor(@Inject(DOCUMENT) private _doc: any) {
constructor() {
super();
this._location = window.location;
this._history = window.history;
Expand Down Expand Up @@ -167,19 +159,11 @@ export class BrowserPlatformLocation extends PlatformLocation {
}

override pushState(state: any, title: string, url: string): void {
if (supportsState()) {
this._history.pushState(state, title, url);
} else {
this._location.hash = url;
}
this._history.pushState(state, title, url);
}

override replaceState(state: any, title: string, url: string): void {
if (supportsState()) {
this._history.replaceState(state, title, url);
} else {
this._location.hash = url;
}
this._history.replaceState(state, title, url);
}

override forward(): void {
Expand All @@ -198,10 +182,3 @@ export class BrowserPlatformLocation extends PlatformLocation {
return this._history.state;
}
}

export function supportsState(): boolean {
return !!window.history.pushState;
}
export function createBrowserPlatformLocation() {
return new BrowserPlatformLocation(ɵɵinject(DOCUMENT));
}
3 changes: 0 additions & 3 deletions packages/core/test/bundling/router/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -1988,9 +1988,6 @@
{
"name": "subsetMatchOptions"
},
{
"name": "supportsState"
},
{
"name": "switchMap"
},
Expand Down

0 comments on commit bc65112

Please sign in to comment.