Rxjs:在 return promise/observable 函数中使用可观察值的值
Rxjs: use values of observables in function that return promise/observable
所以我有 return Promise 的链式函数,并且在参数中我需要使用可观察值。看代码中的描述就明白了。我想我应该使用一些 rxjs 运算符以反应式编程方式编写这段代码?但我现在不知道具体该怎么做,该使用哪一个。
interface User {
email: string;
conversations: string[] | FieldValue;
userId: string;
}
@Injectable({
providedIn: 'root',
})
export class ConversationsClientService {
getUserByEmail(email: string): Observable<any> {
// should return Observable<User[]> but this function return type is Observable<unknown[]> and i can't use this type, but thats not the main issue
return this.firestore
.collection('users', (ref) => ref.where('email', '==', email))
.valueChanges();
}
startNewConversation(
currentUserCredential: Observable<firebase.auth.UserCredential>, //that comes from ngrx selector
email: string
): Promise<void> {
//or return observable by wrapping promise with from() operator
return this.firestore
.doc<User>(
'users/' + 'there should be .user.uid field of value of observable currentUserCredential'
)
.update({
conversations: arrayUnion(
'there should be [0].userId field of value of observable getUserByEmail(email)'
),
});
}
constructor(private firestore: AngularFirestore) {}
}
我建议使用 from
运算符而不是 Promise,并将整个操作建模为 RxJs 流。
一个选项是:
startNewConversation(
currentUserCredential: Observable<firebase.auth.UserCredential>, //that comes from ngrx selector
email: string
): Promise<void> {
/* combineLatest will emit an array with the most recent emitted values of the combined observables */
return combineLatest([currentUserCredential, this.getUserByEmail(email)]).pipe(
switchMap((currentUserAndUserByEmail) => {
return from(this.firestore
.doc<User>(
'users/' + currentUserAndUserByEmail[0].user.uid,
)
.update({
conversations: arrayUnion(
currentUserAndUserByEmail[1][0].userId
),
}))
} )
)
}
所以我有 return Promise 的链式函数,并且在参数中我需要使用可观察值。看代码中的描述就明白了。我想我应该使用一些 rxjs 运算符以反应式编程方式编写这段代码?但我现在不知道具体该怎么做,该使用哪一个。
interface User {
email: string;
conversations: string[] | FieldValue;
userId: string;
}
@Injectable({
providedIn: 'root',
})
export class ConversationsClientService {
getUserByEmail(email: string): Observable<any> {
// should return Observable<User[]> but this function return type is Observable<unknown[]> and i can't use this type, but thats not the main issue
return this.firestore
.collection('users', (ref) => ref.where('email', '==', email))
.valueChanges();
}
startNewConversation(
currentUserCredential: Observable<firebase.auth.UserCredential>, //that comes from ngrx selector
email: string
): Promise<void> {
//or return observable by wrapping promise with from() operator
return this.firestore
.doc<User>(
'users/' + 'there should be .user.uid field of value of observable currentUserCredential'
)
.update({
conversations: arrayUnion(
'there should be [0].userId field of value of observable getUserByEmail(email)'
),
});
}
constructor(private firestore: AngularFirestore) {}
}
我建议使用 from
运算符而不是 Promise,并将整个操作建模为 RxJs 流。
一个选项是:
startNewConversation(
currentUserCredential: Observable<firebase.auth.UserCredential>, //that comes from ngrx selector
email: string
): Promise<void> {
/* combineLatest will emit an array with the most recent emitted values of the combined observables */
return combineLatest([currentUserCredential, this.getUserByEmail(email)]).pipe(
switchMap((currentUserAndUserByEmail) => {
return from(this.firestore
.doc<User>(
'users/' + currentUserAndUserByEmail[0].user.uid,
)
.update({
conversations: arrayUnion(
currentUserAndUserByEmail[1][0].userId
),
}))
} )
)
}