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

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Dec 22, 2024

image

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.

Copy link

coderabbitai bot commented Dec 22, 2024

Walkthrough

The pull request introduces updates to the project's CI workflow, package scripts, and test suite. The GitHub Actions workflow now includes Node.js version 23 for testing. A new test-local script is added to the package.json, and the test file is enhanced with a new SomeServiceClient2 class that extends the Base class. The primary changes focus on improving testing capabilities and expanding the project's compatibility with newer Node.js versions.

Changes

File Change Summary
.github/workflows/nodejs.yml Added Node.js version 23 to CI testing matrix
package.json Added test-local script with 5-second timeout
src/index.ts Modified error event handling to use super.on()
test/index.test.ts Added SomeServiceClient2 class with enhanced event listener tracking

Sequence Diagram

sequenceDiagram
    participant Client as SomeServiceClient2
    participant Base as Base Class
    participant Listener as Event Listener

    Client->>Base: Extends Base class
    Client->>Base: Override on() method
    Client->>Listener: Add error listener
    Client->>Base: Track listener arguments
    Base->>Listener: Trigger event handling
Loading

Poem

🐰 A Rabbit's CI Delight 🚀

Node.js versions dance and play,
Version twenty-three joins the fray!
Tests run swift with timeout's might,
Our code leaps forward, pure and bright!
Hop, hop, hooray! 🎉

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

pkg-pr-new bot commented Dec 22, 2024

Open in Stackblitz

npm i https://pkg.pr.new/node-modules/sdk-base@24

commit: 88fb11e

Copy link

codecov bot commented Dec 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (cde6773) to head (88fb11e).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #24   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          117       117           
  Branches        29        30    +1     
=========================================
  Hits           117       117           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fengmk2 fengmk2 merged commit d1ffb61 into master Dec 22, 2024
23 checks passed
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
test/index.test.ts (3)

9-17: LGTM! Consider adding type safety improvements

The implementation correctly extends the Base class and properly maintains the parent functionality. However, we could improve type safety.

Consider adding proper types for the event arguments:

-  protected _lists: any[] = [];
+  protected _lists: Array<[string, (...args: any[]) => void]> = [];

-  on(...args: any[]) {
+  on(event: string, listener: (...args: any[]) => void) {
-    this._lists.push(args);
+    this._lists.push([event, listener]);
-    super.on(args[0], args[1]);
+    super.on(event, listener);
     return this;
   }

20-24: LGTM! Consider adding more test coverage

The test correctly verifies that subclasses can override the on method while maintaining the base functionality.

Consider adding these additional test cases to improve coverage:

  1. Verify that _lists contains the correct event registration
  2. Test other event methods (e.g., once, addListener)
  3. Test event removal through off or removeListener

Example:

it('should track event registrations correctly', () => {
  const c = new SomeServiceClient2();
  const handler = () => {};
  c.on('test', handler);
  assert.deepEqual(c._lists[0], ['test', handler]);
});

9-24: Solid enhancement to event handling extensibility

The implementation successfully enables subclasses to extend event handling while maintaining backward compatibility. This enhancement aligns well with the Open/Closed principle, allowing for behavior extension without modifying existing code.

This pattern could be useful in several scenarios:

  1. Event logging/monitoring
  2. Event transformation
  3. Conditional event handling
  4. Event debugging

Consider documenting this capability in the README to help other developers understand how to properly extend event handling in their subclasses.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 50f8054 and 88fb11e.

📒 Files selected for processing (4)
  • .github/workflows/nodejs.yml (1 hunks)
  • package.json (1 hunks)
  • src/index.ts (1 hunks)
  • test/index.test.ts (1 hunks)
🔇 Additional comments (3)
src/index.ts (1)

48-50: LGTM! Good fix for allowing subclass event method overrides.

The change from this.on to super.on is a solid improvement that:

  • Allows subclasses to safely override the on method without breaking error handling
  • Maintains the error handling functionality by registering directly on the parent class
  • Aligns with the principle of extensibility
package.json (1)

44-44: LGTM! Consistent test configuration.

The new test-local script maintains consistency with the CI test timeout, which is good for development and testing parity.

.github/workflows/nodejs.yml (1)

14-14: LGTM! Verify event handling in Node.js 23.

Adding Node.js 23 to the test matrix is good for ensuring compatibility. Since this PR modifies event handling behavior, it's particularly important to verify it works correctly across all Node.js versions.

Let's verify the event handling implementation is compatible with Node.js 23:

✅ Verification successful

LGTM! Event handling implementation is compatible with Node.js 23

The event handling implementation uses standard Node.js EventEmitter APIs that are stable across versions:

  • Uses core EventEmitter methods (on, once, emit) without version-specific features
  • Implements wrapper methods that only add async function support
  • No usage of experimental or deprecated event APIs
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any Node.js version-specific event handling APIs or changes

# Search for event-related APIs usage
rg -l 'EventEmitter|emit|on\(|once\(' | while read -r file; do
  echo "=== $file ==="
  rg 'EventEmitter|emit|on\(|once\(' "$file"
done

Length of output: 3807

@fengmk2 fengmk2 deleted the allow-override-event-methods branch December 22, 2024 03:20
fengmk2 pushed a commit that referenced this pull request Dec 22, 2024
[skip ci]

## [5.0.1](v5.0.0...v5.0.1) (2024-12-22)

### Bug Fixes

* allow sub class to override event methods ([#24](#24)) ([d1ffb61](d1ffb61))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant