Для старых RxJs я написал оператор concatLatest
, который делает большую часть того, что вы хотите. С его помощью вы можете получить свое дросселирование с помощью этого кода:
Вот оператор. Я принял удар на его обновления для работы с RxJS5:
Rx.Observable.prototype.concatLatest = function() {
/// <summary>
/// Concatenates an observable sequence of observable sequences, skipping sequences that arrive while the current sequence is being observed.
/// If N new observables arrive while the current observable is being observed, the first N-1 new observables will be thrown
/// away and only the Nth will be observed.
/// </summary>
/// <returns type="Rx.Observable"></returns>
var source = this;
return Rx.Observable.create(function (observer) {
var latest,
isStopped,
isBusy,
outerSubscription,
innerSubscription,
subscriptions = new Rx.Subscription(function() {
if (outerSubscription) {
outerSubscription.unsubscribe();
}
if (innerSubscription) {
innerSubscription.unsubscribe();
}
}),
onError = observer.error.bind(observer),
onNext = observer.next.bind(observer),
innerOnComplete = function() {
var inner = latest;
if (inner) {
latest = undefined;
if (innerSubscription) {
innerSubscription.unsubscribe();
}
innerSubscription = inner.subscribe(onNext, onError, innerOnComplete);
}
else {
isBusy = false;
if (isStopped) {
observer.complete();
}
}
};
outerSubscription = source.subscribe(function (newInner) {
if (isBusy) {
latest = newInner;
}
else {
isBusy = true;
if (innerSubscription) {
innerSubscription.unsubscribe();
}
innerSubscription = newInner.subscribe(onNext, onError, innerOnComplete);
}
}, onError, function() {
isStopped = true;
if (!isBusy) {
observer.complete();
}
});
return subscriptions;
});
};
И вот обновленный plunkr: https://plnkr.co/edit/DSVmSPRijJwj9msefjRi?p=preview
Примечание Я обновил версию lodash до последней версии. В lodash 4.7 я переписал операторы дроссельной заслонки/debounce, чтобы исправить некоторые ошибки в кромке. Вы использовали 4.6.1, в котором все еще были некоторые из этих ошибок, хотя я не думаю, что они влияют на ваш тест.
Привет @Brandon, большой ответ. Можете ли вы предоставить ссылку на plunkr с помощью оператора concatLatest, используя RxJS 4.x? (Я бы не спросил, так как вопрос специфичен для RxJS 5, но вы сказали, что у вас уже было это удобно :) – HipsterZipster
@HipsterZipster Я написал эту версию для RxJS 2. Я предполагаю, что она будет работать и в 4: https: // gist.github.com/bman654/92749cd93cdd84a540e41403f25a2105 – Brandon