Angular 向处理可观察流的函数添加布尔变量
我有一个函数,它接受线程的参数ID。它处理线程流以确定此ID是否与线程匹配Angular 向处理可观察流的函数添加布尔变量,angular,typescript,rxjs,Angular,Typescript,Rxjs,我有一个函数,它接受线程的参数ID。它处理线程流以确定此ID是否与线程匹配 getThreadFromSubscription(threadId: string): Observable<Thread> { return this.threads .filter((threadDictionary: { [key: string]: Thread }) => { return Object.keys(threadDictionary).some((key) =>
getThreadFromSubscription(threadId: string): Observable<Thread> {
return this.threads
.filter((threadDictionary: { [key: string]: Thread }) => {
return Object.keys(threadDictionary).some((key) =>
threadDictionary[key].id === threadId);
})
.map((threadDictionary: { [key: string]: Thread }) => {
for (let key in threadDictionary) {
if (threadDictionary[key].id === threadId)
{
return threadDictionary[key];
}
}
}).first();
}
我想向这个函数添加一个布尔变量,以便知道相应的线程是否存在。
如果相应的线程不存在,“newThread”变量将为true。
我尝试过这个,但是当threadID不存在时,它就不起作用了。我的布尔变量“newThread”未设置为true:
addNewMessage(objMessage: any) : void {
this.getThreadFromSubscription(objMessage.id)
.subscribe((thread: Thread) => {
if(thread !== undefined) {
this.newThread = false;
}
else {
this.newThread = true;
}
});
if(this.newThread === false )
{
this.getThreadFromSubscription(objMessage.id)
.subscribe ((thread: Thread) => {
console.log("id: tRev");
if(thread!== undefined) {
objMessage.thread = thread;
}
objMessage.date = moment().toDate();
const newMessage = new Message(objMessage);
thread.messages = Observable.of([newMessage]);
thread.messages.last().subscribe((message) => {
[thread.lastMessage] = message;
});
this.addMessage(newMessage);
});
}
else
{
console.log("id: flo");
const flo: User = new User(objMessage.author, objMessage.site);
const newThread: Thread = new Thread(objMessage.id, [flo]);
const newMessage = new Message(objMessage);
objMessage.date = moment().toDate();
newThread.messages = Observable.of([newMessage]);
newThread.messages.last().subscribe((message) => {
[newThread.lastMessage] = message;
});
objMessage.thread = newThread;
this.addMessage(newMessage);
this.addThread(newThread);
}
}
您知道如何添加一个变量来判断给定的id是否已经存在吗
addNewMessage(objMessage: any) : void {
this.getThreadFromSubscription(objMessage.id)
.subscribe((thread: Thread) => {
if(thread !== undefined) {
this.newThread = false;
}
else {
this.newThread = true;
}
});
if(this.newThread === false )
{
this.getThreadFromSubscription(objMessage.id)
.subscribe ((thread: Thread) => {
console.log("id: tRev");
if(thread!== undefined) {
objMessage.thread = thread;
}
objMessage.date = moment().toDate();
const newMessage = new Message(objMessage);
thread.messages = Observable.of([newMessage]);
thread.messages.last().subscribe((message) => {
[thread.lastMessage] = message;
});
this.addMessage(newMessage);
});
}
else
{
console.log("id: flo");
const flo: User = new User(objMessage.author, objMessage.site);
const newThread: Thread = new Thread(objMessage.id, [flo]);
const newMessage = new Message(objMessage);
objMessage.date = moment().toDate();
newThread.messages = Observable.of([newMessage]);
newThread.messages.last().subscribe((message) => {
[newThread.lastMessage] = message;
});
objMessage.thread = newThread;
this.addMessage(newMessage);
this.addThread(newThread);
}
}