Home > OS >  How to I interrogate a forkjoin using an Observable?
How to I interrogate a forkjoin using an Observable?

Time:08-20

I am calling a forkjoin to check for the presence of a person on two different independent data stores. I want to take action (perform a delete) when it is not found on either. When the action occurs it should return true, otherwise false. However as I have coded it up, the subscribe in the calling method does not get signaled when the true fires.

How do I restructure the method and how it is called so that it receives the 'true' signal?

This is the calling code (at the component level):

    deletePerson(person: PersonMasterBase) {
        this.confirmationService.confirm({
            message: 'Are you sure you want to delete person: '   person.p_PersonID   '?',
            header: 'Confirm',
            icon: 'pi pi-exclamation-triangle',
            accept: () => {
                this.model.deletePerson(person.p_PersonID, this.persons).subscribe((result) => 
                {
                    if (result)
                    {
                        this.persons = this.persons.filter(val => val.p_PersonID !== person.p_PersonID);
                        this.person = new PersonMasterBase;
                        this.isChanged = true;
                        this.messageService.add({severity:'success', summary: 'Successful', detail: 'Person Deleted', life: 3000});
                    }
               });
            }
        });
    }

This is the method in question (In the repository):

    deletePerson(id: number, allPersons : PersonMasterBase[]): Observable<boolean> {
        let personISPassenger: Object;
        let personISUser : Object;
        let passengerCheck = this.dataSource.getEntryInUseById<PassengerMasterBase[]>("/Passenger/PersonInUse", "PersonID", id);
        let userCheck = this.dataSource.getEntryInUseById<UserMasterBase[]>("/User/PersonInUse", "PersonID", id);


        forkJoin([passengerCheck, userCheck]).subscribe(results => {
            personISPassenger= results[0];
            personISUser= results[1];
            let logString: string = `Person ${id} is in use as a passenger or a user.`;
            if (personISUser || personISPassenger)
            {
                this.dialogService.openModalOk("Delete Person Rejected",logString, ()=>{
                //confirmed
                    console.log(logString);
                });
                return false;
            }
            else
            {
                let index = allPersons.findIndex(p => this.locator(p, id));
                if (index > -1) {
                    this.deletedPersons.push(allPersons[index]);
                    allPersons.splice(index, 1);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        });

        return of(false);
    }

CodePudding user response:

In the subscribe of the forkJoin you create an unnamed arrow function which is executed when both passengerCheck and userCheck complete. In this function you have various returns. These returns are for the unnamed arrow function and not the deletePerson function.

So basically, nothing happens in there. What you have to do is return the Observable that is returned by the forkJoin after you map the results to a boolean:

deletePerson(id: number, allPersons : PersonMasterBase[]): Observable<boolean> {
    let personISPassenger: Object;
    let personISUser : Object;
    let passengerCheck = this.dataSource.getEntryInUseById<PassengerMasterBase[]>("/Passenger/PersonInUse", "PersonID", id);
    let userCheck = this.dataSource.getEntryInUseById<UserMasterBase[]>("/User/PersonInUse", "PersonID", id);


    return forkJoin([passengerCheck, userCheck]).pipe(map((results => {
        personISPassenger= results[0];
        personISUser= results[1];
        let logString: string = `Person ${id} is in use as a passenger or a user.`;
        if (personISUser || personISPassenger)
        {
            this.dialogService.openModalOk("Delete Person Rejected",logString, ()=>{
            //confirmed
                console.log(logString);
            });
            return false;
        }
        else
        {
            let index = allPersons.findIndex(p => this.locator(p, id));
            if (index > -1) {
                this.deletedPersons.push(allPersons[index]);
                allPersons.splice(index, 1);
                return true;
            }
            else
            {
                return false;
            }
        }
    }));
}
  • Related