[英]Returning Observable Array from Rxjs WebSocket Subject call
尝试从rxjs6 Websocket主题调用返回Observable。 难以理解订阅的异步性质。 代码还会过滤到返回的信封中以提取Product []。
我理解当订户检索结果并为新的角度页面路由时进行websocket调用。
getAllProducts() : Observable<Product[]> {
let document: GetAllDocument = new GetAllDocument().init();
let envelope =
new AuctionEnvelope().init(AuctionMethods.getAll,document);
this.ws.subject.next(envelope); // call server
let result__: Product[] = [];
this.ws.subject.asObservable().subscribe( (resp: AuctionEnvelope) => {
pipe(
filter((resp)=>resp.method===AuctionMethods.getAllResponse,
map((resp:AuctionEnvelope) =>resp.getAllResponseDocument.result),
map((products: Product[]) => this.result__ = products)
);
}
);
return from(this.result__);
}
我想以异步方式返回结果,以便应用程序可以在使用Observable时获得结果。
getAllProducts() : Observable<Product[]> {
let document: GetAllDocument_V1 = new GetAllDocument_V1().init();
let envelope = new AuctionEnvelope_V1().init(AuctionMethods.getAll_1, document);
this.ws.subject.next(envelope); // call server
return this.ws.subject.asObservable().pipe(
filter( (resp: AuctionEnvelope_V1) => resp.method === AuctionMethods.getAllResponse_1 ), // mine
map((resp: AuctionEnvelope_V1) => resp.getAllResponseDocument_V1.result) // get result from envelope
);
}
我发现我可以返回pipe()调用的结果。 这允许我从envelope->文档中检索product []以获得结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.