If I have a Service that offers a BehaviorSubject to any component needing the most up-to-date version of certain data, how can these components differentiate what changed in the data when they receive notifications?
export class DataService {
private messageSource = new BehaviorSubject<Data>(this.data);
currentMessage = this.messageSource.asObservable();
}
export class Component implements OnInit {
message:Data;
constructor(private data: DataService) { }
ngOnInit() {
this.data.currentMessage.subscribe(
message => {
this.message = JSON.parse(JSON.stringify(message))
}
)
}
}
The Data structure is complex:
class Data {
id:number
prop1:string
prop2:Array<{prop1:number, prop2:string}>
}
As the Data evolves over time, and components are interested only in the latest updates, how can the components identify specific changes within the data? For instance, if a single field of one item in prop2
is modified, all components will receive the updated data without knowing precisely what was altered. Understanding the changes could potentially enhance rendering optimization.
Is there a native method to achieve this using BehaviorSubject? Should the components determine the differences post-notification? How is this typically addressed?