Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: listener add async function support #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"sourceMaps": "inline",
"presets": ["es2015", "stage-1"],
"plugins": [
"transform-runtime",
["transform-async-to-module-method", {
"module": "bluebird",
"method": "coroutine"
}]
]
}
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ class Base extends EventEmitter {
}

_wrapListener(eventName, listener) {
if (is.generatorFunction(listener)) {
assert(eventName !== 'error', '[sdk-base] `error` event should not have a generator listener.');
if (is.generatorFunction(listener) || is.asyncFunction(listener)) {
assert(eventName !== 'error', '[sdk-base] `error` event should not have a generator/async listener.');

const newListener = (...args) => {
co(function* () {
yield listener(...args);
}).catch(err => {
let promise;
if (is.asyncFunction(listener)) {
promise = listener(...args);
} else {
promise = co(function* () {
yield listener(...args);
});
}
promise.catch(err => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removeListener 的时候也要支持下 async function

err.name = 'EventListenerProcessError';
this.emit('error', err);
});
Expand Down Expand Up @@ -78,7 +84,7 @@ class Base extends EventEmitter {

removeListener(eventName, listener) {
let target = listener;
if (is.generatorFunction(listener)) {
if (is.generatorFunction(listener) || is.asyncFunction(listener)) {
const listeners = this.listeners(eventName);
for (const fn of listeners) {
if (fn.original === listener) {
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
"egg-ci": "^1.8.0",
"eslint": "^4.11.0",
"eslint-config-egg": "^5.1.1",
"pedding": "^1.1.0"
"pedding": "^1.1.0",
"babel-plugin-transform-async-to-module-method": "^6.24.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-1": "^6.24.0",
"babel-register": "^6.26.0",
"bluebird": "^3.3.5"
},
"engine": {
"node": ">= 6.0.0"
Expand Down
9 changes: 9 additions & 0 deletions test/.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const ver = process.version;
let major = ver.split('.')[0];
major = parseInt(major.substr(1), 10);
// below node 8
if (major < 8) {
require('babel-register');
}
62 changes: 62 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,49 @@ describe('sdk-base', () => {
client.emit('event_code', 1, 2);
});

it('should add async listener', done => {
done = pedding(done, 8);
const client = new SomeServiceClient();

client.addListener('event_code_async', async function(a, b) {
console.log('event_code_async in addListener');
assert(a === 1);
assert(b === 2);
done();
});

client.on('event_code_async', async function(a, b) {
console.log('event_code_async in on');
assert(a === 1);
assert(b === 2);
done();
});

client.once('event_code_async', async function(a, b) {
console.log('event_code_async in once');
assert(a === 1);
assert(b === 2);
done();
});

client.prependListener('event_code_async', async function(a, b) {
console.log('event_code_async in prependListener');
assert(a === 1);
assert(b === 2);
done();
});

client.prependOnceListener('event_code_async', async function(a, b) {
console.log('event_code_async in prependOnceListener');
assert(a === 1);
assert(b === 2);
done();
});

client.emit('event_code_async', 1, 2);
client.emit('event_code_async', 1, 2);
});

it('should catch generator exception and emit on error event', done => {
done = pedding(done, 2);
const client = new SomeServiceClient();
Expand Down Expand Up @@ -278,6 +321,25 @@ describe('sdk-base', () => {
assert(client.listeners('event_code').length === 0);
});

it('should remove async listener', done => {
done = pedding(done, 1);
const client = new SomeServiceClient();
const handler = async function(data) {
assert(data === 1);
console.log('async listener');
done();
};
client.on('event_code', data => {
assert(data === 1);
console.log('normal listener');
});
client.on('event_code_async', handler);
client.emit('event_code', 1);
client.emit('event_code_async', 1);
client.removeListener('event_code_async', handler);
assert(client.listeners('event_code_async').length === 0);
});

it('should not allow to add generator listener on error event', () => {
const client = new SomeServiceClient();
assert.throws(() => {
Expand Down