From a620662766a084a11bd528d1f50b9edcfb054395 Mon Sep 17 00:00:00 2001 From: Andy Wermke Date: Fri, 16 Sep 2016 13:18:40 +0200 Subject: [PATCH] fix isObservable() - fixes #29 --- lib/task.js | 9 +++------ lib/utils.js | 5 +++++ package.json | 3 ++- test/utils.js | 21 +++++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 lib/utils.js create mode 100644 test/utils.js diff --git a/lib/task.js b/lib/task.js index 9f48b67..ee2e7e1 100644 --- a/lib/task.js +++ b/lib/task.js @@ -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; @@ -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; @@ -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 => { diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..8deee3e --- /dev/null +++ b/lib/utils.js @@ -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'); diff --git a/package.json b/package.json index 82d5dd5..edc0a41 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,8 @@ "clinton": "*", "delay": "^1.3.1", "hook-std": "^0.2.0", - "xo": "*" + "xo": "*", + "zen-observable": "^0.3.0" }, "xo": { "esnext": true diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 0000000..9351dd4 --- /dev/null +++ b/test/utils.js @@ -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(() => {}))); +});