# Code coverage report for RxJS/dist/cjs/operators/bufferTime.js

## Statements: 97.83% (90 / 92) Branches: 67.65% (23 / 34) Functions: 100% (17 / 17) Lines: 100% (84 / 84) Ignored: none

All files » RxJS/dist/cjs/operators/ » bufferTime.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141

    1
    1

    2

    1

    16

    1

    1

    1

    1

    1
    8
    8

    8

    1
    1
    8

    8
    8
    8

    1
    8

    1

    1
    1

    1
    8

    8
    8
    8
    8
    8
    8
    8
    2
    2
    2
    2

    6
    6

    1
    35
    35
    35
    39

    1
    3
    3

    1
    4
    4
    4

    4

    1
    25
    25
    25

    1
    17
    17
    17

    1

    1
    13
    13
    13
    13

    13
    13
    13

    1
    4
    4
    4
    4

    4
    4
    4
    4
    4

    1
    4
    4

    4

    1'use strict';

    exports.__esModule = true;
    exports['default'] = bufferTime;

    function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

    function _inherits(subClass, superClass) { Iif (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

    function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

    var _Subscriber2 = require('../Subscriber');

    var _Subscriber3 = _interopRequireDefault(_Subscriber2);

    var _schedulersNextTick = require('../schedulers/nextTick');

    var _schedulersNextTick2 = _interopRequireDefault(_schedulersNextTick);

    /**
     * buffers values from the source for a specific time period. Optionally allows new buffers to be set up at an interval.
     * @param {number} the amount of time to fill each buffer for before emitting them and clearing them.
     * @param {number} [bufferCreationInterval] the interval at which to start new buffers.
     * @param {Scheduler} [scheduler] (optional, defaults to `nextTick` scheduler) The scheduler on which to schedule the
     *  intervals that determine buffer boundaries.
     * @returns {Observable<T[]>} an observable of arrays of buffered values.
     */

    function bufferTime(bufferTimeSpan) {
        var bufferCreationInterval = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
        var scheduler = arguments.length <= 2 || arguments[2] === undefined ? _schedulersNextTick2['default'] : arguments[2];

        return this.lift(new BufferTimeOperator(bufferTimeSpan, bufferCreationInterval, scheduler));
    }

    var BufferTimeOperator = (function () {
        function BufferTimeOperator(bufferTimeSpan, bufferCreationInterval, scheduler) {
            _classCallCheck(this, BufferTimeOperator);

            this.bufferTimeSpan = bufferTimeSpan;
            this.bufferCreationInterval = bufferCreationInterval;
            this.scheduler = scheduler;
        }

        BufferTimeOperator.prototype.call = function call(subscriber) {
            return new BufferTimeSubscriber(subscriber, this.bufferTimeSpan, this.bufferCreationInterval, this.scheduler);
        };

        return BufferTimeOperator;
    })();

    var BufferTimeSubscriber = (function (_Subscriber) {
        _inherits(BufferTimeSubscriber, _Subscriber);

        function BufferTimeSubscriber(destination, bufferTimeSpan, bufferCreationInterval, scheduler) {
            _classCallCheck(this, BufferTimeSubscriber);

            _Subscriber.call(this, destination);
            this.bufferTimeSpan = bufferTimeSpan;
            this.bufferCreationInterval = bufferCreationInterval;
            this.scheduler = scheduler;
            this.buffers = [];
            var buffer = this.openBuffer();
            if (bufferCreationInterval !== null && bufferCreationInterval >= 0) {
                var closeState = { subscriber: this, buffer: buffer };
                var creationState = { bufferTimeSpan: bufferTimeSpan, bufferCreationInterval: bufferCreationInterval, subscriber: this, scheduler: scheduler };
                this.add(scheduler.schedule(dispatchBufferClose, bufferTimeSpan, closeState));
                this.add(scheduler.schedule(dispatchBufferCreation, bufferCreationInterval, creationState));
            } else {
                var timeSpanOnlyState = { subscriber: this, buffer: buffer, bufferTimeSpan: bufferTimeSpan };
                this.add(scheduler.schedule(dispatchBufferTimeSpanOnly, bufferTimeSpan, timeSpanOnlyState));
            }
        }

        BufferTimeSubscriber.prototype._next = function _next(value) {
            var buffers = this.buffers;
            var len = buffers.length;
            for (var i = 0; i < len; i++) {
                buffers[i].push(value);
            }
        };

        BufferTimeSubscriber.prototype._error = function _error(err) {
            this.buffers.length = 0;
            this.destination.error(err);
        };

        BufferTimeSubscriber.prototype._complete = function _complete() {
            var buffers = this.buffers;
            while (buffers.length > 0) {
                this.destination.next(buffers.shift());
            }
            this.destination.complete();
        };

        BufferTimeSubscriber.prototype.openBuffer = function openBuffer() {
            var buffer = [];
            this.buffers.push(buffer);
            return buffer;
        };

        BufferTimeSubscriber.prototype.closeBuffer = function closeBuffer(buffer) {
            this.destination.next(buffer);
            var buffers = this.buffers;
            buffers.splice(buffers.indexOf(buffer), 1);
        };

        return BufferTimeSubscriber;
    })(_Subscriber3['default']);

    function dispatchBufferTimeSpanOnly(state) {
        var subscriber = state.subscriber;
        var prevBuffer = state.buffer;
        Eif (prevBuffer) {
            subscriber.closeBuffer(prevBuffer);
        }
        state.buffer = subscriber.openBuffer();
        Eif (!subscriber.isUnsubscribed) {
            this.schedule(state, state.bufferTimeSpan);
        }
    }
    function dispatchBufferCreation(state) {
        var bufferCreationInterval = state.bufferCreationInterval;
        var bufferTimeSpan = state.bufferTimeSpan;
        var subscriber = state.subscriber;
        var scheduler = state.scheduler;

        var buffer = subscriber.openBuffer();
        var action = this;
        Eif (!subscriber.isUnsubscribed) {
            action.add(scheduler.schedule(dispatchBufferClose, bufferTimeSpan, { subscriber: subscriber, buffer: buffer }));
            action.schedule(state, bufferCreationInterval);
        }
    }
    function dispatchBufferClose(_ref) {
        var subscriber = _ref.subscriber;
        var buffer = _ref.buffer;

        subscriber.closeBuffer(buffer);
    }
    module.exports = exports['default'];