I create Observable using 'of' operator and the stream consists of object with array of objects. I subscribe to it and then I push something to this array of objects.
Next I subscribe again to the Observable and I expect the same values as during first subscription, but I get array of objects that has the previous push value. Why is that?
There is some code:
ngOnInit() {
this.observable = of(
{
name: "Mark",
pets: [{
type: "dog",
age: 3
},
{
type: "cat",
age: 2
}]
});
this.getObservable();
this.getObservableOneMoreTime();
}
getObservable() {
this.observable.subscribe(r => {
this.state = r
this.state.pets.push({type: "bird", age: 1});
})
}
getObservableOneMoreTime() {
setTimeout(() => {
this.observable.subscribe(r => {
console.log(r);
})
}, 5000)
}
In console I'm getting an array with 3 elements (it's including "bird"), but I expect to get initial array - only "dog" and "cat" inside it. I don't understand why this is working in this way. Is there some caching or something like that?
I thought that Observables are Cold in default and every new subscription is returning the starting values to new subscriber.