fix: `doing` inconsistent if `parser` throws

Massimo Melina committed Feb 13, 2022 at 17:45 UTC f12a023347bc90df712b9593bf9ff2f043ba322b
1 file changed +11 -9
src/watchLoad.ts
+11 -9
@@ -37,7 +37,7 @@ export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>, {
37 if (!saving)
38 debounced().then()
39 })
40 - debounced().then() // if file is not accessible watch will throw and we won't get here
40 + debounced().then() // if file is not accessible watch will throw, and we won't get here
41 }
42 catch {
43 retry = setTimeout(init, 1000) // manual watching until watch is successful
@@ -49,16 +49,18 @@ export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>, {
49 doing = true
50 let data: any
51 try {
52 - data = await readFileBusy(path)
53 - console.debug('loaded', path)
54 - if (path.endsWith('.yaml'))
55 - data = yaml.parse(data)
56 - } catch (e) {
52 + try {
53 + data = await readFileBusy(path)
54 + console.debug('loaded', path)
55 + if (path.endsWith('.yaml'))
56 + data = yaml.parse(data)
57 + }
58 + catch (e) { return } // silently ignore read errors
59 + await parser(data)
60 + }
61 + finally {
62 doing = false
58 - return
63 }
60 - await parser(data)
61 - doing = false
64 }
65 }
66