Created
August 19, 2019 23:27
-
-
Save william-lohan/7e01eaf4c817efb44f19cb5a8ec23f87 to your computer and use it in GitHub Desktop.
tapIndex rxjs operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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