Skip to content

Commit

Permalink
feat(module-loader): add caching for ngModuleLoader on load
Browse files Browse the repository at this point in the history
  • Loading branch information
brandyscarney committed Mar 6, 2017
1 parent c634395 commit 17359b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 12 additions & 2 deletions src/util/module-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentFactoryResolver, Injectable, Injector, OpaqueToken, Type } from '@angular/core';
import { ComponentFactoryResolver, Injectable, Injector, NgModuleFactory, OpaqueToken, Type } from '@angular/core';
import { DeepLinkConfig } from '../navigation/nav-util';
import { NgModuleLoader } from './ng-module-loader';

Expand All @@ -15,6 +15,8 @@ export class ModuleLoader {
/** @internal */
_cfrMap = new Map<any, ComponentFactoryResolver>();

_promiseMap = new Map<string, Promise<NgModuleFactory<any>>>();

constructor(
private _ngModuleLoader: NgModuleLoader,
private _injector: Injector) {}
Expand All @@ -25,7 +27,15 @@ export class ModuleLoader {

const splitString = modulePath.split(SPLITTER);

return this._ngModuleLoader.load(splitString[0], splitString[1]).then(loadedModule => {
let promise = this._promiseMap.get(modulePath);
if (!promise) {
promise = this._ngModuleLoader.load(splitString[0], splitString[1]);
this._promiseMap.set(modulePath, promise);
}

return promise.then(loadedModule => {
// clear it from the cache
this._promiseMap.delete(modulePath);
console.timeEnd(`ModuleLoader, load: ${modulePath}'`);
const ref = loadedModule.create(this._injector);

Expand Down
12 changes: 2 additions & 10 deletions src/util/test/module-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { DeepLinkConfig } from '../../navigation/nav-util';
import { DeepLinker } from '../../navigation/deep-linker';
import { ModuleLoader } from '../module-loader';
import { mockDeepLinkConfig, mockDeepLinker, mockModuleLoader, mockNgModuleLoader } from '../mock-providers';
import { mockModuleLoader, mockNgModuleLoader } from '../mock-providers';
import { NgModuleLoader } from '../ng-module-loader';


Expand Down Expand Up @@ -47,22 +45,16 @@ describe('module-loader', () => {
// we would expect the same promise to be returned both times
expect(promise).toEqual(secondPromise);

// TODO enable this with caching
// expect(ngModuleLoader.load).toHaveBeenCalledTimes(1);
expect(ngModuleLoader.load).toHaveBeenCalledTimes(1);
});

});

var deepLinker: DeepLinker;
var deepLinkConfig: DeepLinkConfig;
var moduleLoader: ModuleLoader;
var ngModuleLoader: NgModuleLoader;

beforeEach(() => {
deepLinkConfig = mockDeepLinkConfig();
deepLinker = mockDeepLinker(deepLinkConfig);
ngModuleLoader = mockNgModuleLoader();

moduleLoader = mockModuleLoader(ngModuleLoader);
});

Expand Down

0 comments on commit 17359b7

Please sign in to comment.