Timbo773
Timbo773

Reputation: 173

Angular/RxJS - Converting a promise and inner observable to one single observable to be returned

I have a promise that, once it's fulfilled, creates and subscribes to an observable. What I need to do is instead convert this promise to an observable and return this observable for subscription elsewhere (which would include the inner observable). I used from to convert the promise to an observable and then used concatMap to chain the inner observable. However, at the beginning of the concatMap, I'm getting an error in my code for the xfdfString string variable that I'm trying to retrieve from the initial promise. Perhaps my new function's inner observable is not actually returning an observable itself? I've tried a few things to resolve this with no luck, so any ideas would be much appreciated.

Error:

Argument of type '(xfdfString: string) => void' is not assignable to parameter of type '(value: string, index: number) => ObservableInput<any>'.
  Type 'void' is not assignable to type 'ObservableInput<any>'.ts(2345)

Original function:

  save() {
    const { annotManager } = this.wvInstance;
    annotManager.exportAnnotations({ links: false, widgets: false }).then(xfdfString => {
      const requestId = (<SignatureDocumentData>this.task.actionData).requestId;
      const data: SignDocument = { encodedDocument: encodeBase64(xfdfString) };

      this.documentService.signDocument(requestId, data)
        .pipe(
          switchMap(signResponse => this.workflowService.completeAction(this.task.id)),
          switchMap(nextTask => {
            if (!nextTask) {
              return this.workflowService.completeFolder();
            } else {
              return observableOf(nextTask);
            }
          }),
        ).subscribe(response => console.log(response));
    });
  }

My attempt at using a higher-order observable to instead return one observable:

save(): Observable<any> {
  const { annotManager } = this.wvInstance;

  const docViewerobservable = from(annotManager.exportAnnotations({ links: false, widgets: false }));

  return docViewerobservable.pipe(
    concatMap(xfdfString => {
      const requestId = (<SignatureDocumentData>this.task.actionData).requestId;
      let data = { encodedDocument: encodeBase64(xfdfString) };
      

      this.documentService.signDocument(requestId, data)
      .pipe(
        switchMap(signResponse => this.workflowService.completeAction(this.task.id)),
        switchMap(nextTask => {
          if (!nextTask) {
            return this.workflowService.completeFolder();
          } else {
            return observableOf(nextTask);
          }
        })
      );
    })
  );
}

Upvotes: 0

Views: 150

Answers (1)

ggradnig
ggradnig

Reputation: 14149

I think you just need to return the Observable that you've created inside concatMap ;-)

save(): Observable<any> {
  const { annotManager } = this.wvInstance;

  const docViewerobservable = from(annotManager.exportAnnotations({ links: false, widgets: false }));

  return docViewerobservable.pipe(
    concatMap(xfdfString => {
      const requestId = (<SignatureDocumentData>this.task.actionData).requestId;
      let data = { encodedDocument: encodeBase64(xfdfString) };
      

      return this.documentService.signDocument(requestId, data)
      .pipe(
        switchMap(signResponse => this.workflowService.completeAction(this.task.id)),
        switchMap(nextTask => {
          if (!nextTask) {
            return this.workflowService.completeFolder();
          } else {
            return observableOf(nextTask);
          }
        })
      );
    })
  );
}

Otherwise, it looks good!

Upvotes: 1

Related Questions