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

👍 Automatically wait the target plugin on denops.dispatch() #340

Merged
merged 3 commits into from
May 12, 2024

Conversation

lambdalisue
Copy link
Member

@lambdalisue lambdalisue commented May 9, 2024

Now, denops.dispatch() automatically waits for the target plugin to load, enabling child plugins (e.g., Shougo/ddu-ui-filter for Shougo/ddu) to call the parent plugin's API at any time.

Summary by CodeRabbit

  • New Features

    • Modified the Service type to include waitLoaded in addition to dispatch.
    • Asynchronously wait for plugin loading before dispatching commands.
    • Added a new method waitLoaded() to the service object.
  • Bug Fixes

    • Replaced assertThrows with assertRejects in the load() method test for better error handling.
  • Documentation

    • Updated imports in test files for clarity and consistency.

Copy link

coderabbitai bot commented May 9, 2024

Walkthrough

The changes bring enhancements to the denops framework, focusing on plugin event management and service improvements. These updates aim to streamline plugin loading processes and provide better monitoring capabilities during runtime.

Changes

File Path Summary of Changes
denops/@denops-private/denops.ts Modified Service type to include waitLoaded, updated DenopsImpl for asynchronous plugin loading
denops/@denops-private/denops_test.ts Added waitLoaded() method, updated test cases for asynchronous loading
denops/@denops-private/service.ts Added plugin load event dispatching methods, introduced #waiters map for managing waiters
denops/@denops-private/service_test.ts Replaced assertThrows with assertRejects in load() method test

🐇✨
In the realm of code, where the bits align,
A rabbit hopped through, leaving changes behind.
Events now dance, from start to end,
In a digital burrow, where scripts befriend.
Cheers to the devs, from dusk till dawn,
With every commit, a new day is born.
🌟🐰


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

@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: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 2a39384 and ef9809c.
Files selected for processing (2)
  • denops/@denops-private/denops.ts (3 hunks)
  • denops/@denops-private/service.ts (3 hunks)
Additional comments not posted (4)
denops/@denops-private/denops.ts (2)

20-20: The addition of the loaded method to the Service type aligns with the PR's objective to handle plugin load states effectively.


79-92: The updated dispatch method now correctly handles the scenario where a plugin might not be loaded at the time of the dispatch call. This change is crucial for preventing race conditions and ensuring that plugin dependencies are managed correctly.

denops/@denops-private/service.ts (2)

63-65: The implementation of the loaded method in the Service class is straightforward and correctly checks if a plugin is loaded by querying the internal plugins map. This method is essential for the new dispatch logic in denops.ts.


133-162: The addition of custom events (denops:pluginloadstart, denops:pluginloadend, denops:pluginloadfail) enhances the transparency of the plugin loading process. These events provide valuable debugging information and help in managing plugin dependencies more effectively.

denops/@denops-private/service.ts Outdated Show resolved Hide resolved
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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between ef9809c and 96b1f46.
Files selected for processing (4)
  • denops/@denops-private/denops.ts (3 hunks)
  • denops/@denops-private/denops_test.ts (3 hunks)
  • denops/@denops-private/service.ts (3 hunks)
  • denops/@denops-private/service_test.ts (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • denops/@denops-private/denops.ts
  • denops/@denops-private/service.ts
Additional comments not posted (4)
denops/@denops-private/denops_test.ts (3)

25-25: The addition of the loaded method to the Service mock aligns with the changes in the Service class to support checking if a plugin is loaded. This is crucial for the new functionality ensuring plugins are loaded before dispatching.


99-111: The updated test for dispatch() correctly integrates the loaded method to ensure the plugin is loaded before dispatching. This is a critical update for the new functionality and is well implemented here.


115-146: This new test scenario for dispatch() effectively demonstrates the waiting mechanism until the plugin is loaded before proceeding with the dispatch. The use of delay and deadline to simulate asynchronous behavior and the dispatching of a custom event to signal plugin load completion are well implemented. This test is crucial for verifying the new functionality.

denops/@denops-private/service_test.ts (1)

35-36: The new test scenario for the load() method correctly handles the case where no host is bound to the service, ensuring robust error handling. This is an important test for the reliability of the service operations.

Copy link

codecov bot commented May 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.01%. Comparing base (2a39384) to head (897bda7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #340      +/-   ##
==========================================
+ Coverage   89.81%   90.01%   +0.20%     
==========================================
  Files           9        9              
  Lines         648      661      +13     
  Branches       61       62       +1     
==========================================
+ Hits          582      595      +13     
  Misses         66       66              

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

denops/@denops-private/denops_test.ts Outdated Show resolved Hide resolved
denops/@denops-private/denops_test.ts Outdated Show resolved Hide resolved
denops/@denops-private/denops_test.ts Outdated Show resolved Hide resolved
denops/@denops-private/service.ts Outdated Show resolved Hide resolved
denops/@denops-private/denops.ts Outdated Show resolved Hide resolved
@kuuote
Copy link
Member

kuuote commented May 10, 2024

dispatch fails if plugin registered and populate dispatcher at mod.main() not completed

test with

test_a/main.ts

import type { Denops } from "https://deno.land/x/denops_std@v6.0.1/mod.ts";

export async function main(denops: Denops) {
  await new Promise((resolve) => setTimeout(resolve, 100));
  await denops.dispatch("test_b", "hello");
} 

test_a/main.ts

import type { Denops } from "https://deno.land/x/denops_std@v6.0.1/mod.ts";

export async function main(denops: Denops) {
  await new Promise((resolve) => setTimeout(resolve, 200));
  denops.dispatcher = {
    hello() {
      console.log("hello");
    },
  };
  await denops.dispatch("test_b", "hello");
} 

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

Out of diff range and nitpick comments (1)
denops/@denops-private/denops_test.ts (1)

2-2: Ensure consistent use of imports.

Consider organizing imports by grouping them from the same module together for better readability.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 96b1f46 and 50a064d.
Files selected for processing (4)
  • denops/@denops-private/denops.ts (2 hunks)
  • denops/@denops-private/denops_test.ts (3 hunks)
  • denops/@denops-private/service.ts (3 hunks)
  • denops/@denops-private/service_test.ts (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • denops/@denops-private/denops.ts
  • denops/@denops-private/service_test.ts
Additional comments not posted (8)
denops/@denops-private/denops_test.ts (4)

8-8: Good addition of promiseState for asynchronous state checks.


25-25: Correct implementation of waitLoaded as a stub.


99-111: Properly implemented stubbing for waitLoaded and dispatch in the dispatch() test.


116-135: Excellent test to ensure dispatch() waits for waitLoaded() before proceeding.

This test effectively captures the asynchronous behavior introduced in the dispatch() method, ensuring that the waitLoaded method is called and completed before dispatch is executed.

denops/@denops-private/service.ts (4)

14-14: Proper initialization of #waiters to manage asynchronous plugin loading.


22-27: Correct implementation of #getWaiter to ensure a promise is created if not already present.


Line range hint 33-52: Ensure error handling is robust in load method.

This method should robustly handle errors, especially since it deals with loading plugins which can fail for various reasons. The current implementation throws an error if no host is bound, but it should also handle other potential issues like script loading failures.

Verification successful

The verification of the load method in the Service class within the denops/@denops-private/service.ts file has revealed that there are multiple instances where errors are explicitly thrown to handle potential issues:

  1. An error is thrown if no host is bound to the service.
  2. An error is thrown if a requested plugin is not loaded.

These checks ensure that the method handles some critical error scenarios robustly, particularly those related to the absence of necessary components like the host and the plugin. This aligns with the requirement for robust error handling in a method that deals with loading plugins, which can indeed fail for various reasons as noted in the review comment.

  • File: denops/@denops-private/service.ts
    • Error thrown if no host is bound: Ensures that the service cannot operate without a host.
    • Error thrown if a plugin is not loaded: Prevents operations on undefined or uninitiated plugins.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify error handling in the `load` method of the `Service` class.

# Test: Search for error handling patterns in the `load` method. Expect: Proper error handling for plugin loading.
rg --type typescript 'throw new Error' 'denops/@denops-private/service.ts'

Length of output: 109



Script:

#!/bin/bash
# Description: Verify error handling in the `load` method of the `Service` class.

# Test: Search for error handling patterns in the `load` method. Expect: Proper error handling for plugin loading.
rg 'throw new Error' denops/@denops-private/service.ts

Length of output: 228


73-75: Correct implementation of waitLoaded to wait for a plugin to be fully loaded.

Note that wait only happens on `denops.dispatch()` but
`service.dispatch()` so this change only affects plugin-plugin
communication (not denops-plugin communication)
@lambdalisue
Copy link
Member Author

Followed #340 (comment), most of code is re-written from scratch so I rebased & force pushed.

@lambdalisue lambdalisue requested a review from Milly May 11, 2024 02:57
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

Out of diff range and nitpick comments (3)
denops/@denops-private/service.ts (3)

Line range hint 39-58: Consider replacing console.log with a more robust logging mechanism.

Using console.log for debugging is not ideal in production environments. Consider integrating a more robust logging framework that can be configured for different environments.


Line range hint 91-117: Enhance error handling in asynchronous dispatch.

Consider adding more detailed error logging and possibly retries or fallback mechanisms in the dispatchAsync method to handle failures more gracefully.


Line range hint 160-166: Include more error details in the serialized error format.

Consider including more details about the error in the toVimError function to aid in debugging when errors are passed to Vim/Neovim.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 50a064d and 897bda7.
Files selected for processing (4)
  • denops/@denops-private/denops.ts (2 hunks)
  • denops/@denops-private/denops_test.ts (3 hunks)
  • denops/@denops-private/service.ts (3 hunks)
  • denops/@denops-private/service_test.ts (4 hunks)
Files skipped from review as they are similar to previous changes (3)
  • denops/@denops-private/denops.ts
  • denops/@denops-private/denops_test.ts
  • denops/@denops-private/service_test.ts
Additional comments not posted (3)
denops/@denops-private/service.ts (3)

9-13: Define a Waiter type to handle promises with resolvers.

This is a good approach to manage asynchronous operations where you need to manually resolve promises.


28-33: Ensure proper management of asynchronous plugin loading.

The #getWaiter method correctly manages the creation and retrieval of waiters for plugins, which is essential for the new asynchronous loading feature.


79-81: Proper implementation of waiting for plugin loading.

The waitLoaded method correctly returns the promise associated with the waiter for the plugin, ensuring that the caller can await the plugin's loading process.

@lambdalisue lambdalisue added this to the v6.1 milestone May 11, 2024
Copy link
Contributor

@Milly Milly left a comment

Choose a reason for hiding this comment

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

LGTM

@lambdalisue lambdalisue merged commit e0a3bd4 into main May 12, 2024
9 checks passed
@lambdalisue lambdalisue deleted the wait branch May 12, 2024 05:42
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.

4 participants