Skip to content

Commit

Permalink
perf(refCount): optimize the implementation of class RefCounterOperator
Browse files Browse the repository at this point in the history
  • Loading branch information
githubxiaowen committed Jun 18, 2019
1 parent a9c87f6 commit 02a9eac
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/internal/observable/ConnectableObservable.ts
Expand Up @@ -12,8 +12,8 @@ import { refCount as higherOrderRefCount } from '../operators/refCount';
export class ConnectableObservable<T> extends Observable<T> {

protected _subject: Subject<T>;
protected _refCount: number = 0;
protected _connection: Subscription;
private _refCount: number = 0;
private _connection: Subscription;
/** @internal */
_isComplete = false;

Expand Down
10 changes: 4 additions & 6 deletions src/internal/operators/refCount.ts
Expand Up @@ -7,20 +7,18 @@ import { Observable } from '../Observable';

export function refCount<T>(): MonoTypeOperatorFunction<T> {
return function refCountOperatorFunction(source: ConnectableObservable<T>): Observable<T> {
return source.lift(new RefCountOperator(source));
return source.lift(new RefCountOperator());
} as MonoTypeOperatorFunction<T>;
}

class RefCountOperator<T> implements Operator<T, T> {
constructor(private connectable: ConnectableObservable<T>) {
}
call(subscriber: Subscriber<T>, source: any): TeardownLogic {

const { connectable } = this;
call(subscriber: Subscriber<T>, connectable: ConnectableObservable<T>): TeardownLogic {

(<any> connectable)._refCount++;

const refCounter = new RefCountSubscriber(subscriber, connectable);
const subscription = source.subscribe(refCounter);
const subscription = connectable.subscribe(refCounter);

if (!refCounter.closed) {
(<any> refCounter).connection = connectable.connect();
Expand Down

0 comments on commit 02a9eac

Please sign in to comment.