I was looking at the source of ConnectableObservable.
connect(): Subscription {
let connection = this._connection;
if (!connection) {
connection = this._connection = new Subscription();
const subject = this.getSubject();
connection.add(
this.source.subscribe(
new OperatorSubscriber(
subject as any,
//...
return connection;
}
Now I'm wondering why does the Connect method internally create a new connection subscription, if this.source.subscribe returns a subscription already - why not return that subscription from the Connect method instead? I'm aware that this has exactly the same effect, e.g. the subscription returned from the this.source.subscribe call gets added to the connection subscription and whenever connection.unsubscribe() is called both subscriptions get unsubscribed. The only reason I can think of is that currently the this.source.subscribe call returns a Subscription, but if it starts returning an object of a different type that would still get accepted by Subscription.add method, then we wouldn't have to change the return type of the connect method, which we would have to do if Connect returned the result of this.source.subscribe directly.
from ConnectableObservable - why does Connect internally create a new subscription?
No comments:
Post a Comment