我正在为Angular应用编写导航组件。我有以下代码。我想避免多重订阅反模式。我在为RxJs语法和哪种方式(forkJoin,mergeMap等)而苦苦挣扎。
如何重构它们,以删除订阅中的订阅。
这是我所拥有的,目前可以使用,但是在订阅中有一个订阅:
@Component({
selector: 'ehrcc-nav',
templateUrl: './nav.component.html',
styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit {
applicationName: string = 'AppName';
userDisplayName: string = '';
isAuthorizedUser: boolean = false;
isAdminUser: boolean = false;
groupsList: MemberGroup[] = [];
constructor(private userService:UserService,
private auditService: UserAuditService,
private router: Router) { }
ngOnInit() {
this.getDisplayName();
this.userService.getGroupMembershipsForUser().subscribe(members =>{
this.groupsList = members;
for (let g of this.groupsList){
if (g.id === this.userService.usersGroupId){
this.isAuthorizedUser = true;
this.router.navigate(['/workItem']);
}
if (g.id === this.userService.adminGroupId){
this.isAdminUser = true;
}
}
this.logUserInfo(); <---- ANTI-PATTERN
});
}
getDisplayName(){
this.userService.getSignedInAzureADUser().subscribe(
(user) => this.userDisplayName = user.displayName,
(error: any) => {
return console.log(' Error: ' + JSON.stringify(<any>error));
});
}
logUserInfo(){
var audit = new UserAudit();
audit.Application = this.applicationName;
audit.Environment = "UI";
audit.EventType= "Authorization";
audit.UserId = this.userDisplayName;
audit.Details = ` User Is Authorized: ${this.isAuthorizedUser}, User Is Admin: ${this.isAdminUser}`;
this.auditService.logUserInfo(audit)
.subscribe({
next: (id)=> console.log('Id created: '+ id),
error: (error: any) => console.log(' Error: ' + JSON.stringify(<any>error) )
});
}
}
答案 0 :(得分:1)
您可以使用forkJoin https://www.learnrxjs.io/operators/combination/forkjoin.html
forkJoin({
displayName: this.userService.getSignedInAzureADUser() // This will give you the
observable subscription value,
groupMemberShip:this.userService.getGroupMembershipsForUser()
})
一旦订阅了forkJoin,您将获得一个包含所有值的对象,并且可以从中调用logUserInfo,所有可观察对象都需要complete()才能发出forkJoin
答案 1 :(得分:1)
我找到了这个问题,是因为我正在搜索为什么某些import spark.implicits._
val inDf: DataFrame = spark.table("table_name")
val res: Array[DataFrame] = inDf.select($"Col_1")
.distinct()
.collect().map(_.get(0))
.par //optional to parallel execute
.map { col1 =>
inDf.filter($"Col_1" === col1)
.select($"Col_2")
}.toArray
代码块没有明显的理由被多次执行。
我发现这是由于服务返回了一些 Observables ,这些服务尚未加载正确的数据。因此,我用 auditTime 解决了这个问题,该问题:
忽略源值
subscribe
毫秒,然后从源 Observable 发出最新值。
因此,使用问题的代码,可以为此操作符添加 pipe :
duration