protected bind()

in src/lib/services/AsyncApi.ts [167:207]


  protected bind(name: string) {
    const { query: entityQuery, data, error, pending } = this.entities[name];

    merge(...[data.future$, error.future$]).subscribe(() =>
      pending.dispatch(false)
    );

    entityQuery.future$.subscribe(() => pending.dispatch(true));

    entityQuery.future$
      .pipe(
        /** filter out queries that are empty */
        filter(query => {
          if (Object.keys(query).length > 0) {
            /** only proceed if non-empty object */
            return true;
          } else {
            /** otherwise clear the output */
            data.clearValue();
            return false;
          }
        }),
        switchMap(query =>
          /** make the API call */
          this.getData(query).pipe(
            /** dispatch the results */
            tap(returnData => {
              data.dispatch(returnData.data)
            }),
            /** handle any errors */
            catchError(err => {
              /** dispatch the error */
              error.dispatch(err.response.data);
              /** don't let the parent pipe die */
              return EMPTY;
            })
          )
        )
        /** activate the stream */
      ).subscribe()
  }