Can not select the latest ID from store, withLatestFrom returns previous ID and not last, why?

Viewed 176

I am trying to create a master/detail page and I need the master id to retrieve the related records of the detail list.

I have a selector

export const selectRecords= (state: CoreState) => state.records;

export const getId = createSelector(selectRecords,
    (state: myState) => state && state.selectedRecords && state.selectedRecords.id
);

and in my effect I am trying to get the latest id from the store with something like:

@Effect()
    getSomething$ = this.actions$.pipe(
        ofType<GetMyRecords>(MyType.GET_RECORDS),
        withLatestFrom(this.store.select(getId)),
        switchMap(([action, id]) => {
            this.log.debug('Get ID', id);
            ... calling my service here
        })
    );

but even while I am using withLatestFrom, what I am getting is the previous ID and not the latest.

When I am checking the console from Redux, the selectedRecords.id in the state is the correct one (the latest), the one I would like to retrieve.

I know it's a longshot, but does anyone have a tip or advice what I could try to get the correct last id from the store?

1 Answers

I manage to solve the problem by adding a debounceTime(0) before the withLatestFrom:

The update working code is:

@Effect()
    getSomething$ = this.actions$.pipe(
        ofType<GetMyRecords>(MyType.GET_RECORDS),
        debounceTime(0),
        withLatestFrom(this.store.select(getId)),
        switchMap(([action, id]) => {
            this.log.debug('Get ID', id);
            ... calling my service here
        })
    );

and I am adding a link that describes the excact issue I had: https://github.com/ReactiveX/RxSwift/issues/1121

In other words .withLatestFrom(item.asObservable()) can't provide actual latest next event's non nil value, but provide the previous one's nil value!

Related