Skip to content

Instantly share code, notes, and snippets.

@shapkarin
Forked from jebeck/infinite.js
Last active April 23, 2019 22:48
Show Gist options
  • Save shapkarin/a9ca249f0d8ca1328d7ed46f919704ed to your computer and use it in GitHub Desktop.
Save shapkarin/a9ca249f0d8ca1328d7ed46f919704ed to your computer and use it in GitHub Desktop.
infinite interactivity with Inquirer and RxJS's Subject
// 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