dmitriytat / keypress

Deno keypress reader
MIT License
12 stars 4 forks source link

async iterator shutdown mechanic #6

Open andykais opened 2 years ago

andykais commented 2 years ago

I am developing an application with this library and occasionally the application crashes during development. This often leaves my terminal in a 'tty raw' state, where I cant use keys like "ctrl+c". For now the fix is to add a Deno.setRaw(Deno.stdin.rid, false) somewhere in my shutdown code. Ideally though, this library would add a close() hook, or the ability to pass in an AbortController. For instance, this is how Deno.watchFs handles cleanup:

const watcher = Deno.watchFs('./folder')
for await (const event of watcher) {
  console.log(event)
}

setTimeout(() => {
  watcher.close()
}, 1000)
dmitriytat commented 2 years ago

Let's try watcher_test.ts from PR. It is still needed to press something to break for..of cycle, because there are no ways to abort Deno.stdin.read();

May be close method of watcher is a good workaround in some cases, but it can be completely messy if i pass my reader explicitly.

I think we need to think about it a little bit more.