refactor ModuleGraph to use internal dependency

This commit is contained in:
Tobias Koppers 2018-08-16 14:04:37 +02:00
parent 3bb5263bfd
commit a875ddba5b
1 changed files with 41 additions and 16 deletions

View File

@ -30,9 +30,20 @@ class ModuleGraphModule {
}
}
class ModuleGraphDependency {
constructor() {
/** @type {ModuleGraphConnection} */
this.connection = undefined;
/** @type {Module} */
this.parentModule = undefined;
/** @type {DependenciesBlock} */
this.parentBlock = undefined;
}
}
class ModuleGraph {
constructor() {
/** @type {Map<Dependency, ModuleGraphConnection>} */
/** @type {Map<Dependency, ModuleGraphDependency>} */
this._dependencyMap = new Map();
/** @type {Map<Module, ModuleGraphModule>} */
this._moduleMap = new Map();
@ -40,8 +51,6 @@ class ModuleGraph {
this._originMap = new Map();
/** @type {Map<any, Object>} */
this._metaMap = new Map();
/** @type {Map<Dependency, {module: Module, block: DependenciesBlock}>} */
this._parentsMap = new Map();
}
/**
@ -57,6 +66,19 @@ class ModuleGraph {
return mgm;
}
/**
* @param {Dependency} dependency the dependency
* @returns {ModuleGraphDependency} the internal dependency
*/
_getModuleGraphDependency(dependency) {
let mgd = this._dependencyMap.get(dependency);
if (mgd === undefined) {
mgd = new ModuleGraphDependency();
this._dependencyMap.set(dependency, mgd);
}
return mgd;
}
/**
* @param {Dependency} dependency the dependency
* @param {DependenciesBlock} block parent block
@ -64,7 +86,9 @@ class ModuleGraph {
* @returns {void}
*/
setParents(dependency, block, module) {
this._parentsMap.set(dependency, { module, block });
const mgd = this._getModuleGraphDependency(dependency);
mgd.parentBlock = block;
mgd.parentModule = module;
}
/**
@ -72,8 +96,8 @@ class ModuleGraph {
* @returns {Module} parent module
*/
getParentModule(dependency) {
const entry = this._parentsMap.get(dependency);
return entry !== undefined ? entry.module : null;
const mgd = this._getModuleGraphDependency(dependency);
return mgd.parentModule;
}
/**
@ -81,8 +105,8 @@ class ModuleGraph {
* @returns {DependenciesBlock} parent block
*/
getParentBlock(dependency) {
const entry = this._parentsMap.get(dependency);
return entry !== undefined ? entry.block : null;
const mgd = this._getModuleGraphDependency(dependency);
return mgd.parentBlock;
}
/**
@ -97,7 +121,8 @@ class ModuleGraph {
dependency,
module
);
this._dependencyMap.set(dependency, connection);
const mgd = this._getModuleGraphDependency(dependency);
mgd.connection = connection;
const connections = this._getModuleGraphModule(module).incomingConnections;
connections.add(connection);
const originConnections = this._getModuleGraphModule(originModule)
@ -111,7 +136,7 @@ class ModuleGraph {
* @returns {void}
*/
updateModule(dependency, module) {
const connection = this._dependencyMap.get(dependency);
const { connection } = this._getModuleGraphDependency(dependency);
if (connection.module === module) return;
const oldMgm = this._getModuleGraphModule(connection.module);
oldMgm.incomingConnections.delete(connection);
@ -126,7 +151,7 @@ class ModuleGraph {
* @returns {void}
*/
addExplanation(dependency, explanation) {
const connection = this._dependencyMap.get(dependency);
const { connection } = this._getModuleGraphDependency(dependency);
connection.addExplanation(explanation);
}
@ -175,7 +200,7 @@ class ModuleGraph {
* @returns {Module} the referenced module
*/
getResolvedModule(dependency) {
const connection = this._dependencyMap.get(dependency);
const { connection } = this._getModuleGraphDependency(dependency);
return connection !== undefined ? connection.resolvedModule : null;
}
@ -184,7 +209,7 @@ class ModuleGraph {
* @returns {ModuleGraphConnection | undefined} the connection
*/
getConnection(dependency) {
const connection = this._dependencyMap.get(dependency);
const { connection } = this._getModuleGraphDependency(dependency);
return connection;
}
@ -193,7 +218,7 @@ class ModuleGraph {
* @returns {Module} the referenced module
*/
getModule(dependency) {
const connection = this._dependencyMap.get(dependency);
const { connection } = this._getModuleGraphDependency(dependency);
return connection !== undefined ? connection.module : null;
}
@ -202,7 +227,7 @@ class ModuleGraph {
* @returns {Module} the referencing module
*/
getOrigin(dependency) {
const connection = this._dependencyMap.get(dependency);
const { connection } = this._getModuleGraphDependency(dependency);
return connection !== undefined ? connection.originModule : null;
}
@ -211,7 +236,7 @@ class ModuleGraph {
* @returns {Module} the original referencing module
*/
getResolvedOrigin(dependency) {
const connection = this._dependencyMap.get(dependency);
const { connection } = this._getModuleGraphDependency(dependency);
return connection !== undefined ? connection.resolvedOriginModule : null;
}