Skip to content

Commit

Permalink
fix(router): unsubscribe preload if the route has been preloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Wykks committed Oct 18, 2018
1 parent 6c48455 commit c023705
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/router/src/router_preloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {Compiler, Injectable, Injector, NgModuleFactoryLoader, NgModuleRef, OnDestroy} from '@angular/core';
import {Observable, Subscription, from, of } from 'rxjs';
import {catchError, concatMap, filter, map, mergeAll, mergeMap} from 'rxjs/operators';
import {catchError, concatMap, filter, map, mergeAll, mergeMap, takeUntil} from 'rxjs/operators';

import {LoadedRouterConfig, Route, Routes} from './config';
import {Event, NavigationEnd, RouteConfigLoadEnd, RouteConfigLoadStart} from './events';
Expand Down Expand Up @@ -122,12 +122,18 @@ export class RouterPreloader implements OnDestroy {
}

private preloadConfig(ngModule: NgModuleRef<any>, route: Route): Observable<void> {
let preloading = false;
return this.preloadingStrategy.preload(route, () => {
preloading = true;
const loaded$ = this.loader.load(ngModule.injector, route);
return loaded$.pipe(mergeMap((config: LoadedRouterConfig) => {
route._loadedConfig = config;
return this.processRoutes(config.module, config.routes);
}));
});
}).pipe(
takeUntil(this.router.events.pipe(filter((e) =>
(e instanceof RouteConfigLoadEnd) && !preloading && route === e.route
)))
);
}
}

0 comments on commit c023705

Please sign in to comment.