in the link tap, it mentions
"Be careful! You can mutate objects as they pass through the tap operator's handlers."
What is the meaning of this?
I try the following test
obs3 = new Observable<string>((observer) => {
console.log('Observable 3 starts')
observer.next('1');
observer.next('2');
observer.next('3');
observer.next('4');
observer.next('5');
})
ngOnInit() {
this.obs3.pipe(tap(val=>val+1)).subscribe(
val=>{console.log(val);}
);
}
the console output is
Observable 3 starts
1
2
3
4
5
so The observable returned by tap is an exact mirror of the source, but then what is the meaning of the statement
Be careful! You can mutate objects as they pass through the tap operator's handlers.
?
*************[Edit on 20230707] *******************************
respond with Ruth answer, I try to compare the input observable before tap and output observable after tap by ===, and they are not equals.
const inputObj = {
a: 'test obj prop',
b: 5,
c: ['x', 'y', 'z']
};
of(inputObj) === of(inputObj)
.pipe(
tap((input) => {
input.a = 'changed prop value';
input.b = 60;
input.c = ['1', '2', '3'];
//return input;
})
)? console.log('after tap:still equal'):console.log('after tap: Not equal');
the output is
after tap: Not equal
so this becomes another problem, it violates the API about
The observable returned by tap is an exact mirror of the source