Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Reorganize task() and add another method for it
Browse files Browse the repository at this point in the history
  • Loading branch information
thatlittlegit committed Sep 17, 2017
1 parent dea1006 commit ef3e3c2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};

Expand Down
16 changes: 16 additions & 0 deletions task.js
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 16 additions & 1 deletion test/task.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit ef3e3c2

Please sign in to comment.