Skip to content
This repository has been archived by the owner on Jan 10, 2018. It is now read-only.

state snapshot #343

Closed
ghetolay opened this issue Feb 26, 2017 · 8 comments
Closed

state snapshot #343

ghetolay opened this issue Feb 26, 2017 · 8 comments

Comments

@ghetolay
Copy link

ghetolay commented Feb 26, 2017

Sometimes we need to simply access the current state and not react to it.
To do so, we still have to use observable like :

this.store.select( selector )
  .take(1)
  .subscribe( ... );

or in effects :

this.actions$
  .withLatestFrom( this.store.select( selector ) )

But it really seems like doing complicated things to achieve a simple task.
Would it be really bad if we could access the state directly ??

I've created an hacky getStateSnapshot( selector ) in some of my effects to experiment and didn't come into any trouble yet.

@fxck
Copy link

fxck commented Feb 26, 2017

The thing is.. usually when you need a state snapshot, you are doing something "wrong". So what is the use case here?

There used to be getState(), getValue(), and value, but it was removed, so yeah, currently the only way is to do a subscribe.

@ghetolay
Copy link
Author

You're scaring me, I'm doing this a lot on effects !!.

Usually when my actions refer to an entity in my state the payload is the entity id like :

SelectItemAction(payload/* id*/: string)

and on my effects I need the item instance so I do :

actions$.ofType(SELECT_ITEM)
  .map(toPayload)
  .withLatestFrom( this.store.select( getItems ) )
  .map( ([id, items]) => items[id] )

I think I could also do :

actions$.ofType(SELECT_ITEM)
  .withLatestFrom( () => this.store.select( getSelectedItem ) )

But it feels weird and It's still accessing the state.

@fxck
Copy link

fxck commented Feb 28, 2017

Doing it in effects the reactive way is perfectly fine. The "problem" would be if you wanted to do something like..

http.post('/foo', state.value.people['3'].name); // accessing the "sync" state value

.. since there are better ways to handle this

@ghetolay
Copy link
Author

ghetolay commented Feb 28, 2017

What's wrong with accessing the state value ?

Because I've replaced

actions$.ofType(SELECT_ITEM)
  .map(toPayload)
  .withLatestFrom( this.store.select( getItems ) )
  .map( ([id, items]) => items[id] )

with

actions$.ofType(SELECT_ITEM)
  .map(toPayload)
  .map( id => this.getStateSnapshot( getItems )[id] )

in some of my effects and it seems to work good.
I can't see what could go wrong here.

@karolmie1
Copy link

@fxck What are those better ways? Can you show me where I should look for them?
Sometimes I want to have a snapshot, and sometimes I want a subscription.
It would be great if such native function existed.
If it's hard to do, simple alias for select(...).take(1) would be nice.

@AnianZ
Copy link
Contributor

AnianZ commented Mar 13, 2017

For me the only time I need a snapshot is in my logging service. If an error occurs I send a Request with the current state and the error message back to the server. It would be convenient to have something shorter as select(...).take(1) at this moment but it really doesn't hurt much. After all, that's only one case where I'd need it.

@sliekens
Copy link

sliekens commented May 8, 2017

I don't buy into the blanket statement that accessing the current state in a non-reactive manner is always bad. I know my code and at some points in my code I know that the store already contains the data that I need to retrieve. I also don't care if the data is subject to (frequent) changes. A snapshot is usually enough. That means I don't need to wait for data to become available and I should not need to subscribe to state changes just to get the latest available state.

Please provide some examples of why this is bad, or point me to a commit or issue where the removal of getState()/getValue()/value was discussed.

@robwormald
Copy link
Contributor

Please check this against NgRx v4, and if it’s still an issue, please reopen on https://github.com/ngrx/platform. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants