2 var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
9 return function (d, b) {
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 Object.defineProperty(exports, "__esModule", { value: true });
16 var Subject_1 = require("../Subject");
17 var Observable_1 = require("../Observable");
18 var Subscriber_1 = require("../Subscriber");
19 var Subscription_1 = require("../Subscription");
20 var refCount_1 = require("../operators/refCount");
21 var ConnectableObservable = (function (_super) {
22 __extends(ConnectableObservable, _super);
23 function ConnectableObservable(source, subjectFactory) {
24 var _this = _super.call(this) || this;
25 _this.source = source;
26 _this.subjectFactory = subjectFactory;
28 _this._isComplete = false;
31 ConnectableObservable.prototype._subscribe = function (subscriber) {
32 return this.getSubject().subscribe(subscriber);
34 ConnectableObservable.prototype.getSubject = function () {
35 var subject = this._subject;
36 if (!subject || subject.isStopped) {
37 this._subject = this.subjectFactory();
41 ConnectableObservable.prototype.connect = function () {
42 var connection = this._connection;
44 this._isComplete = false;
45 connection = this._connection = new Subscription_1.Subscription();
46 connection.add(this.source
47 .subscribe(new ConnectableSubscriber(this.getSubject(), this)));
48 if (connection.closed) {
49 this._connection = null;
50 connection = Subscription_1.Subscription.EMPTY;
55 ConnectableObservable.prototype.refCount = function () {
56 return refCount_1.refCount()(this);
58 return ConnectableObservable;
59 }(Observable_1.Observable));
60 exports.ConnectableObservable = ConnectableObservable;
61 exports.connectableObservableDescriptor = (function () {
62 var connectableProto = ConnectableObservable.prototype;
64 operator: { value: null },
65 _refCount: { value: 0, writable: true },
66 _subject: { value: null, writable: true },
67 _connection: { value: null, writable: true },
68 _subscribe: { value: connectableProto._subscribe },
69 _isComplete: { value: connectableProto._isComplete, writable: true },
70 getSubject: { value: connectableProto.getSubject },
71 connect: { value: connectableProto.connect },
72 refCount: { value: connectableProto.refCount }
75 var ConnectableSubscriber = (function (_super) {
76 __extends(ConnectableSubscriber, _super);
77 function ConnectableSubscriber(destination, connectable) {
78 var _this = _super.call(this, destination) || this;
79 _this.connectable = connectable;
82 ConnectableSubscriber.prototype._error = function (err) {
84 _super.prototype._error.call(this, err);
86 ConnectableSubscriber.prototype._complete = function () {
87 this.connectable._isComplete = true;
89 _super.prototype._complete.call(this);
91 ConnectableSubscriber.prototype._unsubscribe = function () {
92 var connectable = this.connectable;
94 this.connectable = null;
95 var connection = connectable._connection;
96 connectable._refCount = 0;
97 connectable._subject = null;
98 connectable._connection = null;
100 connection.unsubscribe();
104 return ConnectableSubscriber;
105 }(Subject_1.SubjectSubscriber));
106 var RefCountOperator = (function () {
107 function RefCountOperator(connectable) {
108 this.connectable = connectable;
110 RefCountOperator.prototype.call = function (subscriber, source) {
111 var connectable = this.connectable;
112 connectable._refCount++;
113 var refCounter = new RefCountSubscriber(subscriber, connectable);
114 var subscription = source.subscribe(refCounter);
115 if (!refCounter.closed) {
116 refCounter.connection = connectable.connect();
120 return RefCountOperator;
122 var RefCountSubscriber = (function (_super) {
123 __extends(RefCountSubscriber, _super);
124 function RefCountSubscriber(destination, connectable) {
125 var _this = _super.call(this, destination) || this;
126 _this.connectable = connectable;
129 RefCountSubscriber.prototype._unsubscribe = function () {
130 var connectable = this.connectable;
132 this.connection = null;
135 this.connectable = null;
136 var refCount = connectable._refCount;
138 this.connection = null;
141 connectable._refCount = refCount - 1;
143 this.connection = null;
146 var connection = this.connection;
147 var sharedConnection = connectable._connection;
148 this.connection = null;
149 if (sharedConnection && (!connection || sharedConnection === connection)) {
150 sharedConnection.unsubscribe();
153 return RefCountSubscriber;
154 }(Subscriber_1.Subscriber));
155 //# sourceMappingURL=ConnectableObservable.js.map