Angular 角度4&;RxJS:竞赛条件、服务初始化顺序和准备状态

Angular 角度4&;RxJS:竞赛条件、服务初始化顺序和准备状态,angular,rxjs,observable,rxjs5,Angular,Rxjs,Observable,Rxjs5,我想确认我使用的观测值与服务初始化是正确的,或者没有被标准模式所超越。我对可观察事物还不熟悉 假设我有多个服务(例如profileService),这取决于authService的准备情况:一些数据应该只在知道用户后加载 示例profileService仅当身份验证服务发出(observer.next('userConnected'))用户成功通过应用程序身份验证的信号时,才应“获取连接用户的配置文件” 我的profileService如下所示: 构造函数(@Inject(Authenticat

我想确认我使用的观测值与服务初始化是正确的,或者没有被标准模式所超越。我对可观察事物还不熟悉

假设我有多个服务(例如profileService),这取决于authService的准备情况:一些数据应该只在知道用户后加载

示例
profileService
仅当
身份验证服务发出(
observer.next('userConnected')
)用户成功通过应用程序身份验证的信号时,才应“获取连接用户的配置文件”

我的
profileService
如下所示:

构造函数(@Inject(AuthenticationService)private authSvc:AuthenticationService){
this.authSvc.getAuthChangeObservable().subscribe(数据=>this.onAuthenticationChanged(数据));
}
authenticationService
的编写如下:

private authChange:可见;
私人观察者:观察者;
构造函数(@Inject(FirebaseService)FirebaseService:FirebaseService){
//Cookie或任何允许.auth()在页面刷新后识别用户的内容
//这会导致轻微延迟,请参见下面的问题
firebase.auth().onAuthStateChanged(用户=>{
如果(用户)此.onAuthenticationSuccessfull(用户);
否则。onLogoutSuccessfull();
});
//为身份验证状态创建一个可观察的
this.authChange=新的可观察对象(observer=>{
这个。_authObserver=观察者;
}).share();
}
getAuthChangeObservable(){
返回此.authChange;
}
...
onAuthenticationSuccessfull(用户){
this.authenticatedUser=用户;
//这主要是我发现的警告其他服务的方式,它们可以加载
//与用户相关的数据
这个。_authObserver.next('auth');
}
...
困扰我的是:如果认证没有延迟怎么办?或者,对于更通用的服务,如何确保依赖于它的所有服务都已准备就绪并订阅了可观察的服务

我知道observable.share()存在竞争条件。除非出错,否则只要至少调用一次share(),就会触发第一个“挂起”事件。我需要调用.share(),因为我需要一个“热”可观察对象(即,不要重新运行可观察对象的观察者
.next()
例程)


提前感谢您分享建议和技巧。

您可以使用Subject来简化代码,它是一个热门的可观察对象,默认情况下它不会缓存过去的事件(您描述的共享行为更像BehaviorSubject)

在您的authenticationService中

public onAuth=new Subject()
constructor(@Inject(FirebaseService) firebaseService: FirebaseService){

firebase.auth().onAuthStateChanged(user => {
  if(user) this.onAuth.next(user);
  else this.onLogoutSuccessfull();
})
在您的个人资料服务中

constructor(@Inject(AuthenticationService) private authSvc: 
AuthenticationService){
// onlogin
    this.authSvc.onAuth.filter(user=>user&&user.id).map(data=>
// you can perform other operation here e.g chain, combine 
).subscribe()

// onlogout
    this.authSvc.onAuth.filter(user=>!user).map(data=>
// you can perform other operation here e.g chain, combine 
).subscribe()

}

真不错。使用BehaviorSubject会有所帮助。让我遵循本教程: