Skip to content

fix: use lifecycle instead of beforeStart#297

Merged
fengmk2 merged 2 commits into
masterfrom
fix-typo-2
Mar 22, 2025
Merged

fix: use lifecycle instead of beforeStart#297
fengmk2 merged 2 commits into
masterfrom
fix-typo-2

Conversation

@fengmk2
Copy link
Copy Markdown
Member

@fengmk2 fengmk2 commented Mar 22, 2025

Summary by CodeRabbit

  • Refactor
    • Enhanced the initialization flow by transitioning to a structured lifecycle management approach.
  • Bug Fixes
    • Clarified error messages related to instance creation, ensuring users are informed that asynchronous creation is supported.

@fengmk2 fengmk2 requested a review from Copilot March 22, 2025 15:32
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 22, 2025

Walkthrough

This pull request changes how lifecycle events are registered by replacing the direct call to beforeStart with a call to lifecycle.registerBeforeStart, including an updated naming convention for the event. In addition, error messages in the Singleton implementation and related tests have been updated to correctly indicate support only for asynchronous creation.

Changes

File(s) Change Summary
src/egg.ts Replaced this.beforeStart(async () => { ... } with this.lifecycle.registerBeforeStart(async () => { ... } and updated the event name to ${name}-singleton-init.
src/singleton.ts, test/singleton.test.ts Updated error messages to clarify that the singleton only supports asynchronous creation, aligning documentation and tests with the intended behavior.

Sequence Diagram(s)

sequenceDiagram
    participant EC as EggCore
    participant LS as Lifecycle
    participant CB as Callback
    
    EC->>LS: registerBeforeStart(callback, `${name}-singleton-init`)
    LS->>CB: Schedule async callback for initPromise
    CB-->>LS: Execute callback (await initPromise)
Loading

Poem

I'm just a little rabbit, hopping with delight,
Changing code pathways from morning until night.
Lifecycle's now structured, with a call so keen,
Async creation shines, like a soft moonbeam.
Through error messages clear, our code feels so right.
🐇✨ Happy coding, in every byte!

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/egg.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-eggache".

(The package "eslint-plugin-eggache" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-eggache@latest --save-dev

The plugin "eslint-plugin-eggache" was referenced from the config file in ".eslintrc » eslint-config-egg/typescript » ./index.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

test/singleton.test.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-eggache".

(The package "eslint-plugin-eggache" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-eggache@latest --save-dev

The plugin "eslint-plugin-eggache" was referenced from the config file in ".eslintrc » eslint-config-egg/typescript » ./index.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

src/singleton.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-eggache".

(The package "eslint-plugin-eggache" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-eggache@latest --save-dev

The plugin "eslint-plugin-eggache" was referenced from the config file in ".eslintrc » eslint-config-egg/typescript » ./index.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @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
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR replaces the deprecated beforeStart API with lifecycle.registerBeforeStart and updates an error message in the singleton module.

  • Replaces beforeStart with lifecycle.registerBeforeStart in the EggCore class.
  • Updates the error message in the Singleton module.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/egg.ts Replaced the beforeStart hook with lifecycle.registerBeforeStart, adding a label parameter.
src/singleton.ts Modified an error message regarding supported creation modes.
Comments suppressed due to low confidence (1)

src/singleton.ts:110

  • The error message was updated to say 'only support asynchronous creation', which contradicts the assertion logic that disallows asynchronous creation. Please verify the intended behavior and update the error message to accurately reflect that only synchronous creation is supported.
assert(!isAsyncFunction(this.create),

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 22, 2025

Open in Stackblitz

npm i https://pkg.pr.new/eggjs/core/@eggjs/core@297

commit: c5c7850

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 22, 2025

Codecov Report

Attention: Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 97.17%. Comparing base (500c4ac) to head (c5c7850).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/egg.ts 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #297   +/-   ##
=======================================
  Coverage   97.17%   97.17%           
=======================================
  Files          11       11           
  Lines        3578     3578           
  Branches      632      632           
=======================================
  Hits         3477     3477           
  Misses        101      101           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown

@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 (1)
src/egg.ts (1)

242-244: Consider adding tests for lifecycle event registration

The static analysis indicates these lines aren't covered by tests. Consider adding tests to ensure the singleton lifecycle registration works correctly.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 242-242: src/egg.ts#L242
Added line #L242 was not covered by tests


[warning] 244-244: src/egg.ts#L244
Added line #L244 was not covered by tests

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 41fe40f and c5c7850.

📒 Files selected for processing (3)
  • src/egg.ts (1 hunks)
  • src/singleton.ts (1 hunks)
  • test/singleton.test.ts (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/egg.ts

[warning] 242-242: src/egg.ts#L242
Added line #L242 was not covered by tests


[warning] 244-244: src/egg.ts#L244
Added line #L244 was not covered by tests

🔇 Additional comments (3)
src/singleton.ts (1)

111-111: LGTM: Error message correction for createInstance

The error message has been properly updated to indicate that the Singleton class only supports asynchronous creation through createInstanceAsync. This matches the actual behavior and is more accurate than before.

test/singleton.test.ts (1)

360-360: LGTM: Updated test to match new error message

The test expectation has been correctly updated to match the new error message from the Singleton class, ensuring consistency between implementation and tests.

src/egg.ts (1)

242-245:

Details

❓ Verification inconclusive

LGTM: Using lifecycle manager instead of direct beforeStart

The code now properly uses the lifecycle manager to register the before start event, which aligns with the PR objective to replace direct beforeStart calls with lifecycle management.


🏁 Script executed:

#!/bin/bash
# Check if there are any remaining direct beforeStart calls in the codebase
rg "this\.beforeStart\(" --type ts

Length of output: 36


Action Required: Confirm Complete Removal of Direct beforeStart Calls

The refactoring to use the lifecycle manager (via this.lifecycle.registerBeforeStart) appears correct and aligns with the PR objective. However, the shell script used to search for direct beforeStart calls returned no output, which is ambiguous. Please manually verify that there are no remaining direct invocations of beforeStart in the codebase.

  • Location under review: src/egg.ts (Lines: 242-245)
  • Action: Double-check the entire repository to ensure all direct calls to this.beforeStart() have been replaced with lifecycle manager usage.
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 242-242: src/egg.ts#L242
Added line #L242 was not covered by tests


[warning] 244-244: src/egg.ts#L244
Added line #L244 was not covered by tests

@fengmk2 fengmk2 merged commit 26c95a7 into master Mar 22, 2025
@fengmk2 fengmk2 deleted the fix-typo-2 branch March 22, 2025 15:44
fengmk2 pushed a commit that referenced this pull request Mar 22, 2025
[skip ci]

## [6.4.1](v6.4.0...v6.4.1) (2025-03-22)

### Bug Fixes

* use lifecycle instead of beforeStart ([#297](#297)) ([26c95a7](26c95a7))
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.

2 participants