Je suis à essayer de comprendre quel est le meilleur moyen d'attendre pour de multiples demandes et faire quelque chose quand elles sont faites, gardez à l'esprit que la demande d'un déjeuner dans le même temps, et ils ne devraient pas attendre les uns des autres.
// table filling
public SUB_getActSearch: Subscription;
// table filling 2
public SUB_getDeclarationSearch: Subscription;
public fillTable(): void {
const searchOBJ = {
CreateDateFrom: this.currentDate,
CreateDateTo: this.currentDate,
ActNumber: this.ActNumber == undefined ? '' : this.ActNumber,
vinCode: this.vinCode == undefined ? '' : this.vinCode,
GovNumber: this.GovNumber == undefined ? '' : this.GovNumber,
IsFromDashboard: true
};
const searchOBJ2 = {
ActNumber: this.ActNumber == undefined ? '' : this.ActNumber,
vinCode: this.vinCode == undefined ? '' : this.vinCode,
GovNumber: this.GovNumber == undefined ? '' : this.GovNumber,
};
if ((this.ActNumber && this.ActNumber != undefined && this.ActNumber != null) || (this.vinCode && this.vinCode != undefined && this.vinCode != null) || (this.GovNumber && this.GovNumber != undefined && this.GovNumber != null)) {
this.SUB_getActSearch = this.searchService.getActSearch({ ...searchOBJ2, PageSize: this.pageSize, PageNumber: this.currentPage }).subscribe((res) => {
this.Act_Data = [];
let headerPagin = JSON.parse(res.headers.get('X-Pagination'));
this.elementsLength = headerPagin.totalCount;
this.currentPage = headerPagin.currentPage;
this.pageSize = headerPagin.pageSize;
res.body.map((param) => {
this.Act_Data.push(param);
});
this.ActDataSource = new MatTableDataSource<Element>(this.Act_Data);
// console.log('Your form data : ', this.ActDataSource);
});
} else {
this.SUB_getActSearch = this.searchService.getActSearch({ ...searchOBJ, PageSize: this.pageSize, PageNumber: this.currentPage }).subscribe((res) => {
this.Act_Data = [];
let headerPagin = JSON.parse(res.headers.get('X-Pagination'));
this.elementsLength = headerPagin.totalCount;
this.currentPage = headerPagin.currentPage;
this.pageSize = headerPagin.pageSize;
res.body.map((param) => {
this.Act_Data.push(param);
});
this.ActDataSource = new MatTableDataSource<Element>(this.Act_Data);
// console.log('Your form data : ', this.ActDataSource);
});
}
}
public fillTable2(): void {
const searchOBJ = {
DeclarationCreateDateFrom: this.currentDate.substring(0, 10),
DeclarationCreateDateTo: this.currentDate,
DeclarationNumber: this.ActNumber == undefined ? '' : this.ActNumber,
VinCode: this.vinCode == undefined ? '' : this.vinCode,
TransitNumber: this.GovNumber == undefined ? '' : this.GovNumber,
};
const searchOBJ2 = {
DeclarationNumber: this.ActNumber == undefined ? '' : this.ActNumber,
VinCode: this.vinCode == undefined ? '' : this.vinCode,
TransitNumber: this.GovNumber == undefined ? '' : this.GovNumber,
};
if ((this.ActNumber && this.ActNumber != undefined && this.ActNumber != null) || (this.vinCode && this.vinCode != undefined && this.vinCode != null) || (this.GovNumber && this.GovNumber != undefined && this.GovNumber != null)) {
this.SUB_getDeclarationSearch = this.declarationsService.getDeclarations({ ...searchOBJ, IsFromDashboard: true, PageSize: this.pageSizeDecl, PageNumber: this.currentPageDecl }).subscribe((res) => {
this.Declaration_Data = [];
let headerPaginDecl = JSON.parse(res.headers.get('X-Pagination'));
this.elementsLengthDecl = headerPaginDecl.totalCount;
this.currentPageDecl = headerPaginDecl.currentPage;
this.pageSizeDecl = headerPaginDecl.pageSize;
res.body.map((param) => {
this.Declaration_Data.push(param);
});
this.DeclarationDataSource = new MatTableDataSource<Element>(this.Declaration_Data);
// console.log('Your form data : ', this.DeclarationDataSource);
});
} else {
this.SUB_getDeclarationSearch = this.declarationsService.getDeclarations({ ...searchOBJ, IsFromDashboard: true, PageSize: this.pageSizeDecl, PageNumber: this.currentPageDecl }).subscribe((res) => {
this.Declaration_Data = [];
let headerPaginDecl = JSON.parse(res.headers.get('X-Pagination'));
this.elementsLengthDecl = headerPaginDecl.totalCount;
this.currentPageDecl = headerPaginDecl.currentPage;
this.pageSizeDecl = headerPaginDecl.pageSize;
res.body.map((param) => {
this.Declaration_Data.push(param);
});
this.DeclarationDataSource = new MatTableDataSource<Element>(this.Declaration_Data);
// console.log('Your form data : ', this.DeclarationDataSource);
});
}
}
Il existe deux méthodes qui sont appelées à l'intérieur de ngoninit: fillTable() et fillTable2().
J'ai essayé d'utiliser la méthode suivante pour écouter les abonnements et attendre pour eux
public testingforkJoin(): void {
forkJoin([this.SUB_getActSearch, this.SUB_getDeclarationSearch]
).subscribe(val =>
console.log("xxxxx", val));
// call some method here as soon as both are done
}
Mais comme il s'avère forkJoin ne peux pas écouter les abonnements, est-il un autre moyen semblable à forkjoin pour les abonnements?
Il y sont deux méthodes qui incluent les requêtes HTTP et je veux attendre les deux demandes de réseau de compléter et de faire quelque chose dès lors qu'ils sont finis.