限制流
假设你需要实现自动搜索框,但搜索操作有点昂贵,例如发送 Web 请求或命中数据库。你可能希望限制正在执行的搜索量。
例如,用户在搜索框中键入“C#Reactive Extensions”:
IObservable<string> TypingSearchText()
{
return Observable.Create<string>(o =>
{
const string SearchText = "C# Reactive Extensions";
var builder = new StringBuilder();
foreach (var c in SearchText)
{
builder.Append(c);
// notify that the search text has been changed
o.OnNext(builder.ToString());
// pause between each character to simulate actual typing
Thread.Sleep(125);
// spent some time to think about the next word to type
if (c == ' ')
Thread.Sleep(1000);
}
o.OnCompleted();
return () => { /* nothing to dispose here */ };
});
}
现在,我们不希望每次用户按下键时都执行搜索。相反,只要用户停止输入的时间超过半秒,它就会完成:
TypingSearchText()
// print the changes
.Do(x => Console.WriteLine("Typing: " + x))
// ignore changes that happens within 500ms of each other
.Throttle(TimeSpan.FromMilliseconds(500))
// some costly operation
.Subscribe(x => Console.WriteLine("Searching: " + x));
输出:
Typing: C
Typing: C#
Typing: C#
Searching: C#
Typing: C# R
Typing: C# Re
...
Typing: C# Reactive
Typing: C# Reactive
Searching: C# Reactive
Typing: C# Reactive E
Typing: C# Reactive Ex
...
Typing: C# Reactive Extension
Typing: C# Reactive Extensions
Searching: C# Reactive Extensions