Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(timeout): Getting rid of _unsubscribeAndRecycle #5626

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 61 additions & 85 deletions src/internal/operators/timeout.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/** @prettier */
import { asyncScheduler } from '../scheduler/async';
import { MonoTypeOperatorFunction, SchedulerLike, OperatorFunction, ObservableInput, TeardownLogic } from '../types';
import { MonoTypeOperatorFunction, SchedulerLike, OperatorFunction, ObservableInput } from '../types';
import { isValidDate } from '../util/isDate';
import { Subscriber } from '../Subscriber';
import { Subscription } from '../Subscription';
import { lift } from '../util/lift';
import { Operator } from '../Operator';
import { Observable } from '../Observable';
import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
import { from } from '../observable/from';

export interface TimeoutConfig<T, R = T, M = unknown> {
/**
Expand Down Expand Up @@ -336,7 +335,65 @@ export function timeout<T, R, M>(
throw new TypeError('No timeout provided.');
}

return lift(source, new TimeoutWithOperator(first, each, _with, scheduler, meta));
return lift(source, function (this: Subscriber<T | R>, source: Observable<T>) {
const subscriber = this;
const subscription = new Subscription();
let innerSub: Subscription;
let timerSubscription: Subscription | null = null;
let lastValue: T | null = null;
let seen = 0;
const startTimer = (delay: number) => {
subscription.add(
(timerSubscription = scheduler!.schedule(() => {
let withObservable: Observable<R>;
const info: TimeoutInfo<T, M> = {
meta,
lastValue,
seen,
};
try {
withObservable = from(_with!(info));
} catch (err) {
subscriber.error(err);
return;
}
innerSub.unsubscribe();
subscription.add(withObservable.subscribe(subscriber));
}, delay))
);
};

subscription.add(
(innerSub = source.subscribe({
next: (value) => {
timerSubscription?.unsubscribe();
timerSubscription = null;
seen++;
lastValue = value;
if (each != null && each > 0) {
startTimer(each);
}
subscriber.next(value);
},
error: (err) => subscriber.error(err),
complete: () => subscriber.complete(),
}))
);

let firstTimer: number;
if (first != null) {
if (typeof first === 'number') {
firstTimer = first;
} else {
firstTimer = +first - scheduler!.now();
}
} else {
firstTimer = each!;
}
startTimer(firstTimer);

return subscription;
});
};
}

Expand All @@ -348,84 +405,3 @@ export function timeout<T, R, M>(
function timeoutErrorFactory(info: TimeoutInfo<any>): Observable<never> {
throw new TimeoutError(info);
}

class TimeoutWithOperator<T, R, M> implements Operator<T, R> {
constructor(
private first: number | Date | undefined,
private each: number | undefined,
private _with: (info: TimeoutInfo<T, M>) => ObservableInput<R>,
private scheduler: SchedulerLike,
private meta: any
) {}

call(subscriber: Subscriber<T | R>, source: any): TeardownLogic {
return source.subscribe(new TimeoutWithSubscriber(subscriber, this.first, this.each, this._with, this.scheduler, this.meta));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class TimeoutWithSubscriber<T, R, M> extends SimpleOuterSubscriber<T, R> {
private _timerSubscription?: Subscription;
private lastValue?: T;
private seen = 0;

constructor(
destination: Subscriber<T>,
first: number | Date | undefined,
private each: number | undefined,
private _with: (info: TimeoutInfo<T, M>) => ObservableInput<R>,
private scheduler: SchedulerLike,
private meta: any
) {
super(destination);

let firstTimer: number;

if (first != null) {
if (typeof first === 'number') {
firstTimer = first;
} else {
firstTimer = +first - scheduler.now();
}
} else {
firstTimer = each!;
}

this.startTimer(firstTimer);
}

private startTimer(delay: number): void {
this._timerSubscription = this.scheduler.schedule(() => {
const { meta, seen, lastValue = null } = this;
let result: ObservableInput<R>;
try {
result = this._with({
meta,
seen,
lastValue,
});
} catch (err) {
this.destination.error(err);
return;
}
this._unsubscribeAndRecycle();
this.add(innerSubscribe(result, new SimpleInnerSubscriber(this)));
}, delay);
this.add(this._timerSubscription);
}

protected _next(value: T): void {
const { each } = this;
this._timerSubscription?.unsubscribe();
this.seen++;
this.lastValue = value;
if (each != null) {
this.startTimer(each);
}
super._next(value);
}
}