Skip to content

feat(HikVision): improve dispose performance#803

Merged
ArgoZhang merged 3 commits intomasterfrom
fix-hik
Dec 9, 2025
Merged

feat(HikVision): improve dispose performance#803
ArgoZhang merged 3 commits intomasterfrom
fix-hik

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Dec 9, 2025

Link issues

fixes #802

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Improve HikVision video plugin disposal sequence to enhance performance and stability when cleaning up resources.

Enhancements:

  • Adjust HikVision dispose logic to hide the plugin before stopping playback and logging out, then destroy the plugin and finally remove associated data.
  • Update plugin destruction helper to return a resolved promise for better async handling.

Copilot AI review requested due to automatic review settings December 9, 2025 05:07
@bb-auto bb-auto Bot added the enhancement New feature or request label Dec 9, 2025
@bb-auto bb-auto Bot added this to the v9.2.0 milestone Dec 9, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Dec 9, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Optimizes the HikVision component disposal flow by making plugin teardown asynchronous-safe, ensuring the plugin is hidden and destroyed before removing instance data, and tightening up observer cleanup.

Sequence diagram for the updated HikVision dispose process

sequenceDiagram
    actor Caller
    participant HikVisionModule as HikVisionModule
    participant Data as Data
    participant Observer as Observer
    participant WebVideoCtrl as WebVideoCtrl

    Caller->>HikVisionModule: dispose(id)
    HikVisionModule->>Data: get(id)
    Data-->>HikVisionModule: vision
    HikVisionModule->>Observer: disconnect()
    HikVisionModule->>WebVideoCtrl: I_HidPlugin()
    alt realPlaying is true
        HikVisionModule->>HikVisionModule: stopRealPlay(id)
        HikVisionModule->>WebVideoCtrl: I_StopRealPlay(id)
    end
    alt logined is true
        HikVisionModule->>HikVisionModule: logout(id)
        HikVisionModule->>WebVideoCtrl: I_Logout(id)
    end
    HikVisionModule->>WebVideoCtrl: I_DestroyPlugin()
    HikVisionModule->>Data: remove(id)
    HikVisionModule-->>Caller: dispose complete
Loading

Flow diagram for the new HikVision plugin destroy promise

flowchart LR
    A[Start destroyPlugin] --> B[Call JSVideoPlugin.DestroyPlugin]
    B --> C[Set JSVideoPlugin to null]
    C --> D[Delete window.JSVideoPlugin]
    D --> E[Call removePlugin]
    E --> F[Create new Promise]
    F --> G[Resolve Promise]
    G --> H[End destroyPlugin]
Loading

File-Level Changes

Change Details Files
Adjusts the plugin destroy helper to return a resolved Promise to better integrate with async disposal flows.
  • Updates the destroy helper to return a Promise that resolves immediately after cleaning up the global JSVideoPlugin reference and removing the plugin
src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js
Reorders and augments the HikVision dispose logic to hide the plugin early, ensure observer cleanup, stop real-time playback, log out when needed, and only then destroy the plugin and remove the instance data.
  • Stops removing the vision instance from the data store at the start of dispose and instead removes it at the end after teardown is complete
  • Ensures any active MutationObserver is disconnected before other operations
  • Invokes WebVideoCtrl.I_HidPlugin() early in dispose to hide the plugin before stopping playback and logout
  • Keeps existing real-play stop and logout calls, then invokes WebVideoCtrl.I_DestroyPlugin() to fully tear down the plugin before removing the instance entry
src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js

Assessment against linked issues

Issue Objective Addressed Explanation
#802 Improve the performance and behavior of the HikVision component's dispose logic (JavaScript and related plugin teardown).

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ArgoZhang ArgoZhang merged commit 7a3aebd into master Dec 9, 2025
3 of 4 checks passed
@ArgoZhang ArgoZhang deleted the fix-hik branch December 9, 2025 05:07
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • In hackJSDestroyPlugin, consider returning Promise.resolve() instead of manually constructing a new promise, and ensure the function consistently returns a promise even when JSVideoPlugin is falsy so callers can rely on a stable async contract.
  • In dispose(id), Data.get(id) can potentially return undefined, so it would be safer to guard against vision being nullish before destructuring and using its properties to avoid runtime errors.
  • Before calling WebVideoCtrl.I_HidPlugin() and WebVideoCtrl.I_DestroyPlugin() in dispose, it may be worth checking that WebVideoCtrl is defined to prevent errors if the plugin script fails to load or is not initialized.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `hackJSDestroyPlugin`, consider returning `Promise.resolve()` instead of manually constructing a new promise, and ensure the function consistently returns a promise even when `JSVideoPlugin` is falsy so callers can rely on a stable async contract.
- In `dispose(id)`, `Data.get(id)` can potentially return `undefined`, so it would be safer to guard against `vision` being nullish before destructuring and using its properties to avoid runtime errors.
- Before calling `WebVideoCtrl.I_HidPlugin()` and `WebVideoCtrl.I_DestroyPlugin()` in `dispose`, it may be worth checking that `WebVideoCtrl` is defined to prevent errors if the plugin script fails to load or is not initialized.

## Individual Comments

### Comment 1
<location> `src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js:90-91` </location>
<code_context>
         delete window.JSVideoPlugin;
         removePlugin();
+
+        return new Promise((resolve, reject) => {
+            resolve();
+        });
     }
</code_context>

<issue_to_address>
**suggestion:** Avoid redundant Promise construction and inconsistent return type from `hackJSDestroyPlugin`.

Because this only returns a `Promise` when `JSVideoPlugin` exists and `undefined` otherwise, callers get an inconsistent return type. The manual `new Promise` is also unnecessary since it resolves synchronously. Prefer returning `Promise.resolve()` (or `Promise.resolve(undefined)`) in all paths so the function always returns a `Promise` that can be safely `await`ed.

Suggested implementation:

```javascript
        JSVideoPlugin = null;
        delete window.JSVideoPlugin;
        removePlugin();

        return Promise.resolve();
    }
}

```

To fully implement your suggestion, you should also:
1. Locate the full `hackJSDestroyPlugin` function in this file.
2. Ensure that all code paths within `hackJSDestroyPlugin` return a `Promise`, e.g. `return Promise.resolve();` or `return Promise.resolve(undefined);` when the plugin does not exist or when early-returning for other reasons.
3. If the function is sometimes called without awaiting previously, verify that callers still behave correctly given it now always returns a `Promise`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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 improves the dispose performance for the HikVision component by adjusting the cleanup sequence and adding plugin hiding functionality. The changes address issue #802 by optimizing resource cleanup order and ensuring the plugin is properly hidden during disposal.

Key Changes

  • Added Promise return to hackJSDestroyPlugin for consistency with async operation patterns
  • Reordered cleanup operations in dispose() function, moving Data.remove(id) to the end
  • Added WebVideoCtrl.I_HidPlugin() call early in the dispose process

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js Modified dispose flow to hide plugin earlier and defer data removal; added Promise return to hackJSDestroyPlugin
src/components/BootstrapBlazor.HikVision/BootstrapBlazor.HikVision.csproj Version bump from 10.0.2 to 10.0.3
Comments suppressed due to low confidence (3)

src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js:1

  • Unused import registerBootstrapBlazorModule.
import { addScript, registerBootstrapBlazorModule } from '../BootstrapBlazor/modules/utility.js';

src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js:76

  • Unused variable originalDestroy.
    const originalDestroy = JSVideoPlugin.prototype.JS_DestroyPlugin;

src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js:190

  • Unused variable logined.
    const { szDeviceIdentify, logined } = vision;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 358 to 363
if (realPlaying === true) {
stopRealPlay(id);
}
if (logined === true) {
logout(id);
}
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

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

The dispose function calls async functions stopRealPlay(id) and logout(id) without awaiting them. This creates a race condition where WebVideoCtrl.I_DestroyPlugin() and Data.remove(id) may execute before the stop/logout operations complete, potentially causing resource leaks or errors.

Consider making dispose async and awaiting these operations:

export async function dispose(id) {
    const vision = Data.get(id);
    const { realPlaying, logined, observer } = vision;
    if (observer) {
        observer.disconnect();
    }
    WebVideoCtrl.I_HidPlugin();

    if (realPlaying === true) {
        await stopRealPlay(id);
    }
    if (logined === true) {
        await logout(id);
    }
    WebVideoCtrl.I_DestroyPlugin();

    Data.remove(id);
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(HikVision): improve dispose performance

2 participants