Я не знаю вас другого кода, например, откуда взялся this.items
, почему вы публикуете обновленные элементы в onItemDeleted
. Но я бы, вероятно: a) передал this.items
для удаления метода, такого как delete(id, items)
, потому что в то время, когда придет ответ, вы не знаете, что произойдет с this.items
; б) то, что внутри карты, перейдите к отдельной функции, которая будет removeById(items, id)
; в) упростить pipe
. Как это:
private items = [];
onItemDeleted = new Subject<any>();
removeById(fromItems, id) {
const index1 = fromItems.findIndex((element) => {
return element.id === id;
});
if (index1 >= 0 ) {
fromItems.splice(index1,1);
}
return fromItems;
}
// who ever calls this, should provide copy of items also
// then you will be kinda protected from concurrent
// modification, when http response complete, but this.items
// is completely different, from when http request started
delete(fromItems, id:number): Observable<any> {
return this.http.delete('http://my.api.com/item/' + id)
.pipe(
switchMap(checkServerSuccessResponse),
map(data => this.removeById(fromItems, id)),
tap(items => this.onItemDeleted.next(items)),
catchError(returnFalse),
);
}