0

I am calling a service regularly after a certain interval of time. I get two numbers as result in the service response,I want to compare those numbers and if they are equal I want to end the service calls. How am i supposed to use the takeUntil() method then?

I have tried to use this.ngXUnsubscribe in takeUntil() which is not ending the service calls over a period of time. ngXUnsubscribe is defined as follows :

protected ngXUnsubscribe: Subject<void> = new Subject<void>();
 const source = timer(1000,60000);
        source.subscribe(()=> {
        this._helper.runStatus(id)
        .pipe(first(),takeUntil())
        .subscribe(response => {

            let xyz = response && response.runScenariosDTO ? response.runScenariosDTO : []; 
            this.passDataToParent(xyz);
            this.progressBarData = this.ScenariosBasedOnTypeDTO.map(scenario => {

                let pBar = response && response.runScenariosDTO ? response.runScenariosDTO.find(barData => barData.scenarioId ? barData.scenarioId === scenario.scenarioId : undefined) : undefined;
                this.showStatusAfterLoad = 1;

                return this.prepareProgressBarData(scenario, pBar);
             });

            });
         });

I want to stop the execution when those two numbers are equal until then I should keep calling the service.

The JSON data that I am getting it is

{
  "runId": 0,
  "runScenariosDTO": [
    {
      "scenarioId": 0,
      "totalDataset": 0,
      "totalExecuted": 0,
      "totalFailed": 0,
      "totalPassed": 0
    }
  ],
  "totalScenarios": 0
}

So, in the array of runScenariosDTO, I want to add all totalExecuted and totalDataset, and after that I want to comparae them.

1 Answers1

0

You should leverage the takeWhile operator (examples).

timer(1000, 60000).pipe(
  switchMap(() => this._helper.runStatus(id)),
  takeWhile(response => {
    const totals = response.runScenariosDTO.map(v => ({ 
       dataset: v.totalDataset, 
       executed: v.totalExecuted 
    }))
    .reduce((sums, v) => ({ 
       dataset: sums.dataset + v.dataset,
       executed: sums.executed + v.executed 
    }), { dataset: 0, executed: 0 });

    return totals.dataset !== totals.executed;
  })
).subscribe(...);
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • sorry, I forgot to mention, in my response I am getting an JSON 2d array. So I want to to find sum of all those variables and then compare them...How to compare them? – laZZySpiDer Aug 22 '19 at 10:44
  • @AdityaPandya post a JSON example. And from which service call? `runStatus`? – LppEdd Aug 22 '19 at 10:48
  • I have updated the question. please have a look at it.@LppEdd – laZZySpiDer Aug 22 '19 at 10:52
  • runScenariosDTO is an array, so I directly cannot access it's property. I want to sum all the totalExecuted and totalDataset and then compare them – laZZySpiDer Aug 22 '19 at 11:01
  • I am shown a error at response.runScenariosDTO,sums.dataset and v.dataset that Object is possibly undefined.What to do? – laZZySpiDer Aug 22 '19 at 11:19
  • @AdityaPandya you need to check if runScenariosDTO is defined, and for the other s create new interfaces. For the rest, the code is ok as I tested it. – LppEdd Aug 22 '19 at 11:34
  • @AdityaPandya do you still need help? – LppEdd Aug 23 '19 at 12:11
  • @AdityaPandya the TS compiler is trying to tell you that before assigning you need to check if those values are undefined. – LppEdd Aug 24 '19 at 15:29