Skip to content

Commit

Permalink
fix isObservable() - fixes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
andywer authored and SamVerschueren committed Sep 16, 2016
1 parent 5e11919 commit a620662
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
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 utils = require('./utils');

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 (utils.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 (utils.isObservable(result)) {
result = new Promise((resolve, reject) => {
result.subscribe({
next: data => {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.isListr = obj => Boolean(obj && obj.setRenderer && obj.add && obj.run);
// https://github.com/sindresorhus/is-observable/issues/1
exports.isObservable = obj => Boolean(obj && typeof obj.subscribe === 'function' && obj.constructor.name === 'Observable');
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/utils.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/utils';

test('isListr', t => {
t.false(isListr(null));
t.false(isListr({}));
t.false(isListr(() => {}));
t.true(isListr(new Listr([])));
});

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

0 comments on commit a620662

Please sign in to comment.