Skip to content

Commit 5682f29

Browse files
feat: terminate aborted search request
1 parent 2833d3c commit 5682f29

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/view.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Definition, ParentPort, SgSearch } from './types'
22
import { Unport, ChannelMessage } from 'unport'
33
import * as vscode from 'vscode'
44
import { workspace } from 'vscode'
5-
import { spawn } from 'node:child_process'
5+
import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
66

77
export function activate(context: vscode.ExtensionContext) {
88
const provider = new SearchSidebarProvider(context.extensionUri)
@@ -16,6 +16,8 @@ export function activate(context: vscode.ExtensionContext) {
1616
)
1717
}
1818

19+
let child: ChildProcessWithoutNullStreams | undefined
20+
1921
async function getPatternRes(pattern: string) {
2022
if (!pattern) {
2123
return
@@ -28,17 +30,28 @@ async function getPatternRes(pattern: string) {
2830
// TODO: multi-workspaces support
2931
// TODO: the code here is wrong, but we will change it
3032
let stdout = ''
31-
const child = spawn(
32-
command,
33-
['run', '--pattern', pattern, '--json=compact'],
34-
{
35-
cwd: uris[0]
36-
}
37-
)
33+
if (child) {
34+
// kill previous search
35+
child.kill('SIGTERM')
36+
}
37+
child = spawn(command, ['run', '--pattern', pattern, '--json=compact'], {
38+
cwd: uris[0]
39+
})
3840
child.stdout.on('data', data => {
3941
stdout += data
4042
})
41-
await new Promise(r => child.on('close', r))
43+
await new Promise((resolve, reject) =>
44+
child!.on('exit', (code, signal) => {
45+
// exit without signal, search ends correctly
46+
// TODO: is it correct now?
47+
if (!signal && code === 0) {
48+
resolve(code)
49+
} else {
50+
reject([code, signal])
51+
}
52+
})
53+
)
54+
child = undefined
4255

4356
try {
4457
let res: SgSearch[] = JSON.parse(stdout)

0 commit comments

Comments
 (0)