Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cacheable local storage #86

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { from, Observable, of } from 'rxjs';
import { distinctUntilChanged, mergeMap } from 'rxjs/operators';
import { HalModel } from '../../models/hal.model';
import { HalDocument } from '../hal-document';
import { EtagHalStorage } from './etag-hal-storage';

export class CacheFirstFetchLaterStorage extends EtagHalStorage {
public makeGetRequestWrapper<T extends HalModel>(
urls: { originalUrl: string; cleanUrl: string; urlWithParams: string },
cachedResource: T | HalDocument<T>,
originalGetRequest$: Observable<T | HalDocument<T>>
): Observable<T | HalDocument<T>> {
if (cachedResource) {
return from([of(cachedResource), originalGetRequest$]).pipe(
mergeMap((request) => request),
distinctUntilChanged(this.areModelsEqual.bind(this))
);
}

return originalGetRequest$;
}

private areModelsEqual<T extends HalModel>(model1: T | HalDocument<T>, model2: T | HalDocument<T>): boolean {
const localModel1 = this.getRawStorageModel(model1.uniqueModelIdentificator);
const localModel2 = this.getRawStorageModel(model2.uniqueModelIdentificator);
return localModel1.etag === localModel2.etag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CacheStrategy } from '../../enums/cache-strategy.enum';
import { SimpleHalStorage } from '../../classes/hal-storage/simple-hal-storage';
import { EtagHalStorage } from '../../classes/hal-storage/etag-hal-storage';
import { HalStorage } from './hal-storage';
import { CacheFirstFetchLaterStorage } from './cache-first-fetch-later.storage';

export type HalStorageType = SimpleHalStorage | EtagHalStorage;

Expand All @@ -15,6 +16,9 @@ export function createHalStorage(cacheStrategy: CacheStrategy = CacheStrategy.NO
case CacheStrategy.ETAG:
storage = new EtagHalStorage();
break;
case CacheStrategy.CACHE_FIRST_FETCH_LATER:
storage = new CacheFirstFetchLaterStorage();
break;
case CacheStrategy.CUSTOM:
if (!storageInstance) {
throw new Error('When CacheStrategy.CUSTOM is specified, config.storage is required.');
Expand Down
1 change: 1 addition & 0 deletions projects/ngx-hal/src/lib/enums/cache-strategy.enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum CacheStrategy {
CACHE_FIRST_FETCH_LATER = 'CACHE_FIRST_FETCH_LATER',
CUSTOM = 'CUSTOM',
ETAG = 'ETAG',
NONE = 'NONE'
Expand Down
1 change: 1 addition & 0 deletions projects/ngx-hal/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from './lib/classes/pagination';
export * from './lib/classes/hal-storage/hal-storage';
export * from './lib/classes/hal-storage/etag-hal-storage';
export * from './lib/classes/hal-storage/simple-hal-storage';
export * from './lib/classes/hal-storage/cache-first-fetch-later.storage';

export * from './lib/enums/cache-strategy.enum';

Expand Down