【发布时间】:2018-10-26 02:41:28
【问题描述】:
我有一个类似于this one 的问题。我需要通过以下方式处理一系列用户输入事件(搜索):
- 用 N 毫秒限制每个搜索短语
- 如果搜索短语发生更改,则取消之前运行的搜索(并且即将运行新搜索)
- 仅应用最新搜索
除了取消之外,我的代码似乎几乎以这种方式工作。
Observable.FromEventPattern<TextChangedEventArgs>(
handler => SearchBox.TextChanged += handler,
handler => SearchBox.TextChanged -= handler)
.ObserveOn(SynchronizationContext.Current)
.Select(GetSearchQuery)
.Throttle(TimeSpan.FromMilliseconds(MinimumSearchIntervalMiliseconds))
.DistinctUntilChanged()
.Subscribe(ExecuteSearch, () => { });
string GetSearchQuery(EventPattern<TextChangedEventArgs>) 返回搜索字符串,void ExecuteSearch(string) 运行搜索。
由于某种原因,我在 SO 的所有答案中都找不到 Switch() 扩展名...
我在版本4.0.0中使用System.Reactive和System.Reactive.Linq
我猜这种形式的Select() 和Subscribe() 并不是上面代码中的最佳解决方案。他们可能应该在Tasks 上操作...
知道如何改进上述管道以支持根据需要取消吗?
【问题讨论】:
标签: task-parallel-library system.reactive rx.net