angular - pipe tap/map do rxjs não é acionado quando se faz subscribe
Eu não sei por que, mas quando uso Observable com método de extensão e pipe, o map ou tap do pipe não está sendo acionado.
import { Observable, Subscriber, Observer } from 'rxjs';
import { tap, map } from 'rxjs/operators';
...
declare module 'rxjs' {
interface Observable<T> {
useCache<T>(this: Observable<T>, key: string): Observable<T>;
}
}
Observable.prototype.useCache = function<T>(this: Observable<T>, key: string) {
// executed
const inCache = LocalStorage.get(key);
if (inCache) {
return new Observable<T>(observer => observer.next(inCache));
}
// this.pipe(tap((e: T) => {
// LocalStorage.set(key, e);
// }));
this.pipe(map((e: T) => {
//not executed
LocalStorage.set(key, e);
}));
return this;
};
...
(in somewhere component)
this.service.get(...).subscribe(e=>{
//executed!
});
Em todos os outros lugares, posso definir pontos de interrupção que param lá, mas não dentro da função lambda do map
Resposta #1
O objeto this não está sendo modificado. Tente retornar this com o pipe anexado. E como você não está fazendo mapeamento, você pode simplesmente usar um tap.
Observable.prototype.useCache = function<T>(this: Observable<T>, key: string) {
const inCache = LocalStorage.get(key);
if (inCache) {
return of(inCache);
}
return this.pipe(
tap((e: T) => LocalStorage.set(key, e))
);
};
