Last active
December 19, 2015 21:58
-
-
Save sukima/6023859 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReadlineLoop | |
constructor: -> | |
@rl = readline.createInterface | |
input: process.stdin | |
output: process.stdout | |
@rl.on("line" , @onLine) | |
.on("close", @onClose) | |
onLine: => # ... | |
onClose: => # ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: => #... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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