This file contains hidden or 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
import { range } from "rxjs"; | |
import { debug } from "./debug"; | |
const source = range(1, 2).pipe(debug()); | |
console.log("first use:"); | |
source.subscribe(); | |
console.log("second use:"); | |
source.subscribe(); |
This file contains hidden or 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
import { range } from "rxjs"; | |
import { debug } from "./debug"; | |
const op = debug(); | |
console.log("first use:"); | |
range(1, 2).pipe(op).subscribe(); | |
console.log("second use:"); | |
range(1, 2).pipe(op).subscribe(); |
This file contains hidden or 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
import { MonoTypeOperatorFunction } from "rxjs"; | |
import { map, scan } from "rxjs/operators"; | |
export function debug<T>(): MonoTypeOperatorFunction<T> { | |
return source => source.pipe( | |
scan<T, [T, number]>(([, index], t) => [t, index + 1], [undefined!, -1]), | |
map(([t, index]) => (console.log(`[${index}]: ${t}`), t)) | |
); | |
} |
This file contains hidden or 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
import { defer, MonoTypeOperatorFunction } from "rxjs"; | |
import { tap } from "rxjs/operators"; | |
export function debug<T>(): MonoTypeOperatorFunction<T> { | |
return source => defer(() => { | |
let index = -1; | |
return source.pipe( | |
tap(t => console.log(`[${++index}]: ${t}`)) | |
); | |
}); |
This file contains hidden or 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
import { MonoTypeOperatorFunction, Observable } from "rxjs"; | |
import { tap } from "rxjs/operators"; | |
export function debug<T>(): MonoTypeOperatorFunction<T> { | |
return source => new Observable<T>(subscriber => { | |
let index = -1; | |
return source.pipe( | |
tap(t => console.log(`[${++index}]: ${t}`)) | |
).subscribe(subscriber); | |
}); |
This file contains hidden or 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
import { MonoTypeOperatorFunction } from "rxjs"; | |
import { tap } from "rxjs/operators"; | |
export function debug<T>(): MonoTypeOperatorFunction<T> { | |
let index = -1; | |
// Let's pretend that the map operator doesn't exist and that we have to use | |
// the tap operator and maintain our own internal state for the index, as the | |
// purpose of our operator is to show that the behaviour depends upon where | |
// the state is stored. | |
return tap(t => console.log(`[${++index}]: ${t}`)); |
This file contains hidden or 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
return this.http.get<Something>("https://api.some.com/things/1").pipe( | |
map(this.extractSomeProperty.bind(this)), | |
catchError(this.handleError.bind(this)) | |
); |
This file contains hidden or 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
return this.http.get<Something>("https://api.some.com/things/1").pipe( | |
map(s => this.extractSomeProperty(s)), | |
catchError(e => this.handleError(e)) | |
); |