Skip to content

Commit

Permalink
fix: dispose state after turning off watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed May 17, 2022
1 parent e9ec920 commit a89e61b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
5 changes: 5 additions & 0 deletions src/pure/watch/ws-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface VitestClient {
rpc: BirpcReturn<WebSocketHandlers>
waitForConnection(): Promise<void>
reconnect(): Promise<void>
dispose(): void
}

export function createClient(url: string, options: VitestClientOptions = {}) {
Expand All @@ -118,6 +119,10 @@ export function createClient(url: string, options: VitestClientOptions = {}) {
state: new StateManager(),
waitForConnection,
reconnect: () => reconnect(true),
dispose: () => {
tries = 0
ctx.ws.close()
},
}) as VitestClient

ctx.state.filesMap = reactive(ctx.state.filesMap)
Expand Down
87 changes: 41 additions & 46 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ export class TestWatcher extends Disposable {
) {
super(() => {
this.dispose()
this.vitestState?.client.ws.close()
this.vitestState = undefined
})
}

Expand Down Expand Up @@ -99,52 +97,47 @@ export class TestWatcher extends Disposable {
console.log('VITEST WATCH PROCESS EXIT')
})

if (this.vitestState) {
this.vitestState.client.reconnect()
}
else {
this.vitestState = buildWatchClient({
handlers: {
onTaskUpdate: (packs) => {
try {
if (!this.vitestState)
return

this.isRunning.value = true
const idMap = this.vitestState.client.state.idMap
const fileSet = new Set<File>()
for (const [id] of packs) {
const task = idMap.get(id)
if (!task)
continue

task.file && fileSet.add(task.file)
}

this.onUpdated(Array.from(fileSet), false)
}
catch (e) {
console.error(e)
}
},
onFinished: (files) => {
try {
this.isRunning.value = false
this.onUpdated(files, true)
if (!this.run)
return

this.run.end()
this.run = undefined
this.updateStatus()
this.vitestState = buildWatchClient({
handlers: {
onTaskUpdate: (packs) => {
try {
if (!this.vitestState)
return

this.isRunning.value = true
const idMap = this.vitestState.client.state.idMap
const fileSet = new Set<File>()
for (const [id] of packs) {
const task = idMap.get(id)
if (!task)
continue

task.file && fileSet.add(task.file)
}
catch (e) {
console.error(e)
}
},

this.onUpdated(Array.from(fileSet), false)
}
catch (e) {
console.error(e)
}
},
})
}
onFinished: (files) => {
try {
this.isRunning.value = false
this.onUpdated(files, true)
if (!this.run)
return

this.run.end()
this.run = undefined
this.updateStatus()
}
catch (e) {
console.error(e)
}
},
},
})

effect(() => {
this.onFileUpdated(this.vitestState!.files.value)
Expand Down Expand Up @@ -361,7 +354,9 @@ export class TestWatcher extends Disposable {
public dispose() {
console.log('Stop watch mode')
this.isWatching.value = false
this.vitestState?.client.dispose()
this.process?.kill()
this.process = undefined
this.vitestState = undefined
}
}

0 comments on commit a89e61b

Please sign in to comment.