Skip to content

Commit

Permalink
Add observe() and observeMany() to cache service
Browse files Browse the repository at this point in the history
  • Loading branch information
henryruhs committed Jun 4, 2022
1 parent 87795ad commit a5007ad
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/cache/cache.service.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/common/common.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
2 changes: 1 addition & 1 deletion src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
104 changes: 101 additions & 3 deletions tests/cache.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('CacheService', () =>
})();
});

it('programmatic flush all', done =>
it('programmatic flush many', done =>
{
inject(
[
Expand All @@ -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)))
)
Expand All @@ -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(
Expand All @@ -180,11 +277,12 @@ describe('CacheService', () =>
{
testService
.enableCache()
.setParam('cache', '5')
.setParam('cache', '8')
.find()
.subscribe();
cacheService
.observeAll()
.pipe(take(1))
.subscribe(
{
next: store =>
Expand Down
8 changes: 7 additions & 1 deletion tests/helper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { createUrl, createUrlWithId, Id } from '../src';
import { createUrl, createUrlWithId, stripUrlParams, Id } from '../src';

describe('Helper', () =>
{
Expand Down Expand Up @@ -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');
})
});

0 comments on commit a5007ad

Please sign in to comment.