all files / src/ abstractObservable.js

93.33% Statements 42/45
80% Branches 8/10
82.35% Functions 14/17
92.5% Lines 37/40
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  24× 24× 17×   24×   24× 20× 20×   24× 24× 24×   24× 24× 24× 24× 24×   26× 26×           25× 25× 21×   25× 25× 25×                    
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fetcherUtils_1 = require("./fetcherUtils");
var AbstractObservable = (function () {
    function AbstractObservable() {
        var _this = this;
        this.onNext = function (data) {
            _this.subscribers.forEach(function (subscriber) { return setTimeout(function () { return subscriber.next(data); }, 0); });
        };
        this.onError = function (error) {
            _this.subscribers.forEach(function (subscriber) { return subscriber.error ? setTimeout(function () { return subscriber.error(error); }, 0) : null; });
            _this.terminate();
        };
        this.onComplete = function () {
            _this.subscribers.forEach(function (subscriber) { return subscriber.complete ? setTimeout(subscriber.complete, 0) : null; });
            _this.terminate();
        };
        this.terminate = function () {
            _this.subscribers = [];
            _this.terminated = true;
        };
        this.subscribers = [];
        this.onNext = this.onNext.bind(this);
        this.onError = this.onError.bind(this);
        this.onComplete = this.onComplete.bind(this);
        this.terminated = false;
    }
    AbstractObservable.prototype.subscribe = function (nextOrSubscriber, error, complete) {
        var subscriber = fetcherUtils_1.toSubscriber(nextOrSubscriber, error, complete);
        if (this.terminated) {
            Eif (subscriber.complete) {
                subscriber.complete();
            }
            return {
                get closed() { return true; },
                unsubscribe: function () { return void 0; },
            };
        }
        this.subscribers.push(subscriber);
        if (this.subscribers.length === 1) {
            this.start();
        }
        return (function (subscribers) {
            var stopped = false;
            return {
                get closed() {
                    return stopped;
                },
                unsubscribe: function () {
                    subscribers.splice(subscribers.indexOf(subscriber), 1);
                    stopped = true;
                },
            };
        })(this.subscribers);
    };
    return AbstractObservable;
}());
exports.default = AbstractObservable;
//# sourceMappingURL=abstractObservable.js.map