Skip to content

Commit

Permalink
Refactor Graph - reduce duplication in traverse/initialTraverse
Browse files Browse the repository at this point in the history
Summary:
Refactor out some duplication within `traverseDependencies` / `initalTraverseDependencies` in preparing a delta for commit.

Changelog: Internal

Reviewed By: GijsWeterings

Differential Revision: D51943107

fbshipit-source-id: 3c9b8d1e07bbb2b110e5fc64797e50ff58d17a0b
  • Loading branch information
robhogan authored and facebook-github-bot committed Dec 22, 2023
1 parent 787f5f6 commit 1d11554
Showing 1 changed file with 46 additions and 53 deletions.
99 changes: 46 additions & 53 deletions packages/metro/src/DeltaBundler/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,44 +171,21 @@ export class Graph<T = MixedOutput> {
): Promise<Result<T>> {
const internalOptions = getInternalOptions(options);

const modifiedPathsInBaseGraph = paths.filter(path =>
this.dependencies.has(path),
const modifiedPathsInBaseGraph = new Set(
paths.filter(path => this.dependencies.has(path)),
);

const allModifiedPaths = new Set(paths);

const moduleData = await buildSubgraph(
new Set(modifiedPathsInBaseGraph),
this.#resolvedContexts,
{
resolve: options.resolve,
transform: async (absolutePath, requireContext) => {
internalOptions.onDependencyAdd();
const result = await options.transform(absolutePath, requireContext);
internalOptions.onDependencyAdded();
return result;
},
shouldTraverse: (dependency: Dependency) => {
if (
// Don't traverse into any path that hasn't been modified and as is
// already in the graph.
this.dependencies.has(dependency.absolutePath) &&
!allModifiedPaths.has(dependency.absolutePath)
) {
return false;
}
return !options.shallow && !isWeakOrLazy(dependency, internalOptions);
},
},
const delta = await this._buildDelta(
modifiedPathsInBaseGraph,
internalOptions,
// Traverse new or modified paths
absolutePath =>
!this.dependencies.has(absolutePath) ||
allModifiedPaths.has(absolutePath),
);

const delta = {
added: new Set<string>(),
touched: new Set<string>(),
deleted: new Set<string>(),
moduleData,
};

for (const modified of modifiedPathsInBaseGraph) {
if (this.dependencies.has(modified)) {
this._recursivelyCommitModule(modified, delta, internalOptions);
Expand Down Expand Up @@ -257,28 +234,11 @@ export class Graph<T = MixedOutput> {
this.#gc.color.set(path, 'black');
}

const moduleData = await buildSubgraph(this.entryPoints, new Map(), {
resolve: options.resolve,
transform: async (absolutePath, requireContext) => {
internalOptions.onDependencyAdd();
const result = await options.transform(absolutePath, requireContext);
internalOptions.onDependencyAdded();
return result;
},
shouldTraverse: (dependency: Dependency) =>
!options.shallow && !isWeakOrLazy(dependency, internalOptions),
});

const delta = {
added: new Set<string>(),
touched: new Set<string>(),
deleted: new Set<string>(),
moduleData,
};
const delta = await this._buildDelta(this.entryPoints, internalOptions);

[...this.entryPoints].forEach((path: string) =>
this._recursivelyCommitModule(path, delta, internalOptions),
);
for (const path of this.entryPoints) {
this._recursivelyCommitModule(path, delta, internalOptions);
}

this.reorderGraph({
shallow: options.shallow,
Expand All @@ -291,6 +251,39 @@ export class Graph<T = MixedOutput> {
};
}

async _buildDelta(
pathsToVisit: $ReadOnlySet<string>,
options: InternalOptions<T>,
moduleFilter?: (path: string) => boolean,
): Promise<Delta<T>> {
const moduleData = await buildSubgraph(
pathsToVisit,
this.#resolvedContexts,
{
resolve: options.resolve,
transform: async (absolutePath, requireContext) => {
options.onDependencyAdd();
const result = await options.transform(absolutePath, requireContext);
options.onDependencyAdded();
return result;
},
shouldTraverse: (dependency: Dependency) => {
if (options.shallow || isWeakOrLazy(dependency, options)) {
return false;
}
return moduleFilter == null || moduleFilter(dependency.absolutePath);
},
},
);

return {
added: new Set<string>(),
touched: new Set<string>(),
deleted: new Set<string>(),
moduleData,
};
}

_recursivelyCommitModule(
path: string,
delta: Delta<T>,
Expand Down

0 comments on commit 1d11554

Please sign in to comment.