从 Rxjs WebSocket 主题调用返回可观察数组

Returning Observable Array from Rxjs WebSocket Subject call

尝试 return 来自 rxjs6 Websocket Subject 调用的 Observable。难以理解订阅的异步性质。代码还会过滤到 returned 信封中以提取 Product[]。

我理解当订阅者检索结果并路由到新的 angular 页面时进行 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__);
}

我想return以异步方式得到结果,这样应用程序就可以在 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
        );
    }

我发现我可以 return pipe() 调用的结果。这使我能够从信封->文档中检索产品[]以获得结果。