Skip to content

Commit

Permalink
Merge branch 'fix-preloading' of https://github.com/soumak77/ionic
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed May 1, 2017
2 parents ed66591 + 19a3b66 commit 45e71a6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 25 deletions.
42 changes: 19 additions & 23 deletions src/util/module-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,25 @@ export interface LoadedModule {
/**
* @hidden
*/
export function setupPreloadingImplementation(config: Config, deepLinkConfig: DeepLinkConfig, moduleLoader: ModuleLoader) {
if (config.getBoolean('preloadModules')) {
const linksToLoad = deepLinkConfig.links.filter(link => !!link.loadChildren && link.priority !== 'off');

// Load the high priority modules first
const highPriorityPromises = linksToLoad.map(link => {
if (link.priority === 'high') {
return moduleLoader.load(link.loadChildren);
}
});

Promise.all(highPriorityPromises).then(() => {
// Load the low priority modules after the high priority are done
const lowPriorityPromises = linksToLoad.map(link => {
if (link.priority === 'low') {
return moduleLoader.load(link.loadChildren);
}
});
return Promise.all(lowPriorityPromises);
}).catch(err => {
console.error(err.message);
});
}
export function setupPreloadingImplementation(config: Config, deepLinkConfig: DeepLinkConfig, moduleLoader: ModuleLoader): Promise<any> {
if (config.getBoolean('preloadModules') && deepLinkConfig && deepLinkConfig.links) {
const linksToLoad = deepLinkConfig.links.filter(link => !!link.loadChildren && link.priority !== 'off');

// Load the high priority modules first
const highPriorityPromises = linksToLoad.filter(link => link.priority === 'high')
.map(link => moduleLoader.load(link.loadChildren));

return Promise.all(highPriorityPromises).then(() => {
// Load the low priority modules after the high priority are done
const lowPriorityPromises = linksToLoad.filter(link => link.priority === 'low')
.map(link => moduleLoader.load(link.loadChildren));
return Promise.all(lowPriorityPromises);
}).catch(err => {
console.error(err.message);
});
} else {
return Promise.resolve();
}
}

/**
Expand Down
113 changes: 111 additions & 2 deletions src/util/test/module-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ModuleLoader, LAZY_LOADED_TOKEN } from '../module-loader';
import { mockModuleLoader, mockNgModuleLoader } from '../mock-providers';
import { ModuleLoader, LAZY_LOADED_TOKEN, setupPreloadingImplementation } from '../module-loader';
import { mockModuleLoader, mockNgModuleLoader, mockConfig } from '../mock-providers';
import { NgModuleLoader } from '../ng-module-loader';
import { Config } from '../../config/config';
import { DeepLinkConfig } from '../../navigation/nav-util';


describe('module-loader', () => {
Expand Down Expand Up @@ -115,12 +117,119 @@ describe('module-loader', () => {

});

describe('setupPreloadingImplementation', () => {

it('should return a promise', (done: Function) => {
let promise = setupPreloadingImplementation(config, null, moduleLoader);
promise.then((response) => {
done();
}).catch((err: Error) => {
fail(err);
done(err);
});
});

it('should not call ModuleLoader when preloading disabled', (done: Function) => {
spyOn(moduleLoader, 'load').and.returnValue(Promise.resolve());

config.set('preloadModules', false);
const deepLinkConfig: DeepLinkConfig = {
links: []
};
let promise = setupPreloadingImplementation(config, deepLinkConfig, moduleLoader);
promise.then((response) => {
expect(moduleLoader.load).not.toHaveBeenCalled();
done();
}).catch((err: Error) => {
fail(err);
done(err);
});
});

it('should not call ModuleLoader when deepLinkConfig missing', (done: Function) => {
spyOn(moduleLoader, 'load').and.returnValue(Promise.resolve());

config.set('preloadModules', true);
let promise = setupPreloadingImplementation(config, null, moduleLoader);
promise.then((response) => {
expect(moduleLoader.load).not.toHaveBeenCalled();
done();
}).catch((err: Error) => {
fail(err);
done(err);
});
});

it('should not call ModuleLoader when no low or high priority links', (done: Function) => {
spyOn(moduleLoader, 'load').and.returnValue(Promise.resolve());

config.set('preloadModules', true);
const deepLinkConfig: DeepLinkConfig = {
links: [{
loadChildren: 'offString',
priority: 'off'
}]
};
let promise = setupPreloadingImplementation(config, deepLinkConfig, moduleLoader);
promise.then((response) => {
expect(moduleLoader.load).not.toHaveBeenCalled();
done();
}).catch((err: Error) => {
fail(err);
done(err);
});
});

it('should call ModuleLoader when has low priority links', (done: Function) => {
spyOn(moduleLoader, 'load').and.returnValue(Promise.resolve());

config.set('preloadModules', true);
const deepLinkConfig: DeepLinkConfig = {
links: [{
loadChildren: 'lowString',
priority: 'low'
}]
};
let promise = setupPreloadingImplementation(config, deepLinkConfig, moduleLoader);
promise.then((response) => {
expect(moduleLoader.load).toHaveBeenCalledWith('lowString');
done();
}).catch((err: Error) => {
fail(err);
done(err);
});
});

it('should call ModuleLoader when has high priority links', (done: Function) => {
spyOn(moduleLoader, 'load').and.returnValue(Promise.resolve());

config.set('preloadModules', true);
const deepLinkConfig: DeepLinkConfig = {
links: [{
loadChildren: 'highString',
priority: 'high'
}]
};
let promise = setupPreloadingImplementation(config, deepLinkConfig, moduleLoader);
promise.then((response) => {
expect(moduleLoader.load).toHaveBeenCalledWith('highString');
done();
}).catch((err: Error) => {
fail(err);
done(err);
});
});

});

var moduleLoader: ModuleLoader;
var ngModuleLoader: NgModuleLoader;
var config: Config;

beforeEach(() => {
ngModuleLoader = mockNgModuleLoader();
moduleLoader = mockModuleLoader(ngModuleLoader);
config = mockConfig();
});

});

0 comments on commit 45e71a6

Please sign in to comment.