Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-fix isObservable() and add test - fixes #29 #30

Merged
merged 3 commits into from
Sep 16, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const streamToObservable = require('stream-to-observable');
const Subject = require('rxjs/Subject').Subject;
const renderers = require('./renderer');
const state = require('./state');

const isListr = obj => obj && obj.setRenderer && obj.add && obj.run;
// https://github.com/sindresorhus/is-observable/issues/1
const isObservable = obj => obj && typeof obj.subscribe === 'function' && typeof obj.constructor.create === 'function';
const util = require('./util');

const defaultSkipFn = () => false;

Expand Down Expand Up @@ -86,7 +83,7 @@ class Task extends Subject {
run() {
const handleResult = result => {
// Detect the subtask
if (isListr(result)) {
if (util.isListr(result)) {
result.setRenderer(renderers.silent);
this._subtasks = result.tasks;

Expand All @@ -103,7 +100,7 @@ class Task extends Subject {
}

// Detect Observable
if (isObservable(result)) {
if (util.isObservable(result)) {
result = new Promise((resolve, reject) => {
result.subscribe({
next: data => {
Expand Down
8 changes: 8 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const isListr = obj => obj && obj.setRenderer && obj.add && obj.run;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Export the function right away without intermediate step.

// https://github.com/sindresorhus/is-observable/issues/1
const isObservable = obj => obj && typeof obj.subscribe === 'function' && obj.constructor.name === 'Observable';

exports.isListr = isListr;
exports.isObservable = isObservable;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"clinton": "*",
"delay": "^1.3.1",
"hook-std": "^0.2.0",
"xo": "*"
"xo": "*",
"zen-observable": "^0.3.0"
},
"xo": {
"esnext": true
Expand Down
21 changes: 21 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from 'ava';
import {Observable as RxObservable} from 'rxjs/Observable';
import ZenObservable from 'zen-observable';
import Listr from '../';
import {isListr, isObservable} from '../lib/util';

test('isListr', t => {
t.falsy(isListr(null));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use t.false and t.true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I did. But it failed, since right now isListr() will return null or the listr instance itself if it is. So then I just added a ... ? true : false to isListr(), but xo failed, because for some weird reason it complains that the ternary is superfluous...

Any suggestions?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap it in Boolean(...)

t.falsy(isListr({}));
t.falsy(isListr(() => {}));
t.truthy(isListr(new Listr([])));
});

test('isObservable', t => {
t.falsy(isObservable(null));
t.falsy(isObservable({}));
t.falsy(isObservable(new Listr([])));
t.falsy(isObservable(new Promise(() => {})));
t.truthy(isObservable(new RxObservable(() => {})));
t.truthy(isObservable(new ZenObservable(() => {})));
});