Skip to content

Commit

Permalink
fix: allow sub class to override event methods (#24)
Browse files Browse the repository at this point in the history
![image](https://github.com/user-attachments/assets/6deec8b0-14d1-4176-bc2f-039a68fb6391)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
	- Added support for Node.js version 23 in CI testing.
- Introduced a new script for local testing (`test-local`) in the
project.
- Added a new service client class (`SomeServiceClient2`) to enhance
event handling capabilities.
  
- **Bug Fixes**
	- Improved error handling mechanism by delegating to the superclass.

- **Tests**
- Added test cases for the new service client class and its event
handling.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 22, 2024
1 parent 50f8054 commit d1ffb61
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 d1ffb61

Please sign in to comment.