Skip to content

Commit 6753d97

Browse files
committed
refactor: extract abort signal combination to utility function
1 parent 68f67d6 commit 6753d97

File tree

2 files changed

+34
-52
lines changed

2 files changed

+34
-52
lines changed

src/dbt_client/dbtIntegration.ts

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { DBTConfiguration } from "./configuration";
1313
import path from "path";
1414
import { parse } from "yaml";
1515
import * as crypto from "crypto";
16+
import { combineAbortSignals } from "../utils";
1617

1718
export const DBT_PROJECT_FILE = "dbt_project.yml";
1819
export const MANIFEST_FILE = "manifest.json";
@@ -124,32 +125,7 @@ export class CLIDBTCommandExecutionStrategy
124125
);
125126
}
126127
// Combine signals if multiple are provided
127-
let combinedSignal: AbortSignal | undefined;
128-
const signals: AbortSignal[] = [];
129-
if (signal !== undefined) {
130-
signals.push(signal);
131-
}
132-
if (command.signal !== undefined) {
133-
signals.push(command.signal);
134-
}
135-
136-
if (signals.length > 0) {
137-
if (signals.length === 1) {
138-
combinedSignal = signals[0];
139-
} else {
140-
// Create a combined signal if multiple signals are provided
141-
const controller = new AbortController();
142-
combinedSignal = controller.signal;
143-
144-
signals.forEach((s) => {
145-
if (s.aborted) {
146-
controller.abort();
147-
} else {
148-
s.addEventListener("abort", () => controller.abort());
149-
}
150-
});
151-
}
152-
}
128+
const combinedSignal = combineAbortSignals(signal, command.signal);
153129

154130
return this.commandProcessExecutionFactory.createCommandProcessExecution({
155131
command: this.dbtPath,
@@ -208,32 +184,7 @@ export class PythonDBTCommandExecutionStrategy
208184
);
209185
}
210186
// Combine signals if multiple are provided
211-
let combinedSignal: AbortSignal | undefined;
212-
const signals: AbortSignal[] = [];
213-
if (signal !== undefined) {
214-
signals.push(signal);
215-
}
216-
if (command.signal !== undefined) {
217-
signals.push(command.signal);
218-
}
219-
220-
if (signals.length > 0) {
221-
if (signals.length === 1) {
222-
combinedSignal = signals[0];
223-
} else {
224-
// Create a combined signal if multiple signals are provided
225-
const controller = new AbortController();
226-
combinedSignal = controller.signal;
227-
228-
signals.forEach((s) => {
229-
if (s.aborted) {
230-
controller.abort();
231-
} else {
232-
s.addEventListener("abort", () => controller.abort());
233-
}
234-
});
235-
}
236-
}
187+
const combinedSignal = combineAbortSignals(signal, command.signal);
237188

238189
return this.commandProcessExecutionFactory.createCommandProcessExecution({
239190
command: this.pythonEnvironment.pythonPath,

src/utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,37 @@ export function removeProtocol(input: string): string {
465465
return input.replace(/^[^:]+:\/\//, "");
466466
}
467467

468+
export function combineAbortSignals(
469+
...signals: (AbortSignal | undefined)[]
470+
): AbortSignal | undefined {
471+
// Filter out undefined signals
472+
const validSignals = signals.filter(
473+
(signal): signal is AbortSignal => signal !== undefined,
474+
);
475+
476+
if (validSignals.length === 0) {
477+
return undefined;
478+
}
479+
480+
if (validSignals.length === 1) {
481+
return validSignals[0];
482+
}
483+
484+
// Create a combined signal if multiple signals are provided
485+
const controller = new AbortController();
486+
const combinedSignal = controller.signal;
487+
488+
validSignals.forEach((signal) => {
489+
if (signal.aborted) {
490+
controller.abort();
491+
} else {
492+
signal.addEventListener("abort", () => controller.abort());
493+
}
494+
});
495+
496+
return combinedSignal;
497+
}
498+
468499
export function getDepthColor(depth: number): string {
469500
const mediumDepthThreshold = workspace
470501
.getConfiguration("dbt")

0 commit comments

Comments
 (0)