Skip to content

fix(rtstruct): prevent viewport from becoming blank on second load#5996

Open
Belbin-GK wants to merge 12 commits into
OHIF:masterfrom
Belbin-GK:rtstruct-viewport-blank-on-second-load
Open

fix(rtstruct): prevent viewport from becoming blank on second load#5996
Belbin-GK wants to merge 12 commits into
OHIF:masterfrom
Belbin-GK:rtstruct-viewport-blank-on-second-load

Conversation

@Belbin-GK

@Belbin-GK Belbin-GK commented May 6, 2026

Copy link
Copy Markdown
Contributor

Context

Hydration was being triggered before the viewport was fully initialized, which could lead to rendering issues such as a blank viewport, especially when loading RTSTRUCT multiple times.

Changes & Results

  • Removed the use of setTimeout for delaying hydration
  • Introduced a mechanism to wait for actual viewport readiness
5996-rtstruct-viewport-blank-on-second-load.mp4

Testing

  • Load a study with RT Struct.
  • Drag and drop the RT Struct.
  • Close the RT struct from the segmentation panel.
  • Drag and drop the RT Struct again.
  • Observe that RT struct is not loaded in the viewport and the segmentation panel.

Checklist

PR

  • My Pull Request title is descriptive, accurate and follows the
    semantic-release format and guidelines.

Code

  • My code has been well-documented (function documentation, inline comments,
    etc.)

Public Documentation Updates

  • The documentation page has been updated as necessary for any public API
    additions or removals.

Tested Environment

  • OS: Windows 11
  • Node version: 22.20
  • Browser: Chrome 147.0.7727.138

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of SEG and RTSTRUCT hydration by waiting for the matching viewport to become ready before triggering hydration, using a 5-second timeout safeguard. If readiness fails or hydration throws/rejects, the operation now rejects instead of continuing.
  • Tests

    • Updated the hydration test to align with the new viewport-ready async behavior, removing reliance on immediate deferral while still validating the expected SEG hydration payload.

Greptile Summary

This PR fixes blank viewport regressions when loading RTSTRUCT (and SEG) a second time by replacing the setTimeout(0) deferral with a proper readiness check: if getCornerstoneViewport returns null, hydration now waits for the VIEWPORT_DATA_CHANGED event (with a 5-second timeout) before invoking hydrateCallback.

  • The waitForViewportDataChange helper subscribes to VIEWPORT_DATA_CHANGED and properly rejects on timeout; the outer async executor wraps the await in a try/catch so timeout rejections correctly propagate to callers.
  • The VIEWPORT_DATA_CHANGED payload shape { viewportData, viewportId } is consistent with how other subscribers in the codebase, confirming the viewport-ID comparison is correct.
  • The test suite does not include cornerstoneViewportService in its mock, so the new waiting branch is never exercised; the vestigial window.setTimeout spy in beforeEach is also left over from the old implementation.

Confidence Score: 5/5

The runtime fix is correct and the event payload shape is consistent with existing usage; the only gaps are missing test coverage for the new branch.

The core logic is sound: the event used to detect viewport readiness is the same one used throughout the codebase, the timeout/reject wiring is correct, and no prior-thread issues remain unaddressed. The test file does not cover the new code path, but this does not introduce a runtime defect.

extensions/cornerstone/src/utils/promptHydrationDialog.test.ts — the mock services manager should be extended to exercise the new waitForViewportDataChange branch.

Important Files Changed

Filename Overview
extensions/cornerstone/src/utils/promptHydrationDialog.ts Replaces setTimeout(0) for SEG/RTSTRUCT hydration with a VIEWPORT_DATA_CHANGED subscription that waits for the viewport to be ready; timeout and reject handling are correctly wired. Event payload shape ({ viewportId }) confirmed consistent with other subscribers in the codebase.
extensions/cornerstone/src/utils/promptHydrationDialog.test.ts Removes the setTimeout assertion, but the new waitForViewportDataChange branch is never exercised because cornerstoneViewportService is absent from mockServicesManager; vestigial window.setTimeout spy remains.

Reviews (13): Last reviewed commit: "Merge branch 'master' into rtstruct-view..." | Re-trigger Greptile

@netlify

netlify Bot commented May 6, 2026

Copy link
Copy Markdown

Deploy Preview for ohif-dev failed. Why did it fail? →

Name Link
🔨 Latest commit 74ff9d8
🔍 Latest deploy log https://app.netlify.com/projects/ohif-dev/deploys/6a3c2e6a2afb700008ef01e1

@Belbin-GK Belbin-GK marked this pull request as ready for review May 6, 2026 09:21
Comment thread extensions/cornerstone/src/utils/promptHydrationDialog.ts Outdated
Comment thread extensions/cornerstone/src/utils/promptHydrationDialog.ts Outdated
Comment thread extensions/cornerstone/src/utils/promptHydrationDialog.ts Outdated
@Belbin-GK

Copy link
Copy Markdown
Contributor Author

@wayfarer3130 Could you please review the changes?

Comment thread extensions/cornerstone/src/utils/promptHydrationDialog.ts Outdated
@Belbin-GK Belbin-GK force-pushed the rtstruct-viewport-blank-on-second-load branch 2 times, most recently from c9aa6a8 to 3f6ae2a Compare May 26, 2026 07:57
@sen-trenser

Copy link
Copy Markdown

@wayfarer3130 We have updated the review comments. Could you please take a look at the changes?

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

promptHydrationDialog now waits for Cornerstone viewport readiness before hydrating SEG and RTSTRUCT display sets. The SEG test no longer checks window.setTimeout and still verifies the hydration callback payload.

Changes

Viewport-driven hydration for SEG/RTSTRUCT

Layer / File(s) Summary
Helper docs and service wiring
extensions/cornerstone/src/utils/promptHydrationDialog.ts
Inline documentation is added for the callback and helper functions, and cornerstoneViewportService is destructured from servicesManager.services.
Async viewport-driven hydration flow
extensions/cornerstone/src/utils/promptHydrationDialog.ts
SEG and RTSTRUCT hydration now waits for VIEWPORT_DATA_CHANGED on the target viewportId, with a 5-second timeout, before invoking hydrateCallback.
Test update for async flow
extensions/cornerstone/src/utils/promptHydrationDialog.test.ts
The window.setTimeout assertion is removed from the SEG hydration test, leaving the callback payload assertion intact.

🎯 2 (Simple) | ⏱️ ~12 minutes

🐰 I hop through viewports, wait and see,
No timeout jingle for SEG or RT.
When data changes, I bounce along,
And hydration sings its async song.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, specific, and accurately reflects the main RTSTRUCT viewport fix.
Description check ✅ Passed The description matches the template with context, changes, testing steps, and completed checklist items.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

@Devu-trenser Devu-trenser force-pushed the rtstruct-viewport-blank-on-second-load branch from 531909a to 890f7c4 Compare June 24, 2026 09:14
@Devu-trenser

Copy link
Copy Markdown
Contributor

The Netlify builds for this PR are currently failing with the following error:

sh: 1: cross-env: Permission denied

Based on the build logs, the failure occurs before the application build starts and appears to be related to the CI/build environment rather than the changes introduced in this PR.

@sedghi , could you please take a look at this when you get a chance?

cc: @sen-trenser

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.

5 participants