Skip to content

Instantly share code, notes, and snippets.

@jebeck
Last active July 15, 2022 01:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jebeck/1ca224af150767a25ecefc020d29bbd0 to your computer and use it in GitHub Desktop.
Save jebeck/1ca224af150767a25ecefc020d29bbd0 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) {
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}.`));
} else {
prompts.onCompleted();
}
}, (err) => {
console.warn(err);
}, () => {
console.log('Interactive session is complete. Good bye! 👋\n');
});
prompts.onNext(makePrompt());
@shapkarin
Copy link

shapkarin commented Apr 23, 2019

Run that code in node v10.12.0 with rx v4.x and see nothing in terminal. And looks like you didn't pass i to the makePrompt()

@yunhan0
Copy link

yunhan0 commented Jun 4, 2019

Change onNext() to next(), onComplete() to complete()

@shapkarin
Copy link

@yunhan0 thanks, I will try. Looks like new API

@C-4PO
Copy link

C-4PO commented Jul 15, 2022

@yunhan0 Put a try catch around inquirer.prompt and I get this error TypeError: dest.addListener is not a function. Looks unavoidable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment