The Real Fullstack

The Stale Search Results

The user typed “kafka”. The page shows results for “kaf”. Nobody typed “kaf” last.

工程师8 分钟时间线模拟器代码挑战
angularrxjstypescriptreacthttp

此挑战尚未翻译为简体中文,你正在阅读英文版本。

任务:Reproduce the race between overlapping requests, then pick a strategy that always shows results for the latest query, quickly, without hammering the API.背景 · 学习目标 ▾

Larkspur's event search sends a request on every keystroke. The backend is usually fast, but some short queries are expensive and slow. Users report that results sometimes “jump back” to an older search after a moment.

任务: Reproduce the race between overlapping requests, then pick a strategy that always shows results for the latest query, quickly, without hammering the API.

学习目标

  • Explain why responses can arrive out of order.
  • Compare mergeMap, concatMap, exhaustMap and switchMap.
  • Cancel stale requests and debounce input.
模拟模拟数据

运行模拟以查看输出。

运行模拟
readonly results = toSignal(  this.query.valueChanges.pipe(    mergeMap((q) => this.api.search(q)),   // every response is rendered, in arrival order  ), { initialValue: [] });

简化说明

Four keystrokes with fixed latencies. Real latencies vary, which makes the race intermittent and hard to reproduce. 教学模型——数字用于说明行为,并非基准测试。

概念

建议先完成

相关挑战

参考资料