首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节流Ember数据请求

节流Ember数据请求
EN

Stack Overflow用户
提问于 2018-03-08 21:04:25
回答 2查看 305关注 0票数 0

我使用Ember数据与具有速率限制的REST交互。

对于如何将Ember数据请求节流为每秒X请求,有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-18 14:30:32

首先,如果您想处理所有AJAX请求,我强烈建议使用ember-ajax。这为您提供了一个点来修改所有AJAX请求。下一个是关于节流。我在评论中建议使用ember-concurrency,所以让我详细说明一下。ember-concurrency为您提供了一个timeout方法,该方法返回在一定时间后将解决的承诺。这样可以方便地进行节流:

代码语言:javascript
复制
export default AjaxService.extend({
  ajaxThrottle: task(function * (cb) {
    cb();
    // maxConcurrency with timeout 1000 means at maximum 3 requests per second.
    yield timeout(1000);
  }).maxConcurrency(3).enqueue(),
  async request() {
    await new Promise(cb => ajaxThrottle.perform(cb));
    return this._super(...arguments); // this should make the actual request
  },
});
票数 1
EN

Stack Overflow用户

发布于 2018-03-13 05:25:30

经过一些努力,我设法让承诺节流阀在我的ApplicationController中工作。限制为每秒4个请求:

代码语言:javascript
复制
import PromiseThrottle from 'dispatcher/application/promise-throttle';
import { Promise } from 'rsvp';

export default DS.RESTAdapter.extend(DataAdapterMixin, {
  init() {
    this.set('promiseThrottle', new PromiseThrottle({
      requestsPerSecond: 4,
      promiseImplementation: Promise
    }));
  },
  ajax: function (url, type, options) {
    return this.get('promiseThrottle').add(this._super.bind(this, url, type, options));
  }
  ...
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49182415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档