Skip to content

Commit

Permalink
refactor: interface oriented programming
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Jan 13, 2025
1 parent dbfb84c commit c0f8ec3
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions packages/graph/src/graph/chunk-graph/graph.ts
Original file line number Diff line number Diff line change
@@ -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<string, SDK.AssetInstance> = {};
private _chunkMap: Record<string, SDK.ChunkInstance> = {};
private _entrypointMap: Record<string, SDK.EntryPointInstance> = {};
private _assetMap: Map<string, SDK.AssetInstance> = new Map();
private _chunkMap: Map<string, SDK.ChunkInstance> = new Map();
private _entrypointMap: Map<string, SDK.EntryPointInstance> = 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(),
),
};
}
}

0 comments on commit c0f8ec3

Please # to comment.