Skip to content

Commit

Permalink
simplify solution
Browse files Browse the repository at this point in the history
  • Loading branch information
llllvvuu committed Mar 27, 2024
1 parent 71688d2 commit 56d3b97
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/lib/tsc-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ function killProcesses(currentCompilationId: number, killAll: boolean): Promise<
failureKiller = null;
}

if (emitKiller) {
promisesToWaitFor.push(emitKiller());
emitKiller = null;
}

if (compilationStartedKiller) {
promisesToWaitFor.push(compilationStartedKiller());
compilationStartedKiller = null;
Expand All @@ -80,6 +75,27 @@ function killProcesses(currentCompilationId: number, killAll: boolean): Promise<
return runningKillProcessesPromise;
}

let runningKillEmitProcessesPromise: Promise<number> | null = null;
function killEmitProcesses(currentEmitId: number): Promise<number> {
if (runningKillEmitProcessesPromise) {
return runningKillEmitProcessesPromise.then(() => currentEmitId);
}

const promisesToWaitFor: Promise<any>[] = [];

if (emitKiller) {
promisesToWaitFor.push(emitKiller());
emitKiller = null;
}

runningKillEmitProcessesPromise = Promise.all(promisesToWaitFor).then(() => {
runningKillEmitProcessesPromise = null;
return currentEmitId;
});

return runningKillEmitProcessesPromise;
}

function runOnCompilationStarted(): void {
if (onCompilationStarted) {
compilationStartedKiller = run(onCompilationStarted);
Expand Down Expand Up @@ -170,6 +186,14 @@ tscProcess.stderr.pipe(process.stderr);
const rl = createInterface({ input: tscProcess.stdout });

let compilationId = 0;
let emitId = 0;

function triggerOnEmit() {
if (onEmitCommand) {
killEmitProcesses(++emitId).then((previousEmitId) => previousEmitId === emitId && runOnEmitCommand());
}
}

rl.on('line', function (input) {
if (noClear) {
input = deleteClear(input);
Expand All @@ -189,18 +213,12 @@ rl.on('line', function (input) {

if (state.fileEmitted !== null) {
Signal.emitFile(state.fileEmitted);
if (onEmitCommand) {
killProcesses(++compilationId, false).then((previousCompilationId) => {
previousCompilationId === compilationId && runOnEmitCommand();
});
}
triggerOnEmit();
}

if (compilationStarted) {
(onCompilationStarted
? killProcesses(++compilationId, false)
: Promise.resolve(compilationId)
).then((previousCompilationId) => {
compilationId++;
killProcesses(compilationId, false).then((previousCompilationId) => {
if (previousCompilationId !== compilationId) {
return;
}
Expand All @@ -210,15 +228,8 @@ rl.on('line', function (input) {
}

if (compilationComplete) {
const willRunCommand =
(firstTime && (onFirstSuccessCommand || onEmitCommand)) ||
onCompilationComplete ||
onFailureCommand ||
onSuccessCommand;
(willRunCommand
? killProcesses(++compilationId, false)
: Promise.resolve(compilationId)
).then((previousCompilationId) => {
compilationId++;
killProcesses(compilationId, false).then((previousCompilationId) => {
if (previousCompilationId !== compilationId) {
return;
}
Expand All @@ -232,7 +243,7 @@ rl.on('line', function (input) {
firstTime = false;
Signal.emitFirstSuccess();
runOnFirstSuccessCommand();
runOnEmitCommand();
triggerOnEmit();
}

Signal.emitSuccess();
Expand Down

0 comments on commit 56d3b97

Please sign in to comment.