Skip to content

Commit 49de4bb

Browse files
committed
v1.1.0. Made HookEmitter now both an export and default export. Small bug fix with unhandled promise rejection.
1 parent 11ac690 commit 49de4bb

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hook-emitter",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Event emitter with support for asynchronous handlers and a sweet function hook mechanism.",
55
"main": "./dist/index.js",
66
"author": "Chris Barber <chris@cb1inc.com> (https://github.com/cb1kenobi)",
@@ -18,7 +18,7 @@
1818
},
1919
"dependencies": {
2020
"babel-plugin-transform-async-to-generator": "^6.16.0",
21-
"source-map-support": "^0.4.5"
21+
"source-map-support": "^0.4.6"
2222
},
2323
"devDependencies": {
2424
"babel-eslint": "^7.1.0",
@@ -28,7 +28,7 @@
2828
"coveralls": "^2.11.14",
2929
"del": "^2.2.2",
3030
"esdoc-es7-plugin": "0.0.3",
31-
"eslint": "3.8.1",
31+
"eslint": "3.9.1",
3232
"gulp": "^3.9.1",
3333
"gulp-babel": "^6.1.2",
3434
"gulp-babel-istanbul": "^1.5.0",
@@ -37,7 +37,7 @@
3737
"gulp-eslint": "^3.0.1",
3838
"gulp-filter": "^4.0.0",
3939
"gulp-inject-modules": "^1.0.0",
40-
"gulp-load-plugins": "^1.3.0",
40+
"gulp-load-plugins": "^1.4.0",
4141
"gulp-mocha": "^3.0.1",
4242
"gulp-plumber": "^1.1.0",
4343
"gulp-sourcemaps": "^2.2.0"

src/index.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'source-map-support/register';
33
/**
44
* Emits events and hooks to synchronous and asynchronous listeners.
55
*/
6-
export class HookEmitter {
6+
class HookEmitter {
77
/**
88
* Constructs an HookEmitter object.
99
*/
@@ -248,13 +248,13 @@ export class HookEmitter {
248248
let fired = null;
249249
let pending = false;
250250

251+
// construct the args
251252
const args = [...payload.args, function next(err, result) {
252-
fired = { err, result };
253-
254-
if (err) {
255-
return Promise.reject(err);
256-
}
253+
// we set the fired promise to this result/error
254+
fired = err ? Promise.reject(err) : Promise.resolve(result);
257255

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

269+
// call the listener
269270
let result = listener.apply(null, args);
270271

272+
// if we got back a promise, we have to wait
271273
if (result instanceof Promise) {
272274
if (fired) {
273-
return result.then(resolve, reject);
275+
return result
276+
.then(resolve, reject);
274277
}
275278

276279
return result
@@ -279,8 +282,12 @@ export class HookEmitter {
279282
.catch(reject);
280283
}
281284

285+
// result wasn't a promise, but maybe our old school next()
286+
// callback was called
282287
if (fired) {
283-
return fired.err ? reject(fired.err) : resolve(fired.result || payload);
288+
return fired
289+
.then(result => resolve(result || payload))
290+
.catch(reject);
284291
}
285292

286293
// if the listener has more args than the number of args in
@@ -416,3 +423,6 @@ export class HookEmitter {
416423
return this;
417424
}
418425
}
426+
427+
export { HookEmitter };
428+
export default HookEmitter;

test/test-hookemitter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HookEmitter } from '../src/index';
1+
import HookEmitter from '../src/index';
22
import { expect } from 'chai';
33

44
describe('on', () => {

0 commit comments

Comments
 (0)