Skip to content

Commit

Permalink
fix(app-loader): should not set loading when app assets is loaded #107
Browse files Browse the repository at this point in the history
  • Loading branch information
why520crazy authored and walkerkay committed Dec 12, 2020
1 parent fa7d429 commit 09a3f2e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ describe('PlanetApplicationLoader', () => {
planetApplicationLoader.reroute({ url: '/app1/dashboard' });

appStatusChangeFaker.expectHaveBeenCalledWith({ app: app1, status: ApplicationStatus.assetsLoading });
expect(planetApplicationLoader.loadingDone).toBe(false);

expect(appsLoadingStartSpy).toHaveBeenCalled();
expect(appsLoadingStartSpy).toHaveBeenCalledWith({
Expand Down Expand Up @@ -250,6 +249,35 @@ describe('PlanetApplicationLoader', () => {
tick();
}));

it(`should not set loadingDone as false when app1 navigateByUrl and app1 is active`, fakeAsync(() => {
const loadAppAssets$ = new Subject<[AssetsLoadResult[], AssetsLoadResult[]]>();
const assetsLoaderSpy = spyOn(assetsLoader, 'loadAppAssets');
assetsLoaderSpy.and.returnValue(loadAppAssets$);

const appStatusChangeFaker = AppStatusChangeFaker.create(planetApplicationLoader);
const app1RefFaker = PlanetApplicationRefFaker.create(app1.name);

planetApplicationLoader.reroute({ url: '/app1/dashboard' });
loadAppAssets$.next();
loadAppAssets$.complete();
flush();
app1RefFaker.haveBeenBootstrap();
app1RefFaker.bootstrap();

expect(appStatusChangeFaker.spy).toHaveBeenCalledTimes(5);
expect(app1RefFaker.navigateByUrlSpy).not.toHaveBeenCalled();
planetApplicationLoader.reroute({ url: '/app1/dashboard2' });

// app1 has been loaded
expect(planetApplicationLoader.loadingDone).toEqual(true);
flush();

// app2 has been not loaded
planetApplicationLoader.reroute({ url: '/app2/dashboard1' });
expect(planetApplicationLoader.loadingDone).toEqual(false);
flush();
}));

it(`should not call app1 navigateByUrl when app1 is active and url is same`, fakeAsync(() => {
const loadAppAssets$ = new Subject<[AssetsLoadResult[], AssetsLoadResult[]]>();
const assetsLoaderSpy = spyOn(assetsLoader, 'loadAppAssets');
Expand Down
6 changes: 5 additions & 1 deletion packages/planet/src/application/planet-application-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export class PlanetApplicationLoader {
return of(event).pipe(
// unload apps and return should load apps
map(() => {
this.loadingDone = false;
this.startRouteChangeEvent = event;
const shouldLoadApps = this.planetApplicationService.getAppsByMatchedUrl(event.url);
const shouldUnloadApps = this.getUnloadApps(shouldLoadApps);
Expand All @@ -149,20 +148,25 @@ export class PlanetApplicationLoader {
}),
// Load app assets (static resources)
switchMap(shouldLoadApps => {
let hasAppsNeedLoadingAssets = false;
const loadApps$ = shouldLoadApps.map(app => {
const appStatus = this.appsStatus.get(app);
if (
!appStatus ||
appStatus === ApplicationStatus.assetsLoading ||
appStatus === ApplicationStatus.loadError
) {
hasAppsNeedLoadingAssets = true;
return this.ngZone.runOutsideAngular(() => {
return this.startLoadAppAssets(app);
});
} else {
return of(app);
}
});
if (hasAppsNeedLoadingAssets) {
this.loadingDone = false;
}
return loadApps$.length > 0 ? forkJoin(loadApps$) : of([] as PlanetApplication[]);
}),
// Bootstrap or show apps
Expand Down

0 comments on commit 09a3f2e

Please sign in to comment.