Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
keyboard churn
#!/usr/bin/env coffee
process.stdin.resume()
require('tty').setRawMode(true)
history = {}
plans = {}
spew = () -> console.log(new Date)
setInterval spew, 1000
process.stdin.on 'keypress', (char, key) =>
if (key && key.ctrl && key.name == 'c')
process.exit()
console.log("Event '#{char}'")
clearTimeout plans[char]
if ! history[char]?
history[char] = []
history[char].push new Date
delay = 6000 # timespans in milliseconds
if history[char].length > 1
mean = ( history[char][history[char].length-1] - history[char][0] ) / history[char].length
delay = 3*mean+500
task = () =>
console.log("Churn '#{char}'")
history[char] = []
plans[char] = setTimeout task, delay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.