I have an array of items and I wrap that in an observable using of.
The observable is created before the array is populated.
When the array is finally populated, the observable callback passed to subscribe does not get called.
From what I understand, observable only calls this callback for the items already in the list, which in my opinion makes it redundant.
I have a case where I use this observable inside an *ngFor with async pipe, and this one reacts correctly, but when I pas the observable as data source to a mat-table or I pass my callback to the subscribe function, then I don't get anything when the list is eventually populated.
What's the thing that async pipe does behind the scenes, and I am missing?
export class DiscoveryService {
private deviceList: DeviceModel[] = [];
constructor() {
}
getDeviceList(): void {
// Get devices from server and push them in the deviceList
}
observeDeviceList(): Observable<DeviceModel[]> {
return of(this.deviceList);
}
}
export class DeviceListComponent implements OnInit {
deviceList$: Observable<DeviceModel[]>;
constructor(private discoveryService: DiscoveryService) { }
ngOnInit() {
this.deviceList$ = this.discoveryService.observeDeviceList();
// This callback get's called only once at the beginning, with an empty list
this.deviceList$.subscribe(devices => console.log('got devices: ' , devices));
// When the devices are retrieved from the server, the callback
//from the above subscription is not triggered again
this.discoveryService.getDeviceListx();
}
}
The async pipe gets updated correctly, but I guess this might be because the ngOnInit is called before the *ngFor runs. I'm not sure.
<mat-nav-list *ngFor="let device of deviceList$ | async">