I'm currently working on a project with Angular and struggling to grasp the concept of RxJs and how it can help me solve my issue.
For example, let's say I have a component that needs 2 objects of type Item
export class MyComponent {
private obj1: Item;
private obj2: Item;
constructor(private dataService: DataStore) {
}
}
and the Item object looks like this
export class Item {
name: string;
value: string;
}
In addition, I have a datastore for storing Items
export class DataStore {
public itemList: Item[];
constructor(private apiService: ApiService) {
}
public getItemsFromApi() {
this.apiService.fetchItems(this.itemList)
.subscribe(response => {
this.itemList = response;
}
}
}
The DataStore uses a Service that communicates with an API to retrieve items.
My goal is to keep a real-time list of subscribed Items updated from the API. Each component requiring bindings to Items can add their items to this list, and the store will continuously fetch new Items from the API. These Items represent sensor data, so their values may change over time while their names remain constant.
Is there an efficient method that allows MyComponent to subscribe only to changes in obj1 and obj2 without concerning itself with other items? Or must I subscribe to the entire itemList and then filter out item1 and item2 manually?