Home Manual Reference Source Test Repository

es6/operator/buffer.js

import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
/**
 * 缓冲源 Observable 的值直到 `closingNotifier` 发出。
 *
 * <span class="informal">将过往的值收集到一个数组中,并且仅当另一个 Observable 发出通知时才发出此数组。</span>
 *
 * <img src="./img/buffer.png" width="100%">
 *
 * 将 Observable 发出的值缓冲起来直到 `closingNotifier` 发出数据, 在这个时候在输出
 * Observable 上发出该缓冲区的值并且内部开启一个新的缓冲区, 等待下一个`closingNotifier`的发送。
 *
 * @example <caption>每次点击发出 interval Observable 最新缓冲的数组。</caption>
 * var clicks = Rx.Observable.fromEvent(document, 'click');
 * var interval = Rx.Observable.interval(1000);
 * var buffered = interval.buffer(clicks);
 * buffered.subscribe(x => console.log(x));
 *
 * @see {@link bufferCount}
 * @see {@link bufferTime}
 * @see {@link bufferToggle}
 * @see {@link bufferWhen}
 * @see {@link window}
 *
 * @param {Observable<any>} closingNotifier 该 Observable 向输出 Observale 发出信号以通知发出缓冲区。
 * @return {Observable<T[]>} 数组缓冲的 Observable。
 * @method buffer
 * @owner Observable
 */
export function buffer(closingNotifier) {
    return this.lift(new BufferOperator(closingNotifier));
}
class BufferOperator {
    constructor(closingNotifier) {
        this.closingNotifier = closingNotifier;
    }
    call(subscriber, source) {
        return source.subscribe(new BufferSubscriber(subscriber, this.closingNotifier));
    }
}
/**
 * We need this JSDoc comment for affecting ESDoc.
 * @ignore
 * @extends {Ignored}
 */
class BufferSubscriber extends OuterSubscriber {
    constructor(destination, closingNotifier) {
        super(destination);
        this.buffer = [];
        this.add(subscribeToResult(this, closingNotifier));
    }
    _next(value) {
        this.buffer.push(value);
    }
    notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
        const buffer = this.buffer;
        this.buffer = [];
        this.destination.next(buffer);
    }
}
//# sourceMappingURL=buffer.js.map