Skip to content

Commit 9c3223f

Browse files
committed
Use pattern matching for getChildren
1 parent a45333c commit 9c3223f

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

src/treeViews/currentBranch.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {WorkflowJobNode} from "./shared/workflowJobNode";
1515
import {WorkflowRunNode} from "./shared/workflowRunNode";
1616
import {WorkflowRunTreeDataProvider} from "./workflowRunTreeDataProvider";
1717
import {WorkflowStepNode} from "./workflows/workflowStepNode";
18+
import { match,P } from "ts-pattern";
1819

1920
type CurrentBranchTreeNode =
2021
| CurrentBranchRepoNode
@@ -87,19 +88,32 @@ export class CurrentBranchTreeProvider
8788
})
8889
.filter(x => x !== undefined) as CurrentBranchRepoNode[];
8990
}
90-
} else if (element instanceof CurrentBranchRepoNode) {
91-
return this.getRuns(element.gitHubRepoContext, element.currentBranchName);
92-
} else if (element instanceof WorkflowRunNode) {
93-
return element.getJobs();
94-
} else if (element instanceof PreviousAttemptsNode) {
95-
return element.getAttempts();
96-
} else if (element instanceof AttemptNode) {
97-
return element.getJobs();
98-
} else if (element instanceof WorkflowJobNode) {
99-
return element.getSteps();
10091
}
10192

102-
return [];
93+
const result = match(element)
94+
.with(P.instanceOf(CurrentBranchRepoNode),
95+
e => this.getRuns(e.gitHubRepoContext, e.currentBranchName)
96+
)
97+
.with(P.instanceOf(PreviousAttemptsNode),
98+
e => e.getAttempts()
99+
)
100+
.with(P.instanceOf(AttemptNode),
101+
e => e.getJobs()
102+
)
103+
.with(P.instanceOf(WorkflowJobNode),
104+
e => e.getSteps()
105+
)
106+
.with(P.instanceOf(WorkflowRunNode),
107+
e => e.getJobs()
108+
)
109+
.otherwise(
110+
e => {
111+
logWarn(`Unknown class seen during getChildren: ${e?.constructor?.name}. Has it been implemented yet?`);
112+
return Promise.resolve([]);
113+
}
114+
);
115+
116+
return result;
103117
}
104118

105119
private async getRuns(gitHubRepoContext: GitHubRepoContext, currentBranchName: string): Promise<WorkflowRunNode[]> {

0 commit comments

Comments
 (0)