节流/去抖每秒调用次数
Throttle / Debounce number of calls per second
我正在使用一个 API,它只允许您使用像 request-promise
或 axios
这样的承诺请求库每秒进行 200 次调用(1000 毫秒)您如何去抖动/ 使用 rx.js 限制对 URL / 服务器的请求?我注意到 rx 文档中有一个 throttle method,但它不会计算每秒的调用次数。
这是一个包装 promise 并将它们排队以考虑 API 速率限制的函数。我正在寻找与 Rx 类似的功能。
var Promise = require("bluebird")
//
//
module.exports = promiseDebounce
function promiseDebounce(fn, delay, count) {
var working = 0, queue = [];
function work() {
if ((queue.length === 0) || (working === count)) return;
working++;
Promise.delay(delay).tap(function () { working--; }).then(work);
var next = queue.shift();
next[2](fn.apply(next[0], next[1]));
}
return function debounced() {
var args = arguments;
return new Promise(function(resolve){
queue.push([this, args, resolve]);
if (working < count) work();
}.bind(this));
}
}
所以前几天我遇到了类似的问题,限制对资源的访问。我遇到了这个 repo bahmutov/node-rx-cycle. Here is the example in a Plunker Demo 我把它放在一起来证明这一点。它从文本输入字段获取输入并将其添加到 <ul>
之前。每个 <li>
仅每 1000 毫秒添加一次,其他的都在排队。
// Impure
const textInput = document.querySelector('.example-input');
const prependToOutput = function(item) {
const ul = document.querySelector('.example-output');
const li = document.createElement('li');
li.appendChild(document.createTextNode(item));
ul.insertBefore(li, ul.firstChild);
};
// Pure
const eventTargetValue = function(ele) { return ele.target.value; };
const textInputKeyUpStream = Rx.Observable
.fromEvent(textInput, 'keyup')
.map(eventTargetValue);
// Stream
rateLimit(textInputKeyUpStream, 1000)
.timeInterval()
.map(function(ti) { return ti.value + ' (' + ti.interval + 'ms)'; })
.subscribe(prependToOutput);
希望对您有所帮助。
我正在使用一个 API,它只允许您使用像 request-promise
或 axios
这样的承诺请求库每秒进行 200 次调用(1000 毫秒)您如何去抖动/ 使用 rx.js 限制对 URL / 服务器的请求?我注意到 rx 文档中有一个 throttle method,但它不会计算每秒的调用次数。
这是一个包装 promise 并将它们排队以考虑 API 速率限制的函数。我正在寻找与 Rx 类似的功能。
var Promise = require("bluebird")
//
//
module.exports = promiseDebounce
function promiseDebounce(fn, delay, count) {
var working = 0, queue = [];
function work() {
if ((queue.length === 0) || (working === count)) return;
working++;
Promise.delay(delay).tap(function () { working--; }).then(work);
var next = queue.shift();
next[2](fn.apply(next[0], next[1]));
}
return function debounced() {
var args = arguments;
return new Promise(function(resolve){
queue.push([this, args, resolve]);
if (working < count) work();
}.bind(this));
}
}
所以前几天我遇到了类似的问题,限制对资源的访问。我遇到了这个 repo bahmutov/node-rx-cycle. Here is the example in a Plunker Demo 我把它放在一起来证明这一点。它从文本输入字段获取输入并将其添加到 <ul>
之前。每个 <li>
仅每 1000 毫秒添加一次,其他的都在排队。
// Impure
const textInput = document.querySelector('.example-input');
const prependToOutput = function(item) {
const ul = document.querySelector('.example-output');
const li = document.createElement('li');
li.appendChild(document.createTextNode(item));
ul.insertBefore(li, ul.firstChild);
};
// Pure
const eventTargetValue = function(ele) { return ele.target.value; };
const textInputKeyUpStream = Rx.Observable
.fromEvent(textInput, 'keyup')
.map(eventTargetValue);
// Stream
rateLimit(textInputKeyUpStream, 1000)
.timeInterval()
.map(function(ti) { return ti.value + ' (' + ti.interval + 'ms)'; })
.subscribe(prependToOutput);
希望对您有所帮助。