import { Location } from "history"; import { map } from "rxjs/operators"; import { BehaviorSubject, Observable } from "rxjs"; export class PageStateService { private static instance: PageStateService; private readonly location$: BehaviorSubject = new BehaviorSubject(null); public static getInstance(): PageStateService { if (!PageStateService.instance) { PageStateService.instance = new PageStateService(); } return PageStateService.instance; } private constructor() {} public setLocation(location: Location) { this.location$.next(location); } public getState(): T { return this.location$.getValue().state as T; } public getRoute(): string { return this.location$.value?.pathname; } public get state$(): Observable { return this.location$.pipe(map(location => location.state)); } public get route$(): Observable { return this.location$.pipe(map(location => location?.pathname)); } }