jeudi 16 septembre 2021

Do I create an Observable from my service when I have to optionally use http Observable or a variable?

I have a service that checks and API endpoint for status data. There are situations where I do not need to to do the API call based on other local data. This check happens in a few different components in the application.

Since I want to encapsulate the logic into the service - Do I have to create a custom Observer to return the variable to the components?

My method isClosed checks some local variables and based on that returns true or does the API call.

isClosed(): Observable<boolean> {
    let o: Observable<boolean>;


    if (myCustomLogic) {
      //do some stuff...
      //this is the api call (http.get) which returns Observable<boolean>
      o = this.checkClosed();

    } else {
      //based on myCustomLogic
      //I dont need to call the api, but I am 
      //assuming i still need an observable???
      o = new Observable((observer) => {
        console.log('Custom observer');
        observer.next(true);

        return {
          unsubscribe(): void {
            //i dont what to do here
          }
        };

      });
    }
    return o;
}

Aucun commentaire:

Enregistrer un commentaire