backend-app-api: rename DependencyTree -> DependencyGraph

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-08-11 14:35:16 +02:00
parent a292c9d6d9
commit afe320acec
3 changed files with 27 additions and 27 deletions
@@ -14,11 +14,11 @@
* limitations under the License.
*/
import { DependencyTree } from './DependencyTree';
import { DependencyGraph } from './DependencyGraph';
describe('DependencyTree', () => {
describe('DependencyGraph', () => {
it('should be empty', async () => {
const empty = DependencyTree.fromMap({});
const empty = DependencyGraph.fromMap({});
expect(empty.findUnsatisfiedDeps()).toEqual([]);
expect(empty.detectCircularDependency()).toBeUndefined();
await expect(
@@ -28,7 +28,7 @@ describe('DependencyTree', () => {
it('should detect circular dependencies', () => {
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: {},
2: {},
3: {},
@@ -37,7 +37,7 @@ describe('DependencyTree', () => {
).toBeUndefined();
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'] },
2: { consumes: ['a'], provides: ['b', 'c'] },
3: { consumes: ['b'] },
@@ -46,20 +46,20 @@ describe('DependencyTree', () => {
).toBeUndefined();
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'], consumes: ['a'] },
}).detectCircularDependency(),
).toEqual(['1', '1']);
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'], consumes: ['b'] },
2: { provides: ['b'], consumes: ['a'] },
}).detectCircularDependency(),
).toEqual(['1', '2', '1']);
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'] },
2: { provides: ['b'], consumes: ['a', 'e'] },
3: { provides: ['c'], consumes: ['b'] },
@@ -70,7 +70,7 @@ describe('DependencyTree', () => {
it('should find unsatisfied dependencies', () => {
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: {},
2: {},
3: {},
@@ -79,7 +79,7 @@ describe('DependencyTree', () => {
).toEqual([]);
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'] },
2: { consumes: ['a'], provides: ['b', 'c'] },
3: { consumes: ['b'] },
@@ -88,20 +88,20 @@ describe('DependencyTree', () => {
).toEqual([]);
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { consumes: ['a'] },
}).findUnsatisfiedDeps(),
).toEqual([{ value: '1', unsatisfied: ['a'] }]);
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'], consumes: ['b'] },
2: { provides: ['b'], consumes: ['a', 'd', 'e'] },
}).findUnsatisfiedDeps(),
).toEqual([{ value: '2', unsatisfied: ['d', 'e'] }]);
expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'] },
2: { provides: ['b'], consumes: ['a', 'd', 'e'] },
3: { provides: [], consumes: ['b'] },
@@ -115,7 +115,7 @@ describe('DependencyTree', () => {
it('should traverse dependencies in topological order', async () => {
await expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: {},
2: {},
3: {},
@@ -124,7 +124,7 @@ describe('DependencyTree', () => {
).resolves.toEqual(['1', '2', '3', '4']);
await expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'] },
2: { consumes: ['a'], provides: ['b', 'c'] },
3: { consumes: ['b'] },
@@ -133,7 +133,7 @@ describe('DependencyTree', () => {
).resolves.toEqual(['1', '2', '3', '4']);
await expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { consumes: ['c'] },
2: { provides: ['c'], consumes: ['b'] },
3: { provides: ['b'], consumes: ['a'] },
@@ -142,7 +142,7 @@ describe('DependencyTree', () => {
).resolves.toEqual(['4', '3', '2', '1']);
await expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'] },
2: { provides: ['b'], consumes: ['a'] },
3: { provides: ['c'], consumes: ['a'] },
@@ -153,7 +153,7 @@ describe('DependencyTree', () => {
// Same as above, but with 2 being delayed
await expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'] },
2: { provides: ['b'], consumes: ['a'] },
3: { provides: ['c'], consumes: ['a'] },
@@ -169,18 +169,18 @@ describe('DependencyTree', () => {
).resolves.toEqual(['1', '3', '5', '2', '4']);
await expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'], consumes: ['a'] },
}).parallelTopologicalTraversal(async id => id),
).rejects.toThrow('Circular dependency detected');
await expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'], consumes: ['b'] },
2: { provides: ['b'], consumes: ['a'] },
}).parallelTopologicalTraversal(async id => id),
).rejects.toThrow('Circular dependency detected');
await expect(
DependencyTree.fromMap({
DependencyGraph.fromMap({
1: { provides: ['a'] },
2: { provides: ['c'], consumes: ['a', 'b'] },
3: { provides: ['b'], consumes: ['a', 'c'] },
@@ -38,10 +38,10 @@ class Node<T> {
}
/** @internal */
export class DependencyTree<T> {
export class DependencyGraph<T> {
static fromMap(
nodes: Record<string, Omit<NodeInput<unknown>, 'value'>>,
): DependencyTree<string> {
): DependencyGraph<string> {
return this.fromIterable(
Object.entries(nodes).map(([key, node]) => ({
value: String(key),
@@ -52,13 +52,13 @@ export class DependencyTree<T> {
static fromIterable<T>(
nodeInputs: Iterable<NodeInput<T>>,
): DependencyTree<T> {
): DependencyGraph<T> {
const nodes = new Array<Node<T>>();
for (const nodeInput of nodeInputs) {
nodes.push(Node.from(nodeInput));
}
return new DependencyTree(nodes);
return new DependencyGraph(nodes);
}
#nodes: Array<Node<T>>;
@@ -28,7 +28,7 @@ import { EnumerableServiceHolder, ServiceOrExtensionPoint } from './types';
import { InternalBackendFeature } from '@backstage/backend-plugin-api/src/wiring/types';
import { ForwardedError, ConflictError } from '@backstage/errors';
import { featureDiscoveryServiceRef } from '@backstage/backend-plugin-api/alpha';
import { DependencyTree } from '../lib/DependencyTree';
import { DependencyGraph } from '../lib/DependencyGraph';
export interface BackendRegisterInit {
consumes: Set<ServiceOrExtensionPoint>;
@@ -213,7 +213,7 @@ export class BackendInitializer {
// Modules are initialized before plugins, so that they can provide extension to the plugin
const modules = moduleInits.get(pluginId);
if (modules) {
const tree = DependencyTree.fromIterable(
const tree = DependencyGraph.fromIterable(
Array.from(modules).map(([moduleId, moduleInit]) => ({
value: { moduleId, moduleInit },
// Relationships are reversed at this point since we're only interested in the extension points.