Skip to content

Commit

Permalink
Convert Graph<T> to a class
Browse files Browse the repository at this point in the history
Summary: Converts `Graph<T>` to a class and all of `graphOperations.js`'s exports to methods on that class. `Graph<T>` has had opaque private state since D36403391 (832ee06), so here we take the next logical step to make it easier to work on.

Reviewed By: huntie

Differential Revision: D40233734

fbshipit-source-id: 204183707c824f9801275dbdb34861db55366ac0
  • Loading branch information
motiz88 authored and facebook-github-bot committed Oct 11, 2022
1 parent 728089f commit 78634cb
Show file tree
Hide file tree
Showing 8 changed files with 1,155 additions and 1,145 deletions.
26 changes: 9 additions & 17 deletions packages/metro/src/DeltaBundler/DeltaCalculator.js
Expand Up @@ -11,14 +11,8 @@

'use strict';

import {
createGraph,
initialTraverseDependencies,
markModifiedContextModules,
reorderGraph,
traverseDependencies,
} from './graphOperations';
import type {DeltaResult, Graph, Options} from './types.flow';
import {Graph} from './Graph';
import type {DeltaResult, Options} from './types.flow';

const {EventEmitter} = require('events');

Expand Down Expand Up @@ -49,7 +43,7 @@ class DeltaCalculator<T> extends EventEmitter {
this._options = options;
this._changeEventSource = changeEventSource;

this._graph = createGraph({
this._graph = new Graph({
entryPoints,
transformOptions: this._options.transformOptions,
});
Expand All @@ -69,7 +63,7 @@ class DeltaCalculator<T> extends EventEmitter {
this.removeAllListeners();

// Clean up all the cache data structures to deallocate memory.
this._graph = createGraph({
this._graph = new Graph({
entryPoints: this._graph.entryPoints,
transformOptions: this._options.transformOptions,
});
Expand Down Expand Up @@ -134,7 +128,7 @@ class DeltaCalculator<T> extends EventEmitter {
// a weird state. As a safe net we clean the dependency modules to force
// a clean traversal of the graph next time.
if (this._graph.dependencies.size !== numDependencies) {
this._graph.dependencies = new Map();
this._graph.dependencies.clear();
}

throw error;
Expand All @@ -144,7 +138,7 @@ class DeltaCalculator<T> extends EventEmitter {

// Return all the modules if the client requested a reset delta.
if (reset) {
reorderGraph(this._graph, {shallow});
this._graph.reorderGraph({shallow});

return {
added: this._graph.dependencies,
Expand Down Expand Up @@ -237,8 +231,7 @@ class DeltaCalculator<T> extends EventEmitter {
addedFiles: Set<string>,
): Promise<DeltaResult<T>> {
if (!this._graph.dependencies.size) {
const {added} = await initialTraverseDependencies(
this._graph,
const {added} = await this._graph.initialTraverseDependencies(
this._options,
);

Expand Down Expand Up @@ -274,7 +267,7 @@ class DeltaCalculator<T> extends EventEmitter {
// module as an inverse dependency, (2) modified files don't invalidate the contents
// of the context module.
addedFiles.forEach(filePath => {
markModifiedContextModules(this._graph, filePath, modifiedFiles);
this._graph.markModifiedContextModules(filePath, modifiedFiles);
});
}

Expand All @@ -293,9 +286,8 @@ class DeltaCalculator<T> extends EventEmitter {
};
}

const {added, modified, deleted} = await traverseDependencies(
const {added, modified, deleted} = await this._graph.traverseDependencies(
modifiedDependencies,
this._graph,
this._options,
);

Expand Down

0 comments on commit 78634cb

Please sign in to comment.