Skip to content

fix: setSource() doesn't invalidate rendered results (v4.0.3) - #10

Merged
michaeljfalk merged 1 commit into
mainfrom
fix/setsource-stale-results
Jun 17, 2026
Merged

michaeljfalk merged 1 commit into
mainfrom
fix/setsource-stale-results

Conversation

@michaeljfalk

Copy link
Copy Markdown
Owner

Summary

Fixes a bug where setSource() on a closed control left the previous source's options rendered, so the next open showed stale data.

Root cause: setSource() updated opts.source and cleared _cache, but never cleared this.results. The focus path only re-runs the search when results are empty (if (… && !self.results.length)), so with stale results present the re-search was skipped. setScope() already cleared this.results — the two source-mutating methods were inconsistent.

Fix: setSource() now clears results / _total / activeIndex (matching setScope()) before re-searching if open. The current selection/value is left untouched — only the available options change.

Real-world impact: dependent dropdowns (pick Country → setSource(provincesForCountry) on a Region select) now refresh correctly. The setScope({}) workaround consumers used is no longer needed.

Changes

  • dist/liveselect.js — setSource() invalidates rendered state
  • test/client.test.js — 2 regression tests (stale-options-on-reopen via the focus path; selected value survives a swap) → 47/47 pass
  • CHANGELOG.md + package.json — 4.0.3

Test plan

  • npm test → 47/47 (was 45)

Thanks to the reporter for the precise root-cause analysis and suggested fix.

🤖 Generated with Claude Code

Swapping the source while the control was closed left the old source's options
in this.results. _onFocus only re-runs the search when results are empty
(`!self.results.length`), so the next open showed stale options — e.g. a
dependent Country → Region dropdown never refreshed after the country changed.

setSource() now clears results/_total/activeIndex (matching setScope, which
already did), so the next open reflects the new source. The current
selection/value is left untouched. Removes the need for the setScope({}) workaround.

Adds 2 regression tests (47 total): stale-options-on-reopen, and value survival
across a swap.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@michaeljfalk
michaeljfalk merged commit 31dd513 into main Jun 17, 2026
@michaeljfalk
michaeljfalk deleted the fix/setsource-stale-results branch June 17, 2026 22:48
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.

1 participant