stop the file watcher

pull/2894/head
yann300 2 years ago
parent b22352aeae
commit 8ff4e29ffe
  1. 8
      libs/remixd/src/services/hardhatClient.ts
  2. 12
      libs/remixd/src/services/remixdClient.ts

@ -9,6 +9,7 @@ export class HardhatClient extends PluginClient {
methods: Array<string> methods: Array<string>
websocket: WS websocket: WS
currentSharedFolder: string currentSharedFolder: string
watcher: chokidar.FSWatcher
constructor (private readOnly = false) { constructor (private readOnly = false) {
super() super()
@ -17,6 +18,9 @@ export class HardhatClient extends PluginClient {
setWebSocket (websocket: WS): void { setWebSocket (websocket: WS): void {
this.websocket = websocket this.websocket = websocket
this.websocket.addEventListener('close', () => {
if (this.watcher) this.watcher.close()
})
} }
sharedFolder (currentSharedFolder: string): void { sharedFolder (currentSharedFolder: string): void {
@ -54,8 +58,8 @@ export class HardhatClient extends PluginClient {
listenOnHardhatCompilation () { listenOnHardhatCompilation () {
try { try {
const buildPath = utils.absolutePath('artifacts/build-info', this.currentSharedFolder) const buildPath = utils.absolutePath('artifacts/build-info', this.currentSharedFolder)
const watcher = chokidar.watch(buildPath, { depth: 0, ignorePermissionErrors: true }) this.watcher = chokidar.watch(buildPath, { depth: 0, ignorePermissionErrors: true })
watcher.on('change', async (f: string) => { this.watcher.on('change', async (f: string) => {
const content = await fs.readFile(f, { encoding: 'utf-8' }) const content = await fs.readFile(f, { encoding: 'utf-8' })
const compilationResult = JSON.parse(content) const compilationResult = JSON.parse(content)
// @ts-ignore // @ts-ignore

@ -11,6 +11,7 @@ export class RemixdClient extends PluginClient {
trackDownStreamUpdate: TrackDownStreamUpdate = {} trackDownStreamUpdate: TrackDownStreamUpdate = {}
websocket: WS websocket: WS
currentSharedFolder: string currentSharedFolder: string
watcher: chokidar.FSWatcher
constructor (private readOnly = false) { constructor (private readOnly = false) {
super() super()
@ -19,6 +20,9 @@ export class RemixdClient extends PluginClient {
setWebSocket (websocket: WS): void { setWebSocket (websocket: WS): void {
this.websocket = websocket this.websocket = websocket
this.websocket.addEventListener('close', () => {
if (this.watcher) this.watcher.close()
})
} }
sharedFolder (currentSharedFolder: string): void { sharedFolder (currentSharedFolder: string): void {
@ -246,7 +250,7 @@ export class RemixdClient extends PluginClient {
const absPath = utils.absolutePath('./', path) const absPath = utils.absolutePath('./', path)
if (!isRealPath(absPath)) return if (!isRealPath(absPath)) return
const watcher = chokidar.watch(path, { depth: 0, ignorePermissionErrors: true }) this.watcher = chokidar.watch(path, { depth: 0, ignorePermissionErrors: true })
console.log('setup notifications for ' + path) console.log('setup notifications for ' + path)
/* we can't listen on created file / folder /* we can't listen on created file / folder
watcher.on('add', (f, stat) => { watcher.on('add', (f, stat) => {
@ -260,7 +264,7 @@ export class RemixdClient extends PluginClient {
this.emit('created', { path: utils.relativePath(f, this.currentSharedFolder), isReadOnly: false, isFolder: true }) this.emit('created', { path: utils.relativePath(f, this.currentSharedFolder), isReadOnly: false, isFolder: true })
}) })
*/ */
watcher.on('change', async (f: string) => { this.watcher.on('change', async (f: string) => {
if (this.trackDownStreamUpdate[f]) { if (this.trackDownStreamUpdate[f]) {
delete this.trackDownStreamUpdate[f] delete this.trackDownStreamUpdate[f]
return return
@ -269,12 +273,12 @@ export class RemixdClient extends PluginClient {
this.emit('changed', utils.relativePath(f, this.currentSharedFolder)) this.emit('changed', utils.relativePath(f, this.currentSharedFolder))
} }
}) })
watcher.on('unlink', async (f: string) => { this.watcher.on('unlink', async (f: string) => {
if (this.isLoaded) { if (this.isLoaded) {
this.emit('removed', utils.relativePath(f, this.currentSharedFolder), false) this.emit('removed', utils.relativePath(f, this.currentSharedFolder), false)
} }
}) })
watcher.on('unlinkDir', async (f: string) => { this.watcher.on('unlinkDir', async (f: string) => {
if (this.isLoaded) { if (this.isLoaded) {
this.emit('removed', utils.relativePath(f, this.currentSharedFolder), true) this.emit('removed', utils.relativePath(f, this.currentSharedFolder), true)
} }

Loading…
Cancel
Save