Skip to content

Commit

Permalink
loadFromObservable now uses a lazy promise, to fix issues when you do…
Browse files Browse the repository at this point in the history
…n't subscribe to the returned observable.
  • Loading branch information
darthdie committed May 15, 2018
1 parent af2ddcd commit e50c3ec
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { merge } from 'rxjs/observable/merge';
import { share } from 'rxjs/operators/share';
import { map } from 'rxjs/operators/map';
import { catchError } from 'rxjs/operators/catchError';
import { defer } from 'rxjs/observable/defer';
import { Storage } from '@ionic/storage';

export interface RawCacheItem {
Expand Down Expand Up @@ -348,20 +349,22 @@ export class CacheService {

observable = observable.pipe(share());

return fromPromise(this.getItem(key)).pipe(
catchError(e => {
observable.subscribe(
res => {
return this.saveItem(key, res, groupKey, ttl);
},
error => {
return _throw(error);
}
);

return observable;
})
);
return defer(() => {
return fromPromise(this.getItem(key)).pipe(
catchError(e => {
observable.subscribe(
res => {
return this.saveItem(key, res, groupKey, ttl);
},
error => {
return _throw(error);
}
);

return observable;
})
);
});
}

/**
Expand Down

0 comments on commit e50c3ec

Please sign in to comment.