all files / src/ abstractObservable.js

100% Statements 48/48
91.67% Branches 11/12
100% Functions 17/17
100% Lines 43/43
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  34× 34× 34× 34×   34× 34× 34× 34× 34×   36× 36×       34× 34× 32×   34× 34× 34× 34×                 15×   15× 15×   19× 19×      
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var linkUtils_1 = require("./linkUtils");
var AbstractObservable = (function () {
    function AbstractObservable() {
        var _this = this;
        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 = linkUtils_1.toSubscriber(nextOrSubscriber, error, complete);
        if (this.terminated) {
            if (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 _this = this;
            var finished = false;
            return {
                get closed() {
                    return finished;
                },
                unsubscribe: function () {
                    subscribers.splice(subscribers.indexOf(subscriber), 1);
                    if (subscribers.length === 0) {
                        _this.stop();
                    }
                    finished = true;
                },
            };
        }).bind(this)(this.subscribers);
    };
    AbstractObservable.prototype.onNext = function (data) {
        this.subscribers.forEach(function (subscriber) { return setTimeout(function () { return subscriber.next(data); }, 0); });
    };
    AbstractObservable.prototype.onError = function (error) {
        this.subscribers.forEach(function (subscriber) { return subscriber.error ? setTimeout(function () { return subscriber.error(error); }, 0) : null; });
        this.terminate();
    };
    AbstractObservable.prototype.onComplete = function () {
        this.subscribers.forEach(function (subscriber) { return subscriber.complete ? setTimeout(subscriber.complete, 0) : null; });
        this.terminate();
    };
    return AbstractObservable;
}());
exports.default = AbstractObservable;
//# sourceMappingURL=abstractObservable.js.map