Skip to content

Instantly share code, notes, and snippets.

@william-lohan
Created August 19, 2019 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save william-lohan/7e01eaf4c817efb44f19cb5a8ec23f87 to your computer and use it in GitHub Desktop.
Save william-lohan/7e01eaf4c817efb44f19cb5a8ec23f87 to your computer and use it in GitHub Desktop.
tapIndex rxjs operator
class TapIndexSubscriber<T> extends Subscriber<T> {
private count: number = 0;
constructor(
protected destination: Subscriber<T>,
private nextEmit: (value: T, index: number) => void
) {
super(destination);
}
protected _next(value: T): void {
this.nextEmit(value, this.count++);
this.destination.next(value);
}
}
class TapIndexOperator<T> implements Operator<T, T> {
constructor(private nextEmit: (value: T, index: number) => void) { }
public call(subscriber: Subscriber<T>, source: Subscribable<T>): TeardownLogic {
return source.subscribe(new TapIndexSubscriber(subscriber, this.nextEmit));
}
}
export function tapIndex<T>(nextEmit: (value: T, index: number) => void): MonoTypeOperatorFunction<T> {
return function tapIndexOperation(source: Observable<T>): Observable<T> {
return source.lift(new TapIndexOperator(nextEmit));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment