ConnectableObservable.js
5.38 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/** PURE_IMPORTS_START tslib,_Subject,_Observable,_Subscriber,_Subscription,_operators_refCount PURE_IMPORTS_END */
import * as tslib_1 from "tslib";
import { SubjectSubscriber } from '../Subject';
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { Subscription } from '../Subscription';
import { refCount as higherOrderRefCount } from '../operators/refCount';
var ConnectableObservable = /*@__PURE__*/ (function (_super) {
tslib_1.__extends(ConnectableObservable, _super);
function ConnectableObservable(source, subjectFactory) {
var _this = _super.call(this) || this;
_this.source = source;
_this.subjectFactory = subjectFactory;
_this._refCount = 0;
_this._isComplete = false;
return _this;
}
ConnectableObservable.prototype._subscribe = function (subscriber) {
return this.getSubject().subscribe(subscriber);
};
ConnectableObservable.prototype.getSubject = function () {
var subject = this._subject;
if (!subject || subject.isStopped) {
this._subject = this.subjectFactory();
}
return this._subject;
};
ConnectableObservable.prototype.connect = function () {
var connection = this._connection;
if (!connection) {
this._isComplete = false;
connection = this._connection = new Subscription();
connection.add(this.source
.subscribe(new ConnectableSubscriber(this.getSubject(), this)));
if (connection.closed) {
this._connection = null;
connection = Subscription.EMPTY;
}
}
return connection;
};
ConnectableObservable.prototype.refCount = function () {
return higherOrderRefCount()(this);
};
return ConnectableObservable;
}(Observable));
export { ConnectableObservable };
export var connectableObservableDescriptor = /*@__PURE__*/ (function () {
var connectableProto = ConnectableObservable.prototype;
return {
operator: { value: null },
_refCount: { value: 0, writable: true },
_subject: { value: null, writable: true },
_connection: { value: null, writable: true },
_subscribe: { value: connectableProto._subscribe },
_isComplete: { value: connectableProto._isComplete, writable: true },
getSubject: { value: connectableProto.getSubject },
connect: { value: connectableProto.connect },
refCount: { value: connectableProto.refCount }
};
})();
var ConnectableSubscriber = /*@__PURE__*/ (function (_super) {
tslib_1.__extends(ConnectableSubscriber, _super);
function ConnectableSubscriber(destination, connectable) {
var _this = _super.call(this, destination) || this;
_this.connectable = connectable;
return _this;
}
ConnectableSubscriber.prototype._error = function (err) {
this._unsubscribe();
_super.prototype._error.call(this, err);
};
ConnectableSubscriber.prototype._complete = function () {
this.connectable._isComplete = true;
this._unsubscribe();
_super.prototype._complete.call(this);
};
ConnectableSubscriber.prototype._unsubscribe = function () {
var connectable = this.connectable;
if (connectable) {
this.connectable = null;
var connection = connectable._connection;
connectable._refCount = 0;
connectable._subject = null;
connectable._connection = null;
if (connection) {
connection.unsubscribe();
}
}
};
return ConnectableSubscriber;
}(SubjectSubscriber));
var RefCountOperator = /*@__PURE__*/ (function () {
function RefCountOperator(connectable) {
this.connectable = connectable;
}
RefCountOperator.prototype.call = function (subscriber, source) {
var connectable = this.connectable;
connectable._refCount++;
var refCounter = new RefCountSubscriber(subscriber, connectable);
var subscription = source.subscribe(refCounter);
if (!refCounter.closed) {
refCounter.connection = connectable.connect();
}
return subscription;
};
return RefCountOperator;
}());
var RefCountSubscriber = /*@__PURE__*/ (function (_super) {
tslib_1.__extends(RefCountSubscriber, _super);
function RefCountSubscriber(destination, connectable) {
var _this = _super.call(this, destination) || this;
_this.connectable = connectable;
return _this;
}
RefCountSubscriber.prototype._unsubscribe = function () {
var connectable = this.connectable;
if (!connectable) {
this.connection = null;
return;
}
this.connectable = null;
var refCount = connectable._refCount;
if (refCount <= 0) {
this.connection = null;
return;
}
connectable._refCount = refCount - 1;
if (refCount > 1) {
this.connection = null;
return;
}
var connection = this.connection;
var sharedConnection = connectable._connection;
this.connection = null;
if (sharedConnection && (!connection || sharedConnection === connection)) {
sharedConnection.unsubscribe();
}
};
return RefCountSubscriber;
}(Subscriber));
//# sourceMappingURL=ConnectableObservable.js.map