Skip to content

Commit

Permalink
fix(eventbus): workaround for ngxs router plugin issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Nov 18, 2018
1 parent 49c3056 commit 600837e
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions libs/core/src/lib/state/eventbus.ts
Expand Up @@ -8,11 +8,12 @@ import {
WebSocketConnected,
WebSocketDisconnected,
} from '@ngx-starter-kit/socketio-plugin';
import { RouterNavigation } from '@ngxs/router-plugin';
import { RouterNavigation, RouterState } from '@ngxs/router-plugin';
import { RouterStateData } from '@ngx-starter-kit/core';
import { distinctUntilChanged, map } from 'rxjs/operators';
import { distinctUntilChanged, filter, map } from 'rxjs/operators';
import { PageTitleService } from '../services/page-title.service';
import { GoogleAnalyticsService } from '../services/google-analytics.service';
import { NavigationEnd, Router, RouterEvent } from '@angular/router';

@Injectable({
providedIn: 'root',
Expand All @@ -21,6 +22,7 @@ export class EventBus {
constructor(
private actions$: Actions,
private store: Store,
private router: Router,
private analytics: GoogleAnalyticsService,
private pageTitle: PageTitleService,
) {
Expand All @@ -47,17 +49,32 @@ export class EventBus {
.subscribe(action => console.log('WebSocketDisconnected........Action Successful'));

// FIXME : https://github.com/ngxs/store/issues/542
this.actions$
// this.actions$
// .pipe(
// ofActionSuccessful(RouterNavigation),
// map((action: RouterNavigation) => action.routerState as any),
// distinctUntilChanged((previous: RouterStateData, current: RouterStateData) => {
// return previous.url === current.url;
// }),
// )
// .subscribe(data => {
// this.pageTitle.setTitle(data.breadcrumbs);
// this.analytics.setPage(data.url);
// });

// WORKAROUND
this.router.events
.pipe(
ofActionSuccessful(RouterNavigation),
map((action: RouterNavigation) => action.routerState as any),
distinctUntilChanged((previous: RouterStateData, current: RouterStateData) => {
filter(event => event instanceof NavigationEnd),
distinctUntilChanged((previous: any, current: RouterEvent) => {
return previous.url === current.url;
}),
)
.subscribe(data => {
.subscribe((event: NavigationEnd) => {
const data = this.store.selectSnapshot<RouterStateData>(RouterState.state);
this.pageTitle.setTitle(data.breadcrumbs);
this.analytics.setPage(data.url);
});

}
}

0 comments on commit 600837e

Please sign in to comment.