Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ 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);
const ret = yield listener(...args);
if (is.promise(ret)) {
yield ret;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这里要 generator 和 async 分开判断,async 不要包 co 了

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

如果分开的话,实现上面并不是很优雅。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

没办法,包一层 async 的化就只处理下 catch 就好了

}).catch(err => {
err.name = 'EventListenerProcessError';
this.emit('error', err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sdk-base",
"version": "3.4.0",
"version": "3.4.1",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

由发布的人来改版本,这里应该发 minor

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

这里可以改进

"description": "a base class for sdk with default error handler",
"main": "index.js",
"scripts": {
Expand Down
43 changes: 43 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