Replies: 1 comment 1 reply
-
I experience the exact same thing when subscribing to a select observable in a brand new project. subscribing in a lazy loaded submodule
state, in redux dev tools
Since the state is always correct, it makes me believe it's only a problem of the selectEntity/selectAll observable. That left my completely puzzled for a day. One would assume that the redux dev tools would reflect the state changing three times, which it doesn't. Why does the observable emit a value that is not reflected in the state? However I'm new to akita, so maybe I'm doing something wrong here. After I found your issue describing that it depends on where you subscribe to the select observable, I tried that myself and sure enough, same thing here. subscribing in app.component
The subscription code blocks are below and seem identical to me. things i consideredFirst of it is a new project, with two stores. The first store is only in the app.module, the second in the submodule. This makes debugging considerably easier, since you can still do simple project wide string searches to see where methods are called. 1. is the service setting the active entity correctly?Yes, if I tap the pipe that sets it like so: .pipe(
tap(id => { console.log('store setting active id:', id); }),
tap(id => { this.store.setActive(id); })
); I get the correct and expected output of 2. is the store updated from any other place?No. As said, with a new project you can still do a simple search. 3. are there .subscribe side effects?Since I just bind the result to a template, pipe it through submodule/component.ts this.siteSetting$ = this.siteSettingsQuery.selectActive();
this.siteSetting$.subscribe({
next: value => console.log('submodule: ', value),
}); submodule/component.html <p>current: {{ siteSetting$ | async | json }}</p> app.component.ts this.siteSettingsQuery.selectActive().subscribe({
next: value => console.log('app:', value),
}); why undefined?Since it's the default value. If you change the entityStore's default active value in it's constructor, that is the value that you're going to get as the last, incorrect value from the observable. So changing it to foo you end up with: workaroundWell filtering undefined is not really an option for me, since my activeEntityId can be undefined. This is expected if nothing is selected. So I introduced three 'states':
Then I defined a custom query that replaces the inherited public selectActive$ = this.select('active').pipe(
filter(value => value !== null)
); And last but not least you cannot set the activeId to undefined (again, undefined is expected in my project if the user selects nothing). I mean you can... it just won't update the state and doesn't throw an error or anything 🙃. Oh and that only doesn't work with the provided Anyhow, the pipe that set's the activeId then also needs to be changed accordingly. .pipe(
tap(id => { console.log('store setting active id:', id); }),
tap(id => { this.store.update({active: id}); })
); Success ✨ I might setup a minimal project on stackblitz to reproduce it, as soon as I find time to do it. However the project is brand new and unless there are side effects I don't see (possible), it should be trivial to reproduce. Mostly I put this here for people that encounter the same thing and a possible workaround. |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing a weird behavior and I can not find out through what it is caused.
I'm using a factory provider to get an active id.
I import the provider in AppModule and the weird thing is the output:
undefined -> activeId -> undefined
But when undefined is emitted (3rd value) and I check the store value, the activeId is still the one that has been emitted before.
This only happens, when I'm using the query in a factory. When I subscribe in app module or in app.component, I get undefined and then the activeId and no 3rd emission.
The activeId gets set in an auth guard. Once the app is running everything works as expected.
If after the filter(l => !l), I introduce an observeOn(asyncScheduler), everything works as expected.
This is not a problem, because I can just filter the undefined values, because I don't need them. But I would really like to know what is going on.
Regards
Benny
Beta Was this translation helpful? Give feedback.
All reactions