diff --git a/packages/graph/src/graph/chunk-graph/graph.ts b/packages/graph/src/graph/chunk-graph/graph.ts index 570a1ae8..27576a2c 100644 --- a/packages/graph/src/graph/chunk-graph/graph.ts +++ b/packages/graph/src/graph/chunk-graph/graph.ts @@ -1,74 +1,68 @@ import { SDK } from '@rsdoctor/types'; export class ChunkGraph implements SDK.ChunkGraphInstance { - private _assets: SDK.AssetInstance[] = []; - private _chunks: SDK.ChunkInstance[] = []; - private _entrypoints: SDK.EntryPointInstance[] = []; - private _assetMap: Record = {}; - private _chunkMap: Record = {}; - private _entrypointMap: Record = {}; + private _assetMap: Map = new Map(); + private _chunkMap: Map = new Map(); + private _entrypointMap: Map = new Map(); getAssets(): SDK.AssetInstance[] { - return this._assets.slice(); + return Array.from(this._assetMap.values()); } getChunks(): SDK.ChunkInstance[] { - return this._chunks.slice(); + return Array.from(this._chunkMap.values()); } addAsset(...assets: SDK.AssetInstance[]): void { assets.forEach((asset) => { - if (!this._assets.includes(asset)) { - this._assets.push(asset); - this._assetMap[asset.path] = asset; - } + this._assetMap.set(asset.path, asset); }); } addChunk(...chunks: SDK.ChunkInstance[]): void { chunks.forEach((chunk) => { - if (!this._chunks.includes(chunk)) { - this._chunks.push(chunk); - this._chunkMap[chunk.id] = chunk; - } + this._chunkMap.set(chunk.id, chunk); }); } getChunkById(id: string): SDK.ChunkInstance | undefined { - return this._chunkMap[id]; + return this._chunkMap.get(id); } getChunkByModule(module: SDK.ModuleInstance): SDK.ChunkInstance | undefined { - return this._chunks.find((item) => item.hasModule(module)); + return Array.from(this._chunkMap.values()).find((item) => + item.hasModule(module), + ); } getAssetByPath(path: string): SDK.AssetInstance | undefined { - return this._assetMap[path]; + return this._assetMap.get(path); } getEntryPoints(): SDK.EntryPointInstance[] { - return this._entrypoints.slice(); + return Array.from(this._entrypointMap.values()); } getEntryPointByName(name: string): SDK.EntryPointInstance | undefined { - return this._entrypointMap[name]; + return this._entrypointMap.get(name); } addEntryPoint(...entrypoints: SDK.EntryPointInstance[]): void { entrypoints.forEach((entrypoint) => { - if (!this._entrypoints.includes(entrypoint)) { - this._entrypoints.push(entrypoint); - this._entrypointMap[entrypoint.name] = entrypoint; - } + this._entrypointMap.set(entrypoint.name, entrypoint); }); } /** output the chunk graph data */ toData(type: SDK.ToDataType): SDK.ChunkGraphData { return { - assets: this._assets.map((item) => item.toData(type)), - chunks: this._chunks.map((item) => item.toData()), - entrypoints: this._entrypoints.map((item) => item.toData()), + assets: Array.from(this._assetMap.values()).map((item) => + item.toData(type), + ), + chunks: Array.from(this._chunkMap.values()).map((item) => item.toData()), + entrypoints: Array.from(this._entrypointMap.values()).map((item) => + item.toData(), + ), }; } }