I have a code to process some data that comes from an observable, this observable is acquired through a @Input annotation, I subscribe to the observable inside my NgOnInit(), everything was working fine, but I needed to add code to reduce the processing time of the data, which is an array of arrays, my data looks like this:
[ ['id1', object, object], ['id2', object, object] ]
So to reduce the processing time I created a variable that will hold the previous post-processed value of the Observable, and I compare the previous data with the new data and update only the data that changed, my code looks like this:
private dataSetHolder: Array<any> = [];
constructor() { }
ngOnInit() {
this.dataSet$.subscribe(data => { //line 43
console.log('inicio: ', this.dataSetHolder);
data.forEach(array => {
const dataNoId = Object.assign([], array);
const id = array[0];
dataNoId.shift();
let pacote: Package = { eixo_x: [], eixo_y: [] };
if (this.dataSetHolder.length === 0) {
pacote = this.separaDados(dataNoId, id, pacote);
this.loadData(pacote);
} else {
this.dataSetHolder.forEach(arrayHolder => {
if (id === arrayHolder[0]) {
if (array.length > arrayHolder.length) {
pacote = this.separaDados(dataNoId, id, pacote);
this.loadData(pacote);
}
}
});
}
});
this.dataSetHolder = data; //line 64
});
}
When line 64 is executed the current value of data is passed to this.dataSetHolder, when the observable receive a new value, line 43 is executed again, and when line 43 is executed the value of this.dataSetHolder is updated, that shouldn't happen, it should be updated only at line 64.
Does anybody understand what is going on there?
Please note, that from this last image to the next one I have not hit continue on the debug, I am still on line 44
I have also tried changing line 64 to this
this.dataSetHolder = Object.assign([], data);
Because I thought it could be caused by javascript, because when I do this.dataSetHolder = data; it is not actually passing the object, only the reference, but it still didn't work.