1 /** PURE_IMPORTS_START tslib,_Subject,_Observable,_Subscriber,_Subscription,_operators_refCount PURE_IMPORTS_END */
2 import * as tslib_1 from "tslib";
3 import { SubjectSubscriber } from '../Subject';
4 import { Observable } from '../Observable';
5 import { Subscriber } from '../Subscriber';
6 import { Subscription } from '../Subscription';
7 import { refCount as higherOrderRefCount } from '../operators/refCount';
8 var ConnectableObservable = /*@__PURE__*/ (function (_super) {
9 tslib_1.__extends(ConnectableObservable, _super);
10 function ConnectableObservable(source, subjectFactory) {
11 var _this = _super.call(this) || this;
12 _this.source = source;
13 _this.subjectFactory = subjectFactory;
15 _this._isComplete = false;
18 ConnectableObservable.prototype._subscribe = function (subscriber) {
19 return this.getSubject().subscribe(subscriber);
21 ConnectableObservable.prototype.getSubject = function () {
22 var subject = this._subject;
23 if (!subject || subject.isStopped) {
24 this._subject = this.subjectFactory();
28 ConnectableObservable.prototype.connect = function () {
29 var connection = this._connection;
31 this._isComplete = false;
32 connection = this._connection = new Subscription();
33 connection.add(this.source
34 .subscribe(new ConnectableSubscriber(this.getSubject(), this)));
35 if (connection.closed) {
36 this._connection = null;
37 connection = Subscription.EMPTY;
42 ConnectableObservable.prototype.refCount = function () {
43 return higherOrderRefCount()(this);
45 return ConnectableObservable;
47 export { ConnectableObservable };
48 export var connectableObservableDescriptor = /*@__PURE__*/ (function () {
49 var connectableProto = ConnectableObservable.prototype;
51 operator: { value: null },
52 _refCount: { value: 0, writable: true },
53 _subject: { value: null, writable: true },
54 _connection: { value: null, writable: true },
55 _subscribe: { value: connectableProto._subscribe },
56 _isComplete: { value: connectableProto._isComplete, writable: true },
57 getSubject: { value: connectableProto.getSubject },
58 connect: { value: connectableProto.connect },
59 refCount: { value: connectableProto.refCount }
62 var ConnectableSubscriber = /*@__PURE__*/ (function (_super) {
63 tslib_1.__extends(ConnectableSubscriber, _super);
64 function ConnectableSubscriber(destination, connectable) {
65 var _this = _super.call(this, destination) || this;
66 _this.connectable = connectable;
69 ConnectableSubscriber.prototype._error = function (err) {
71 _super.prototype._error.call(this, err);
73 ConnectableSubscriber.prototype._complete = function () {
74 this.connectable._isComplete = true;
76 _super.prototype._complete.call(this);
78 ConnectableSubscriber.prototype._unsubscribe = function () {
79 var connectable = this.connectable;
81 this.connectable = null;
82 var connection = connectable._connection;
83 connectable._refCount = 0;
84 connectable._subject = null;
85 connectable._connection = null;
87 connection.unsubscribe();
91 return ConnectableSubscriber;
92 }(SubjectSubscriber));
93 var RefCountOperator = /*@__PURE__*/ (function () {
94 function RefCountOperator(connectable) {
95 this.connectable = connectable;
97 RefCountOperator.prototype.call = function (subscriber, source) {
98 var connectable = this.connectable;
99 connectable._refCount++;
100 var refCounter = new RefCountSubscriber(subscriber, connectable);
101 var subscription = source.subscribe(refCounter);
102 if (!refCounter.closed) {
103 refCounter.connection = connectable.connect();
107 return RefCountOperator;
109 var RefCountSubscriber = /*@__PURE__*/ (function (_super) {
110 tslib_1.__extends(RefCountSubscriber, _super);
111 function RefCountSubscriber(destination, connectable) {
112 var _this = _super.call(this, destination) || this;
113 _this.connectable = connectable;
116 RefCountSubscriber.prototype._unsubscribe = function () {
117 var connectable = this.connectable;
119 this.connection = null;
122 this.connectable = null;
123 var refCount = connectable._refCount;
125 this.connection = null;
128 connectable._refCount = refCount - 1;
130 this.connection = null;
133 var connection = this.connection;
134 var sharedConnection = connectable._connection;
135 this.connection = null;
136 if (sharedConnection && (!connection || sharedConnection === connection)) {
137 sharedConnection.unsubscribe();
140 return RefCountSubscriber;
142 //# sourceMappingURL=ConnectableObservable.js.map