Skip to content

Commit

Permalink
refactor(router-plugin): reduce RxJS depedency
Browse files Browse the repository at this point in the history
In this commit, we only unsubscribe from `router.events`. The `Store` is automatically completed
by NGXS when the root injector is destroyed; therefore, we remove the replay subject
and the `takeUntil` dependency.
  • Loading branch information
arturovt committed Jan 2, 2025
1 parent fbc4fdb commit a860243
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions packages/router-plugin/src/router.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
NavigationActionTiming,
ɵNGXS_ROUTER_PLUGIN_OPTIONS
} from '@ngxs/router-plugin/internals';
import { ReplaySubject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import type { Subscription } from 'rxjs';

import {
Navigate,
Expand Down Expand Up @@ -84,7 +83,7 @@ export class RouterState {

private _options = inject(ɵNGXS_ROUTER_PLUGIN_OPTIONS);

private _destroy$ = new ReplaySubject<void>(1);
private _subscription!: Subscription;

@Selector()
static state<T = RouterStateSnapshot>(state: RouterStateModel<T>) {
Expand All @@ -102,7 +101,7 @@ export class RouterState {
this._setUpStoreListener();
this._setUpRouterEventsListener();

inject(DestroyRef).onDestroy(() => this._destroy$.next());
inject(DestroyRef).onDestroy(() => this._subscription.unsubscribe());
}

@Action(Navigate)
Expand Down Expand Up @@ -135,9 +134,8 @@ export class RouterState {
}

private _setUpStoreListener(): void {
const routerState$ = this._store
.select(ROUTER_STATE_TOKEN)
.pipe(takeUntil(this._destroy$));
const routerState$ = this._store.select(ROUTER_STATE_TOKEN);

routerState$.subscribe((state: RouterStateModel | undefined) => {
this._navigateIfNeeded(state);
});
Expand Down Expand Up @@ -172,8 +170,7 @@ export class RouterState {

let lastRoutesRecognized: RoutesRecognized;

const events$ = this._router.events.pipe(takeUntil(this._destroy$));
events$.subscribe(event => {
this._subscription = this._router.events.subscribe(event => {
this._lastEvent = event;

if (event instanceof NavigationStart) {
Expand Down

0 comments on commit a860243

Please sign in to comment.