Return 来自包含订阅和另一个可观察对象的函数的可观察对象
Return Observable from Function that contains subscription and another observable
我正在尝试获取连接到我的应用程序的打印机的状态。这是功能,我在其中一个页面中有:
checkPrinterStatus() { //I want to this this function into an observable
this.afdb.object('food/settings/printer').snapshotChanges().subscribe((res: any) => {
const printerSettings = res.payload.val();
if(printerSettings.isConnected) {
timer(5000, 3000).pipe(
switchMap(_ => this.getConnPrinterStatus()),
distinctUntilChanged()
).subscribe(async res => {
let printerStatus = res;
return printerStatus; // I want this to be an observable
});
}
});
}
所以基本上,我正在订阅我的 firebase 数据库,它为我提供了打印机是否 connected/set 是否启动的值。如果它是连接的,我正在创建一个轮询,我调用我的 getConnPrinterStatus() returns 如果状态发生变化,我每 3 秒向我发送一次状态。
现在,目前,我的一个页面中有这个函数,但我想将这个函数变成可观察的,这样我就可以把它放在其中一个服务中,然后在每个页面中订阅 printerStatus 值。我怎样才能做到这一点?非常感谢任何帮助或指导。
checkPrinterStatus() { //I want to this this function into an observable
return this.afdb.object('food/settings/printer').snapshotChanges().pipe(
switchMap(res => iif(() => res.payload.val().isConnected, timer(5000, 3000).pipe(
switchMap(_ => this.getConnPrinterStatus()),
distinctUntilChanged()
)
))
);
}
这个函数returns一个可观察的
您可以 return 从函数 checkPrinterStatus
中观察到一个像这样的 -
checkPrinterStatus() {
return this.afdb.object('food/settings/printer').snapshotChanges()
.pipe(
map(res => {
return res.payload.val().isConnected;
}),
filter(isConnected => !!isConnected),
take(1),//use this if you want only 1 value; otherwise remove it
switchMap(() => {
return timer(5000, 3000).pipe(
switchMap(_ => this.getConnPrinterStatus()),
distinctUntilChanged()
)
})
)
}
由于函数 return 是一个可观察对象,调用者可以通过该方法订阅可观察对象 return。
我正在尝试获取连接到我的应用程序的打印机的状态。这是功能,我在其中一个页面中有:
checkPrinterStatus() { //I want to this this function into an observable
this.afdb.object('food/settings/printer').snapshotChanges().subscribe((res: any) => {
const printerSettings = res.payload.val();
if(printerSettings.isConnected) {
timer(5000, 3000).pipe(
switchMap(_ => this.getConnPrinterStatus()),
distinctUntilChanged()
).subscribe(async res => {
let printerStatus = res;
return printerStatus; // I want this to be an observable
});
}
});
}
所以基本上,我正在订阅我的 firebase 数据库,它为我提供了打印机是否 connected/set 是否启动的值。如果它是连接的,我正在创建一个轮询,我调用我的 getConnPrinterStatus() returns 如果状态发生变化,我每 3 秒向我发送一次状态。
现在,目前,我的一个页面中有这个函数,但我想将这个函数变成可观察的,这样我就可以把它放在其中一个服务中,然后在每个页面中订阅 printerStatus 值。我怎样才能做到这一点?非常感谢任何帮助或指导。
checkPrinterStatus() { //I want to this this function into an observable
return this.afdb.object('food/settings/printer').snapshotChanges().pipe(
switchMap(res => iif(() => res.payload.val().isConnected, timer(5000, 3000).pipe(
switchMap(_ => this.getConnPrinterStatus()),
distinctUntilChanged()
)
))
);
}
这个函数returns一个可观察的
您可以 return 从函数 checkPrinterStatus
中观察到一个像这样的 -
checkPrinterStatus() {
return this.afdb.object('food/settings/printer').snapshotChanges()
.pipe(
map(res => {
return res.payload.val().isConnected;
}),
filter(isConnected => !!isConnected),
take(1),//use this if you want only 1 value; otherwise remove it
switchMap(() => {
return timer(5000, 3000).pipe(
switchMap(_ => this.getConnPrinterStatus()),
distinctUntilChanged()
)
})
)
}
由于函数 return 是一个可观察对象,调用者可以通过该方法订阅可观察对象 return。