If you want to lazily get your store[] from your backend you will have to fetch these upon the first subscribe to the this.stores. All other subscribes may use the same values returned from your http.get. To accomplish this we can use .shareReplay() to have all subscribers multicast to the same source and have it replay its previous values instead of re-invoking http.get
function getStores() {
//return http.get<Store[]>(URL)
return Rx.Observable.from(['asdf', 'foo', 'bar']).delay(500);
}
const stores = getStores()
.do(undefined, undefined, _ => console.log('retrieved values from http backend'))
.shareReplay();
const $buttonClicks = Rx.Observable.fromEvent(document.getElementById('button'), 'click');
$buttonClicks
.do(_ => console.log('CLICKED'))
.switchMap(_ => stores
.map((val, idx) => [val, idx])
.filter(tuple => tuple[0] === 'foo')
.map(tuple => tuple[1])
)
.subscribe(
idx => console.log('got index of `foo`: ' + idx)
);
The switchMap is a bit ugly (map/filter/map) because this example code does not utilise an array but instead single emissions. .toArray() can fix this. Depends on how you want to proceed with using the index (or value)