4
votes

some searching on SO leaves me to believe that there is no good (simple) way of achieving the question in the title. The following threads are all very related:

Get current state in ngrx

Getting current state in ngrx

Get current ngrx state without subscribe

Using ngrx to obtain store's current state once

How to get current value of State object with @ngrx/store?

From the last thread ^, it seems that the only way of achieving this is to use withLatestFrom() or combineLatest().

It can't be the case that the following is the only way to make sure you only receive 1 item and that this item is the most recent one:

of('terribleLatestValueHack').pipe(
  withLatestFrom(this.store.select(itemSelector))
  ).subscribe((stringAndItem: [string, Item]) => {
    const [, item] = stringAndItem;
    // do something with item
});

Given that selecting one most recent item from a Store

  • is a (very) simple use-case
  • seems to be a highly requested functionality

I would really like to know why the existing support (apparently there was - in NGRX v2 - according to How to get current value of State object with @ngrx/store?) for it has been removed.

1
May I ask why simply subscribing to this.store.select(...) with a pipe(first()) is not possible? - ggradnig
store suppose to be dynamic and keep emitting no? - Fan Cheung

1 Answers

4
votes

I forgot to add that I was of the impression that store.select(...).pipe(first()) does not return the most recent item from the store. But it seems it does and the cause for any faulty values it returns lies in our own code.

store.select(...).pipe(first()) indeed just does that: return one current item from a Store (and complete the Observable).