Files
ed69cbd8fb Unify the TomSelect repaint workaround in one place (#9059)
Follow-up to #9058, answering your question there: yes, other TomSelects
suffer the same fate.

### The underlying behaviour

`clearOptions()` and `addOption()` only mark a tom-select drop-down's
rendered content stale. Nothing repaints it. `open()` does not, and only
the focus and input handlers call `refreshOptions()`. So a drop-down
that is open, **or that is reopened without first losing focus**, keeps
showing the previous option list.

### It had already been solved twice

| Pane | Selector | Rebuilt from | Repaint before this PR |
|---|---|---|---|
| `gccdump-view` | pass list | compile result | yes |
| `diff` | difftype | compile result | yes, as of #9058 |
| `cfg-view` | function selector | `onCompileResult` | **no** |
| `opt-pipeline` | group selector | `updateResults` | **no** |

`gccdump-view` even carried a comment describing the exact mechanism. It
just never made it to the other panes, so #9058 was the third
independent rediscovery.

This moves it into `static/tom-select-utils.ts` as `replaceOptions()`
and routes all four sites through it, so the next pane that rebuilds a
picker gets the repaint for free instead of finding out the hard way.

### This also corrects #9058

That PR guarded the refresh on `picker.isOpen`. **That was wrong.** A
drop-down can be closed while the control still has focus, and reopening
it in that state does not repaint, so the stale list survives.
`gccdump-view` had it right by refreshing unconditionally, and the
shared helper now does the same.

### Two subtleties worth checking in review

- **`gccdump-view` keeps a trailing `refreshOptions()`.** Its
`addItem()` after the rebuild goes through `refreshItems()`, which
re-marks the content stale, so the repaint done inside
`replaceOptions()` no longer covers that path. Verified in the
tom-select source rather than assumed.
- **`cfg-view` cleared its options before deciding whether it had a cfg
at all.** Folding the clear into `replaceOptions()` would have left the
previous functions listed when a compile yields no cfg (a blank input
file, for instance), so the no-cfg path now empties the picker
explicitly.

### Verification

`ts-check`, biome and the frontend unit tests pass. As with #9058 the
user-visible symptom is a race, so I can't demonstrate it
deterministically; what I've checked is the mechanism in tom-select's
own source, in particular that `refreshOptions(false)` gates both its
open and close branches on that argument and so leaves the drop-down's
state alone.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: mattgodbolt-molty <mattgodbolt-molty@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 07:43:00 -06:00

51 lines
2.7 KiB
TypeScript

// Copyright (c) 2026, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import TomSelect from 'tom-select';
import type {TomClearFilter, TomOption} from 'tom-select/dist/esm/types/core.js';
/**
* Replace a picker's entire option set and repaint the drop-down.
*
* `clearOptions()` and `addOption()` only mark the rendered content stale; neither they nor
* a later `open()` repaint it. Only tom-select's focus and input handlers call
* `refreshOptions()`, so a drop-down that is open, or that is reopened without first losing
* focus, keeps showing the previous option list until something forces a re-render.
*
* `refreshOptions(false)` is what forces it: tom-select gates both its open and its close
* branch on that argument, so passing `false` re-renders the list while leaving the
* drop-down's open/closed state alone.
*
* @param picker the tom-select instance to update
* @param options the new option set
* @param clearFilter passed through to `clearOptions()`. Return true to keep an option.
* Pass `() => false` to drop every option, including those belonging to the current
* selection, which tom-select's default filter would otherwise keep.
*/
export function replaceOptions(picker: TomSelect, options: TomOption[], clearFilter?: TomClearFilter): void {
picker.clearOptions(clearFilter);
picker.addOptions(options);
picker.refreshOptions(false);
}