Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple keys for DependencyGraph.getAllDependencies #596

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/DependencyGraph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ describe('DependencyGraph', () => {
'e'
]);
});

it('works with multiple keys', () => {
graph.addOrReplace('a', ['b', 'c']);
graph.addOrReplace('b', ['c', 'd']);
expect(graph.getAllDependencies(['a', 'b']).sort()).to.eql([
'b', 'c', 'd'
]);
});
});

describe('onchange', () => {
Expand Down
17 changes: 14 additions & 3 deletions src/DependencyGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,22 @@ export class DependencyGraph {

/**
* Get a list of the dependencies for the given key, recursively.
* @param key the key for which to get the dependencies
* @param key the key (or keys) for which to get the dependencies
* @param exclude a list of keys to exclude from traversal. Anytime one of these nodes is encountered, it is skipped.
*/
public getAllDependencies(key: string, exclude?: string[]) {
return this.nodes[key]?.getAllDependencies(exclude) ?? [];
public getAllDependencies(keys: string | string[], exclude?: string[]) {
if (typeof keys === 'string') {
return this.nodes[keys]?.getAllDependencies(exclude) ?? [];
} else {
const set = new Set<string>();
for (const key of keys) {
const dependencies = this.getAllDependencies(key, exclude);
for (const dependency of dependencies) {
set.add(dependency);
}
}
return [...set];
}
}

/**
Expand Down