Skip to content

Commit

Permalink
upgrade to RxJS 5 (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
user3587412 authored and SBoudrias committed Jan 8, 2018
1 parent fb37720 commit 258064f
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 43 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -286,7 +286,7 @@ ui.updateBottomBar('new bottom bar content');
## Reactive interface


Internally, Inquirer uses the [JS reactive extension](https://github.com/Reactive-Extensions/RxJS) to handle events and async flows.
Internally, Inquirer uses the [JS reactive extension](https://github.com/ReactiveX/rxjs) to handle events and async flows.

This mean you can take advantage of this feature to provide more advanced flows. For example, you can dynamically add questions to be asked:

Expand All @@ -295,11 +295,11 @@ var prompts = new Rx.Subject();
inquirer.prompt(prompts);

// At some point in the future, push new questions
prompts.onNext({ /* question... */ });
prompts.onNext({ /* question... */ });
prompts.next({ /* question... */ });
prompts.next({ /* question... */ });

// When you're done
prompts.onCompleted();
prompts.complete();
```

And using the return value `process` property, you can access more fine grained callbacks:
Expand Down
4 changes: 2 additions & 2 deletions examples/rx-observable-array.js
@@ -1,5 +1,5 @@
var inquirer = require('..');
var Rx = require('rx-lite-aggregates');
var Rx = require('rxjs/Rx');

var questions = [
{
Expand Down Expand Up @@ -32,7 +32,7 @@ var questions = [
}
];

var observable = Rx.Observable.fromArray(questions);
var observable = Rx.Observable.from(questions);

inquirer.prompt(observable).ui.process.subscribe(
function(ans) {
Expand Down
10 changes: 5 additions & 5 deletions examples/rx-observable-create.js
@@ -1,14 +1,14 @@
var inquirer = require('..');
var Rx = require('rx-lite-aggregates');
var Rx = require('rxjs/Rx');

var observe = Rx.Observable.create(function(obs) {
obs.onNext({
obs.next({
type: 'input',
name: 'first_name',
message: "What's your first name"
});

obs.onNext({
obs.next({
type: 'input',
name: 'last_name',
message: "What's your last name",
Expand All @@ -17,7 +17,7 @@ var observe = Rx.Observable.create(function(obs) {
}
});

obs.onNext({
obs.next({
type: 'input',
name: 'phone',
message: "What's your phone number",
Expand All @@ -32,7 +32,7 @@ var observe = Rx.Observable.create(function(obs) {
return 'Please enter a valid phone number';
}
});
obs.onCompleted();
obs.complete();
});

inquirer.prompt(observe).then(answers => {
Expand Down
2 changes: 1 addition & 1 deletion lib/inquirer.js
Expand Up @@ -69,7 +69,7 @@ inquirer.createPromptModule = function(opt) {

/**
* Public CLI helper interface
* @param {Array|Object|rx.Observable} questions - Questions settings array
* @param {Array|Object|Rx.Observable} questions - Questions settings array
* @param {Function} cb - Callback being passed the user answers
* @return {inquirer.ui.Prompt}
*/
Expand Down
10 changes: 5 additions & 5 deletions lib/prompts/editor.js
Expand Up @@ -7,7 +7,7 @@ var chalk = require('chalk');
var ExternalEditor = require('external-editor');
var Base = require('./base');
var observe = require('../utils/events');
var rx = require('rx-lite-aggregates');
var Rx = require('rxjs/Rx');

class EditorPrompt extends Base {
/**
Expand All @@ -19,7 +19,7 @@ class EditorPrompt extends Base {
_run(cb) {
this.done = cb;

this.editorResult = new rx.Subject();
this.editorResult = new Rx.Subject();

// Open Editor on "line" (Enter Key)
var events = observe(this.rl);
Expand Down Expand Up @@ -75,14 +75,14 @@ class EditorPrompt extends Base {
endExternalEditor(error, result) {
this.rl.resume();
if (error) {
this.editorResult.onError(error);
this.editorResult.error(error);
} else {
this.editorResult.onNext(result);
this.editorResult.next(result);
}
}

onEnd(state) {
this.editorResult.dispose();
this.editorResult.unsubscribe();
this.lineSubscription.dispose();
this.answer = state.value;
this.status = 'answered';
Expand Down
22 changes: 11 additions & 11 deletions lib/ui/prompt.js
@@ -1,6 +1,6 @@
'use strict';
var _ = require('lodash');
var rx = require('rx-lite-aggregates');
var Rx = require('rxjs/Rx');
var runAsync = require('run-async');
var utils = require('../utils/utils');
var Base = require('./baseUI');
Expand All @@ -27,7 +27,7 @@ class PromptUI extends Base {
// Create an observable, unless we received one as parameter.
// Note: As this is a public interface, we cannot do an instanceof check as we won't
// be using the exact same object in memory.
var obs = _.isArray(questions) ? rx.Observable.from(questions) : questions;
var obs = _.isArray(questions) ? Rx.Observable.from(questions) : questions;

this.process = obs
.concatMap(this.processQuestion.bind(this))
Expand Down Expand Up @@ -57,8 +57,8 @@ class PromptUI extends Base {

processQuestion(question) {
question = _.clone(question);
return rx.Observable.defer(() => {
var obs = rx.Observable.of(question);
return Rx.Observable.defer(() => {
var obs = Rx.Observable.of(question);

return obs
.concatMap(this.setDefaultType.bind(this))
Expand All @@ -79,8 +79,8 @@ class PromptUI extends Base {
fetchAnswer(question) {
var Prompt = this.prompts[question.type];
this.activePrompt = new Prompt(question, this.rl, this.answers);
return rx.Observable.defer(() =>
rx.Observable.fromPromise(
return Rx.Observable.defer(() =>
Rx.Observable.fromPromise(
this.activePrompt.run().then(answer => ({ name: question.name, answer: answer }))
)
);
Expand All @@ -91,21 +91,21 @@ class PromptUI extends Base {
if (!this.prompts[question.type]) {
question.type = 'input';
}
return rx.Observable.defer(() => rx.Observable.return(question));
return Rx.Observable.defer(() => Rx.Observable.of(question));
}

filterIfRunnable(question) {
if (question.when === false) {
return rx.Observable.empty();
return Rx.Observable.empty();
}

if (!_.isFunction(question.when)) {
return rx.Observable.return(question);
return Rx.Observable.of(question);
}

var answers = this.answers;
return rx.Observable.defer(() =>
rx.Observable.fromPromise(
return Rx.Observable.defer(() =>
Rx.Observable.fromPromise(
runAsync(question.when)(answers).then(shouldRun => {
if (shouldRun) {
return question;
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/events.js
@@ -1,17 +1,17 @@
'use strict';
var rx = require('rx-lite-aggregates');
var Rx = require('rxjs/Rx');

function normalizeKeypressEvents(value, key) {
return { value: value, key: key || {} };
}

module.exports = function(rl) {
var keypress = rx.Observable.fromEvent(rl.input, 'keypress', normalizeKeypressEvents)
var keypress = Rx.Observable.fromEvent(rl.input, 'keypress', normalizeKeypressEvents)
// Ignore `enter` key. On the readline, we only care about the `line` event.
.filter(({ key }) => key.name !== 'enter' && key.name !== 'return');

return {
line: rx.Observable.fromEvent(rl, 'line'),
line: Rx.Observable.fromEvent(rl, 'line'),
keypress: keypress,

normalizedUpKey: keypress
Expand Down
8 changes: 4 additions & 4 deletions lib/utils/utils.js
@@ -1,6 +1,6 @@
'use strict';
var _ = require('lodash');
var rx = require('rx-lite-aggregates');
var Rx = require('rxjs/Rx');
var runAsync = require('run-async');

/**
Expand All @@ -9,15 +9,15 @@ var runAsync = require('run-async');
* @param {Object} question - Question object
* @param {String} prop - Property to fetch name
* @param {Object} answers - Answers object
* @return {rx.Obsersable} - Observable emitting once value is known
* @return {Rx.Observable} - Observable emitting once value is known
*/

exports.fetchAsyncQuestionProperty = function(question, prop, answers) {
if (!_.isFunction(question[prop])) {
return rx.Observable.return(question);
return Rx.Observable.of(question);
}

return rx.Observable.fromPromise(
return Rx.Observable.fromPromise(
runAsync(question[prop])(answers).then(value => {
question[prop] = value;
return question;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -46,8 +46,7 @@
"lodash": "^4.3.0",
"mute-stream": "0.0.7",
"run-async": "^2.2.0",
"rx-lite": "^4.0.8",
"rx-lite-aggregates": "^4.0.8",
"rxjs": "^5.5.2",
"string-width": "^2.1.0",
"strip-ansi": "^4.0.0",
"through": "^2.3.6"
Expand Down
10 changes: 5 additions & 5 deletions test/specs/inquirer.js
Expand Up @@ -5,7 +5,7 @@
var expect = require('chai').expect;
var sinon = require('sinon');
var _ = require('lodash');
var rx = require('rx-lite-aggregates');
var Rx = require('rxjs/Rx');
var inquirer = require('../../lib/inquirer');
var autosubmit = require('../helpers/events').autosubmit;

Expand Down Expand Up @@ -386,20 +386,20 @@ describe('inquirer.prompt', function() {

it('takes an Observable as question', function() {
var promise;
var prompts = rx.Observable.create(function(obs) {
obs.onNext({
var prompts = Rx.Observable.create(function(obs) {
obs.next({
type: 'confirm',
name: 'q1',
message: 'message'
});
setTimeout(() => {
obs.onNext({
obs.next({
type: 'confirm',
name: 'q2',
message: 'message',
default: false
});
obs.onCompleted();
obs.complete();
promise.ui.rl.emit('line');
}, 30);
});
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -2243,7 +2243,7 @@ rx-lite@*, rx-lite@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/rx-lite/-/rx-lite-4.0.8.tgz#0b1e11af8bc44836f04a6407e92da42467b79444"

rxjs@^5.4.2:
rxjs@^5.4.2, rxjs@^5.5.2:
version "5.5.2"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.5.2.tgz#28d403f0071121967f18ad665563255d54236ac3"
dependencies:
Expand Down

0 comments on commit 258064f

Please sign in to comment.