{"success":true,"name":"tame-trigger-happy-frontends-debounce-searches-and-disable-b","description":"A frontend input fires requests per keystroke or a button can be clicked mid-request","category":"ai-brimind","path":"ai-brimind/tame-trigger-happy-frontends-debounce-searches-and-disable-b/SKILL.md","file":"SKILL.md","content":"---\nname: tame-trigger-happy-frontends-debounce-searches-and-disable-b\ndescription: \"A frontend input fires requests per keystroke or a button can be clicked mid-request\"\n---\n\n# Tame Trigger-Happy Frontends: Debounce Searches and Disable Buttons While a Request Flies\n\n## When to use\nYou are wiring a frontend interaction that triggers backend calls — a live search box, a filter, a submit button — and right now every keystroke fires a request, or the button stays clickable while its request is still in flight.\n\n## Example request\nSearch box calls the API on every keystroke → typing 'tennis' fires six requests in one second, five of them for text nobody wanted results for; waiting until the user pauses has a name — debounce — and it is a small, standard utility. Fast typing makes an older, slower response arrive AFTER the newest one → results for 'ten' overwrite results for 'tennis' on screen; stale responses must be ignored (cancel the previous request with AbortController, or track 'latest request wins'). Submit button stays active during the 2-second save → impatient users click three times; disabling the button and showing a pending state while the request flies is the frontend half of the duplicate-write defense — the backend idempotency guard remains the law, this is the courtesy. A filter dropdown with distinct clicks → each click is one intentional request, nothing to debounce; this discipline targets rapid-fire sources like typing, scrolling, resizing.\n\n## Instructions\nAct as a frontend developer who counts requests the way the server feels them — every keystroke, every impatient double-click is real load and a real chance for out-of-order chaos — because the UI's job is to translate messy human input into calm, intentional traffic. For every interaction that triggers backend calls, do this: (1) Classify the trigger: RAPID-FIRE (typing in a search, dragging a slider, window events) or DISCRETE (a deliberate click, a form submit). Rapid-fire sources never get a raw 1-event-1-request wiring. (2) For rapid-fire, apply debounce: wait until the user pauses (~300ms of silence) before firing, so 'tennis' costs one request, not six. This is a solved, named utility — use lodash's debounce, your data-fetching library's built-in delay, or a ten-line hook the project may already have; check before writing a new one, and beware the classic React trap of recreating the debounced function on every render, which silently disables it. (3) Handle the race that debouncing alone does not fix: two searches can still overlap, and the SLOWER older one can land last, painting stale results over fresh ones. Make 'latest wins' explicit — abort the previous request via AbortController when a new one starts, or ignore responses that do not match the current input; if the project uses a data-fetching library (React Query etc.), keying the query by the search term gives you this for free — prefer the mechanism that exists. (4) For discrete submits, close the double-fire door at the UI: on click, disable the button and show a pending state (spinner, 'Saving…') until the request settles, then re-enable on error so retrying stays possible. Say the honest hierarchy out loud: this protects against accidents; the backend idempotency/unique-constraint guard protects against everything — the frontend courtesy never replaces it. (5) Verify like a hostile user: open the network tab, type fast, click fast — count the requests and watch the order of responses. Only call the interaction done when frantic typing costs one request per pause, a double-click costs one write, and a slow stale response can no longer overwrite a fresh one — if the network tab shows a waterfall of cancelled-too-late chaos, the wiring is not finished, it is just usually lucky.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #218\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}