distolma / storeon-observable

Module for storeon which allows to create async actions
MIT License
5 stars 0 forks source link

state$.value returns empty object #60

Closed baloghbence0915 closed 3 years ago

baloghbence0915 commented 3 years ago

Hi Guys,

I have created a short snippet, to describe issue: https://gist.github.com/baloghbence0915/41a7060bb876b3a0a982f49641811ad3

I continously receive logs like: {} It seems like the value of state observable stuck in the initialization state.

Could you help me on this please?

Thanks in advance, Bence

distolma commented 3 years ago

Hi @baloghbence0915,

Thanks for the snippet, which describes the problem. As I understand it, you want to get state value on after event dispatched, but you do it not in a reactive way :)

The state$ argument of an epic is not actually the state of the store. It is a reactive representative of the store. That's why you can't get the value directly. You have to subscribe to the state$

Here is a modyfied verson of your snippet:

export default createStoreon([
  createEpicModule((event$, state$) =>
    event$.pipe(
      mergeMap((event) => {
-       console.log(state$.value);
+       state$.subscribe(state => console.log(state.value))
        return empty();
      })
    )
  ),
  todoModule,
  pingPongModule
  storeonLogger
]);
baloghbence0915 commented 3 years ago

Hey @distolma,

Thanks for the answer!

I'm familiar with this way, but it is not luckiest when I want to reach some data from state and then init an API call by that. (This exmaple is not the full way, how I use epics, just a primitive one, in order to illustrate the root of my problem)

Wouldn't be the purpose to achive this on: https://github.com/distolma/storeon-observable/blob/master/src/StateObservable.ts#L11-L13

According to this, I suppose the value of '_value' should be the latest state that has been dispatched by '@ changed' event. Shouldn't it?