How to pass results between chained observables

前端 未结 7 1931
一整个雨季
一整个雨季 2020-12-29 04:03

Abstract problem: Every time a source Observable emits and event, a sequence of API calls and Angular services need to be triggered. Some of those invocatio

7条回答
  •  春和景丽
    2020-12-29 04:21

    Could you use an object for the set of data? Something like this:

    Interface:

    export interface Packet {
      event: string;
      headers?: string;
      id?: number;
      pdfId?: number;
      cloudId?: number;
    }
    

    Then in the code, something like this:

    Service:

      this.startUploadEvent$.pipe(
        concatMap(packet => this.doThingOne(packet)),
        map(packet => this.doThingTwo(packet)),
        tap(packet => this.doThingThree(packet)),
        // ...
      );
    

    That way each method can use the bits of the object it needs and pass along the rest. Though this does require changing each of the methods to take in and work with the object.

提交回复
热议问题