mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 12:29:55 +08:00
feat(module-loader): add caching for ngModuleLoader on load
This commit is contained in:
@ -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';
|
||||
|
||||
@ -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) {}
|
||||
@ -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);
|
||||
|
||||
|
@ -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';
|
||||
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user