Skip to content

fix: resolve 4 bugs in termui - #3540

Closed
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-23396
Closed

fix: resolve 4 bugs in termui#3540
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-23396

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • Simplified empty-string validation: comparing trim() to '' misses whitespace-only input; .trim().length === 0 is explicit.
  • Prevented interval leak: repeated mounts now clear the previous interval before scheduling a new one.
  • Removed redundant boolean comparison: x === true is equivalent to x (and x === false to !x), and shorter to read.
  • Replaced global isNaN with Number.isNaN: the global version coerces its argument, so isNaN('1') returns false while Number.isNaN is strict.

Type of Change

  • Bug fix (non-breaking change fixing an issue)

How Has This Been Tested?

  • Local manual testing

Checklist

  • My code follows the style guidelines
  • I have performed a self-review

Related Issue

Ref: #3539

Summary by CodeRabbit

  • Bug Fixes
    • Prevented duplicate streaming timers to ensure text continues updating reliably.
    • Improved blank-line handling when parsing chat content.
    • Made prompt selection validation more precise for invalid input.

@github-actions github-actions Bot added type:bug +10 pts. Bug fix. area:examples Example apps. area:ui @termuijs/ui labels Aug 5, 2026
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The changes update three validation and timer checks: streaming interval cleanup, whitespace-only line detection, and parsed-number validation.

Changes

Streaming timer cleanup

Layer / File(s) Summary
Reset the streaming interval
examples/ai-streaming/src/index.tsx
The constructor clears the stored interval before creating a new 50 ms streaming timer.

Chat block parsing

Layer / File(s) Summary
Detect blank lines by trimmed length
examples/chat-app/src/index.tsx
parseBlocks checks line.trim().length === 0 for blank and whitespace-only lines.

Prompt selection validation

Layer / File(s) Summary
Validate parsed selection numbers
packages/ui/src/prompts.ts
promptSelect uses Number.isNaN to reject invalid parsed numbers. Range handling remains unchanged.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: karanjot786

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the four bug fixes and follows the required type: short description format.
Description check ✅ Passed The description explains the changes, identifies the bug-fix type, records manual testing, and references related issue #3539.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@examples/ai-streaming/src/index.tsx`:
- Around line 43-45: Update AIStreamingApp’s streaming timer setup to store the
interval handle in a private instance field instead of the shared
window.__interval, so each instance owns its timer. Extend cleanup() to clear
that interval and release the handle, preventing tick() after unmount and
allowing multiple instances to run independently.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a09ad106-c4fa-4497-983b-04694a07795b

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7584e and 83c9eed.

📒 Files selected for processing (3)
  • examples/ai-streaming/src/index.tsx
  • examples/chat-app/src/index.tsx
  • packages/ui/src/prompts.ts

Comment on lines +43 to 45
clearInterval(window.__interval); window.__interval = setInterval(() => {
this._streamingText.tick();
}, 50);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Give the streaming interval instance ownership and teardown.

window.__interval is shared by all AIStreamingApp instances. Constructing a second instance clears the first instance's timer. The class also does not clear the interval when the widget is unmounted, so the callback can continue calling tick() on a detached widget and retain the instance.

Store the handle in a private field and clear it from cleanup(), as shown by examples/widget-gallery/src/tabs/ai-tab.ts’s cleanup() implementation.

Proposed fix
+    private _streamInterval: ReturnType<typeof setInterval>;
+
     constructor() {
...
-        clearInterval(window.__interval); window.__interval = setInterval(() => {
+        this._streamInterval = setInterval(() => {
             this._streamingText.tick();
         }, 50);
     }
+
+    cleanup(): void {
+        clearInterval(this._streamInterval);
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
clearInterval(window.__interval); window.__interval = setInterval(() => {
this._streamingText.tick();
}, 50);
private _streamInterval: ReturnType<typeof setInterval>;
constructor() {
this._streamInterval = setInterval(() => {
this._streamingText.tick();
}, 50);
}
cleanup(): void {
clearInterval(this._streamInterval);
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/ai-streaming/src/index.tsx` around lines 43 - 45, Update
AIStreamingApp’s streaming timer setup to store the interval handle in a private
instance field instead of the shared window.__interval, so each instance owns
its timer. Extend cleanup() to clear that interval and release the handle,
preventing tick() after unmount and allowing multiple instances to run
independently.

@saurabhhhcodes saurabhhhcodes mentioned this pull request Aug 5, 2026
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Aug 5, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:examples Example apps. area:ui @termuijs/ui type:bug +10 pts. Bug fix.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant