最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I use a withLatestFrom in an effect with selector with props (MemoizedSelectorWithProps) coming from the act

programmeradmin0浏览0评论

I have a selector with props (of type MemoizedSelectorWithProps). I'd like to use it in an effect inside WithLatestFrom. The thing is - the parameter for the selector (the props) is ing from the action payload. And I can't make the withLatestFrom access the action payload.

I'm using angular 7 (and naturally ngrx7). I've tried using a map to somehow create a new observable, but nothing is working...

these are some demo lines I've wrote here, to simplify my use case:

action:

export const GET_INVENTORY = '[App] Get Inventory';

export class GetInventory implements Action {
  readonly type = GET_INVENTORY;
  constructor (public branchId: number) {} 
}

effect:

@Effect()
getInventory$ = this.actions$.pipe(
  ofType(GET_INVENTORY)
  withLatestFrom(this.store$.pipe(select(getIsStoreInventoryLoaded, {branchId: action.branchId}))), // this is not working obviously, since action is unknown
  switchMap([action, loaded]: [GetInventory, boolean] => {
  if (loaded) {
    console.log('already loaded inventory for this branch', action.branchId);
  } else {
    console.log('never loaded inventory for this branch', action.branchId);
  }
}

although this is a simplified version of my code, the design is kinda similar in my real project - I have a store with keys per "branch" inventory. say I'm a chain of supermarkets and each branch has it's own inventory page with lots of data and I want to not-fetch again if I already fetched. So if you have a different approach then working with MemoizedSelectorWithProps - feel free to suggest that too.

I have a selector with props (of type MemoizedSelectorWithProps). I'd like to use it in an effect inside WithLatestFrom. The thing is - the parameter for the selector (the props) is ing from the action payload. And I can't make the withLatestFrom access the action payload.

I'm using angular 7 (and naturally ngrx7). I've tried using a map to somehow create a new observable, but nothing is working...

these are some demo lines I've wrote here, to simplify my use case:

action:

export const GET_INVENTORY = '[App] Get Inventory';

export class GetInventory implements Action {
  readonly type = GET_INVENTORY;
  constructor (public branchId: number) {} 
}

effect:

@Effect()
getInventory$ = this.actions$.pipe(
  ofType(GET_INVENTORY)
  withLatestFrom(this.store$.pipe(select(getIsStoreInventoryLoaded, {branchId: action.branchId}))), // this is not working obviously, since action is unknown
  switchMap([action, loaded]: [GetInventory, boolean] => {
  if (loaded) {
    console.log('already loaded inventory for this branch', action.branchId);
  } else {
    console.log('never loaded inventory for this branch', action.branchId);
  }
}

although this is a simplified version of my code, the design is kinda similar in my real project - I have a store with keys per "branch" inventory. say I'm a chain of supermarkets and each branch has it's own inventory page with lots of data and I want to not-fetch again if I already fetched. So if you have a different approach then working with MemoizedSelectorWithProps - feel free to suggest that too.

Share Improve this question edited Aug 21, 2019 at 4:50 Tony 20.2k7 gold badges41 silver badges62 bronze badges asked Aug 20, 2019 at 15:01 Eyal ElkevityEyal Elkevity 692 silver badges8 bronze badges 2
  • Possible duplicate of NGRX Effects how to pass parameter to withLatestFrom operator – voll Commented Aug 20, 2019 at 21:03
  • Can you show your selector – Tony Commented Aug 21, 2019 at 4:49
Add a ment  | 

1 Answer 1

Reset to default 9

A simple switchMap or a mergeMap with a bineLatest should do the trick.

For example:

@Effect()
getInventory$ = this.actions$.pipe(
  ofType(GET_INVENTORY),
  mergeMap(action =>
    bineLatest(
      of(action),
      this.store$.pipe(select(getIsStoreInventoryLoaded, {branchId: action.branchId}))
    )
  ),
  tap(([action, loaded]) => {
    // The rest of your code...
  })
)
发布评论

评论列表(0)

  1. 暂无评论