尝试从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时可以获取结果。
答案 0 :(得分:0)
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()调用的结果。这使我可以从信封->文档内部检索product []以获得结果。