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

fix: allow sub class to override event methods #24

Merged
merged 1 commit into from
Dec 22, 2024
Merged
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
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
Loading