Skip to content

Commit 88fb11e

Browse files
committed
fix: allow sub class to override event methods
1 parent 50f8054 commit 88fb11e

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

.github/workflows/nodejs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ jobs:
1111
name: Node.js
1212
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1313
with:
14-
version: '18.19.0, 18, 20, 22'
14+
version: '18.19.0, 18, 20, 22, 23'
1515
secrets:
1616
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"lint": "eslint --cache src test --ext .ts",
4242
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
4343
"test": "egg-bin test -p --timeout 5000",
44+
"test-local": "egg-bin test --timeout 5000",
4445
"preci": "npm run lint && npm run prepublishOnly && attw --pack",
4546
"ci": "egg-bin cov -p",
4647
"prepublishOnly": "tshy && tshy-after"

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export abstract class Base<T = any> extends ReadyEventEmitter {
4545
}
4646
this.options = options ?? {};
4747
this.#localStorage = this.options.localStorage ?? getAsyncLocalStorage<T>();
48-
this.on('error', err => {
48+
super.on('error', err => {
4949
this._defaultErrorHandler(err);
5050
});
5151
}

test/index.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,23 @@ import { Base, BaseOptions } from '../src/index.js';
66
describe('test/index.test.ts', () => {
77
class SomeServiceClient extends Base {}
88

9+
class SomeServiceClient2 extends Base {
10+
protected _lists: any[] = [];
11+
12+
on(...args: any[]) {
13+
this._lists.push(args);
14+
super.on(args[0], args[1]);
15+
return this;
16+
}
17+
}
18+
919
describe('default error handler', () => {
20+
it('should allow subclass to override on methods', () => {
21+
const c = new SomeServiceClient2();
22+
c.on('error', () => {});
23+
assert.equal(c.listeners('error').length, 2);
24+
});
25+
1026
it('should auto add the default error handler and show error message', () => {
1127
const c = new SomeServiceClient();
1228
assert.equal(c.listeners('error').length, 1);

0 commit comments

Comments
 (0)