Skip to content

Commit

Permalink
fix: allow sub class to override event methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 22, 2024
1 parent 50f8054 commit 88fb11e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ jobs:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
version: '18.19.0, 18, 20, 22'
version: '18.19.0, 18, 20, 22, 23'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"lint": "eslint --cache src test --ext .ts",
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
"test": "egg-bin test -p --timeout 5000",
"test-local": "egg-bin test --timeout 5000",
"preci": "npm run lint && npm run prepublishOnly && attw --pack",
"ci": "egg-bin cov -p",
"prepublishOnly": "tshy && tshy-after"
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export abstract class Base<T = any> extends ReadyEventEmitter {
}
this.options = options ?? {};
this.#localStorage = this.options.localStorage ?? getAsyncLocalStorage<T>();
this.on('error', err => {
super.on('error', err => {
this._defaultErrorHandler(err);
});
}
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@ import { Base, BaseOptions } from '../src/index.js';
describe('test/index.test.ts', () => {
class SomeServiceClient extends Base {}

class SomeServiceClient2 extends Base {
protected _lists: any[] = [];

on(...args: any[]) {
this._lists.push(args);
super.on(args[0], args[1]);
return this;
}
}

describe('default error handler', () => {
it('should allow subclass to override on methods', () => {
const c = new SomeServiceClient2();
c.on('error', () => {});
assert.equal(c.listeners('error').length, 2);
});

it('should auto add the default error handler and show error message', () => {
const c = new SomeServiceClient();
assert.equal(c.listeners('error').length, 1);
Expand Down

0 comments on commit 88fb11e

Please sign in to comment.