From ef3e3c2c334e4f2671ba05f5ee8b06ba506be929 Mon Sep 17 00:00:00 2001 From: thatlittlegit Date: Sun, 17 Sep 2017 19:40:53 -0400 Subject: [PATCH] Reorganize task() and add another method for it --- index.js | 10 ++++++++-- task.js | 16 ++++++++++++++++ test/task.test.js | 17 ++++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 task.js diff --git a/index.js b/index.js index 9502db3..f6d46ec 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,16 @@ import {EventEmitter2} from 'eventemitter2'; +import * as tasks from './task'; + export const emitter = new EventEmitter2(); const gupp = { - task(name, cb) { - emitter.on(`tasks.${name}`, cb); + task(name, cb, cb1) { + if (Array.isArray(cb)) { + tasks.taskDepsCb(name, cb, cb1); + } else if (cb instanceof Function) { + tasks.taskCb(name, cb); + } } }; diff --git a/task.js b/task.js new file mode 100644 index 0000000..641244a --- /dev/null +++ b/task.js @@ -0,0 +1,16 @@ +/* + * Manages tasks. Provides various gupp.task's. + */ +import {emitter} from '.'; + +export function taskCb(name, cb) { + emitter.on(`tasks.${name}`, cb); +} + +export function taskDepsCb(name, deps, cb) { + deps.forEach(dep => { + emitter.on(`tasks.${name}`, () => (emitter.emit(`tasks.${dep}`))); + }); + + emitter.on(`tasks.${name}`, cb); +} diff --git a/test/task.test.js b/test/task.test.js index 272bcb5..0c9f666 100644 --- a/test/task.test.js +++ b/test/task.test.js @@ -3,9 +3,24 @@ import isEmpty from 'is-empty'; import noop from 'noop3'; import {default as gupp, emitter as ee} from '../dist/'; -test('When gupp.task is called, a new entry should be made in the EventEmitter under \'tasks\'', t => { +function clean() { + ee.removeAllListeners('tasks.hello'); +} + +test('When gupp.task(name, cb) is called, a new entry should be made in the EventEmitter under \'tasks\'', t => { gupp.task('hello', noop); t.false(isEmpty(ee.listeners('tasks.hello'))); t.is(ee.listeners('tasks.hello')[0], noop); + + clean(); +}); + +test('When gupp.task(name, deps, cb) is called, a number of new entries should be made in the EventEmitter', t => { + gupp.task('hello', ['world'], noop); + + t.false(isEmpty(ee.listeners('tasks.hello'))); + t.is(ee.listeners('tasks.hello').length, 2); + + clean(); });