From a5007add71b988083f2c748cb3b3cb93999aebac Mon Sep 17 00:00:00 2001 From: henryruhs Date: Sat, 4 Jun 2022 12:54:48 +0200 Subject: [PATCH] Add observe() and observeMany() to cache service --- src/cache/cache.service.ts | 15 +++++- src/common/common.helper.ts | 5 ++ src/common/index.ts | 2 +- tests/cache.service.spec.ts | 104 ++++++++++++++++++++++++++++++++++-- tests/helper.spec.ts | 8 ++- 5 files changed, 127 insertions(+), 7 deletions(-) diff --git a/src/cache/cache.service.ts b/src/cache/cache.service.ts index a4ed0ed..334d7e1 100644 --- a/src/cache/cache.service.ts +++ b/src/cache/cache.service.ts @@ -1,7 +1,8 @@ import { HttpContextToken, HttpRequest, HttpResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import { from, timer, Observable, Subscription } from 'rxjs'; +import { from, filter, timer, Observable, Subscription } from 'rxjs'; import { Context, Store } from './cache.interface'; +import { stripUrlParams } from '../common'; @Injectable() export class CacheService @@ -62,7 +63,7 @@ export class CacheService flushMany(url : string) : this { - this.store.forEach((store, urlWithParams) => urlWithParams.startsWith(url) ? this.flush(urlWithParams) : null); + this.store.forEach((store, urlWithParams) => stripUrlParams(urlWithParams) === url ? this.flush(urlWithParams) : null); return this; } @@ -72,6 +73,16 @@ export class CacheService return this; } + observe(urlWithParams : string) : Observable<[string, Store]> + { + return this.observeAll().pipe(filter(value => value[0] === urlWithParams)); + } + + observeMany(url : string) : Observable<[string, Store]> + { + return this.observeAll().pipe(filter(value => stripUrlParams(value[0]) === url)); + } + observeAll() : Observable<[string, Store]> { return from(this.store); diff --git a/src/common/common.helper.ts b/src/common/common.helper.ts index 9d75503..e5c201d 100644 --- a/src/common/common.helper.ts +++ b/src/common/common.helper.ts @@ -24,3 +24,8 @@ export function createUrlWithId(apiUrl : string, apiRoute : string, id : Id) : s return apiUrl + route; } + +export function stripUrlParams(urlWithParams : string) : string +{ + return urlWithParams.split('?')[0]; +} diff --git a/src/common/index.ts b/src/common/index.ts index 0a206d3..1b3aff7 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -2,4 +2,4 @@ export { Context, Options, OptionsWithBody } from './common.interface'; export { CommonService } from './common.service'; export { Id, Method, UniversalMethod } from './common.type'; export { ApiUrl, ApiRoute } from './common.decorator'; -export { createUrl, createUrlWithId } from './common.helper'; +export { createUrl, createUrlWithId, stripUrlParams } from './common.helper'; diff --git a/tests/cache.service.spec.ts b/tests/cache.service.spec.ts index 9b5ba99..487ac6e 100644 --- a/tests/cache.service.spec.ts +++ b/tests/cache.service.spec.ts @@ -1,7 +1,7 @@ import { HttpClientModule } from '@angular/common/http'; import { inject, TestBed } from '@angular/core/testing'; import { expect } from 'chai'; -import { concatMap, delay, tap } from 'rxjs/operators'; +import { concatMap, delay, take, tap } from 'rxjs/operators'; import { CrudModule, CacheService } from '../src'; import { TestService } from './test.service'; import { mockRequest } from './test.helper'; @@ -139,7 +139,7 @@ describe('CacheService', () => })(); }); - it('programmatic flush all', done => + it('programmatic flush many', done => { inject( [ @@ -151,6 +151,37 @@ describe('CacheService', () => .enableCache() .setParam('cache', '4') .find() + .pipe( + concatMap(() => cacheService.flushMany('https://jsonplaceholder.typicode.com/posts').get(mockRequest(testService))) + ) + .subscribe( + { + next: () => + { + testService.clear(); + done('error'); + }, + error: () => + { + testService.clear(); + done(); + } + }); + })(); + }); + + it('programmatic flush all', done => + { + inject( + [ + CacheService, + TestService + ], (cacheService : CacheService, testService : TestService) => + { + testService + .enableCache() + .setParam('cache', '5') + .find() .pipe( concatMap(() => cacheService.flushAll().get(mockRequest(testService))) ) @@ -170,6 +201,72 @@ describe('CacheService', () => })(); }); + it('observe', done => + { + inject( + [ + CacheService, + TestService + ], (cacheService : CacheService, testService : TestService) => + { + testService + .enableCache() + .setParam('cache', '6') + .find() + .subscribe(); + cacheService + .observe('https://jsonplaceholder.typicode.com/posts?cache=6') + .pipe(take(1)) + .subscribe( + { + next: store => + { + expect(store.length).to.be.above(0); + testService.clear(); + done(); + }, + error: () => + { + testService.clear(); + done('error'); + } + }); + })(); + }); + + it('observe many', done => + { + inject( + [ + CacheService, + TestService + ], (cacheService : CacheService, testService : TestService) => + { + testService + .enableCache() + .setParam('cache', '7') + .find() + .subscribe(); + cacheService + .observeMany('https://jsonplaceholder.typicode.com/posts') + .pipe(take(1)) + .subscribe( + { + next: store => + { + expect(store.length).to.be.above(0); + testService.clear(); + done(); + }, + error: () => + { + testService.clear(); + done('error'); + } + }); + })(); + }); + it('observe all', done => { inject( @@ -180,11 +277,12 @@ describe('CacheService', () => { testService .enableCache() - .setParam('cache', '5') + .setParam('cache', '8') .find() .subscribe(); cacheService .observeAll() + .pipe(take(1)) .subscribe( { next: store => diff --git a/tests/helper.spec.ts b/tests/helper.spec.ts index a523652..52d667c 100644 --- a/tests/helper.spec.ts +++ b/tests/helper.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { createUrl, createUrlWithId, Id } from '../src'; +import { createUrl, createUrlWithId, stripUrlParams, Id } from '../src'; describe('Helper', () => { @@ -42,4 +42,10 @@ describe('Helper', () => testArray.map(testSet => expect(createUrlWithId(testSet.apiUrl, testSet.apiRoute, testSet.id)).to.be.equal(testSet.url)); }); + + it('strip url params', () => + { + expect(stripUrlParams('http://localhost/v1.0.0/posts/1?cache=1'), 'http://localhost/v1.0.0/posts/1'); + expect(stripUrlParams('../v1.0.0/posts/1?cache=1'), '../v1.0.0/posts/1'); + }) });