# Code coverage report for RxJS/dist/cjs/observables/IteratorObservable.js

## Statements: 52.38% (77 / 147) Branches: 23.33% (21 / 90) Functions: 43.48% (10 / 23) Lines: 51.08% (71 / 139) Ignored: none

All files » RxJS/dist/cjs/observables/ » IteratorObservable.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
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232

    1

    3

    2

    1

    1

    1

    1

    1

    1

    1

    1

    1

    1
    1

    1
    2

    2
    2
    2
    2
    2

    1

    1
    7
    7
    7
    7
    7
    7

    7

    7
    7
    2
    2

    5

    5
    5

    5

    5

    1
    2
    2
    2
    2
    2
    2
    2

    1

    1

    1

    1
    1

    1

    1

    1

    1
    1

    1

    1

    1

    1
    2
    2

    2

    2

    2

    1

    1

    1

    1

    1'use strict';

    exports.__esModule = true;

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

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

    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; }

    var _Observable2 = require('../Observable');

    var _Observable3 = _interopRequireDefault(_Observable2);

    var _utilRoot = require('../util/root');

    var _utilSymbol_iterator = require('../util/Symbol_iterator');

    var _utilSymbol_iterator2 = _interopRequireDefault(_utilSymbol_iterator);

    var _utilTryCatch = require('../util/tryCatch');

    var _utilTryCatch2 = _interopRequireDefault(_utilTryCatch);

    var _utilErrorObject = require('../util/errorObject');

    var IteratorObservable = (function (_Observable) {
        _inherits(IteratorObservable, _Observable);

        function IteratorObservable(iterator, project, thisArg, scheduler) {
            _classCallCheck(this, IteratorObservable);

            _Observable.call(this);
            this.iterator = iterator;
            this.project = project;
            this.thisArg = thisArg;
            this.scheduler = scheduler;
        }

        IteratorObservable.create = function create(iterator, project, thisArg, scheduler) {
            if (iterator == null) {
                throw new Error('iterator cannot be null.');
            }
            if (project && typeof project !== 'function') {
                throw new Error('When provided, `project` must be a function.');
            }
            return new IteratorObservable(iterator, project, thisArg, scheduler);
        };

        IteratorObservable.dispatch = function dispatch(state) {
            var index = state.index;
            var hasError = state.hasError;
            var thisArg = state.thisArg;
            var project = state.project;
            var iterator = state.iterator;
            var subscriber = state.subscriber;

            Iif (hasError) {
                subscriber.error(state.error);
                return;
            }
            var result = iterator.next();
            if (result.done) {
                subscriber.complete();
                return;
            }
            Iif (project) {
                result = _utilTryCatch2['default'](project).call(thisArg, result.value, index);
                if (result === _utilErrorObject.errorObject) {
                    state.error = _utilErrorObject.errorObject.e;
                    state.hasError = true;
                } else {
                    subscriber.next(result);
                    state.index = index + 1;
                }
            } else {
                subscriber.next(result.value);
                state.index = index + 1;
            }
            Iif (subscriber.isUnsubscribed) {
                return;
            }
            this.schedule(state);
        };

        IteratorObservable.prototype._subscribe = function _subscribe(subscriber) {
            var index = 0;
            var project = this.project;
            var thisArg = this.thisArg;
            var iterator = getIterator(Object(this.iterator));
            var scheduler = this.scheduler;
            Eif (scheduler) {
                subscriber.add(scheduler.schedule(IteratorObservable.dispatch, 0, {
                    index: index, thisArg: thisArg, project: project, iterator: iterator, subscriber: subscriber
                }));
            } else {
                do {
                    var result = iterator.next();
                    if (result.done) {
                        subscriber.complete();
                        break;
                    } else if (project) {
                        result = _utilTryCatch2['default'](project).call(thisArg, result.value, index++);
                        if (result === _utilErrorObject.errorObject) {
                            subscriber.error(_utilErrorObject.errorObject.e);
                            break;
                        }
                        subscriber.next(result);
                    } else {
                        subscriber.next(result.value);
                    }
                    if (subscriber.isUnsubscribed) {
                        break;
                    }
                } while (true);
            }
        };

        return IteratorObservable;
    })(_Observable3['default']);

    exports['default'] = IteratorObservable;

    var maxSafeInteger = Math.pow(2, 53) - 1;

    var StringIterator = (function () {
        function StringIterator(str) {
            var idx = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
            var len = arguments.length <= 2 || arguments[2] === undefined ? str.length : arguments[2];
            return (function () {
                _classCallCheck(this, StringIterator);

                this.str = str;
                this.idx = idx;
                this.len = len;
            }).apply(this, arguments);
        }

        StringIterator.prototype[_utilSymbol_iterator2['default']] = function () {
            return this;
        };

        StringIterator.prototype.next = function next() {
            return this.idx < this.len ? {
                done: false,
                value: this.str.charAt(this.idx++)
            } : {
                done: true,
                value: undefined
            };
        };

        return StringIterator;
    })();

    var ArrayIterator = (function () {
        function ArrayIterator(arr) {
            var idx = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
            var len = arguments.length <= 2 || arguments[2] === undefined ? toLength(arr) : arguments[2];
            return (function () {
                _classCallCheck(this, ArrayIterator);

                this.arr = arr;
                this.idx = idx;
                this.len = len;
            }).apply(this, arguments);
        }

        ArrayIterator.prototype[_utilSymbol_iterator2['default']] = function () {
            return this;
        };

        ArrayIterator.prototype.next = function next() {
            return this.idx < this.len ? {
                done: false,
                value: this.arr[this.idx++]
            } : {
                done: true,
                value: undefined
            };
        };

        return ArrayIterator;
    })();

    function getIterator(o) {
        var i = o[_utilSymbol_iterator2['default']];
        Iif (!i && typeof o === 'string') {
            return new StringIterator(o);
        }
        Iif (!i && o.length !== undefined) {
            return new ArrayIterator(o);
        }
        Iif (!i) {
            throw new TypeError('Object is not iterable');
        }
        return o[_utilSymbol_iterator2['default']]();
    }
    function toLength(o) {
        var len = +o.length;
        if (isNaN(len)) {
            return 0;
        }
        if (len === 0 || !numberIsFinite(len)) {
            return len;
        }
        len = sign(len) * Math.floor(Math.abs(len));
        if (len <= 0) {
            return 0;
        }
        if (len > maxSafeInteger) {
            return maxSafeInteger;
        }
        return len;
    }
    function numberIsFinite(value) {
        return typeof value === 'number' && _utilRoot.root.isFinite(value);
    }
    function isNan(n) {
        return n !== n;
    }
    function sign(value) {
        var valueAsNumber = +value;
        if (valueAsNumber === 0) {
            return valueAsNumber;
        }
        if (isNaN(valueAsNumber)) {
            return valueAsNumber;
        }
        return valueAsNumber < 0 ? -1 : 1;
    }
    module.exports = exports['default'];