Skip to content

Commit

Permalink
refactor: simplify inner and outer subscription
Browse files Browse the repository at this point in the history
- Reduces memory pressure by no longer capturing outer values where they are not needed. For example, every inner subscription in mergeMap was retaining the outer value, if the outer value was a large array, that could add up
- Makes subscribeToResult less ridiculous, I am not sure what I was thinking there
- Leaves a few operators with more complex inner subscriptions, to be refactored later.
- Removes some redundant code that removed inner subscriptions manually, this was necessary in the early days of RxJS 5, but has not been necessary in a long time
  • Loading branch information
benlesh committed Jul 30, 2020
1 parent 3db2eaa commit d115113
Show file tree
Hide file tree
Showing 34 changed files with 316 additions and 650 deletions.
197 changes: 0 additions & 197 deletions spec/util/subscribeToResult-spec.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/internal/InnerSubscriber.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/internal/OuterSubscriber.ts

This file was deleted.

99 changes: 99 additions & 0 deletions src/internal/innies-and-outies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/** @prettier */
import { Subscription } from './Subscription';
import { Subscriber } from './Subscriber';
import { Observable } from './Observable';
import { subscribeTo } from './util/subscribeTo';

export interface ISimpleOuterSubscriber<T> {
notifyNext(innerValue: T): void;
notifyError(err: any): void;
notifyComplete(): void;
}

export interface IComplexOuterSubscriber<T, R> {
notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number, innerSub: ComplexInnerSubscriber<T, R>): void;

notifyError(error: any): void;

notifyComplete(innerSub: ComplexInnerSubscriber<T, R>): void;
}

export class SimpleInnerSubscriber<T> extends Subscriber<T> {
constructor(private parent: ISimpleOuterSubscriber<any>) {
super();
}

protected _next(value: T): void {
this.parent.notifyNext(value);
}

protected _error(error: any): void {
this.parent.notifyError(error);
this.unsubscribe();
}

protected _complete(): void {
this.parent.notifyComplete();
this.unsubscribe();
}
}

export class ComplexInnerSubscriber<T, R> extends Subscriber<R> {
private index = 0;

constructor(private parent: IComplexOuterSubscriber<T, R>, public outerValue: T, public outerIndex: number) {
super();
}

protected _next(value: R): void {
this.parent.notifyNext(this.outerValue, value, this.outerIndex, this.index++, this);
}

protected _error(error: any): void {
this.parent.notifyError(error);
this.unsubscribe();
}

protected _complete(): void {
this.parent.notifyComplete(this);
this.unsubscribe();
}
}

export class SimpleOuterSubscriber<T, R> extends Subscriber<T> implements ISimpleOuterSubscriber<R> {
notifyNext(innerValue: R): void {
this.destination.next(innerValue);
}

notifyError(err: any): void {
this.destination.error(err);
}

notifyComplete(): void {
this.destination.complete();
}
}

export class ComplexOuterSubscriber<T, R> extends Subscriber<T> implements IComplexOuterSubscriber<T, R> {
notifyNext(_outerValue: T, innerValue: R, _outerIndex: number, _innerIndex: number, _innerSub: ComplexInnerSubscriber<T, R>): void {
this.destination.next(innerValue);
}

notifyError(error: any): void {
this.destination.error(error);
}

notifyComplete(_innerSub: ComplexInnerSubscriber<T, R>): void {
this.destination.complete();
}
}

export function subscribeToResult2(result: any, innerSubscriber: Subscriber<any>): Subscription | undefined {
if (innerSubscriber.closed) {
return undefined;
}
if (result instanceof Observable) {
return result.subscribe(innerSubscriber);
}
return subscribeTo(result)(innerSubscriber) as Subscription;
}
15 changes: 6 additions & 9 deletions src/internal/observable/combineLatest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { ObservableInput, SchedulerLike, ObservedValueOf } from '../types';
import { isScheduler } from '../util/isScheduler';
import { isArray } from '../util/isArray';
import { Subscriber } from '../Subscriber';
import { OuterSubscriber } from '../OuterSubscriber';
import { ComplexOuterSubscriber, ComplexInnerSubscriber, subscribeToResult2 } from '../innies-and-outies';
import { Operator } from '../Operator';
import { InnerSubscriber } from '../InnerSubscriber';
import { isObject } from '../util/isObject';
import { subscribeToResult } from '../util/subscribeToResult';
import { fromArray } from './fromArray';
import { lift } from '../util/lift';

Expand Down Expand Up @@ -284,7 +282,7 @@ export class CombineLatestOperator<T, R> implements Operator<T, R> {
* @ignore
* @extends {Ignored}
*/
export class CombineLatestSubscriber<T, R> extends OuterSubscriber<T, R> {
export class CombineLatestSubscriber<T, R> extends ComplexOuterSubscriber<T, R> {
private active: number = 0;
private values: any[] = [];
private observables: any[] = [];
Expand All @@ -309,20 +307,19 @@ export class CombineLatestSubscriber<T, R> extends OuterSubscriber<T, R> {
this.toRespond = len;
for (let i = 0; i < len; i++) {
const observable = observables[i];
this.add(subscribeToResult(this, observable, observable, i));
this.add(subscribeToResult2(observable, new ComplexInnerSubscriber(this, observable, i)));
}
}
}

notifyComplete(unused: Subscriber<R>): void {
notifyComplete(): void {
if ((this.active -= 1) === 0) {
this.destination.complete();
}
}

notifyNext(outerValue: T, innerValue: R,
outerIndex: number, innerIndex: number,
innerSub: InnerSubscriber<T, R>): void {
notifyNext(_outerValue: T, innerValue: R,
outerIndex: number): void {
const values = this.values;
const oldVal = values[outerIndex];
const toRespond = !this.toRespond
Expand Down
Loading

0 comments on commit d115113

Please sign in to comment.