You can write something along these lines:
function getCustomer(id) {
return Observable.of({'name': 'John', id}).delay(500);
}
Observable.of({'id': 42})
.distinctUntilChanged(params => params['id'])
.do(() => {
})
.switchMap((params) => {
return Observable.combineLatest(
Observable.of(true).delay(300).startWith(null),
getCustomer(params['id']).startWith(null),
function(delay, customer) {
if (customer) {
return customer;
}
if (delay && !customer) {
console.log('this.isLoading = true;');
}
return null;
})
.filter(customer => customer)
.distinctUntilChanged(customer => customer['id']);
})
.subscribe(
customer => {
console.log('this.isLoading = false;');
console.log(customer);
},
error => {
}
);
-: https://jsbin.com/nebutup/5/edit?js,console
combineLatest() :
, , . Observables .startWith(null), , , combineLatest() . , , , .
filter() null distinctUntilChanged(), , ( , ).
, , :
this.isLoading = true;
this.isLoading = false;
{ name: 'John', id: 42 }
, , .
, getCustomer() :
function getCustomer(id) {
return Observable.of({'name': 'John', id}).delay(100);
}
:
this.isLoading = false;
{ name: 'John', id: 42 }
, .