In a component, I subscribe a Subject to some part of my state using the store. From then on, when that part of the state changes, I will start receiving updates and my component will reflect this state. So far so good.
However, I also want my component to initialize correctly based on the current value of this state. So as soon as I subscribe, I also want the current value to get emitted - so, e.g., my view can initialize correctly.
Example with an Angular component:
my.component.ts
export class MyComponent implements OnInit {
constructor(private store: Store<State>) { }
// As soon as I subscribe, I want this to also emit the current
// value, e.g. so my view can correctly reflect the current state.
public mode$ = new Subject<ApplicationMode>();
ngOnInit() {
this.store.select(getApplicationState)
.select(s => s.applicationMode).subscribe(this.mode$);
}
}
my.component.html
{{ mode$ | async }}
I believe that what I would like/expect is that this part of my store would return a BehaviorSubject or a ReplaySubject(1). But selecting anything from the store always returns a Store<T> which is obviously some sort of subject, but doesn't seem to emit the current value upon subscription?
What might work, I guess, is to subscribe to this piece of state on application initialization, and then pass that value on through all its child components, all the way down to this one, so this component turns into a dumb one instead of selecting from the store itself. Is that perhaps the way to go? Or is there something basic I'm missing to make this work?