-
-
Save shapkarin/a9ca249f0d8ca1328d7ed46f919704ed to your computer and use it in GitHub Desktop.
infinite interactivity with Inquirer and RxJS's Subject
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
// run with `node infinite.js` in node v4.x+ | |
// must have Inquirer installed (`npm install inquirer`) | |
const inquirer = require('inquirer'); | |
const Rx = require('rx'); | |
const prompts = new Rx.Subject(); | |
function makePrompt(msg, i) { | |
return { | |
type: 'input', | |
name: `userInput-${i}`, | |
message: `${msg || 'Say something to start chatting!'}\n\n`, | |
}; | |
} | |
let i = 0; | |
inquirer.prompt(prompts).ui.process.subscribe(({ answer }) => { | |
if (answer !== '') { | |
i += 1; | |
prompts.onNext(makePrompt(`This is prompt #${i}.`, i)); | |
} else { | |
prompts.onCompleted(); | |
} | |
}, (err) => { | |
console.warn(err); | |
}, () => { | |
console.log('Interactive session is complete. Good bye! 👋\n'); | |
}); | |
prompts.onNext(makePrompt()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment