[6a3a178] | 1 | import { SubjectSubscriber } from '../Subject';
|
---|
| 2 | import { Observable } from '../Observable';
|
---|
| 3 | import { Subscriber } from '../Subscriber';
|
---|
| 4 | import { Subscription } from '../Subscription';
|
---|
| 5 | import { refCount as higherOrderRefCount } from '../operators/refCount';
|
---|
| 6 | export class ConnectableObservable extends Observable {
|
---|
| 7 | constructor(source, subjectFactory) {
|
---|
| 8 | super();
|
---|
| 9 | this.source = source;
|
---|
| 10 | this.subjectFactory = subjectFactory;
|
---|
| 11 | this._refCount = 0;
|
---|
| 12 | this._isComplete = false;
|
---|
| 13 | }
|
---|
| 14 | _subscribe(subscriber) {
|
---|
| 15 | return this.getSubject().subscribe(subscriber);
|
---|
| 16 | }
|
---|
| 17 | getSubject() {
|
---|
| 18 | const subject = this._subject;
|
---|
| 19 | if (!subject || subject.isStopped) {
|
---|
| 20 | this._subject = this.subjectFactory();
|
---|
| 21 | }
|
---|
| 22 | return this._subject;
|
---|
| 23 | }
|
---|
| 24 | connect() {
|
---|
| 25 | let connection = this._connection;
|
---|
| 26 | if (!connection) {
|
---|
| 27 | this._isComplete = false;
|
---|
| 28 | connection = this._connection = new Subscription();
|
---|
| 29 | connection.add(this.source
|
---|
| 30 | .subscribe(new ConnectableSubscriber(this.getSubject(), this)));
|
---|
| 31 | if (connection.closed) {
|
---|
| 32 | this._connection = null;
|
---|
| 33 | connection = Subscription.EMPTY;
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 | return connection;
|
---|
| 37 | }
|
---|
| 38 | refCount() {
|
---|
| 39 | return higherOrderRefCount()(this);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | export const connectableObservableDescriptor = (() => {
|
---|
| 43 | const connectableProto = ConnectableObservable.prototype;
|
---|
| 44 | return {
|
---|
| 45 | operator: { value: null },
|
---|
| 46 | _refCount: { value: 0, writable: true },
|
---|
| 47 | _subject: { value: null, writable: true },
|
---|
| 48 | _connection: { value: null, writable: true },
|
---|
| 49 | _subscribe: { value: connectableProto._subscribe },
|
---|
| 50 | _isComplete: { value: connectableProto._isComplete, writable: true },
|
---|
| 51 | getSubject: { value: connectableProto.getSubject },
|
---|
| 52 | connect: { value: connectableProto.connect },
|
---|
| 53 | refCount: { value: connectableProto.refCount }
|
---|
| 54 | };
|
---|
| 55 | })();
|
---|
| 56 | class ConnectableSubscriber extends SubjectSubscriber {
|
---|
| 57 | constructor(destination, connectable) {
|
---|
| 58 | super(destination);
|
---|
| 59 | this.connectable = connectable;
|
---|
| 60 | }
|
---|
| 61 | _error(err) {
|
---|
| 62 | this._unsubscribe();
|
---|
| 63 | super._error(err);
|
---|
| 64 | }
|
---|
| 65 | _complete() {
|
---|
| 66 | this.connectable._isComplete = true;
|
---|
| 67 | this._unsubscribe();
|
---|
| 68 | super._complete();
|
---|
| 69 | }
|
---|
| 70 | _unsubscribe() {
|
---|
| 71 | const connectable = this.connectable;
|
---|
| 72 | if (connectable) {
|
---|
| 73 | this.connectable = null;
|
---|
| 74 | const connection = connectable._connection;
|
---|
| 75 | connectable._refCount = 0;
|
---|
| 76 | connectable._subject = null;
|
---|
| 77 | connectable._connection = null;
|
---|
| 78 | if (connection) {
|
---|
| 79 | connection.unsubscribe();
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | class RefCountOperator {
|
---|
| 85 | constructor(connectable) {
|
---|
| 86 | this.connectable = connectable;
|
---|
| 87 | }
|
---|
| 88 | call(subscriber, source) {
|
---|
| 89 | const { connectable } = this;
|
---|
| 90 | connectable._refCount++;
|
---|
| 91 | const refCounter = new RefCountSubscriber(subscriber, connectable);
|
---|
| 92 | const subscription = source.subscribe(refCounter);
|
---|
| 93 | if (!refCounter.closed) {
|
---|
| 94 | refCounter.connection = connectable.connect();
|
---|
| 95 | }
|
---|
| 96 | return subscription;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | class RefCountSubscriber extends Subscriber {
|
---|
| 100 | constructor(destination, connectable) {
|
---|
| 101 | super(destination);
|
---|
| 102 | this.connectable = connectable;
|
---|
| 103 | }
|
---|
| 104 | _unsubscribe() {
|
---|
| 105 | const { connectable } = this;
|
---|
| 106 | if (!connectable) {
|
---|
| 107 | this.connection = null;
|
---|
| 108 | return;
|
---|
| 109 | }
|
---|
| 110 | this.connectable = null;
|
---|
| 111 | const refCount = connectable._refCount;
|
---|
| 112 | if (refCount <= 0) {
|
---|
| 113 | this.connection = null;
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 | connectable._refCount = refCount - 1;
|
---|
| 117 | if (refCount > 1) {
|
---|
| 118 | this.connection = null;
|
---|
| 119 | return;
|
---|
| 120 | }
|
---|
| 121 | const { connection } = this;
|
---|
| 122 | const sharedConnection = connectable._connection;
|
---|
| 123 | this.connection = null;
|
---|
| 124 | if (sharedConnection && (!connection || sharedConnection === connection)) {
|
---|
| 125 | sharedConnection.unsubscribe();
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | //# sourceMappingURL=ConnectableObservable.js.map |
---|