是否有相当于Observable.Throttle的Streams?如果没有 - 是否有任何合理的优雅方式来实现类似的效果?等同于Observable.Throttle的流?
4
A
回答
2
的rate_limit package提供Streams的节流和消除。
3
现在还没有这样的流式方法。一个增强请求已被提交,您可以登录issue 8492。
但是,您可以使用where方法来做到这一点。在下面为例,我已经定义了一个ThrottleFilter
类忽略定持续时间期间的活动:
import 'dart:async';
class ThrottleFilter<T> {
DateTime lastEventDateTime = null;
final Duration duration;
ThrottleFilter(this.duration);
bool call(T e) {
final now = new DateTime.now();
if (lastEventDateTime == null ||
now.difference(lastEventDateTime) > duration) {
lastEventDateTime = now;
return true;
}
return false;
}
}
main() {
final sc = new StreamController<int>();
final stream = sc.stream;
// filter stream with ThrottleFilter
stream.where(new ThrottleFilter<int>(const Duration(seconds: 10)).call)
.listen(print);
// send ints to stream every second, but ThrottleFilter will give only one int
// every 10 sec.
int i = 0;
new Timer.repeating(const Duration(seconds:1), (t) { sc.add(i++); });
}
1
以下版本更接近于什么Observable.Throttle做:
class Throttle extends StreamEventTransformer {
final duration;
Timer lastTimer;
Throttle(millis) :
duration = new Duration(milliseconds : millis);
void handleData(event, EventSink<int> sink) {
if(lastTimer != null){
lastTimer.cancel();
}
lastTimer = new Timer(duration,() => sink.add(event));
}
}
main(){
//...
stream.transform(new Throttle(500)).listen((_) => print(_));
//..
}
相关问题
- 1. 等同于PosthreadMessage
- 2. “等同”与“等于”(或“绝对等于”)相同吗?
- 3. freopen()等效于C++流
- 4. 等待关于流继续
- 5. Dart流,相当于等待
- 6. 组等同于C#
- 7. Coolite等同于jQuery?
- 8. CompilerServices.Operators等同于C#
- 9. 'gdk_get_default_root_window'等同于Gtk#?
- 10. grails等同于grails.plugin.location
- 11. Mylyn等同于Netbeans?
- 12. DataList等同于WP7?
- 13. ARC等同于autorelease?
- 14. JNA等同于PVOID
- 15. mysqli_info()等同于PDO?
- 16. setTextColor等同于TextInputLayout
- 17. MySQL的IFNULL等同于PHP
- 18. 等同于np.where()的Lua Torch?
- 19. 等同于此Makefile的Windows
- 20. 等同于Office.js中的Office.Application.CompareDocuments
- 21. iPhone的等同于-doCommandBySelector:?
- 22. 等同于OutputDebugString()的OS X?
- 23. 等同于JComponent.setDefaultLocale()的SWT/JFace?
- 24. jQuery.fn.jquery等同于jQuery Mobile?
- 25. C法FREAD()等同于C#
- 26. 现代等同于DPMI
- 27. ActiveModel等同于ActiveRecord has_attribute?
- 28. std :: stringstream等同于u32string?
- 29. Git - 等同于“添加”?
- 30. UrlHelper.Action是否等同于UrlHelper.RouteUrl?