Skip to content

Add reference to open windows in Butil (#10043)#10045

Merged
msynk merged 2 commits intobitfoundation:developfrom
msynk:10043-butil-window-open-reference
Feb 25, 2025
Merged

Add reference to open windows in Butil (#10043)#10045
msynk merged 2 commits intobitfoundation:developfrom
msynk:10043-butil-window-open-reference

Conversation

@msynk
Copy link
Member

@msynk msynk commented Feb 25, 2025

closes #10043

Summary by CodeRabbit

  • New Features

    • Introduced new options to adjust operation performance, allowing users to toggle between optimized invocation modes.
    • Enhanced window management by enabling unique identifiers for opened windows and allowing targeted window closures.
    • Added asynchronous resource cleanup and updated the UI with a button to close the last opened window.
  • Refactor

    • Streamlined the tracking and disposal of window instances for improved resource management.

@msynk msynk requested a review from yasmoradi February 25, 2025 20:47
@coderabbitai
Copy link

coderabbitai bot commented Feb 25, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The pull request enhances several components. XML documentation comments are added to two public methods in the BitButil class. The Window class is updated to implement asynchronous disposal and its Open and Close methods have modified signatures, including passing unique window identifiers. The TypeScript script for window management now tracks opened windows using an internal reference map and adds functions for closing and disposing windows. Finally, the demo page is updated with a new close button and a mechanism to maintain and remove window identifiers.

Changes

File(s) Change Summary
src/Butil/Bit.Butil/BitButil.cs Added XML documentation for public static methods UseFastInvoke and UseNormalInvoke.
src/Butil/Bit.Butil/Publics/Window.cs Modified class to implement IAsyncDisposable; updated Close to accept an optional id and Open to return a GUID (Task<string>); added async disposal methods.
src/Butil/Bit.Butil/Scripts/window.ts Introduced a private _refs to track windows; updated open to require an id; modified close to accept an optional id; added a dispose function to clear references.
src/Butil/Demo/.../Pages/WindowPage.razor Added a close button and a CloseWindow method; introduced a list to track window IDs; modified OpenWindow to store the new GUID and update window target.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant WP as WindowPage
    participant W as Window (C#)
    participant JS as window.ts

    U->>WP: Click "OpenWindow" button
    WP->>W: Invoke Open(url, target, features)
    W->>JS: Call open(guid, url, target, features)
    JS-->>W: Return window reference
    W-->>WP: Return generated GUID
    WP->>WP: Append GUID to windowIds

    U->>WP: Click "CloseWindow" button
    WP->>W: Invoke Close(lastGUID)
    W->>JS: Call close(id)
    JS-->>W: Confirm window closure
    W-->>WP: (operation complete)
    WP->>WP: Remove GUID from windowIds
Loading

Poem

I hopped through code, my whiskers a-twitch,
Adding docs and async flair with a switch.
Windows now open with a GUID in sight,
And closing them is simply a delight.
With TypeScript and Razor, our code is so neat—
A rabbit celebrates changes with a happy beat! 🐇


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

@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 (4)
src/Butil/Demo/Bit.Butil.Demo.Core/Pages/WindowPage.razor (1)

367-372: Consider minimal error handling during window closure.
If a browser blocked the window or if it was already closed externally, window.Close(id) might fail silently. You could add a try/catch or console logging to inform the user when closure cannot be fulfilled.

src/Butil/Bit.Butil/Scripts/window.ts (3)

4-5: Use of a global _refs object to store window references.
Storing references in _refs works, but consider adding TypeScript type annotations or other safeguards (e.g., ensuring unique IDs), to prevent collisions or potential misuse.


53-60: Close function logic is clear and safe.
It checks for an undefined ID and closes the current window if none is provided, or a specific reference otherwise. Optionally, logging a warning when an unknown ID is supplied could improve diagnosability.


79-82: Handle possible null returns from window.open.
If a popup blocker prevents opening a new window, window.open can return null. Consider detecting this scenario and gracefully informing the caller.

 function open(id: string, url?: string, target?: string, windowFeatures?: string) {
   const ref = window.open(url, target, windowFeatures);
+  if (!ref) {
+    console.warn("Window blocked or failed to open.");
+    return null;
+  }
   _refs[id] = ref;
   return id;
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 59db305 and 7ac91d2.

📒 Files selected for processing (4)
  • src/Butil/Bit.Butil/BitButil.cs (1 hunks)
  • src/Butil/Bit.Butil/Publics/Window.cs (4 hunks)
  • src/Butil/Bit.Butil/Scripts/window.ts (6 hunks)
  • src/Butil/Demo/Bit.Butil.Demo.Core/Pages/WindowPage.razor (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • src/Butil/Bit.Butil/BitButil.cs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build and test
🔇 Additional comments (11)
src/Butil/Demo/Bit.Butil.Demo.Core/Pages/WindowPage.razor (4)

11-19: No functional impact in these snippet lines.
They appear to display example code within the <pre> block. No further action needed.


100-100: The new "CloseWindow" button is a helpful addition.
It neatly complements the "OpenWindow" functionality by allowing users to close opened windows from the UI.


360-360: Good approach to keep track of opened window IDs.
Storing them in a List<string> is straightforward and aligns well with the updated API in the Window class.


364-365: Storing the opened window’s ID for subsequent closure.
This use of await window.Open(...) and storing the resulting GUID in windowIds correctly matches the new JavaScript implementation.

src/Butil/Bit.Butil/Scripts/window.ts (2)

26-26: Newly exposed methods in the butil.window object.
Exporting close, stop, and dispose broadens window management. Confirm these additions are documented so consumers are aware of the new functionality.

Also applies to: 37-38


101-103: Dispose function effectively resets stored references.
Clearing _refs ensures no lingering references after disposal. This aligns with the .NET side’s DisposeAsync.

src/Butil/Bit.Butil/Publics/Window.cs (5)

13-13: Adoption of IAsyncDisposable is well-structured.
Implementing asynchronous disposal indicates a well-considered approach to resource cleanup on the JS side.


182-185: Enhanced Close method with an optional ID.
Consistently mirrors the JavaScript close change. This design is easy to call for general or specific window closure.


236-237: Open method returning a string ID.
Switching from Task<bool> to Task<string> is a solid improvement that aligns with referencing windows by a unique identifier.


243-244: Overload for Open with WindowFeatures is consistent.
Clarifies usage by providing a typed parameter for window features, further enhancing correctness.


300-313: DisposeAsync implementation is coherent with JavaScript cleanup.
Invoking BitButil.window.dispose from the .NET side effectively releases references. Consider logging or try/catch for failures, but otherwise this is good.

@msynk msynk merged commit a497979 into bitfoundation:develop Feb 25, 2025
3 checks passed
@msynk msynk deleted the 10043-butil-window-open-reference branch February 25, 2025 21:10
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.

The Window API of the Butil needs some improvements

1 participant