K. Kowalczyk
K. Kowalczyk

Reputation: 987

How to pass this object to concatMap mapping function?

I'm building Angular2 module for caching images. I'm using provider service which returns Observables of loaded resources, either sync if they are already cached or async if not.

I tried however to preload batch of images on app start and ran into problem with concatMap function.

public preloadImages(imgUrls: string[]) {
    console.log(this.cachedResources); //Everything is fine here
    return Observable.from(imgUrls).concatMap(this.getImage);
}

My provider uses collection object private cachedResources: DictMap<CachedResource>; to store cached data, which is inaccessible from getImage function if it's called by concatMap.

public getImage(imgUrl: string) {
    console.log(this.cachedResources); //Unidentified
}

So how can I make it visible in function?

Upvotes: 0

Views: 1282

Answers (1)

El houcine bougarfaoui
El houcine bougarfaoui

Reputation: 37373

concatMap return un observable and its callback must return a inner Observable

 public preloadImages(imgUrls: string[]) {
        console.log(this.cachedResources); //Everything is fine here
        return Observable.from(imgUrls).concatMap(url=> Observable.of(this.getImage(url));
    }

Upvotes: 1

Related Questions