Skip to content

Commit

Permalink
refactor(router-plugin): reduce RxJS depedency (#2291)
Browse files Browse the repository at this point in the history
* refactor(router-plugin): reduce RxJS depedency

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.

* chore: update CHANGELOG.md
  • Loading branch information
arturovt authored Jan 2, 2025
1 parent fbc4fdb commit 0d2e6d0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $ npm install @ngxs/store@dev
- Build(store): Use `ngServerMode` to check whether we are in SSR [#2287](https://github.com/ngxs/store/pull/2287)
- Build(storage-plugin): Use `ngServerMode` to check whether we are in SSR [#2288](https://github.com/ngxs/store/pull/2288)
- Refactor(form-plugin): Replace `takeUntil` with `takeUntilDestroyed` [#2283](https://github.com/ngxs/store/pull/2283)
- Refactor(router-plugin): Reduce RxJS depedency [#2291](https://github.com/ngxs/store/pull/2291)

### 19.0.0 2024-12-3

Expand Down
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 0d2e6d0

Please sign in to comment.