Skip to content

Commit

Permalink
v1.1.0. Made HookEmitter now both an export and default export. Small…
Browse files Browse the repository at this point in the history
… bug fix with unhandled promise rejection.
  • Loading branch information
cb1kenobi committed Nov 2, 2016
1 parent 11ac690 commit 49de4bb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hook-emitter",
"version": "1.0.0",
"version": "1.1.0",
"description": "Event emitter with support for asynchronous handlers and a sweet function hook mechanism.",
"main": "./dist/index.js",
"author": "Chris Barber <[email protected]> (https://github.com/cb1kenobi)",
Expand All @@ -18,7 +18,7 @@
},
"dependencies": {
"babel-plugin-transform-async-to-generator": "^6.16.0",
"source-map-support": "^0.4.5"
"source-map-support": "^0.4.6"
},
"devDependencies": {
"babel-eslint": "^7.1.0",
Expand All @@ -28,7 +28,7 @@
"coveralls": "^2.11.14",
"del": "^2.2.2",
"esdoc-es7-plugin": "0.0.3",
"eslint": "3.8.1",
"eslint": "3.9.1",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-babel-istanbul": "^1.5.0",
Expand All @@ -37,7 +37,7 @@
"gulp-eslint": "^3.0.1",
"gulp-filter": "^4.0.0",
"gulp-inject-modules": "^1.0.0",
"gulp-load-plugins": "^1.3.0",
"gulp-load-plugins": "^1.4.0",
"gulp-mocha": "^3.0.1",
"gulp-plumber": "^1.1.0",
"gulp-sourcemaps": "^2.2.0"
Expand Down
26 changes: 18 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'source-map-support/register';
/**
* Emits events and hooks to synchronous and asynchronous listeners.
*/
export class HookEmitter {
class HookEmitter {
/**
* Constructs an HookEmitter object.
*/
Expand Down Expand Up @@ -248,13 +248,13 @@ export class HookEmitter {
let fired = null;
let pending = false;

// construct the args
const args = [...payload.args, function next(err, result) {
fired = { err, result };

if (err) {
return Promise.reject(err);
}
// we set the fired promise to this result/error
fired = err ? Promise.reject(err) : Promise.resolve(result);

// if somebody mixes paradigms and calls next().then(),
// at least their function will wait for the next listener
return dispatch(transform(result, payload), i + 1)
.then(result => {
if (pending) {
Expand All @@ -266,11 +266,14 @@ export class HookEmitter {
.catch(reject);
}];

// call the listener
let result = listener.apply(null, args);

// if we got back a promise, we have to wait
if (result instanceof Promise) {
if (fired) {
return result.then(resolve, reject);
return result
.then(resolve, reject);
}

return result
Expand All @@ -279,8 +282,12 @@ export class HookEmitter {
.catch(reject);
}

// result wasn't a promise, but maybe our old school next()
// callback was called
if (fired) {
return fired.err ? reject(fired.err) : resolve(fired.result || payload);
return fired
.then(result => resolve(result || payload))
.catch(reject);
}

// if the listener has more args than the number of args in
Expand Down Expand Up @@ -416,3 +423,6 @@ export class HookEmitter {
return this;
}
}

export { HookEmitter };
export default HookEmitter;
2 changes: 1 addition & 1 deletion test/test-hookemitter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HookEmitter } from '../src/index';
import HookEmitter from '../src/index';
import { expect } from 'chai';

describe('on', () => {
Expand Down

0 comments on commit 49de4bb

Please sign in to comment.