Skip to content

Instantly share code, notes, and snippets.

@sukima
Last active December 19, 2015 21:58
Show Gist options
  • Save sukima/6023859 to your computer and use it in GitHub Desktop.
Save sukima/6023859 to your computer and use it in GitHub Desktop.
class ReadlineLoop
constructor: ->
@rl = readline.createInterface
input: process.stdin
output: process.stdout
@rl.on("line" , @onLine)
.on("close", @onClose)
onLine: => # ...
onClose: => # ...
class ReadlineLoop
actions =
line: "onLine"
close: "onClose"
constructor: ->
@rl = readline.createInterface
input: process.stdin
output: process.stdout
for action,fn of actions
@rl.on action, @[fn]
onLine: => #...
onClose: => #...
partial = (func, a...) -> (b...) ->
func (for arg in a then arg ?= b.shift())...
class ReadlineLoop
actions = [
"live"
"close"
]
constructor: ->
@rl = readline.createInterface
input: process.stdin
output: process.stdout
@rl.on(action, partial(action, @onAction)) for action in actions
onAction: (action) =>
switch action
when "live"
console.log("Live!")
when "close"
console.log("Closed!")
else
throw "Opps!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment