I have a service: ScheduleService
that holds a ReplaySubject<Shift>
:
private readonly shiftSubject = new ReplaySubject<Shift>();
I expose this subject to consumers via this method:
currentShift(): Observable<Shift> {
return this.shiftSubject.asObservable();
}
It's my intention that a new Shift
value can be pushed into the shiftSubject
via this method:
updateShift(shift: Shift) {
console.log('updating shift: ' + shift.shiftName); // I AM SEEING THIS LOG
this.shiftSubject.next(shift); // I'VE VERIFIED THAT THIS VALUE IS BEING UPDATED
}
However, it does not appear that subscribers to currentShift()
are receiving new emissions when this.shiftSubject.next(shift)
is called. I have bound the value of this subject to a mat-select
component like this:
<mat-select [formControl]="shiftSelect" (selectionChange)="updateShift($event)" [value]="(scheduleService.currentShift() | async)">
<mat-option *ngFor="let shift of (scheduleService.availableShifts() | async)" [value]="shift">
{{shift.shiftName | titlecase}}
</mat-option>
</mat-select>
...and I've also subscribed in the consuming component like this, just for debugging:
export class ShiftSelectorComponent implements OnInit, OnDestroy {
shiftSelect: FormControl;
constructor(private scheduleService: ScheduleService) {
this.shiftSelect = new FormControl();
}
ngOnInit() {
this.scheduleService.currentShift().subscribe((shift) => console.log('SHFIT SELECTOR RECEIVES NEW SHIFT: ' + shift.shiftName));
}
...
Nothing I try seems to cause subscribers to this subject to receive updates. I can't figure out what I'm doing wrong. I have double checked that the service is a singleton, as other posts have mentioned that. The ScheduleService
is provided to consumers in app.module.ts
.
Thanks.