rxjs 减少不继续
rxjs reduce does not continue
我在 angular 7 应用程序中有以下设置:
@Effect({dispatch:false})
LoadInstances$ = this.actions$.pipe(
ofType(fromAppAction.AppActionTypes.LoadInstances),
take(1), // <-- solved my problem
switchMap((action: fromAppAction.LoadInstances) =>
this.entityService.GetInstances()
),
switchMap(instances=>from(instances)), // flatten the array to single values
flatMap( // combine the instance with its UserData
(inst)=>this.entityService.GetCurrentUserInfo().pipe(take(1)),
(inst,usr)=>({...inst, UserData:usr})
),
flatMap(
(inst)=>this.entityService.GetUserPersonalSettings(inst.id).pipe(take(1)),
(inst,settings)=>({...inst, Settings:settings})
),
tap(result=>console.log('before reduce:',result)), // <-- this gets called 3 times (I have 3 instances)
reduce<Instance>((a, c) => [...a, c], []), // accumulate all results to one array
tap(result=>console.log('instances: ', result)), // <-- this gets never called
...
本质上,我有一个实例数组,将它们展平,为每个实例调用 GetCurrentUserInfo 和 GetUserPersonalSettings,将结果作为 属性 添加到实例中,然后想将它们累积回一个实例[].
到目前为止,这仍然有效,只是 reduce 函数没有继续。我知道这是因为其他 observables 之一没有完成。但这怎么可能呢?
最初的 from(instances) 遍历数组然后应该完成,是吗?根据文档,对 GetPersonalSettings 的调用只是简单的 httpClient.get() 调用,是在成功调用后完成的单值可观察值。
有人可以帮我吗?
[编辑]
扫描在这里不是一个选项,因为我需要完整的实例数组才能继续。
[Edit2] toArray 与 reduce 有相同的问题,它只在前一个 Observable(s?) 完成时才发布结果。
无论如何:我不想更改此设置,因为它基本上可以正常工作。我只想了解哪个 Observable 没有完成以及原因。这将解决我的问题。
最近我学习了运算符 toArray,可能会简化您的代码。
您的直播未完成,因为第一个 switchMap
之前的直播未完成
/* What is here? Probably still open stream? */
switchMap((action: fromAppAction.LoadInstances) =>
this.entityService.GetInstances()
),
快速尝试将在第一个 switchMap
之前放置 first()
或 take(1)
我在 angular 7 应用程序中有以下设置:
@Effect({dispatch:false})
LoadInstances$ = this.actions$.pipe(
ofType(fromAppAction.AppActionTypes.LoadInstances),
take(1), // <-- solved my problem
switchMap((action: fromAppAction.LoadInstances) =>
this.entityService.GetInstances()
),
switchMap(instances=>from(instances)), // flatten the array to single values
flatMap( // combine the instance with its UserData
(inst)=>this.entityService.GetCurrentUserInfo().pipe(take(1)),
(inst,usr)=>({...inst, UserData:usr})
),
flatMap(
(inst)=>this.entityService.GetUserPersonalSettings(inst.id).pipe(take(1)),
(inst,settings)=>({...inst, Settings:settings})
),
tap(result=>console.log('before reduce:',result)), // <-- this gets called 3 times (I have 3 instances)
reduce<Instance>((a, c) => [...a, c], []), // accumulate all results to one array
tap(result=>console.log('instances: ', result)), // <-- this gets never called
...
本质上,我有一个实例数组,将它们展平,为每个实例调用 GetCurrentUserInfo 和 GetUserPersonalSettings,将结果作为 属性 添加到实例中,然后想将它们累积回一个实例[].
到目前为止,这仍然有效,只是 reduce 函数没有继续。我知道这是因为其他 observables 之一没有完成。但这怎么可能呢? 最初的 from(instances) 遍历数组然后应该完成,是吗?根据文档,对 GetPersonalSettings 的调用只是简单的 httpClient.get() 调用,是在成功调用后完成的单值可观察值。
有人可以帮我吗?
[编辑] 扫描在这里不是一个选项,因为我需要完整的实例数组才能继续。 [Edit2] toArray 与 reduce 有相同的问题,它只在前一个 Observable(s?) 完成时才发布结果。
无论如何:我不想更改此设置,因为它基本上可以正常工作。我只想了解哪个 Observable 没有完成以及原因。这将解决我的问题。
最近我学习了运算符 toArray,可能会简化您的代码。
您的直播未完成,因为第一个 switchMap
之前的直播未完成
/* What is here? Probably still open stream? */
switchMap((action: fromAppAction.LoadInstances) =>
this.entityService.GetInstances()
),
快速尝试将在第一个 switchMap
first()
或 take(1)