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

## Statements: 61.11% (44 / 72) Branches: 50% (14 / 28) Functions: 50% (9 / 18) Lines: 60.32% (38 / 63) Ignored: none

All files » RxJS/dist/cjs/observables/ » PromiseObservable.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

    1

    3

    34

    1

    1

    1

    1

    1

    1

    1

    1
    1

    1
    34

    34
    34
    34
    34

    1
    9

    9

    1
    34

    34
    34
    34
    34

    34
    21
    21
    21
    21

    13

    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 _Subscription = require('../Subscription');

    var _Subscription2 = _interopRequireDefault(_Subscription);

    var _schedulersImmediate = require('../schedulers/immediate');

    var _schedulersImmediate2 = _interopRequireDefault(_schedulersImmediate);

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

        function PromiseObservable(promise, scheduler) {
            _classCallCheck(this, PromiseObservable);

            _Observable.call(this);
            this.promise = promise;
            this.scheduler = scheduler;
            this._isScalar = false;
        }

        PromiseObservable.create = function create(promise) {
            var scheduler = arguments.length <= 1 || arguments[1] === undefined ? _schedulersImmediate2['default'] : arguments[1];

            return new PromiseObservable(promise, scheduler);
        };

        PromiseObservable.prototype._subscribe = function _subscribe(subscriber) {
            var _this = this;

            var scheduler = this.scheduler;
            var promise = this.promise;
            Eif (scheduler === _schedulersImmediate2['default']) {
                Iif (this._isScalar) {
                    subscriber.next(this.value);
                    subscriber.complete();
                } else {
                    promise.then(function (value) {
                        _this._isScalar = true;
                        _this.value = value;
                        subscriber.next(value);
                        subscriber.complete();
                    }, function (err) {
                        return subscriber.error(err);
                    }).then(null, function (err) {
                        // escape the promise trap, throw unhandled errors
                        setTimeout(function () {
                            throw err;
                        });
                    });
                }
            } else {
                var _ret = (function () {
                    var subscription = new _Subscription2['default']();
                    if (_this._isScalar) {
                        var value = _this.value;
                        subscription.add(scheduler.schedule(dispatchNext, 0, { value: value, subscriber: subscriber }));
                    } else {
                        promise.then(function (value) {
                            _this._isScalar = true;
                            _this.value = value;
                            subscription.add(scheduler.schedule(dispatchNext, 0, { value: value, subscriber: subscriber }));
                        }, function (err) {
                            return subscription.add(scheduler.schedule(dispatchError, 0, { err: err, subscriber: subscriber }));
                        }).then(null, function (err) {
                            // escape the promise trap, throw unhandled errors
                            scheduler.schedule(function () {
                                throw err;
                            });
                        });
                    }
                    return {
                        v: subscription
                    };
                })();

                if (typeof _ret === 'object') return _ret.v;
            }
        };

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

    exports['default'] = PromiseObservable;

    function dispatchNext(_ref) {
        var value = _ref.value;
        var subscriber = _ref.subscriber;

        subscriber.next(value);
        subscriber.complete();
    }
    function dispatchError(_ref2) {
        var err = _ref2.err;
        var subscriber = _ref2.subscriber;

        subscriber.error(err);
    }
    module.exports = exports['default'];